Skip to content

Commit

Permalink
Fix sys attrs in history (#597)
Browse files Browse the repository at this point in the history
* add system entities to temporal index per default

* Remove dev literals

* add sys attributes in history test

* add context info for meta-db-test counts, fix gc-test counts
  • Loading branch information
kordano authored Jan 20, 2023
1 parent bdd20b6 commit 11adbeb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/datahike/db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -776,9 +776,9 @@
:meta (tools/meta-data)
:op-count (if attribute-refs? (count ref-datoms) 0)}
(when keep-history? ;; no difference for attribute references since no update possible
{:temporal-eavt (di/empty-index index store :eavt index-config)
:temporal-aevt (di/empty-index index store :aevt index-config)
:temporal-avet (di/empty-index index store :avet index-config)}))))))
{:temporal-eavt eavt
:temporal-aevt aevt
:temporal-avet avet}))))))

(defn get-max-tx [eavt]
(transduce (map (fn [^Datom d] (datom-tx d))) max tx0 (di/-all eavt)))
Expand Down
39 changes: 29 additions & 10 deletions test/datahike/test/api_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,13 @@
:db/cardinality :db.cardinality/one
:db/valueType :db.type/long}]
conn (utils/setup-db cfg)
schema-on-write? (= (:schema-flexibility (.-config @conn)) :write)
schema-on-write? (= (:schema-flexibility (.-config @conn)) :write)
attribute-refs? (:attribute-refs? (:config @conn))
schema-count 11 ;; amount of user schema datoms in temporal eavt index
temporal-count 10 ;; amount of user data datoms in temporal eavt index when using schema-on-write
temporal-avet-count 9 ;; amount of user data datoms in temporal avet index when using schema-on write
sys-attr-count 69 ;; amount of system schema datoms in temporal eavt index when using attribute refs
sys-attr-avet-count 48 ;; amount of system schema datoms in temporal avet index when using attribute refs
update-for-schema-on-write
(fn [metrics]
(-> (update metrics :count #(+ % 11))
Expand All @@ -764,16 +770,29 @@
5 2
6 3}})))))
update-for-history
(fn [metrics schema-on-write?]
(fn [metrics schema-on-write? attribute-refs?]
(->> (update metrics :count #(+ % 4))
(merge-with merge {:per-attr-counts {:db/txInstant 4}
:per-entity-counts {(+ tx0 1) 1
(+ tx0 2) 1
(+ tx0 3) 1
(+ tx0 4) 1}
:per-entity-counts {(+ tx0 1) 1
(+ tx0 2) 1
(+ tx0 3) 1
(+ tx0 4) 1}
; 10 == 11 minus 1 parent datom that wouldn't get added unless retracted
:temporal-count (+ 11 (if schema-on-write? 10 0))
:temporal-avet-count (if schema-on-write? 9 0)})))
:temporal-count (+ schema-count
(if schema-on-write?
(if attribute-refs?
(+ temporal-count sys-attr-count)
temporal-count)
(if attribute-refs?
(+ temporal-count sys-attr-count)
0)))
:temporal-avet-count (if schema-on-write?
(if attribute-refs?
sys-attr-avet-count
temporal-avet-count)
(if attribute-refs?
sys-attr-avet-count
0))})))
update-for-attr-refs
(fn [metrics]
(let [update-counts (fn [coll] (reduce (fn [m counted] (update m counted #(if % (inc %) 1)))
Expand All @@ -791,7 +810,7 @@
(fn [metrics expected]
(doseq [[metric val] metrics]
(testing (str (name metric) " is correct")
(is (= val (metric expected))))))]
(is (= (metric expected) val)))))]
(when schema-on-write?
(d/transact conn {:tx-data schema}))
(d/transact conn {:tx-data [{:name "Donald" :age 35}
Expand All @@ -817,7 +836,7 @@
3 3}
:avet-count 0}
schema-on-write? update-for-schema-on-write
(dbi/-keep-history? @conn) (update-for-history schema-on-write?)
(dbi/-keep-history? @conn) (update-for-history schema-on-write? attribute-refs?)
(:attribute-refs? (.-config @conn)) update-for-attr-refs))))

(deftest test-metrics-hht
Expand Down
21 changes: 21 additions & 0 deletions test/datahike/test/attribute_refs/temporal_search.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(ns datahike.test.attribute-refs.temporal-search
(:require
#?(:cljs [cljs.test :as t :refer-macros [is deftest testing]]
:clj [clojure.test :refer [is deftest]])
[datahike.api :as d]))

(deftest q-with-history-should-contain-sys-attributes
(let [cfg {:store {:backend :mem
:id "attr-refs-test.temporal-serach"}
:attribute-refs? true
:keep-history? true
:schema-flexibility :write}
conn (do (d/delete-database cfg)
(d/create-database cfg)
(d/connect cfg))
query '[:find ?e ?a ?v ?t ?s
:where [?e ?a ?v ?t ?s]]]
(is (= (d/q query @conn)
(d/q query (d/history @conn))))
(is (< 1
(count (d/q query (d/history @conn)))))))
10 changes: 5 additions & 5 deletions test/datahike/test/gc_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@
(d/create-database cfg)
(d/connect cfg))
;; everything will fit into the root nodes of each index here
num-roots 6
num-roots 3
fresh-count (+ num-roots 3) ;; :branches + :db + cid + roots
]
history-count 3]
(testing "Test initial store counts."
(is (= 1 (count (-mark (:eavt @conn)))))
(is (= fresh-count (count-store @conn)))
(d/transact conn schema)
(is (= 1 (count (-mark (:eavt @conn)))))
(is (= (+ 1 fresh-count num-roots) (count-store @conn))))
(is (= (+ 1 history-count fresh-count num-roots) (count-store @conn))))
(testing "Delete old db with roots."
(is (= (+ 1 num-roots) (count (<?? S (gc! @conn (Date.))))))
(is (= fresh-count (count-store @conn))))
(is (= (+ num-roots 1) (count (<?? S (gc! @conn (Date.))))))
(is (= (+ history-count fresh-count) (count-store @conn))))
(testing "Try to run on dirty index and fail."
(is (thrown-msg? "Index needs to be properly flushed before marking."
(-mark (:eavt
Expand Down

0 comments on commit 11adbeb

Please sign in to comment.