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

Refine default context handling #437

Merged
merged 4 commits into from
Apr 4, 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
16 changes: 9 additions & 7 deletions src/fluree/db/conn/file.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(:require [clojure.core.async :as async :refer [go]]
[clojure.string :as str]
[fluree.crypto :as crypto]
[fluree.db.util.context :as ctx-util]
[fluree.json-ld :as json-ld]
[fluree.db.index :as index]
[fluree.db.conn.proto :as conn-proto]
Expand Down Expand Up @@ -243,7 +244,7 @@
(-method [_] :file)
(-parallelism [_] parallelism)
(-id [_] id)
(-context [_] (:context ledger-defaults))
(-default-context [_] (:context ledger-defaults))
(-new-indexer [_ opts]
(let [indexer-fn (:indexer ledger-defaults)]
(indexer-fn opts)))
Expand Down Expand Up @@ -280,12 +281,13 @@
s))

(defn ledger-defaults
[{:keys [context-type context did indexer]}]
{:context (util/normalize-context context-type context)
:did did
:indexer (cond
(fn? indexer)
indexer
[{:keys [context context-type did indexer]}]
{:context (ctx-util/stringify-context context)
:context-type context-type
:did did
:indexer (cond
(fn? indexer)
indexer

(or (map? indexer) (nil? indexer))
(fn [opts]
Expand Down
22 changes: 12 additions & 10 deletions src/fluree/db/conn/ipfs.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns fluree.db.conn.ipfs
(:require [fluree.db.storage.core :as storage]
[fluree.db.index :as index]
[fluree.db.util.context :as ctx-util]
[fluree.db.util.core :as util :refer [exception?]]
#?(:clj [fluree.db.full-text :as full-text])
[fluree.db.util.log :as log :include-macros true]
Expand Down Expand Up @@ -109,8 +110,8 @@
(-method [_] :ipfs)
(-parallelism [_] parallelism)
(-id [_] id)
(-context [_] (:context ledger-defaults))
(-new-indexer [_ opts] ;; default new ledger indexer
(-default-context [_] (:context ledger-defaults))
(-new-indexer [_ opts] ;; default new ledger indexer
(let [indexer-fn (:indexer ledger-defaults)]
(indexer-fn opts)))
(-did [_] (:did ledger-defaults))
Expand Down Expand Up @@ -147,7 +148,7 @@

(defn ledger-defaults
"Normalizes ledger defaults settings"
[ipfs-endpoint {:keys [ipns context-type context did indexer] :as defaults}]
[ipfs-endpoint {:keys [ipns context context-type did indexer] :as _defaults}]
(go-try
(let [ipns-default-key (or (:key ipns) "self")
ipns-default-address (<? (ipfs-keys/address ipfs-endpoint ipns-default-key))
Expand All @@ -167,11 +168,12 @@
(throw (ex-info (str "IPNS publishing appears to have an issue. No corresponding ipns address found for key: "
ipns-default-key)
{:status 400 :error :db/ipfs-keys})))
{:ipns {:key ipns-default-key
:address ipns-default-address}
:context (util/normalize-context context-type context)
:did did
:indexer new-indexer-fn})))
{:ipns {:key ipns-default-key
:address ipns-default-address}
:context (ctx-util/stringify-context context)
:context-type context-type
:did did
:indexer new-indexer-fn})))


(defn connect
Expand All @@ -186,8 +188,8 @@
conn-id (str (random-uuid))
state (state-machine/blank-state)

cache-size (conn-cache/memory->cache-size memory)
lru-cache-atom (or lru-cache-atom (atom (conn-cache/create-lru-cache cache-size)))]
cache-size (conn-cache/memory->cache-size memory)
lru-cache-atom (or lru-cache-atom (atom (conn-cache/create-lru-cache cache-size)))]
;; TODO - need to set up monitor loops for async chans
(map->IPFSConnection {:id conn-id
:ipfs-endpoint ipfs-endpoint
Expand Down
10 changes: 6 additions & 4 deletions src/fluree/db/conn/memory.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [clojure.core.async :as async :refer [go]]
[fluree.db.storage.core :as storage]
[fluree.db.index :as index]
[fluree.db.util.context :as ctx-util]
[fluree.db.util.core :as util]
[fluree.db.util.log :as log :include-macros true]
#?(:clj [fluree.db.full-text :as full-text])
Expand Down Expand Up @@ -143,7 +144,7 @@
(-method [_] :memory)
(-parallelism [_] parallelism)
(-id [_] id)
(-context [_] (:context ledger-defaults))
(-default-context [_] (:context ledger-defaults))
(-new-indexer [_ opts] (idx-default/create opts)) ;; default new ledger indexer
(-did [_] (:did ledger-defaults))
(-msg-in [_ msg] (go-try
Expand Down Expand Up @@ -172,10 +173,11 @@

(defn ledger-defaults
"Normalizes ledger defaults settings"
[{:keys [context-type context did] :as _defaults}]
[{:keys [context did context-type] :as _defaults}]
(async/go
{:context (util/normalize-context context-type context)
:did did}))
{:context (ctx-util/stringify-context context)
:context-type context-type
:did did}))

(defn connect
"Creates a new memory connection."
Expand Down
2 changes: 1 addition & 1 deletion src/fluree/db/conn/proto.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
(-method [conn] "Returns connection method type (as keyword)")
(-parallelism [conn] "Returns parallelism integer to use for running multi-thread operations (1->8)")
(-id [conn] "Returns internal id for connection object")
(-context [conn] "Returns optional default context set at connection level")
(-default-context [conn] "Returns optional default context set at connection level")
(-new-indexer [conn opts] "Returns optional default new indexer object for a new ledger with optional opts.")
(-did [conn] "Returns optional default did map if set at connection level")
(-msg-in [conn msg] "Handler for incoming message from connection service")
Expand Down
112 changes: 74 additions & 38 deletions src/fluree/db/db/json_ld.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
[fluree.db.json-ld.transact :as jld-transact]
[fluree.db.indexer.proto :as idx-proto]
[fluree.db.util.log :as log]
[fluree.db.util.context :as ctx-util]
[fluree.db.json-ld.commit-data :as commit-data])
#?(:clj (:import (java.io Writer))))

Expand Down Expand Up @@ -242,11 +243,12 @@

(defn expand-iri
"Expands an IRI from the db's context."
([{:keys [schema] :as _db} iri]
(json-ld/expand-iri iri (:context schema)))
([{:keys [schema] :as _db} iri provided-context]
(->> (json-ld/parse-context (:context schema) provided-context)
(json-ld/expand-iri iri))))
([db iri]
(expand-iri db iri nil))
([db iri provided-context]
(if (keyword? iri)
(json-ld/expand-iri iri (dbproto/-context db provided-context :keyword))
(json-ld/expand-iri iri (dbproto/-context db provided-context :string)))))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this would simplify my work for fluree/http-api-gateway#38. In order to support policy opts via http-api, I need to make sure that role iris will be correctly expanded/resolved to subject ids. That has required weaving in some context-type logic into places where it wasn't previously, but with this I think I wouldn't need to do that at all.


(defn iri->sid
"Returns subject id or nil if no match.
Expand Down Expand Up @@ -418,13 +420,36 @@
(assoc-in [:stats :indexed] index-t))
db)))

(defn retrieve-context
"Returns the parsed context. Caches."
[default-context context-cache supplied-context context-type]
(log/trace "retrieve-context - default: " default-context "supplied:" supplied-context "context-type: " context-type)
(or (get-in @context-cache [context-type supplied-context])
(let [context (if supplied-context
(if (sequential? supplied-context)
(mapv #(if (= "" %)
;; we need to substitute in the default context, keywordize if of type :keyword
(if (= :keyword context-type)
(ctx-util/keywordize-context default-context)
default-context)
%)
supplied-context)
supplied-context)
(if (= :keyword context-type)
(ctx-util/keywordize-context default-context)
default-context))
parsed-ctx (json-ld/parse-context context)]
(vswap! context-cache assoc-in [context-type supplied-context] parsed-ctx)
parsed-ctx)))


;; ================ end GraphDB record support fns ============================

;; TODO - conn is included here because current index-range query looks for conn on the db
;; TODO - this can likely be excluded once index-range is changed to get 'conn' from (:conn ledger) where it also exists
(defrecord JsonLdDb [ledger conn method alias branch commit block t tt-id stats
spot psot post opst tspo schema comparators novelty
policy ecount]
spot psot post opst tspo schema comparators novelty policy ecount
default-context context-type context-cache new-context?]
dbproto/IFlureeDb
(-latest-db [this] (graphdb-latest-db this))
(-rootdb [this] (graphdb-root-db this))
Expand Down Expand Up @@ -457,7 +482,11 @@
(-db-type [_] :json-ld)
(-stage [db json-ld] (jld-transact/stage db json-ld nil))
(-stage [db json-ld opts] (jld-transact/stage db json-ld opts))
(-index-update [db commit-index] (index-update db commit-index)))
(-index-update [db commit-index] (index-update db commit-index))
(-context [_] (retrieve-context default-context context-cache nil context-type))
(-context [_ context] (retrieve-context default-context context-cache context context-type))
(-context [_ context type] (retrieve-context default-context context-cache context (or type context-type)))
(-default-context [_] default-context))

