Skip to content

Commit

Permalink
normalize iri-cache structure and cache subgraph lookups
Browse files Browse the repository at this point in the history
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 <iri>})`, 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
  • Loading branch information
dpetran committed Aug 23, 2023
1 parent cf26cc5 commit 135fc2b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/fluree/db/query/exec/select.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<? (dbproto/-iri db v compact))]
(vswap! iri-cache assoc v iri)
(vswap! iri-cache assoc v {:as iri})
iri)
(catch* e
(log/error e "Error displaying iri:" v)
Expand Down
33 changes: 16 additions & 17 deletions src/fluree/db/query/json_ld/response.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

(defn wildcard-spec
[db cache compact-fn pid]
(or (get @cache pid)
(let [p-spec (if-let [spec (get-in db [:schema :pred pid])]
(assoc spec :as (compact-fn (:iri spec)))
{:as pid})]
(vswap! cache assoc pid p-spec)
p-spec)))
(go-try
(or (get @cache pid)
(let [p-spec (if-let [spec (get-in db [:schema :pred pid])]
(assoc spec :as (compact-fn (:iri spec)))
{:as (<? (dbproto/-iri db pid compact-fn))})]
(vswap! cache assoc pid p-spec)
p-spec))))

(defn iri?
[pid]
Expand All @@ -38,16 +39,15 @@
[db cache compact-fn sid]
(go-try
(when-let [iri (<? (sid->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]
(go-try
(let [sub-flakes (<? (query-range/index-range db :spot = [flake-sid]))]
(<? (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]
Expand All @@ -65,16 +65,15 @@
;; have a sub-selection
(<? (crawl-ref-item db context compact-fn ref-sid spec cache fuel-vol max-fuel (inc depth-i)))
;; no sub-selection, just return IRI
(or (get @cache ref-sid)
(<? (cache-sid->iri db cache compact-fn ref-sid))))]
(or (:as (get @cache ref-sid))
(:as (<? (cache-sid->iri db cache compact-fn ref-sid)))))]
(recur r (conj acc-item result)))
(if (= 1 (count acc-item))
(first acc-item)
acc-item)))]
(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"
Expand All @@ -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)))
(<? (wildcard-spec db cache compact-fn p))))
p-iri (:as spec)
v (cond
(nil? spec)
Expand All @@ -107,13 +106,13 @@
acc []]
(if type-id
(recur rest-types
(conj acc (or (get @cache type-id)
(<? (cache-sid->iri db cache compact-fn type-id)))))
(conj acc (:as (or (get @cache type-id)
(<? (cache-sid->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)
Expand All @@ -132,7 +131,7 @@
;; no sub-selection, just return {@id <iri>} 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 (<? (wildcard-spec db cache compact-fn const/$xsd:anyURI)))
c-iri (<? (dbproto/-iri db (flake/o f) compact-fn))]
{id-key c-iri}))
(flake/o f))]
Expand Down

0 comments on commit 135fc2b

Please sign in to comment.