Skip to content

Commit

Permalink
Make as-of always include the time point and since always exclude the…
Browse files Browse the repository at this point in the history
… time point (#713)
  • Loading branch information
jonasseglare authored Sep 27, 2024
1 parent 1d6762b commit ddff1ac
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
17 changes: 11 additions & 6 deletions src/datahike/db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,20 @@
#?(:cljs (instance? js/Date d)
:clj (instance? Date d)))

(defn- as-of-pred [time-point]
(defn- date<= [^Date a ^Date b]
(not (.before b a)))

(defn- as-of-pred
"Create an as-of predicate, *including* the time-point"
[time-point]
(if (date? time-point)
(fn [^Datom d] (.before ^Date (.-v d) ^Date time-point))
(fn [^Datom d] (date<= (.-v d) time-point))
(fn [^Datom d] (<= (dd/datom-tx d) time-point))))

(defn- since-pred [time-point]
(if (date? time-point)
(fn [^Datom d] (.after ^Date (.-v d) ^Date time-point))
(fn [^Datom d] (>= (.-tx d) time-point))))
(defn- since-pred
"Create a since predicate, *excluding* the time-point. The opposite of `as-of-pred`."
[time-point]
(complement (as-of-pred time-point)))

(defn assemble-datoms-xform [db]
(mapcat
Expand Down
2 changes: 2 additions & 0 deletions test/datahike/test/api_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@

(def date (java.util.Date.))

(Thread/sleep 100)

(d/transact conn {:tx-data [{:db/id [:name "Alice"] :age 35}]})

(is (= #{["Alice" 25] ["Bob" 30]}
Expand Down
6 changes: 3 additions & 3 deletions test/datahike/test/attribute_refs/db_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

(deftest test-partial-db
(let [conn (setup-db ref-cfg)
_tx0 (d/transact conn name-schema)
tx0 (-> (d/transact conn name-schema) :db-after :max-tx)
tx1 (-> (d/transact conn [{:name "Peter"}]) :db-after :max-tx)
tx2 (-> (d/transact conn [{:name "Maria"}]) :db-after :max-tx)]
(testing "AsOfDB"
Expand All @@ -35,8 +35,8 @@
(testing "SinceDB"
(is (= #{["Maria"] ["Peter"]}
(d/q '[:find ?v :where [?e :name ?v]]
(d/since @conn tx1))))
(d/since @conn tx0))))
(is (= #{["Maria"]}
(d/q '[:find ?v :where [?e :name ?v]]
(d/since @conn tx2)))))
(d/since @conn tx1)))))
(d/release conn)))
8 changes: 4 additions & 4 deletions test/datahike/test/time_variance_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
#{[30] [25]}
(d/q '[:find ?a :in $ ?e :where [?e :age ?a]] (d/history @conn) [:name "Alice"])))
(testing "historical values after with retraction"
(let [;;tx-id1 (:max-tx @conn)
(let [tx-id1 (:max-tx @conn)
_ (d/transact conn [[:db/retractEntity [:name "Alice"]]])
tx-id2 (:max-tx @conn)]
(is (thrown-with-msg? Throwable #"Nothing found for entity id"
Expand All @@ -90,7 +90,7 @@
(d/q '[:find ?a :in $ ?e :where [?e :age ?a]]
db
[:name "Alice"]))))
(doseq [db (vary-db-ops @conn d/history #(d/since % tx-id2))]
(doseq [db (vary-db-ops @conn d/history #(d/since % tx-id1))]
(is (= #{[30]}
(d/q '[:find ?a :in $ ?e :where [?e :age ?a]]
db
Expand Down Expand Up @@ -199,7 +199,7 @@
first-date (now)
;; sleep to make sure that transact thread has newer timestamp
_ (sleep 10)
tx-id 536870914
tx-id 536870913
query '[:find ?a :where [?e :age ?a]]]
(testing "empty after first insertion"
(is (= #{}
Expand All @@ -212,7 +212,7 @@
(is (= #{[new-age]}
(d/q query (d/since @conn tx-id))))))
(testing "print DB"
(is (= "#datahike/SinceDB {:origin #datahike/DB {:store-id [[:mem \"test.datahike.io\" \"test-since-db\"] :db] :commit-id :REPLACED :max-tx 536870914 :max-eid 4} :time-point 536870914}"
(is (= "#datahike/SinceDB {:origin #datahike/DB {:store-id [[:mem \"test.datahike.io\" \"test-since-db\"] :db] :commit-id :REPLACED :max-tx 536870914 :max-eid 4} :time-point 536870913}"
(replace-commit-id (pr-str (d/since @conn tx-id))))))
(d/release conn)))

Expand Down

0 comments on commit ddff1ac

Please sign in to comment.