From 135fc2b57d40180b8749ca111ee45c927aca955d Mon Sep 17 00:00:00 2001 From: Daniel Petranek Date: Wed, 23 Aug 2023 17:14:29 -0500 Subject: [PATCH] normalize iri-cache structure and cache subgraph lookups Before this commit each selector used the iri-cache differently, and therefore gave broken results if they were combined in a scenario that required iri lookups. This normalizes the cache structure by making every cache update insert an entry in the form of `(vswap! iri-cache assoc pid {:as })`, so that every cache read can use the results regardless of where it was first inserted. The other fix in this commit is adding a `dbproto/-iri` lookup to the `response/wildcard-spec` function. Before it would return a raw pid in the response, which is unhelpful for users. fixes #521 --- src/fluree/db/query/exec/select.cljc | 4 +-- src/fluree/db/query/json_ld/response.cljc | 33 +++++++++++------------ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/fluree/db/query/exec/select.cljc b/src/fluree/db/query/exec/select.cljc index 826a21085..71329e3b9 100644 --- a/src/fluree/db/query/exec/select.cljc +++ b/src/fluree/db/query/exec/select.cljc @@ -29,10 +29,10 @@ [match db iri-cache compact error-ch] (go (let [v (::where/val match)] - (if-let [cached (get @iri-cache v)] + (if-let [cached (-> @iri-cache (get v) :as)] cached (try* (let [iri (iri db sid compact-fn))] - (vswap! cache assoc sid iri) - iri))) + (vswap! cache assoc sid {:as iri}) + {:as iri}))) (defn crawl-ref-item [db context compact-fn flake-sid sub-select cache fuel-vol max-fuel depth-i] @@ -47,7 +48,6 @@ (let [sub-flakes (res db cache context compact-fn fuel-vol max-fuel sub-select depth-i sub-flakes))))) - (defn add-reverse-specs "When @reverse variables are present, crawl for the reverse specs." [db cache context compact-fn fuel-vol max-fuel {:keys [reverse] :as select-spec} depth-i flakes] @@ -65,8 +65,8 @@ ;; have a sub-selection (iri db cache compact-fn ref-sid))))] + (or (:as (get @cache ref-sid)) + (:as (iri db cache compact-fn ref-sid)))))] (recur r (conj acc-item result))) (if (= 1 (count acc-item)) (first acc-item) @@ -74,7 +74,6 @@ (recur r (assoc acc as result))) acc))))) - (defn flakes->res "depth-i param is the depth of the graph crawl. Each successive 'ref' increases the graph depth, up to the requested depth within the select-spec" @@ -91,7 +90,7 @@ list? (contains? (flake/m ff) :i) spec (or (get select-spec p) (when wildcard? - (wildcard-spec db cache compact-fn p))) + (iri db cache compact-fn type-id))))) + (conj acc (:as (or (get @cache type-id) + (iri db cache compact-fn type-id)))))) (if (= 1 (count acc)) (first acc) acc))) - :else ;; display all values + :else ;; display all values (loop [[f & r] (if list? (sort-by #(:i (flake/m %)) p-flakes) p-flakes) @@ -132,7 +131,7 @@ ;; no sub-selection, just return {@id } for each ref iri :else ;; TODO - we generate id-key here every time, this should be done in the :spec once beforehand and used from there - (let [id-key (:as (wildcard-spec db cache compact-fn const/$xsd:anyURI)) + (let [id-key (:as (