-
Notifications
You must be signed in to change notification settings - Fork 18
/
strata.js
113 lines (93 loc) · 2.87 KB
/
strata.js
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
const fs = require('fs').promises
const Sheaf = require('./sheaf')
const Cursor = require('./cursor')
const assert = require('assert')
const Magazine = require('magazine')
class Strata {
static MIN = Symbol('MIN')
static MAX = Symbol('MAX')
static Error = require('./error')
constructor (destructible, options) {
this._sheaf = new Sheaf(destructible, options)
}
get storage () {
return this._sheaf.storage
}
get options () {
return this._sheaf.options
}
get destructible () {
return this._sheaf.destructible
}
get deferrable () {
return this._sheaf.deferrable
}
descend (trampoline, comparator, key, found) {
this._search(trampoline, {
key: key,
rightward: false,
fork: false,
comparator: { branch: comparator, leaf: comparator }
}, found)
}
search (trampoline, ...vargs) {
const key = vargs.shift()
const found = vargs.pop()
let fork = false, reversal = 1
while (vargs.length != 0) {
switch (typeof vargs[0]) {
case 'boolean':
fork = vargs.shift()
break
case 'number':
reversal = vargs.shift()
break
}
}
const query = {
key: key === Strata.MIN ? null : key,
rightward: key === Strata.MAX,
fork: fork,
reversal: reversal,
approximate: true
}
this._sheaf.search(trampoline, query, descent => {
const cursor = new Cursor(this._sheaf, descent, key)
try {
found(cursor)
} finally {
cursor.release()
}
})
}
_search (trampoline, query, found) {
this._sheaf.search(trampoline, query, descent => {
const cursor = new Cursor(this._sheaf, descent, query.key)
try {
found(cursor)
} finally {
cursor.release()
}
})
}
min (trampoline, found) {
this._search(trampoline, { key: null, rightward: false, fork: false }, found)
}
max (trampoline, found) {
this._search(trampoline, { key: null, rightward: true, fork: false }, found)
}
find (trampoline, key, found) {
this._search(trampoline, { key, rightward: false, fork: false, approximate: true }, found)
}
fork (trampoline, key, found) {
this._search(trampoline, { key, rightward: false, fork: true, approximate: true }, found)
}
after (trampoline, key, partial, found) {
key = key.concat(null)
this._search(trampoline, { key, partial: key.length - 1, rightward: false, fork: false, approximate: true }, found)
}
drain () {
return this._sheaf.drain()
}
}
module.exports = Strata