|
7 | 7 | [fluree.db.util.core :as util :refer [try* catch*]]
|
8 | 8 | [fluree.db.util.json :as json]
|
9 | 9 | [fluree.db.util.log :as log]
|
10 |
| - [fluree.db.virtual-graph :as vg])) |
| 10 | + [fluree.db.virtual-graph :as vg] |
| 11 | + [fluree.db.json-ld.iri :as iri])) |
11 | 12 |
|
12 | 13 | #?(:clj (set! *warn-on-reflection* true))
|
13 | 14 |
|
|
16 | 17 | (reduce
|
17 | 18 | (fn [acc idx-flake]
|
18 | 19 | (cond
|
| 20 | + (false? (flake/op idx-flake)) |
| 21 | + (assoc acc :retraction? true) |
| 22 | + |
19 | 23 | (= (flake/p idx-flake) const/$fluree:virtualGraph-name)
|
20 | 24 | (assoc acc :vg-name (flake/o idx-flake))
|
21 | 25 |
|
|
32 | 36 | :error :db/invalid-index}))))
|
33 | 37 |
|
34 | 38 | :else acc))
|
35 |
| - {:id (flake/s (first index-flakes)) |
36 |
| - :type [] |
37 |
| - :vg-name nil |
38 |
| - :query nil} |
| 39 | + {:id (flake/s (first index-flakes)) |
| 40 | + :type [] |
| 41 | + :vg-name nil |
| 42 | + :retraction? false ;; if we find a 'retract' op flake, flag so we know it is an attempt to update/delete |
| 43 | + :query nil} |
39 | 44 | index-flakes))
|
40 | 45 |
|
| 46 | +(defn throw-if-defined |
| 47 | + "Ensures a new virtual graph does not reuse an existing virtual |
| 48 | + graph alias, or create a new virtual graph using an existing vg IRI" |
| 49 | + [{:keys [vg] :as db} named-graph vg-sid] |
| 50 | + (when (contains? vg named-graph) |
| 51 | + (throw (ex-info (str "Virtual graph alias: " named-graph " already exists in db.") |
| 52 | + {:status 400 |
| 53 | + :error :db/invalid-index}))) |
| 54 | + |
| 55 | + (when (some #(= vg-sid (:id %))(vals vg)) |
| 56 | + (throw (ex-info (str "Virtual graph IRI already exists in db: " (iri/decode-sid db vg-sid)) |
| 57 | + {:status 400 |
| 58 | + :error :db/invalid-index})))) |
| 59 | + |
41 | 60 | #?(:clj
|
42 | 61 | (defn create
|
43 |
| - [{:keys [t] :as db} vg-flakes] |
| 62 | + [{:keys [t] :as db} vg-opts vg-flakes] |
44 | 63 | (let [db-vol (volatile! db) ;; needed to potentially add new namespace codes based on query IRIs
|
45 | 64 |
|
46 |
| - {:keys [type] :as vg-opts} |
47 |
| - (-> (idx-flakes->opts vg-flakes) |
48 |
| - (vg-parse/parse-document-query db-vol) |
| 65 | + {:keys [type vg-name] :as vg-opts*} |
| 66 | + (-> (vg-parse/parse-document-query vg-opts db-vol) |
49 | 67 | (assoc :genesis-t t))
|
50 | 68 |
|
| 69 | + named-graph (vg/named-graph-str vg-name) |
| 70 | + |
| 71 | + _ (throw-if-defined db named-graph (:id vg-opts*)) |
| 72 | + |
51 | 73 | db* @db-vol
|
52 | 74 | vg (cond
|
53 | 75 | ;; add vector index and other types of virtual graphs here
|
54 | 76 | (bm25/bm25-iri? type)
|
55 |
| - (bm25/new-bm25-index db vg-flakes vg-opts) |
| 77 | + (bm25/new-bm25-index db vg-flakes vg-opts*) |
56 | 78 |
|
57 | 79 | :else (throw (ex-info "Unrecognized virtual graph creation attempted."
|
58 | 80 | {:status 400
|
59 |
| - :error :db/invalid-index}))) |
60 |
| - vg* (vg/initialize vg db*)] |
61 |
| - [db* vg*])) |
| 81 | + :error :db/invalid-index})))] |
| 82 | + (assoc-in db* [:vg named-graph] (vg/initialize vg db*)))) |
62 | 83 |
|
63 | 84 | :cljs
|
64 | 85 | (defn create
|
65 |
| - [_ _] |
| 86 | + [_ _ _] |
66 | 87 | (throw (ex-info "Creating BM25 indexes not supported in cljs"
|
67 | 88 | {:status 400, :error :db/invalid-index}))))
|
68 | 89 |
|
| 90 | +(defn remove-vg? |
| 91 | + [vg-flakes] |
| 92 | + (some #(and (false? (flake/op %)) |
| 93 | + (= const/$rdf:type (flake/p %)) |
| 94 | + (= const/$fluree:VirtualGraph (flake/o %))) |
| 95 | + vg-flakes)) |
| 96 | + |
| 97 | +(defn modify |
| 98 | + [{:keys [t] :as db} {:keys [vg-name] :as _vg-opts} vg-flakes] |
| 99 | + (let [named-graph (vg/named-graph-str vg-name)] |
| 100 | + (if (remove-vg? vg-flakes) |
| 101 | + (update db :vg dissoc named-graph) |
| 102 | + (throw (ex-info "Virtual graph update not supported." |
| 103 | + {:status 400 |
| 104 | + :error :db/invalid-index}))))) |
| 105 | + |
69 | 106 | (defn load-virtual-graph
|
70 | 107 | [db alias]
|
71 | 108 | (or (get-in db [:vg alias])
|
|
92 | 129 | {} vg)]
|
93 | 130 | (assoc db :vg vg*)))
|
94 | 131 |
|
95 |
| -(defn create-virtual-graphs |
| 132 | +(defn update-virtual-graphs |
96 | 133 | "Creates a new virtual graph. If the virtual graph is invalid, an
|
97 | 134 | exception will be thrown and the transaction will not complete."
|
98 |
| - [db add new-vgs] |
99 |
| - (loop [[new-vg & r] new-vgs |
| 135 | + [db add vg-sids] |
| 136 | + (loop [[vg-sid & r] vg-sids |
100 | 137 | db db]
|
101 |
| - (if new-vg |
102 |
| - (let [vg-flakes (filter #(= (flake/s %) new-vg) add) |
103 |
| - [db* vg] (create db vg-flakes) |
104 |
| - named-graph (vg/named-graph-alias vg)] |
105 |
| - ;; TODO - VG - ensure alias is not being used, throw if so |
106 |
| - (recur r (assoc-in db* [:vg named-graph] vg))) |
| 138 | + (if vg-sid |
| 139 | + (let [vg-flakes (filter #(= (flake/s %) vg-sid) add) |
| 140 | + {:keys [retraction?] :as vg-opts} (idx-flakes->opts vg-flakes) |
| 141 | + db* (if retraction? |
| 142 | + (modify db vg-opts vg-flakes) |
| 143 | + (create db vg-opts vg-flakes))] |
| 144 | + (recur r db*)) |
107 | 145 | db)))
|
108 | 146 |
|
109 | 147 | (defn has-vgs?
|
|
115 | 153 | (-> f flake/o (= const/$fluree:VirtualGraph)))
|
116 | 154 |
|
117 | 155 | (defn extract-vgs
|
| 156 | + "Extracts and returns unique SIDs for virtual graphs defined in a stage operation." |
118 | 157 | [fs]
|
119 |
| - (->> fs |
120 |
| - (keep (fn [f] |
121 |
| - (when (virtual-graph? f) |
122 |
| - (flake/s f)))) |
123 |
| - set)) |
| 158 | + (reduce |
| 159 | + (fn [acc f] |
| 160 | + (if (virtual-graph? f) |
| 161 | + (conj acc (flake/s f)) |
| 162 | + acc)) |
| 163 | + #{} fs)) |
124 | 164 |
|
125 | 165 | (defn check-virtual-graph
|
126 | 166 | [db add rem]
|
127 |
| - ;; TODO - VG - should also check for retractions to "delete" virtual graph |
128 |
| - ;; TODO - VG - check flakes if user updated existing virtual graph |
129 |
| - (let [new-vgs (extract-vgs add)] |
| 167 | + (let [vg-sids (extract-vgs add)] |
130 | 168 | (cond-> db
|
131 |
| - (seq new-vgs) (create-virtual-graphs add new-vgs) |
| 169 | + (seq vg-sids) (update-virtual-graphs add vg-sids) |
132 | 170 | (has-vgs? db) (update-vgs add rem))))
|
0 commit comments