-
Notifications
You must be signed in to change notification settings - Fork 19
/
tree.cljc
570 lines (514 loc) · 19.7 KB
/
tree.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
(ns hitchhiker.tree
"Default tree implementation
The parts of the serialization system that seem like they're need hooks are:
* Must provide a function that takes a node, serializes it, and returns an addr
* Must be able to rollback writing an addr
* Whatever the addr it returns, it should cache its resolve in-mem
somehow
* The serialize a node & rollback a node functions should accept a
'stats' object as well
* The 'stats' object must be convertible to a summary or whatever at the end"
(:refer-clojure :exclude [subvec])
(:require
[hitchhiker.tree.utils.async :as ha :include-macros true]
[hitchhiker.tree.node :as n]
[hitchhiker.tree.backend :as b]
[hitchhiker.tree.key-compare :as c]
[clojure.core.rrb-vector :refer [catvec subvec]]
#?(:clj [clojure.core.async :as async]
:cljs [cljs.core.async :as async :include-macros true])))
(def ^:const version 0)
(defrecord Config [index-b data-b op-buf-size])
(defrecord Split [left right median])
(declare data-node
index-node
flush-tree
flush-tree-without-root
index-node?
data-node?
resolved?)
(defn index-node-keys
"Calculates the separating keys given the children of an index node"
[children]
(into []
(map n/-last-key)
(pop children)))
(defmacro <?-resolve
[n]
`(let [n# ~n]
(if (resolved? n#)
n#
(ha/<? (n/-resolve-chan n#)))))
(defn <-cache
[cache calc-fn]
(let [c @cache]
(if (#?(:clj identical?
:cljs keyword-identical?)
c ::nothing)
(vreset! cache (calc-fn))
c)))
(defn cache
[]
(volatile! ::nothing))
(defrecord IndexNode [children
storage-addr
op-buf
cfg
*last-key-cache
version]
n/IIndexNode
n/IResolved
n/IAddress
(-dirty? [this]
(not (async/poll! storage-addr)))
(-dirty! [this]
(assoc this
:storage-addr (async/promise-chan)
:*last-key-cache (cache)))
n/INode
(-last-key [this]
(<-cache *last-key-cache
#(n/-last-key (peek children))))
(-overflow? [this]
(>= (count children)
(* 2 (:index-b cfg))))
(-underflow? [this]
(< (count children)
(:index-b cfg)))
(-split-node [this]
(let [b (:index-b cfg)
median (some-> (nth children (dec b)) n/-last-key)
;; TODO this should use msg/affects-key
op-bufs (sort-by :key c/-compare op-buf)]
(loop [op-bufs op-bufs
left-buf (transient [])
right-buf (transient [])]
(if-let [op-buf (first op-bufs)]
;; check if we are still on left side
(if (not (pos? (c/-compare (:key op-buf) median)))
(recur (next op-bufs)
(conj! left-buf op-buf)
right-buf)
;; otherwise just copy the rest on the right
(recur nil
left-buf
(reduce conj! right-buf op-bufs)))
(->Split (index-node (subvec children 0 b)
(persistent! left-buf)
cfg)
(index-node (subvec children b)
(persistent! right-buf)
cfg)
median)))))
(-merge-node [this other]
(index-node (catvec children (:children other))
(catvec op-buf (:op-buf other))
cfg))
(-lookup [this k]
;; This is written like so because it's performance critical
(let [l (dec (count children))
a (object-array l)
_ (dotimes [i l]
(aset a i (n/-last-key (nth children i))))
x #?(:clj (java.util.Arrays/binarySearch a 0 l k c/-compare)
:cljs (goog.array/binarySearch a k c/-compare))]
(if (neg? x)
(- (inc x))
x))))
(defn index-node
[children op-buf cfg]
(->IndexNode children
(async/promise-chan)
op-buf
cfg
(cache)
version))
(defn nth-of-set
"Like nth, but for sorted sets. O(n) in worst case, 0(1) when idx out
of bounds."
[set index]
;; we can escape early for free since sorted-sets are ICounted
(when (> (count set) index)
(loop [i 0
set set]
(if (< i index)
(recur (unchecked-inc i)
(next set))
(first set)))))
;; To allow extensions of the comparison protocol after loading the
;; hitchhiker-tree we deref the comparator at invocation time. An alternative of
;; constructing the empty sets as non-singletons during runtime was explored in
;; https://github.com/replikativ/hitchhiker-tree/pull/22, but performed worse in
;; the benchmarks.
(def empty-sorted-map-by-compare (sorted-map-by (fn [a b] (@#'c/-compare a b))))
(defrecord DataNode [children storage-addr cfg *last-key-cache version]
n/IDataNode
n/IResolved
n/IAddress
(-dirty? [this] (not (async/poll! storage-addr)))
(-dirty! [this]
(assoc this
:storage-addr (async/promise-chan)
:*last-key-cache (cache)))
n/INode
(-last-key [this]
(<-cache *last-key-cache
#(when (seq children)
(-> children
(rseq)
(first)
(key)))))
;; Should have between b & 2b-1 children
(-overflow? [this]
(>= (count children) (* 2 (:data-b cfg))))
(-underflow? [this]
(< (count children) (:data-b cfg)))
(-split-node [this]
(let [data-b (:data-b cfg)]
(loop [children children
i 0
left empty-sorted-map-by-compare
right empty-sorted-map-by-compare]
(if-let [child (first children)]
(if (< i data-b)
(recur (next children)
(inc i)
(conj left child)
right)
(recur nil
(inc i)
left
(reduce conj right children)))
(->Split (data-node left cfg)
(data-node right cfg)
(nth-of-set children (dec data-b)))))))
(-merge-node [this other]
(data-node (into children (:children other))
cfg))
(-lookup [root k]
(let [x #?(:clj (java.util.Collections/binarySearch (vec (keys children))
k
c/-compare)
:cljs (goog.array/binarySearch (into-array (keys children))
k
c/-compare))]
(if (neg? x)
(- (inc x))
x))))
(defn data-node
"Creates a new data node"
[children cfg]
(->DataNode children
(async/promise-chan)
cfg
(cache)
version))
(defn data-node?
[node]
(instance? DataNode node))
(defn index-node?
[node]
(instance? IndexNode node))
(defn resolved?
[node]
(or (index-node? node)
(data-node? node)))
(defn backtrack-up-path-until
"Given a path (starting with root and ending with an index), searches
backwards, passing each pair of parent & index we just came from to
the predicate function. When that function returns true, we return
the path ending in the index for which it was true, or else we
return the empty path"
[path pred]
(loop [path path]
(when (pos? (count path))
(let [from-index (peek path)
tmp (pop path)
parent (peek tmp)]
(if (pred parent from-index)
path
(recur (pop tmp)))))))
(defn right-successor
"Given a node on a path, find's that node's right successor node"
[path]
;; FIXME this function would benefit from a prefetching hint to keep
;; the next several sibs in mem
(ha/go-try
(when-let [common-parent-path
(backtrack-up-path-until path
(fn [parent index]
(< (inc index)
(count (:children parent)))))]
(let [next-index (-> common-parent-path peek inc)
parent (-> common-parent-path pop peek)
new-sibling (-> (nth (:children parent)
next-index)
<?-resolve)
sibling-lineage (loop [res (transient [new-sibling])
s new-sibling]
(let [first-child (-> s :children first)]
(if (n/address? first-child)
(let [resolved-first-child (<?-resolve first-child)]
(when (n/address? resolved-first-child)
(recur (conj! res resolved-first-child)
resolved-first-child)))
(persistent! res))))
path-suffix (-> (interleave sibling-lineage
(repeat 0))
;; butlast ensures we end w/ node
(butlast))]
(-> (pop common-parent-path)
(conj next-index)
(into path-suffix))))))
(defn lookup-path
"Given a B-tree and a key, gets a path into the tree"
[tree key]
(ha/go-try
(loop [;; alternating node/index/node/index/node... of the search taken
path (transient [tree])
;; current search node
cur tree]
(let [children (:children cur)]
(when (> (count children) 0)
(if (data-node? cur)
(persistent! path)
(let [index (n/-lookup cur key)
child (when-not (data-node? cur)
(-> children
;; TODO what are the semantics for
;; exceeding on the right? currently
;; it's trunc to the last element
(nth index (peek children))
(<?-resolve)))]
(recur (-> path
(conj! index)
(conj! child))
child))))))))
(defn lookup-key
"Given a B-tree and a key, gets an iterator into the tree"
([tree key]
(lookup-key tree key nil))
([tree key not-found]
(ha/go-try
(-> (ha/<? (lookup-path tree key))
(peek)
(<?-resolve)
:children
(get key not-found)))))
(defn insert
[{:keys [cfg] :as tree} k v]
(ha/go-try
(let [path (ha/<? (lookup-path tree k))
{:keys [children] :or {children empty-sorted-map-by-compare}} (peek path)
updated-data-node (data-node (assoc children k v)
cfg)]
(loop [node updated-data-node
path (pop path)]
(if (empty? path)
(if (n/-overflow? node)
(let [{:keys [left right median]} (n/-split-node node)]
(index-node [left right]
[]
cfg))
node)
(let [index (peek path)
init-path (pop path)
{:keys [children keys] :as parent} (peek init-path)]
;; splice the split into the parent
(if (n/-overflow? node)
;; TODO refactor paths to be node/index pairs or 2 vectors or something
(let [{:keys [left right median]} (n/-split-node node)
new-children (catvec (conj (subvec children 0 index)
left right)
(subvec children (inc index)))]
(recur (-> parent
(assoc :children new-children)
(n/-dirty!))
(pop init-path)))
(recur (-> parent
;;TODO this assoc seems to be a bottleneck
(assoc :children (assoc children index node))
(n/-dirty!))
(pop init-path)))))))))
;;TODO: cool optimization: when merging children, push as many operations as you can
;;into them to opportunistically minimize overall IO costs
(defn delete
[{:keys [cfg] :as tree} key]
(ha/go-try
(let [path (ha/<? (lookup-path tree key)) ;; don't care about the found key or its index
{:keys [children]
:or {children empty-sorted-map-by-compare}} (peek path)
updated-data-node (data-node (dissoc children key)
cfg)]
(loop [node updated-data-node
path (pop path)]
(if (empty? path)
;; Check for special root underflow case
(if (and (index-node? node)
(= 1 (count (:children node))))
(first (:children node))
node)
(let [index (peek path)
init-path (pop path)
{:keys [children keys op-buf] :as parent} (peek init-path)]
(if (n/-underflow? node) ;; splice the split into the parent
;;TODO this needs to use a polymorphic sibling-count
;;to work on serialized nodes
(let [bigger-sibling-idx
(cond
(= (dec (count children)) index) (dec index) ; only have left sib
(zero? index) 1 ;only have right sib
(> (count (:children (nth children (dec index))))
(count (:children (nth children (inc index)))))
(dec index) ; right sib bigger
:else (inc index))
node-first? (> bigger-sibling-idx index)
;; if true, `node` is left
merged (if node-first?
(n/-merge-node node (<?-resolve (nth children bigger-sibling-idx)))
(n/-merge-node (<?-resolve (nth children bigger-sibling-idx)) node))
old-left-children (subvec children 0 (min index bigger-sibling-idx))
old-right-children (subvec children (inc (max index bigger-sibling-idx)))]
(if (n/-overflow? merged)
(let [{:keys [left right median]} (n/-split-node merged)]
(recur (index-node (catvec (conj old-left-children left right)
old-right-children)
op-buf
cfg)
(pop init-path)))
(recur (index-node (catvec (conj old-left-children merged)
old-right-children)
op-buf
cfg)
(pop init-path))))
(recur (index-node (assoc children index node)
op-buf
cfg)
(pop init-path)))))))))
(defn b-tree
[cfg & kvs]
(ha/go-try
(loop [[[k v] & r] (partition 2 kvs)
t (data-node empty-sorted-map-by-compare
cfg)]
(if k
(recur r (ha/<? (insert t k v)))
t))))
;;TODO make this a loop/recur instead of mutual recursion
(defn flush-children
[children backend session]
(ha/go-try
(loop [[c & r] children
res (transient [])]
(if-not c
(persistent! res)
(recur r (conj! res (ha/<? (flush-tree c backend session))))))))
(defn flush-tree
"Given the tree, finds all dirty nodes, delivering addrs into them.
Every dirty node also gets replaced with its TestingAddr.
These form a GC cycle, have fun with the unmanaged memory port :)"
([tree backend]
(ha/go-try
(let [session (b/-new-session backend)
flushed (ha/<? (flush-tree tree backend session))
root (b/-anchor-root backend flushed)]
{:tree (<?-resolve root) ;; root should never be unresolved for API
:stats session})))
([tree backend stats]
(ha/go-try
(if (n/-dirty? tree)
(let [cleaned-children (if (data-node? tree)
(:children tree)
(->> (flush-children (:children tree) backend stats)
ha/<?
catvec))
cleaned-node (assoc tree :children cleaned-children)
new-addr (ha/<? (b/-write-node backend cleaned-node stats))]
(async/>!! (:storage-addr tree)
new-addr)
new-addr)
tree))))
;; TODO merge this with the code above
(defn flush-children-without-root
[children backend session]
(ha/go-try
(loop [[c & r] children
res (transient [])]
(if-not c
(persistent! res)
(recur r (conj! res (ha/<? (flush-tree-without-root c backend session false))))))))
(defn flush-tree-without-root
"Given the tree, finds all dirty nodes, delivering addrs into them.
Does not flush root node, but returns it."
([tree backend]
(ha/go-try
(let [session (b/-new-session backend)
flushed (ha/<? (flush-tree-without-root tree backend session true))
root (b/-anchor-root backend flushed)]
{:tree (<?-resolve root) ; root should never be unresolved for API
:stats session})))
([tree backend stats root-node?]
(ha/go-try
(if (n/-dirty? tree)
(let [cleaned-children (if (data-node? tree)
(:children tree)
;; TODO throw on nested errors
(->> (flush-children-without-root (:children tree) backend stats)
ha/<?
catvec))
cleaned-node (assoc tree :children cleaned-children)]
(if root-node?
cleaned-node
(let [new-addr (ha/<? (b/-write-node backend cleaned-node stats))]
(async/>!! (:storage-addr tree)
new-addr)
new-addr)))
tree))))
(ha/if-async?
(do
(defn forward-iterator
"Takes the result of a search and puts the iterated elements onto iter-ch
going forward over the tree as needed. Does lg(n) backtracking sometimes."
[iter-ch path start-key]
(ha/go-try
(loop [path path]
(if path
(let [start-node (peek path)
elements (subseq (:children start-node)
>=
start-key)]
(ha/<? (async/onto-chan iter-ch
elements false))
(recur (ha/<? (right-successor (pop path)))))
(async/close! iter-ch)))))
#?(:clj
(defn lookup-fwd-iter
"Compatibility helper to clojure sequences. Please prefer the channel
interface of forward-iterator, as this function blocks your thread, which
disturbs async contexts and might lead to poor performance. It is mainly here
to facilitate testing."
[tree key]
(let [path (ha/<?? (lookup-path tree key))
iter-ch (async/chan)]
(forward-iterator iter-ch path key)
(ha/chan-seq iter-ch)))))
;; else
(do
(defn forward-iterator
"Takes the result of a search and returns an iterator going
forward over the tree. Does lg(n) backtracking sometimes."
[path start-key]
(let [start-node (peek path)]
(assert (data-node? start-node))
(let [first-elements (-> start-node
:children ; Get the indices of it
(subseq >= start-key)) ; skip to the start-index
next-elements (lazy-seq
(when-let [succ (right-successor (pop path))]
(forward-iterator succ start-key)))]
(concat first-elements next-elements))))
(defn lookup-fwd-iter
[tree key]
(let [path (lookup-path tree key)]
(when path
(forward-iterator path key))))))