#?(:cljs
(extend-type JsonLdDb
Expand Down Expand Up @@ -498,39 +527,46 @@


(defn create
[{:keys [method alias conn] :as ledger}]
(let [novelty (new-novelty-map index/default-comparators)
[{:keys [method alias conn] :as ledger} default-context context-type new-context?]
(let [novelty (new-novelty-map index/default-comparators)
{spot-cmp :spot
psot-cmp :psot
post-cmp :post
opst-cmp :opst
tspo-cmp :tspo} index/default-comparators

spot (index/empty-branch method alias spot-cmp)
psot (index/empty-branch method alias psot-cmp)
post (index/empty-branch method alias post-cmp)
opst (index/empty-branch method alias opst-cmp)
tspo (index/empty-branch method alias tspo-cmp)
stats {:flakes 0, :size 0, :indexed 0}
schema (vocab/base-schema)
branch (branch/branch-meta ledger)]
(map->JsonLdDb {:ledger ledger
:conn conn
:method method
:alias alias
:branch (:name branch)
:commit (:commit branch)
:block 0
:t 0
:tt-id nil
:stats stats
:spot spot
:psot psot
:post post
:opst opst
:tspo tspo
:schema schema
:comparators index/default-comparators
:novelty novelty
:policy root-policy-map
:ecount genesis-ecount})))
spot (index/empty-branch method alias spot-cmp)
psot (index/empty-branch method alias psot-cmp)
post (index/empty-branch method alias post-cmp)
opst (index/empty-branch method alias opst-cmp)
tspo (index/empty-branch method alias tspo-cmp)
stats {:flakes 0, :size 0, :indexed 0}
schema (vocab/base-schema)
branch (branch/branch-meta ledger)
context-type* (if (not= :keyword context-type)
:string
context-type)]
(map->JsonLdDb {:ledger ledger
:conn conn
:method method
:alias alias
:branch (:name branch)
:commit (:commit branch)
:block 0
:t 0
:tt-id nil
:stats stats
:spot spot
:psot psot
:post post
:opst opst
:tspo tspo
:schema schema
:comparators index/default-comparators
:novelty novelty
:policy root-policy-map
:default-context default-context
:context-type context-type*
:context-cache (volatile! nil)
:new-context? new-context?
:ecount genesis-ecount})))
4 changes: 3 additions & 1 deletion src/fluree/db/dbproto.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
(-add-predicate-to-idx [db pred-id] "Adds predicate to idx, return updated db.")
(-db-type [db] "Returns db type, e.g. :json-ld, :json")
(-stage [db tx] [db tx opts] "Stages a database transaction.")
(-index-update [db commit-index] "Updates db to reflect a new index point described by commit-index metadata"))
(-index-update [db commit-index] "Updates db to reflect a new index point described by commit-index metadata")
(-context [db] [db context] [db context context-type] "Returns parsed context given supplied context. If no context is supplied, returns default context.")
(-default-context [db] "Returns the default context the db is configured to use."))

