From 1698b2226c4f34f8a41738e8c3c79753b136f572 Mon Sep 17 00:00:00 2001 From: Nikita Prokopov Date: Fri, 28 Jul 2023 20:38:55 +0200 Subject: [PATCH] Allow passing storage to db during creation --- src/datascript/core_storage.clj | 21 ++++++------ src/datascript/db.cljc | 58 ++++++++++++++++++++------------- src/datascript/serialize.cljc | 2 +- 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/src/datascript/core_storage.clj b/src/datascript/core_storage.clj index 452aa66c..930d89f3 100644 --- a/src/datascript/core_storage.clj +++ b/src/datascript/core_storage.clj @@ -73,16 +73,17 @@ (defn- store-impl! [db adapter opts] (binding [*store-buffer* (volatile! (transient []))] - (let [eavt ^PersistentSortedSet (:eavt db) - settings (.-_settings eavt) - meta (merge - {:schema (:schema db) - :max-eid (:max-eid db) - :max-tx (:max-tx db) - :eavt (set/store (:eavt db) adapter) - :aevt (set/store (:aevt db) adapter) - :avet (set/store (:avet db) adapter)} - (@#'set/settings->map settings))] + (let [eavt-addr (set/store (:eavt db) adapter) + aevt-addr (set/store (:aevt db) adapter) + avet-addr (set/store (:avet db) adapter) + meta (merge + {:schema (:schema db) + :max-eid (:max-eid db) + :max-tx (:max-tx db) + :eavt eavt-addr + :aevt aevt-addr + :avet avet-addr} + (set/settings (:eavt db)))] (vswap! *store-buffer* conj! [root-addr meta]) (-store (:storage adapter) (persistent! @*store-buffer*)) db))) diff --git a/src/datascript/db.cljc b/src/datascript/db.cljc index fc60dbf1..f52abd16 100644 --- a/src/datascript/db.cljc +++ b/src/datascript/db.cljc @@ -953,6 +953,16 @@ (when (= :db.cardinality/many (:db/cardinality (get schema attr))) (raise a " :db/tupleAttrs can’t depend on :db.cardinality/many attribute: " attr ex-data)))))))) +#?(:clj + (declare+ ^:private make-adapter [storage opts])) + +(defn- update-opts [opts] + #?(:clj + (if-some [storage (:storage opts)] + (assoc opts :storage (make-adapter storage opts)) + opts) + :cljs opts)) + (defn+ ^DB empty-db ([] (empty-db nil)) @@ -961,17 +971,18 @@ ([schema opts] {:pre [(or (nil? schema) (map? schema))]} (validate-schema schema) - (map->DB - {:schema schema - :rschema (rschema (merge implicit-schema schema)) - :eavt (set/sorted-set* (assoc opts :cmp cmp-datoms-eavt)) - :aevt (set/sorted-set* (assoc opts :cmp cmp-datoms-aevt)) - :avet (set/sorted-set* (assoc opts :cmp cmp-datoms-avet)) - :max-eid e0 - :max-tx tx0 - :pull-patterns (lru/cache 100) - :pull-attrs (lru/cache 100) - :hash (atom 0)}))) + (let [opts (update-opts opts)] + (map->DB + {:schema schema + :rschema (rschema (merge implicit-schema schema)) + :eavt (set/sorted-set* (assoc opts :cmp cmp-datoms-eavt)) + :aevt (set/sorted-set* (assoc opts :cmp cmp-datoms-aevt)) + :avet (set/sorted-set* (assoc opts :cmp cmp-datoms-avet)) + :max-eid e0 + :max-tx tx0 + :pull-patterns (lru/cache 100) + :pull-attrs (lru/cache 100) + :hash (atom 0)})))) (defn- init-max-eid [eavt] (or (-> (set/rslice eavt (datom (dec tx0) nil nil txmax) (datom e0 nil nil tx0)) @@ -989,7 +1000,8 @@ (raise "init-db expects list of Datoms, got " (type not-datom) {:error :init-db})) (validate-schema schema) - (let [rschema (rschema (merge implicit-schema schema)) + (let [opts (update-opts opts) + rschema (rschema (merge implicit-schema schema)) indexed (:db/index rschema) arr (cond-> datoms (not (arrays/array? datoms)) (arrays/into-array)) @@ -1003,17 +1015,17 @@ avet (set/from-sorted-array cmp-datoms-avet avet-arr (arrays/alength avet-arr) opts) max-eid (init-max-eid eavt) max-tx (transduce (map (fn [^Datom d] (datom-tx d))) max tx0 eavt)] - (map->DB { - :schema schema - :rschema rschema - :eavt eavt - :aevt aevt - :avet avet - :max-eid max-eid - :max-tx max-tx - :pull-patterns (lru/cache 100) - :pull-attrs (lru/cache 100) - :hash (atom 0)})))) + (map->DB + {:schema schema + :rschema rschema + :eavt eavt + :aevt aevt + :avet avet + :max-eid max-eid + :max-tx max-tx + :pull-patterns (lru/cache 100) + :pull-attrs (lru/cache 100) + :hash (atom 0)})))) (defn restore-db [{:keys [schema eavt aevt avet max-eid max-tx]}] (map->DB diff --git a/src/datascript/serialize.cljc b/src/datascript/serialize.cljc index dc3e1471..fc7adbdf 100644 --- a/src/datascript/serialize.cljc +++ b/src/datascript/serialize.cljc @@ -161,7 +161,7 @@ attrs (amap freeze-kw attrs) kws (amap freeze-kw (persistent! @*kws)) #?@(:clj - [settings (@#'set/settings->map (.-_settings ^PersistentSortedSet (:eavt db)))])] + [settings (set/settings (:eavt db))])] (dict "count" (count (:eavt db)) "tx0" db/tx0