Skip to content

Commit

Permalink
Allow passing storage to db during creation
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jul 28, 2023
1 parent 1fd4a7e commit 1698b22
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
21 changes: 11 additions & 10 deletions src/datascript/core_storage.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
58 changes: 35 additions & 23 deletions src/datascript/db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/datascript/serialize.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1698b22

Please sign in to comment.