(defn db?
[db]
Expand Down
10 changes: 3 additions & 7 deletions src/fluree/db/json_ld/api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,9 @@
"
([conn] (create conn nil nil))
([conn ledger-alias] (create conn ledger-alias nil))
([conn ledger-alias {:keys [context context-type] :as opts}]
(let [conn-context (conn-proto/-context conn)
ledger-context (->> context
(util/normalize-context context-type)
(clojure.core/merge conn-context))
res-ch (jld-ledger/create conn ledger-alias (assoc opts :context ledger-context))]
(promise-wrap res-ch))))
([conn ledger-alias opts]
(promise-wrap
(jld-ledger/create conn ledger-alias opts))))

(defn load-from-address
"Loads a ledger defined with a Fluree address, e.g.:
Expand Down
29 changes: 13 additions & 16 deletions src/fluree/db/json_ld/commit.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -392,32 +392,28 @@

(defn link-context-to-commit
"Takes a commit with an embedded :context and pulls it out, saves it to
storage separately (content-addressed), and puts the address of that data
back into the commit under the :context key."
[{:keys [conn] :as ledger} commit]
storage separately (content-addressed).

Returns a two-tuple of [updated-commit context-file-write-response]"
[{:keys [conn ledger default-context] :as db} commit]
(go-try
(let [context (get commit (keyword const/iri-default-context))
context-res (when (map? context)
(->> context
util/stringify-keys
(conn-proto/-ctx-write conn ledger)
<?))]
{:context-res context-res
:commit (if context-res
(assoc commit (keyword const/iri-default-context) (:address context-res))
commit)})))
(let [context-res (<? (conn-proto/-ctx-write conn ledger default-context))
commit* (assoc commit (keyword const/iri-default-context) (:address context-res))]
[commit* context-res])))

