diff --git a/README.md b/README.md index 15b8708..f041b83 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,8 @@ Here's a taste of how your queries could look like: ``` ## History -Since the database is immutable, all previous values are accessing by reading + +Since the database is immutable, all previous values are accessed by reading from the respective `history index`. The root data structure of a xitdb database is a ArrayList, called 'history'. Each transaction adds a new entry into this array, which points to the latest value @@ -143,6 +144,18 @@ of the database (usually a map). (xdb/deref-at db 1) ;; the second value ``` +You can get the latest history index from the `count` of the database: + +```clojure +(def history-index (dec (count db))) +``` + +After making further transactions, you can revert back to it simply like this: + +```clojure +(reset! db (xdb/deref-at db history-index)) +``` + It is also possible to create a transaction which returns the previous and current values of the database, by setting the `*return-history?*` binding to `true`. diff --git a/src/xitdb/db.clj b/src/xitdb/db.clj index ecc7152..a854640 100644 --- a/src/xitdb/db.clj +++ b/src/xitdb/db.clj @@ -122,10 +122,7 @@ [^Database db] (ReadArrayList. (-> db .rootCursor))) -(defn history-index - "Returns the current size of the transaction history array." - [xdb] - (.count (read-history (-> xdb .tldbro .get)))) +(def ^:deprecated history-index count) (defn deref-at "Returns the version of the data at the specified index." @@ -145,6 +142,10 @@ (deref [this] (deref-at this -1)) + clojure.lang.Counted + (count [this] + (.count (read-history (.get tldbro)))) + clojure.lang.IAtom (reset [this new-value] diff --git a/test/xitdb/history_test.clj b/test/xitdb/history_test.clj index 7f2149e..e5ba7d8 100644 --- a/test/xitdb/history_test.clj +++ b/test/xitdb/history_test.clj @@ -1,7 +1,7 @@ (ns xitdb.history-test "Tests for database history and versioning features: - deref-at: access historical versions - - history-index: get current transaction count" + - count: get current transaction count" (:require [clojure.test :refer :all] [xitdb.db :as xdb] @@ -55,17 +55,17 @@ (is (= [1 2] (tu/materialize (xdb/deref-at db 1)))) (is (= [1 2 3] (tu/materialize (xdb/deref-at db 2))))))) -(deftest history-index-test - (testing "history-index returns the current transaction count" +(deftest count-test + (testing "count returns the current transaction count" (with-open [db (xdb/xit-db :memory)] (reset! db {:a 1}) - (is (= 1 (xdb/history-index db))) + (is (= 1 (count db))) (swap! db assoc :b 2) - (is (= 2 (xdb/history-index db))) + (is (= 2 (count db))) (swap! db assoc :c 3) - (is (= 3 (xdb/history-index db)))))) + (is (= 3 (count db)))))) (deftest deref-at-with-nil-values-test (testing "deref-at works correctly with nil values in history"