Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`.

Expand Down
9 changes: 5 additions & 4 deletions src/xitdb/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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]
Expand Down
12 changes: 6 additions & 6 deletions test/xitdb/history_test.clj
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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"
Expand Down