Comparing version 0.0.5 to 0.0.6
@@ -18,11 +18,34 @@ class SubTree { | ||
this.isKey = (this.i & 1) === 1 | ||
if (!this.isKey && !this.node.children.length) this.i++ | ||
return this.update() | ||
} | ||
if (!this.isKey && !this.node.children.length) { | ||
this.i++ | ||
this.isKey = true | ||
async bisect (key, incl) { | ||
let s = 0 | ||
let e = this.node.keys.length | ||
let c | ||
while (s < e) { | ||
const mid = (s + e) >> 1 | ||
c = cmp(key, await this.node.getKey(mid)) | ||
if (c === 0) { | ||
if (incl) this.i = mid * 2 + 1 | ||
else this.i = mid * 2 + (this.node.children.length ? 2 : 3) | ||
return true | ||
} | ||
if (c < 0) e = mid | ||
else s = mid + 1 | ||
} | ||
const i = c < 0 ? e : s | ||
this.i = 2 * i + (this.node.children.length ? 0 : 1) | ||
return this.node.children.length === 0 | ||
} | ||
update () { | ||
this.isKey = (this.i & 1) === 1 | ||
this.n = this.i >> 1 | ||
if (this.n >= (this.isKey ? this.node.keys.length : this.node.children.length)) return false | ||
const child = this.isKey ? null : this.node.children[this.n] | ||
@@ -45,5 +68,10 @@ this.seq = child !== null ? child.seq : this.node.keys[this.n].seq | ||
class TreeIterator { | ||
constructor (db) { | ||
constructor (db, opts) { | ||
this.db = db | ||
this.stack = [] | ||
this.lt = opts.lt || opts.lte || null | ||
this.lte = !!opts.lte | ||
this.gt = opts.gt || opts.gte || null | ||
this.gte = !!opts.gte | ||
this.seeking = !!this.gt | ||
} | ||
@@ -54,5 +82,17 @@ | ||
if (!node.keys.length) return | ||
this.stack.push(new SubTree(node, null)) | ||
const tree = new SubTree(node, null) | ||
if (this.seeking && !(await this._seek(tree))) return | ||
this.stack.push(tree) | ||
} | ||
async _seek (tree) { | ||
const done = await tree.bisect(this.gt, this.gte) | ||
const oob = !tree.update() | ||
if (done || oob) { | ||
this.seeking = false | ||
if (oob) return false | ||
} | ||
return true | ||
} | ||
peek () { | ||
@@ -71,3 +111,8 @@ if (!this.stack.length) return null | ||
while (this.stack.length && n === null) n = await this.next() | ||
return n | ||
if (!this.lt) return n.final() | ||
const c = cmp(n.key, this.lt) | ||
if (this.lte ? c <= 0 : c < 0) return n.final() | ||
this.stack = [] | ||
return null | ||
} | ||
@@ -81,9 +126,16 @@ | ||
if (!top.next()) this.stack.pop() | ||
if (!top.next()) { | ||
this.stack.pop() | ||
} | ||
if (isKey) return (await this.db.getBlock(seq)).final() | ||
if (isKey) { | ||
this.seeking = false | ||
return this.db.getBlock(seq) | ||
} | ||
const child = await top.node.getChildNode(n) | ||
top.node.children[n] = null // unlink to save memory | ||
this.stack.push(new SubTree(child, top)) | ||
const tree = new SubTree(child, top) | ||
if (this.seeking && !(await this._seek(tree))) return | ||
this.stack.push(tree) | ||
@@ -95,5 +147,6 @@ return null | ||
module.exports = class DiffIterator { | ||
constructor (left, right) { | ||
this.left = new TreeIterator(left) | ||
this.right = new TreeIterator(right) | ||
constructor (left, right, opts = {}) { | ||
this.left = new TreeIterator(left, opts) | ||
this.right = new TreeIterator(right, opts) | ||
this.limit = typeof opts.limit === 'number' ? opts.limit : -1 | ||
} | ||
@@ -106,2 +159,10 @@ | ||
async next () { | ||
if (this.limit < 0) return null | ||
const res = await this._next() | ||
if (!res || (res.left === null && res.right === null)) return null | ||
this.limit-- | ||
return res | ||
} | ||
async _next () { | ||
const a = this.left | ||
@@ -108,0 +169,0 @@ const b = this.right |
{ | ||
"name": "hyperbee", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "An append-only Btree running on a Hypercore.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -134,3 +134,3 @@ # Hyperbee 🐝 | ||
#### `stream = db.createDiffStream(otherVersion)` | ||
#### `stream = db.createDiffStream(otherVersion, [options])` | ||
@@ -153,2 +153,4 @@ Efficiently create a stream of the shallow changes between two versions of the db. | ||
Currently accepts the same options as the read stream except for reverse. | ||
#### `dbCheckout = db.checkout(version)` | ||
@@ -155,0 +157,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63569
1861
169