001 (ns wildwood.mongo-ka
002 "A knowledge accessor fetching from and storing to Mongo DB.
003
004 Hierarchical databases seem a very natural fit for how we're storing
005 knowledge. Mongo DB seems a particularly natural fit since its
006 internal representation is JSON, which can be transformed to EDN
007 extremely naturally."
008 (:require [monger.core :as mg]
009 [monger.collection :as mc]
010 [wildwood.knowledge-accessor :refer [Accessor]])
011 (:import [com.mongodb MongoOptions ServerAddress]
012 [com.mongodb DB WriteConcern]
013 [org.bson.types ObjectId]))
014
015 ;; MongoDB data items are identified by ObjectId objects. In the retrieved
016 ;; record from MongoDB, key value is the value of a keyword `:_id` I don't
017 ;; think there's any *in principle* reason why we should not use these objects
018 ;; as key values - they're presumably designed to be globally unique.
019 ;;
020 ;; In which case, on the way down we have to set `:_id` to the value of `:id`
021 ;; and vice versa on the way back up.
022
023 (defrecord MongoKA
024 ;; It's not clear to me whether we need to pass both the connection and the
025 ;; database in - it's possible that the connected database handle is
026 ;; sufficient. The value of `:collection` is the name of the collection
027 ;; within the database to which this accessor writes.
028 [connection db ^String collection]
029 Accessor
030 (fetch
031 [_ id]
032 (let [oid (cond
033 (instance? ObjectId id) id
034 (string? id) (ObjectId. id)
035 (keyword? id) (ObjectId. (name id)))
036 record (mc/find-by-id db collection oid)]
037 (when record
038 (assoc
039 (dissoc record :_id)
040 :id id))))
041 (match [_ proposition]
042 ;; I know I've seen how to do this in the Mongo documentation...
043 )
044 (store [_ proposition]
045 ;; don't really know how to do this and am too tired just now.
046 ))
047