Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 22 additions & 38 deletions src/compute/datomic_client_memdb/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@
(merge {:cognitect.anomalies/category :cognitect.anomalies/unsupported}
data))))

(defn memdb-uri
"Returns a Datomic mem database URI for `db-name`."
[db-name]
(str "datomic:mem://" db-name))

(defn free-uri
"Returns the default Datomic free database URI for `db-name`."
[db-name]
(str "datomic:free://localhost:4334/" db-name))

(defn collection-query? [query]
(let [[{fnd :find}] (q-support/parse-as query)]
(or (and (= 2 (count fnd))
Expand All @@ -34,16 +24,16 @@
(= 2 (count (first fnd)))
(= '... (second (first fnd)))))))

(deftype LocalDb [db db-name]
(deftype LocalDb [db]
client-proto/Db
(as-of [_ time-point]
(LocalDb. (peer/as-of db time-point) db-name))
(LocalDb. (peer/as-of db time-point)))
(datoms [_ arg-map]
(apply peer/datoms db (:index arg-map) (:components arg-map)))
(db-stats [_]
(throw-unsupported {}))
(history [_]
(LocalDb. (peer/history db) db-name))
(LocalDb. (peer/history db)))
(index-range [_ arg-map]
(peer/index-range db (:attrid arg-map) (:start arg-map) (:end arg-map)))
(pull [this arg-map]
Expand All @@ -62,11 +52,11 @@
:datomic.client.impl.shared.validator/op :pull
:datomic.client.impl.shared.validator/requirements '{:eid value :selector value}}))))
(since [_ t]
(LocalDb. (peer/since db t) db-name))
(LocalDb. (peer/since db t)))
(with [_ arg-map]
(-> (peer/with db (:tx-data arg-map))
(update :db-before #(LocalDb. % db-name))
(update :db-after #(LocalDb. % db-name))))
(update :db-before #(LocalDb. %))
(update :db-after #(LocalDb. %))))

client-impl/Queryable
(q [_ arg-map]
Expand All @@ -89,18 +79,17 @@
(case k
:t (peer/basis-t db)
:next-t (inc (:t this))
:db-name db-name
not-found)))


(deftype LocalConnection [conn db-name]
(deftype LocalConnection [conn]
client-proto/Connection
(db [_]
(LocalDb. (peer/db conn) db-name))
(LocalDb. (peer/db conn)))

(transact [_ arg-map]
(-> @(peer/transact conn (:tx-data arg-map))
(update-vals #{:db-before :db-after} #(LocalDb. % db-name))))
(update-vals #{:db-before :db-after} #(LocalDb. %))))

(tx-range [_ arg-map]
(peer/tx-range (peer/log conn) (:start arg-map) (:end arg-map)))
Expand All @@ -115,27 +104,22 @@
(get (client/db this) k not-found)))


(defrecord Client [db-name-as-uri-fn]

(defrecord Client [uri]
client-proto/Client
(administer-system [_ arg-map] (throw-unsupported {}))
(list-databases [_ _]
(or (peer/get-database-names (db-name-as-uri-fn "*")) (list)))

(connect [client arg-map]
(let [db-name (:db-name arg-map)]
(if (contains? (set (client/list-databases client {})) db-name)
(LocalConnection. (peer/connect (db-name-as-uri-fn db-name)) db-name)
(let [msg (format "Unable to find keyfile %s. Make sure that your endpoint and db-name are correct." db-name)]
(throw (ex-info msg {:cognitect.anomalies/category :cognitect.anomalies/not-found
:cognitect.anomalies/message msg}))))))

(create-database [_ arg-map]
(let [db-name (:db-name arg-map)]
(peer/create-database (db-name-as-uri-fn db-name)))
(or (peer/get-database-names uri) (list)))

(connect [_client arg-map]
(LocalConnection. (peer/connect uri)))

(create-database [_ _arg-map]
(peer/create-database uri)
true)

(delete-database [_ arg-map]
(peer/delete-database (db-name-as-uri-fn (:db-name arg-map)))
(delete-database [_ _arg-map]
(peer/delete-database uri)
true)

Closeable
Expand All @@ -154,5 +138,5 @@
takes :db-name-as-uri-fn which is a function that is passed a db-name and is
expected to return a Datomic Peer database URI. Note that this function is passed
a '*' when list-databases is called."
[arg-map]
(map->Client {:db-name-as-uri-fn (or (:db-name-as-uri-fn arg-map) memdb-uri)}))
[params]
(map->Client params))