hyperbee2 2.0.0 → 2.0.3

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.
Files changed (2) hide show
  1. package/lib/ranges.js +52 -2
  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, { activeRequests = [], reverse = false, limit = -1, gte, gt, lte, lt } = {}) {
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,8 @@ 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.prefetch = prefetch
28
+ this.prefetching = null
16
29
  }
17
30
 
18
31
  async open() {
@@ -88,7 +101,7 @@ class RangeIterator {
88
101
  if (k < v.keys.length) {
89
102
  const j = this.reverse ? v.keys.length - 1 - k : k
90
103
 
91
- const data = top.node.value.keys.get(j)
104
+ const data = v.keys.get(j)
92
105
 
93
106
  const c = this.reverse
94
107
  ? this.start
@@ -100,6 +113,8 @@ class RangeIterator {
100
113
 
101
114
  if (c > this.compare) break
102
115
 
116
+ if (this.prefetch && !v.children.length && this.stack.length) this.prefetchNext()
117
+
103
118
  this.stack.push(top)
104
119
  if (this.limit !== -1) this.limit--
105
120
  return data
@@ -108,6 +123,39 @@ class RangeIterator {
108
123
 
109
124
  return null
110
125
  }
126
+
127
+ prefetchNext() {
128
+ // TODO: dbl check this for off-by-ones with the offset and keys and children
129
+ let limit = this.limit
130
+
131
+ if (limit < this.tree.context.minKeys) return
132
+
133
+ const parent = this.stack[this.stack.length - 1]
134
+ const pv = parent.node.value
135
+ if (!pv || pv === this.prefetching) return
136
+
137
+ this.prefetching = pv
138
+
139
+ for (let i = parent.offset >> 1; i < pv.children.length; i++) {
140
+ const k = pv.keys.get(i)
141
+
142
+ const cmp = this.reverse
143
+ ? this.start
144
+ ? b4a.compare(this.start, k.key)
145
+ : -1
146
+ : this.end
147
+ ? b4a.compare(k.key, this.end)
148
+ : -1
149
+
150
+ if (cmp > this.compare) break
151
+
152
+ const c = pv.children.get(i)
153
+ if (!c.value) this.tree.inflate(c, this.activeRequests).catch(noop)
154
+
155
+ limit = Math.max(limit - this.tree.context.minKeys, 0)
156
+ if (limit === 0) break
157
+ }
158
+ }
111
159
  }
112
160
 
113
161
  class RangeStream extends Readable {
@@ -142,3 +190,5 @@ class RangeStream extends Readable {
142
190
 
143
191
  exports.RangeIterator = RangeIterator
144
192
  exports.RangeStream = RangeStream
193
+
194
+ function noop() {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hyperbee2",
3
- "version": "2.0.0",
3
+ "version": "2.0.3",
4
4
  "description": "btree",
5
5
  "main": "index.js",
6
6
  "files": [