From 52e1d54e71436aed3fb78a1eeffbe565f2420381 Mon Sep 17 00:00:00 2001 From: Daniel Petranek Date: Fri, 3 Feb 2023 09:54:27 -0600 Subject: [PATCH] give the history resolver its own cache key prefix 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. --- src/data_readers.clj | 2 +- src/fluree/db/index.cljc | 2 +- src/fluree/db/query/range.cljc | 5 +-- test/fluree/db/query/history_test.clj | 44 +++++++++++++++++++-------- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/data_readers.clj b/src/data_readers.clj index a14348510..a84a3993a 100644 --- a/src/data_readers.clj +++ b/src/data_readers.clj @@ -1 +1 @@ -{Flake fluree.db.flake/parts->Flake} \ No newline at end of file +{Flake fluree.db.flake/parts->Flake} diff --git a/src/fluree/db/index.cljc b/src/fluree/db/index.cljc index 85e09d47e..6bc41ad40 100644 --- a/src/fluree/db/index.cljc +++ b/src/fluree/db/index.cljc @@ -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 (> (index/tree-chan resolver idx-root in-range? resolved-leaf? 1 query-xf error-ch) (filter-authorized db start-flake end-flake error-ch) diff --git a/test/fluree/db/query/history_test.clj b/test/fluree/db/query/history_test.clj index 7da722b39..19fb067bd 100644 --- a/test/fluree/db/query/history_test.clj +++ b/test/fluree/db/query/history_test.clj @@ -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}] @@ -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") @@ -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}}))))))))