Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json-ld response formatting #399

Merged
merged 3 commits into from
Feb 28, 2023
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
21 changes: 11 additions & 10 deletions src/fluree/db/query/exec/select.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
(defprotocol ValueSelector
"Format a where search solution (map of pattern matches) by extracting and
displaying relevant pattern matches."
(format-value [fmt db iri-cache compact error-ch solution]))
(format-value [fmt db iri-cache context compact error-ch solution]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're passing both the context value and the compacting function compact here. Was there an issue with adding the new 2-arity function to compact data maps by accepting both a context and the data value that needs compacting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The json-ld/compact-fn function take a context and optionally a "used-atom" to track which context entries are used. It then transforms the supplied context and closes over the result in order to do efficient compaction and returns a single-arity function that takes an iri.

I considered adding another arity to the returned compact-fn that would accept a context along with the iri, but that forces us to re-transform the context for every compaction, an operation that can be quite costly if the context is large. So I elected to do the more clumsy approach of passing both.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I think about it, though, if we did go with the 2-arity compact-fn it would be called in multiple places with the same context, so if we added an LRU cache of size 1 in front of it that could make the code more elegant and not incur the penalty of re-processing the context for each compaction. Or just use a delay?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The atom isn't used for any of the compacting functions in this pull request; it's only used for commits.

As I mentioned on our last call, I think we should define a new function that takes a context and data needed to be compacted using that context and returns the compacted data for these usages, and we should only pass the context through these call stacks, not any compacting functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that compact is called multiple times in the same call stack, and doing the work to compact is expensive, which is why we rely on the compact-fn to do the expensive work once.

In the the fluree.db.query.exec.select namespace it looks like each of the different Selectors can call compact to format the results, and my understanding is that several Selectors may be called during one query. If that's the case, then re-creating the compact-fn for each of those calls could get expensive.

But! My understanding of how Selectors work may be wrong - if only one of them is used than we would only have one compact-fn per query.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, your understanding is correct. I think we can leave the extra contexts in for now possibly to re-think how we do the compaction and expansion later.


(defrecord VariableSelector [var]
ValueSelector
(format-value
[_ db iri-cache compact error-ch solution]
[_ db iri-cache context compact error-ch solution]
(-> solution
(get var)
(display db iri-cache compact error-ch))))
Expand All @@ -60,7 +60,7 @@
(defrecord AggregateSelector [agg-fn]
ValueSelector
(format-value
[_ db iri-cache compact error-ch solution]
[_ db iri-cache context compact error-ch solution]
(go (try* (agg-fn solution)
(catch* e
(log/error e "Error applying aggregate selector")
Expand All @@ -77,15 +77,15 @@
(defrecord SubgraphSelector [var selection depth spec]
ValueSelector
(format-value
[_ db iri-cache compact error-ch solution]
[_ db iri-cache context compact error-ch solution]
(go
(let [sid (-> solution
(get var)
::where/val)]
(try*
(let [flakes (<? (query-range/index-range db :spot = [sid]))]
;; TODO: Replace these nils with fuel values when we turn fuel back on
(<? (json-ld-resp/flakes->res db iri-cache compact nil nil spec 0 flakes)))
(<? (json-ld-resp/flakes->res db iri-cache context compact nil nil spec 0 flakes)))
(catch* e
(log/error e "Error formatting subgraph for subject:" sid)
(>! error-ch e)))))))
Expand All @@ -101,22 +101,23 @@
(defn format-values
"Formats the values from the specified where search solution `solution`
according to the selector or collection of selectors specified by `selectors`"
[selectors db iri-cache compact error-ch solution]
[selectors db iri-cache context compact error-ch solution]
(if (sequential? selectors)
(go-loop [selectors selectors
values []]
(if-let [selector (first selectors)]
(let [value (<! (format-value selector db iri-cache compact error-ch solution))]
(let [value (<! (format-value selector db iri-cache context compact error-ch solution))]
(recur (rest selectors)
(conj values value)))
values))
(format-value selectors db iri-cache compact error-ch solution)))
(format-value selectors db iri-cache context compact error-ch solution)))

(defn format
"Formats each solution within the stream of solutions in `solution-ch` according
to the selectors within the select clause of the supplied parsed query `q`."
[db q error-ch solution-ch]
(let [compact (->> q :context json-ld/compact-fn)
(let [context (:context q)
compact (json-ld/compact-fn context)
selectors (or (:select q)
(:select-one q)
(:select-distinct q))
Expand All @@ -126,7 +127,7 @@
format-ch
(fn [solution ch]
(log/debug "select/format solution:" solution)
(-> (format-values selectors db iri-cache compact error-ch solution)
(-> (format-values selectors db iri-cache context compact error-ch solution)
(async/pipe ch)))
solution-ch)
format-ch))
Expand Down
Loading