From 11adbebec45077de1c17a304ad08b914f1a80a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20K=C3=BChne?= Date: Fri, 20 Jan 2023 11:44:06 +0100 Subject: [PATCH] Fix sys attrs in history (#597) * 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 --- src/datahike/db.cljc | 6 +-- test/datahike/test/api_test.cljc | 39 ++++++++++++++----- .../test/attribute_refs/temporal_search.cljc | 21 ++++++++++ test/datahike/test/gc_test.cljc | 10 ++--- 4 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 test/datahike/test/attribute_refs/temporal_search.cljc diff --git a/src/datahike/db.cljc b/src/datahike/db.cljc index f7eaa1fe1..43c7bd8a7 100644 --- a/src/datahike/db.cljc +++ b/src/datahike/db.cljc @@ -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))) diff --git a/test/datahike/test/api_test.cljc b/test/datahike/test/api_test.cljc index b84b43c44..a3528e696 100644 --- a/test/datahike/test/api_test.cljc +++ b/test/datahike/test/api_test.cljc @@ -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)) @@ -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))) @@ -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} @@ -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 diff --git a/test/datahike/test/attribute_refs/temporal_search.cljc b/test/datahike/test/attribute_refs/temporal_search.cljc new file mode 100644 index 000000000..b66d450d0 --- /dev/null +++ b/test/datahike/test/attribute_refs/temporal_search.cljc @@ -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))))))) diff --git a/test/datahike/test/gc_test.cljc b/test/datahike/test/gc_test.cljc index bf35241a3..1be488ef9 100644 --- a/test/datahike/test/gc_test.cljc +++ b/test/datahike/test/gc_test.cljc @@ -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 (