(defn do-commit+push
"Writes commit and pushes, kicks off indexing if necessary."
[{:keys [ledger commit] :as db} {:keys [branch push? did private] :as _opts}]
[{:keys [ledger commit new-context?] :as db} {:keys [branch push? did private] :as _opts}]
(go-try
(let [{:keys [conn state]} ledger
ledger-commit (:commit (ledger-proto/-status ledger branch))
new-t? (or (nil? (commit-data/t ledger-commit))
(> (commit-data/t commit) (commit-data/t ledger-commit)))
new-commit (commit-data/use-latest-index commit ledger-commit)
_ (log/debug "do-commit+push new-commit:" new-commit)
{new-commit* :commit context-res :context-res} (<? (link-context-to-commit ledger new-commit))
[new-commit* context-res] (if new-context?
(<? (link-context-to-commit db new-commit))
[new-commit nil])
_ (log/debug "do-commit+push new-commit w/ linked context:"
new-commit*)
[new-commit** jld-commit] (commit-data/commit-jsonld new-commit*)
Expand All @@ -426,7 +422,8 @@
jld-commit)
commit-res (<? (conn-proto/-c-write conn ledger signed-commit)) ;; write commit credential
new-commit*** (commit-data/update-commit-address new-commit** (:address commit-res))
db* (assoc db :commit new-commit***) ;; branch published to
db* (assoc db :commit new-commit***
:new-context? false)
db** (if new-t?
(<? (add-commit-flakes (:prev-commit db) db*))
db*)
Expand Down
Loading