Skip to content

Commit a8d8155

Browse files
committed
Fixed :max-eid for dangling entities during reader-based serialization (closes #463)
1 parent 8e1abae commit a8d8155

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# WIP
22

33
- Implement “constant substitution” optimization for queries #462
4+
- Fixed :max-eid for dangling entities during reader-based serialization #463
45

56
# 1.6.3
67

src/datascript/db.cljc

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -981,11 +981,26 @@
981981
:pull-attrs (lru/cache 100)
982982
:hash (atom 0)}))
983983

984-
(defn- init-max-eid [eavt]
985-
(or (-> (set/rslice eavt (datom (dec tx0) nil nil txmax) (datom e0 nil nil tx0))
986-
(first)
987-
(:e))
988-
e0))
984+
(defn- init-max-eid [rschema eavt avet]
985+
(let [max #(if (and %2 (> %2 %1)) %2 %1)
986+
max-eid (some->
987+
(set/rslice eavt
988+
(datom (dec tx0) nil nil txmax)
989+
(datom e0 nil nil tx0))
990+
first :e)
991+
res (max e0 max-eid)
992+
max-ref (fn [attr]
993+
(some->
994+
(set/rslice avet
995+
(datom (dec tx0) attr (dec tx0) txmax)
996+
(datom e0 attr e0 tx0))
997+
first :v))
998+
refs (:db.type/ref rschema)
999+
res (reduce
1000+
(fn [res attr]
1001+
(max res (max-ref attr)))
1002+
res refs)]
1003+
res))
9891004

9901005
(defn ^DB init-db [datoms schema opts]
9911006
(when-some [not-datom (first (drop-while datom? datoms))]
@@ -1004,7 +1019,7 @@
10041019
avet-arr (to-array avet-datoms)
10051020
_ (arrays/asort avet-arr cmp-datoms-avet-quick)
10061021
avet (set/from-sorted-array cmp-datoms-avet avet-arr (arrays/alength avet-arr) opts)
1007-
max-eid (init-max-eid eavt)
1022+
max-eid (init-max-eid rschema eavt avet)
10081023
max-tx (transduce (map (fn [^Datom d] (datom-tx d))) max tx0 eavt)]
10091024
(map->DB
10101025
{:schema schema

test/datascript/test/serialize.cljc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@
110110
(d/init-db [[:add -1 :name "Ivan"] {:add -1 :age 35}] schema))))))
111111

112112

113+
(deftest ^{:doc "issue-463"} test-max-eid-from-refs
114+
(let [db (-> (d/empty-db {:ref {:db/valueType :db.type/ref}})
115+
(d/db-with [[:db/add 1 :name "Ivan"]])
116+
(d/db-with [{:db/id 1 :ref {}}]))]
117+
(is (= 2 (:max-eid db)))
118+
(doseq [[r read-fn] readers]
119+
(testing r
120+
(let [db' (read-fn (pr-str db))]
121+
(is (= 2 (:max-eid db'))))))))
122+
113123

114124
(deftest serialize
115125
(let [db (d/db-with

0 commit comments

Comments
 (0)