Skip to content

Commit

Permalink
give the history resolver its own cache key prefix
Browse files Browse the repository at this point in the history
If the exact same node key came through the Resolver the CachedHistoryRangeResolver
could return the results cached by the CachedTRangeResolver, since they shared the same
cache key prefix. This gives it a unique prefix.

Added a test case that fails without the fix. Also removed some unused opts from the
`extract-query-flakes` call.
  • Loading branch information
dpetran committed Feb 3, 2023
1 parent 6fb8d24 commit 52e1d54
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/data_readers.clj
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{Flake fluree.db.flake/parts->Flake}
{Flake fluree.db.flake/parts->Flake}
2 changes: 1 addition & 1 deletion src/fluree/db/index.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@
(if (branch? node)
(resolve node-resolver node)
(async-cache
[::t-range id tempid tt-id from-t to-t]
[::history-t-range id tempid tt-id from-t to-t]
(fn [_]
(go-try
(let [resolved (<? (resolve node-resolver node))
Expand Down
5 changes: 1 addition & 4 deletions src/fluree/db/query/range.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,10 @@
range-set (flake/sorted-set-by cmp start-flake end-flake)
in-range? (fn [node] (intersects-range? node range-set))
query-xf (extract-query-flakes {:idx idx
:from-t from-t
:to-t to-t
:start-test start-test
:start-flake start-flake
:end-test end-test
:end-flake end-flake
:flake-limit limit})]
:end-flake end-flake})]
(go-try
(let [history-ch (->> (index/tree-chan resolver idx-root in-range? resolved-leaf? 1 query-xf error-ch)
(filter-authorized db start-flake end-flake error-ch)
Expand Down
44 changes: 32 additions & 12 deletions test/fluree/db/query/history_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,21 @@
{:f/t 2
:f/assert [{:ex/x "foo-2" :id :ex/dan}]
:f/retract [{:ex/x "foo-1" :id :ex/dan}]}
{:f/t 1 :f/assert [{:ex/x "foo-1" :id :ex/dan}] :f/retract []}]
{:f/t 1
:f/assert [{:ex/x "foo-1" :id :ex/dan}]
:f/retract []}]
@(fluree/history ledger {:history [:ex/dan :ex/x] :t {:to 3}}))))
(testing "t-range"
(is (= [{:f/t 4
:f/assert [{:ex/x "foo-dog" :id :ex/dog} {:ex/x "foo-cat" :id :ex/cat}]
:f/retract [{:ex/x "foo-1" :id :ex/dog} {:ex/x "foo-1" :id :ex/cat}]}
{:f/t 3
:f/assert [{:ex/x "foo-3" :id :ex/dan}]
:f/retract [{:ex/x "foo-2" :id :ex/dan}]}
{:f/t 2
:f/assert [{:ex/x "foo-2" :id :ex/dan}]
:f/retract [{:ex/x "foo-1" :id :ex/dan}]}]
@(fluree/history ledger {:history [nil :ex/x] :t {:from 2 :to 4}}))))
:f/assert [{:ex/x "foo-dog" :id :ex/dog} {:ex/x "foo-cat" :id :ex/cat}]
:f/retract [{:ex/x "foo-1" :id :ex/dog} {:ex/x "foo-1" :id :ex/cat}]}
{:f/t 3
:f/assert [{:ex/x "foo-3" :id :ex/dan}]
:f/retract [{:ex/x "foo-2" :id :ex/dan}]}
{:f/t 2
:f/assert [{:ex/x "foo-2" :id :ex/dan}]
:f/retract [{:ex/x "foo-1" :id :ex/dan}]}]
@(fluree/history ledger {:history [nil :ex/x] :t {:from 2 :to 4}}))))
(testing "datetime-t"
(is (= [{:f/t 3
:f/assert [{:ex/x "foo-3" :id :ex/dan}]
Expand All @@ -154,7 +156,9 @@
:f/retract [{:ex/x "foo-1" :id :ex/dan}]}]
@(fluree/history ledger {:history [nil :ex/x] :t {:from ts2 :to ts3}}))
"does not include t 1, 4, or 5")
(is (= [{:f/t 5 :f/assert [{:ex/x "foo-cat" :id :ex/dan}] :f/retract []}]
(is (= [{:f/t 5
:f/assert [{:ex/x "foo-cat" :id :ex/dan}]
:f/retract [{:ex/x "foo-3" :id :ex/dan}]}]
@(fluree/history ledger {:history [:ex/dan :ex/x] :t {:from (util/current-time-iso)}}))
"timestamp translates to first t before ts")

Expand All @@ -168,4 +172,20 @@
(is (= "History query not properly formatted. Provided {:history []}"
(-> @(fluree/history ledger {:history []})
(Throwable->map)
:cause))))))
:cause))))

(testing "small cache"
(let [conn (test-utils/create-conn)
ledger @(fluree/create conn "historycachetest" {:context {:ex "http://example.org/ns/"}})

db1 @(test-utils/transact ledger [{:id :ex/dan
:ex/x "foo-1"
:ex/y "bar-1"}])
db2 @(test-utils/transact ledger {:id :ex/dan
:ex/x "foo-2"
:ex/y "bar-2"})]
(testing "no t-range cache collision"
(is (= [#:f{:t 2,
:assert [{:ex/x "foo-2", :ex/y "bar-2", :id :ex/dan}],
:retract [{:ex/x "foo-1", :ex/y "bar-1", :id :ex/dan}]}]
@(fluree/history ledger {:history [:ex/dan] :t {:from 2}}))))))))

0 comments on commit 52e1d54

Please sign in to comment.