-
Notifications
You must be signed in to change notification settings - Fork 8
/
doxa.cljc
419 lines (351 loc) · 11.3 KB
/
doxa.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
(ns ribelo.doxa
(:refer-clojure :exclude [-next -first filter])
(:require
[ribelo.extropy :as ex]
[ribelo.doxa.extend]
[ribelo.doxa.protocols :as p]
[ribelo.doxa.pull-api :refer [-pull -mpull]]
[ribelo.doxa.query :refer [-q -mq]]
[ribelo.doxa.pick-api :refer [-pick]]
[ribelo.doxa.util :as u]))
(comment
(require '[taoensso.encore :as enc])
(require '[clj-async-profiler.core :as prof]))
#?(:clj (set! *warn-on-reflection* true))
(def dx? u/-dx?)
(def normalize u/-normalize)
(def denormalize u/-denormalize)
(def pull -pull)
(def mpull -mpull)
(def q -q)
(def mq -mq)
(def pick -pick)
(defmulti -submit-commit (fn [_db ^clojure.lang.IPersistentVector tx] (nth tx 0)))
(defmulti -submit-failure
(fn
([tx] (nth tx 0))
([tx _msg] (nth tx 0))))
(defmethod -submit-failure :default
([tx] (throw (ex-info "Using default submit error handler, consider using your own!" {:tx tx})))
([tx msg] (throw (ex-info msg {:tx tx}))))
(defmethod -submit-failure :dx/put
([tx]
(throw (ex-info "Invalid put commit" {:tx tx})))
([tx msg]
(throw (ex-info msg {:tx tx}))))
(defmethod -submit-commit :dx/put
[dx tx]
(let [cnt (count tx)]
(case cnt
2 (let [x (nth tx 1)]
(cond
(u/-entity? x)
(u/-put-entity dx x)
(u/-entities? x)
(u/-put-entities dx x)
:else
(-submit-failure tx)))
(let [ref (nth tx 1)
tid (nth ref 0)
eid (nth ref 1)]
(when-not (u/-ref-lookup? ref)
(-submit-failure tx))
(case cnt
3 (let [m (nth tx 2)]
(if-let [ref (u/-entity-ref m)]
(if (ex/kw-identical? eid ref)
(u/-put-entity dx m)
(-submit-failure tx))
(if (map? m)
(u/-put-entity dx (assoc m tid eid))
(-submit-failure tx))))
4 (let [k (nth tx 2)
v (nth tx 3)]
(if (u/-entity? v)
(-> (u/-safe-put-kv dx ref k (u/-entity-ref v))
(u/-put-entity (u/-safe-merge v (u/-key->rvd k) ref)))
(if-let [ids (u/-entities-refs v)]
(-> (u/-safe-put-kv dx ref k ids)
(u/-put-entities (mapv (fn [m] (assoc m (u/-key->rvd k) ref)) v)))
(if (u/-ref-lookup? v)
(if (dx v)
(u/-safe-put-kv dx ref k v)
(-submit-failure tx))
(u/-safe-put-kv dx ref k v)))))
(if (pos? cnt)
(apply u/-safe-put-kvs dx ref (into [] (drop 2) tx))
(-submit-failure tx)))))))
(defmethod -submit-commit :dx/merge
[dx tx]
(let [cnt (count tx)]
(case cnt
2 (let [x (nth tx 1)]
(cond
(u/-entity? x)
(u/-merge-entity dx x)
(u/-entities? x)
(u/-merge-entities dx x)
:else
(-submit-failure tx)))
(let [ref (nth tx 1)
tid (nth ref 0)
eid (nth ref 1)]
(when-not (u/-ref-lookup? ref)
(-submit-failure tx))
(case cnt
3 (let [m (nth tx 2)]
(if-let [ref (u/-entity-ref m)]
(if (ex/kw-identical? eid ref)
(u/-merge-entity dx m)
(-submit-failure tx))
(if (map? m)
(u/-merge-entity dx (assoc m tid eid))
(-submit-failure tx))))
4 (let [k (nth tx 2)
v (nth tx 3)]
(if (u/-entity? v)
(-> (u/-safe-put-kv dx ref k (u/-entity-ref v))
(u/-merge-entity v))
(if-let [ids (u/-entities-refs v)]
(-> (u/-safe-put-kv dx ref k ids)
(u/-merge-entities v))
(if (u/-ref-lookup? v)
(if (dx v)
(u/-safe-put-kv dx ref k v)
(-submit-failure tx))
(u/-safe-merge-kv dx ref k v)))))
(if (pos? cnt)
(apply u/-safe-put-kvs dx ref (into [] (drop 2) tx))
(-submit-failure tx)))))))
(defmethod -submit-failure :dx/delete
([tx]
(throw (ex-info "Invalid delete commit" {:tx tx})))
([tx msg]
(throw (ex-info msg {:tx tx}))))
(defmethod -submit-commit :dx/delete
[dx tx]
(let [cnt (count tx)]
(case cnt
2 (let [x (nth tx 1)]
(cond
(map? x)
(u/-delete-entity dx (u/-entity-ref x))
(u/-ref-lookup? x)
(u/-delete-entity dx x)
(keyword? x)
(u/-delete-table dx x)
))
3 (let [ref (nth tx 1)
k (nth tx 2)]
(if (vector? k)
(ex/loop-it [j k :let [acc dx]]
(recur (u/-delete-key acc ref j))
acc)
(u/-delete-key dx ref k)))
4 (let [ref (nth tx 1)
k (nth tx 2)
x (nth tx 3)]
(u/-delete-val dx ref k x)))))
(defmethod -submit-commit :dx/update
[dx tx]
(let [cnt (count tx)
ref (nth tx 1)]
(if (>= cnt 3)
(let [k-or-f (nth tx 2)]
(cond
(keyword? k-or-f)
(let [k k-or-f
f (nth tx 3)
x (get-in dx [ref k])]
(p/-put dx ref (assoc (dx ref {}) k (apply f x (drop 4 tx)))))
(fn? k-or-f)
(let [f k-or-f
m (dx ref)]
(p/-put dx ref (apply f m (drop 3 tx))))
:else
(-submit-failure tx)))
(-submit-failure tx))))
(defmethod -submit-commit :dx/match
[dx tx]
(let [cnt (count tx)
ref-or-map (nth tx 1)]
(case cnt
2 (if (map? ref-or-map)
(if-let [ref (u/-entity-ref ref-or-map)]
(let [e (dx ref)]
(reduce-kv
(fn [_ k v]
(if (= v (get e k)) true (reduced false)))
false
(dx ref)))
(-submit-failure tx))
(-submit-failure tx))
(let [x (nth tx 2)
e (dx ref-or-map)]
(case cnt
3 (cond
(fn? x)
(x e)
(map? x)
(reduce-kv
(fn [_ k v]
(if (= v (get e k)) true (reduced false)))
false
x))
4 (let [v (nth tx 3)]
(if (fn? v)
(v (get e x))
(if-not (fn? x)
(let [v' (get e x)]
(or (= v v')
(when (coll? v') (ex/some #{v} v'))))
(-submit-failure tx "function should be the 3rd element")))))))))
(defn commit
([dx txs] (commit dx txs nil))
([dx txs meta']
(let [txs (if (and (vector? txs) (vector? (first txs))) txs [txs])
dx'
(ex/loop-it [tx txs :let [acc (p/-clear-tx dx) match? true]]
(let [kind (nth tx 0)]
(if (ex/kw-identical? :dx/match kind)
(recur acc (-submit-commit acc tx))
(if match?
(if-let [db' (-submit-commit acc tx)]
(recur db' match?)
(throw (ex-info "db is nil!" {})))
(recur acc match?))))
acc)
dx' (-> (p/-set-cache! dx' (p/-refresh (some-> (p/-cache dx') deref) (p/-tx dx'))) (p/-reindex))]
(when-let [listeners (some-> dx' meta ::listeners deref)]
(ex/run! (fn [[_ f]] (f dx')) listeners))
dx')))
(defn commit! [conn_ txs]
(swap! conn_ commit txs))
(defn dx-with
([data] (dx-with {} data))
([dx data]
(commit dx (mapv (fn [m] [:dx/merge m]) data))))
(defn create-dx
([] (create-dx {}))
([empty-db]
(create-dx empty-db []))
([empty-db data]
(create-dx empty-db data {}))
([empty-db data meta]
(let [default-meta {::listeners (atom {})}]
(if (not-empty data)
(vary-meta (dx-with empty-db data) merge default-meta meta)
(vary-meta empty-db merge default-meta meta)))))
(defn connect!
([dx]
(p/-connect dx))
([dx x]
(p/-connect dx x)))
(declare entity)
(defn listen! [dx k f]
(if-let [atm_ (some-> dx meta ::listeners)]
(do (swap! atm_ assoc k f)
(fn [] (swap! atm_ dissoc k)))
(throw (ex-info "cannot add the listener, the atom does not exist" {:meta (meta dx)}))))
(defn unlisten! [dx k]
(if-let [atm_ (some-> dx meta ::listeners)]
(do (swap! atm_ dissoc k) true)
(throw (ex-info "cannot remove the listener, the atom does not exist" {:meta (meta dx)}))))
(defn -entity-lookup [dx ref k denormalize?]
(let [e (dx ref)]
(if denormalize?
(when-let [x (get e k)]
(if (u/-ref-lookup? x)
(entity dx ref {:denormalize? denormalize?})
(if (u/-ref-lookups? x)
(into (empty x) (fn [ref] (entity dx ref {:denormalize? denormalize?})) x)
x)))
(get e k))))
(defn entity
([dx ref] (entity dx ref {}))
([dx ref {:keys [denormalize?]}]
{:pre [(u/-probably-dx? dx)]}
(when (dx ref)
#?(:clj
(reify
clojure.lang.ILookup
(valAt [this k]
(-entity-lookup dx ref k denormalize?))
clojure.lang.Associative
(assoc [this k v]
(entity (commit! dx [:dx/put ref k v]) ref))
clojure.lang.IDeref
(deref [this]
(when-let [e (dx ref)]
(if denormalize?
(denormalize dx e)
e))))
:cljs
(reify
IMap
IPrintWithWriter
(-pr-writer [this writer opts]
(-pr-writer (-deref this) writer opts))
ILookup
(-lookup [this k]
(-entity-lookup dx ref k denormalize?))
IAssociative
(-assoc [this k v]
(entity (commit! dx [:dx/put ref k v]) ref))
IDeref
(-deref [this]
(when-let [e (dx ref)]
(if denormalize?
(denormalize dx e)
e))))))))
(defn table [dx table]
(if-let [xs (some-> dx p/-index (get table))]
(ex/select-keys dx xs)
(empty dx)))
;; (defn- -filter [pred dx]
;; (persistent!
;; (reduce
;; (fn [acc ref]
;; (let [e (entity dx ref {:denormalize? true})]
;; (if (pred e) (assoc! acc ref e) acc)))
;; (transient {})
;; (-keys dx))))
;; (defn- -view [dx f]
;; (let [cache_ (volatile! nil)]
;; (reify
;; clojure.lang.IRef
;; (deref [_]
;; (if-let [snapshot @cache_]
;; snapshot
;; (vreset! cache_ (f dx)))))))
;; (defn filter [pred dx]
;; (let [view (-view dx (partial -filter pred))
;; err (ex-info "you shouldn't use put on view" {:dx dx})]
;; (reify
;; clojure.lang.IRef
;; (deref [_]
;; @view)
;; clojure.lang.IKVReduce
;; (kvreduce [_ f init]
;; (reduce-kv f init (-filter pred dx)))
;; i/IDoxa
;; (i/-put [_ _ _]
;; (throw err))
;; (i/-put [_ _ _ _]
;; (throw err))
;; (i/-pick [this e]
;; (i/-pick (-filter pred dx) e))
;; (i/-pick [this e a]
;; (i/-pick (-filter pred dx) e a))
;; (i/-pick [this e a v]
;; (i/-pick (-filter pred dx) e a v))
;; (i/-del [_ _]
;; (throw err))
;; (i/-del [_ _ _]
;; (throw err))
;; (i/-del [_ _ _ _]
;; (throw err))
;; (-keys [this]
;; (-keys (-filter pred dx)))
;; (-entities [this]
;; (vals (-filter pred dx))))))