hyperbee2 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/ranges.js +45 -2
- package/package.json +1 -1
package/lib/ranges.js
CHANGED
|
@@ -2,7 +2,18 @@ const { Readable } = require('streamx')
|
|
|
2
2
|
const b4a = require('b4a')
|
|
3
3
|
|
|
4
4
|
class RangeIterator {
|
|
5
|
-
constructor(tree,
|
|
5
|
+
constructor(tree, opts = {}) {
|
|
6
|
+
const {
|
|
7
|
+
activeRequests = [],
|
|
8
|
+
prefetch = true,
|
|
9
|
+
reverse = false,
|
|
10
|
+
limit = -1,
|
|
11
|
+
gte,
|
|
12
|
+
gt,
|
|
13
|
+
lte,
|
|
14
|
+
lt
|
|
15
|
+
} = opts
|
|
16
|
+
|
|
6
17
|
this.tree = tree
|
|
7
18
|
this.root = null
|
|
8
19
|
this.stack = []
|
|
@@ -13,6 +24,7 @@ class RangeIterator {
|
|
|
13
24
|
this.end = lte || lt || null
|
|
14
25
|
this.inclusive = !!(reverse ? lte : gte)
|
|
15
26
|
this.compare = (reverse ? gte : lte) ? 0 : -1
|
|
27
|
+
this.prefetching = prefetch
|
|
16
28
|
}
|
|
17
29
|
|
|
18
30
|
async open() {
|
|
@@ -88,7 +100,7 @@ class RangeIterator {
|
|
|
88
100
|
if (k < v.keys.length) {
|
|
89
101
|
const j = this.reverse ? v.keys.length - 1 - k : k
|
|
90
102
|
|
|
91
|
-
const data =
|
|
103
|
+
const data = v.keys.get(j)
|
|
92
104
|
|
|
93
105
|
const c = this.reverse
|
|
94
106
|
? this.start
|
|
@@ -100,6 +112,8 @@ class RangeIterator {
|
|
|
100
112
|
|
|
101
113
|
if (c > this.compare) break
|
|
102
114
|
|
|
115
|
+
if (this.prefetching && !v.children.length && this.stack.length) this.prefetch()
|
|
116
|
+
|
|
103
117
|
this.stack.push(top)
|
|
104
118
|
if (this.limit !== -1) this.limit--
|
|
105
119
|
return data
|
|
@@ -108,6 +122,33 @@ class RangeIterator {
|
|
|
108
122
|
|
|
109
123
|
return null
|
|
110
124
|
}
|
|
125
|
+
|
|
126
|
+
prefetch() {
|
|
127
|
+
// TODO: dbl check this for off-by-ones with the offset and keys and children
|
|
128
|
+
this.prefetching = false
|
|
129
|
+
if (this.limit < this.tree.context.minKeys) return
|
|
130
|
+
|
|
131
|
+
const parent = this.stack[this.stack.length - 1]
|
|
132
|
+
const pv = parent.node.value
|
|
133
|
+
if (!pv) return
|
|
134
|
+
|
|
135
|
+
for (let i = parent.offset >> 1; i < pv.children.length; i++) {
|
|
136
|
+
const k = pv.keys.get(i)
|
|
137
|
+
|
|
138
|
+
const cmp = this.reverse
|
|
139
|
+
? this.start
|
|
140
|
+
? b4a.compare(this.start, k.key)
|
|
141
|
+
: -1
|
|
142
|
+
: this.end
|
|
143
|
+
? b4a.compare(k.key, this.end)
|
|
144
|
+
: -1
|
|
145
|
+
|
|
146
|
+
if (cmp > this.compare) break
|
|
147
|
+
|
|
148
|
+
const c = pv.children.get(i)
|
|
149
|
+
if (!c.value) this.tree.inflate(c, this.activeRequests).catch(noop)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
111
152
|
}
|
|
112
153
|
|
|
113
154
|
class RangeStream extends Readable {
|
|
@@ -142,3 +183,5 @@ class RangeStream extends Readable {
|
|
|
142
183
|
|
|
143
184
|
exports.RangeIterator = RangeIterator
|
|
144
185
|
exports.RangeStream = RangeStream
|
|
186
|
+
|
|
187
|
+
function noop() {}
|