hyperbee2 2.0.3 → 2.1.0

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/index.js CHANGED
@@ -45,6 +45,10 @@ class Hyperbee {
45
45
  this.ready().catch(noop)
46
46
  }
47
47
 
48
+ static isHyperbee(bee) {
49
+ return bee instanceof Hyperbee
50
+ }
51
+
48
52
  head() {
49
53
  if (!this.root) return null
50
54
  if (this.root === EMPTY) return { length: 0, key: this.context.core.key }
@@ -182,7 +186,9 @@ class Hyperbee {
182
186
  children[i] = inflateChild(context, d, ptr, block, activeRequests)
183
187
  }
184
188
 
185
- ptr.value = new TreeNode(await Promise.all(keys), await Promise.all(children))
189
+ const value = new TreeNode(await Promise.all(keys), await Promise.all(children))
190
+ if (!ptr.value) ptr.value = value
191
+
186
192
  this.bump(ptr)
187
193
 
188
194
  return ptr.value
package/lib/cache.js CHANGED
@@ -10,10 +10,11 @@ module.exports = class NodeCache {
10
10
  }
11
11
 
12
12
  empty() {
13
- const maxSize = this.maxSize
14
- this.maxSize = 0
15
- this.gc()
16
- this.maxSize = maxSize
13
+ while (this.size > 0) {
14
+ const old = this.oldest()
15
+ this.remove(old)
16
+ old.value = null
17
+ }
17
18
  }
18
19
 
19
20
  gc() {
@@ -94,7 +94,14 @@ class CompressedArray {
94
94
  }
95
95
 
96
96
  flush(max, min) {
97
- if (this.delta.length <= max) return this.delta
97
+ let overflow = false
98
+ for (const d of this.delta) {
99
+ if (d.index < 256) continue // has to be uint3, only happens in rebalances/splits
100
+ overflow = true
101
+ break
102
+ }
103
+
104
+ if (this.delta.length <= max && !overflow) return this.delta
98
105
 
99
106
  const direct = []
100
107
  while (this.delta.length && this.delta[this.delta.length - 1].type !== OP_COHORT) {
@@ -102,7 +109,7 @@ class CompressedArray {
102
109
  }
103
110
  direct.reverse()
104
111
 
105
- if (direct.length > min && direct.length < this.entries.length) {
112
+ if (direct.length > min && direct.length < this.entries.length && !overflow) {
106
113
  const co = new DeltaCohort(true, null, [])
107
114
  for (const d of direct) {
108
115
  co.deltas.push(d)
package/lib/write.js CHANGED
@@ -8,7 +8,6 @@ const {
8
8
  ValuePointer,
9
9
  TreeNode,
10
10
  TreeNodePointer,
11
- MIN_KEYS,
12
11
  INSERTED,
13
12
  NEEDS_SPLIT
14
13
  } = require('./tree.js')
@@ -272,7 +271,7 @@ module.exports = class WriteBatch {
272
271
 
273
272
  const v = ptr.value ? this.snapshot.bump(ptr) : await this.snapshot.inflate(ptr)
274
273
 
275
- if (v.keys.length >= MIN_KEYS) return root
274
+ if (v.keys.length >= minKeys) return root
276
275
 
277
276
  const p = parent.value ? this.snapshot.bump(parent) : await this.snapshot.inflate(parent)
278
277
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hyperbee2",
3
- "version": "2.0.3",
3
+ "version": "2.1.0",
4
4
  "description": "btree",
5
5
  "main": "index.js",
6
6
  "files": [