Skip to content

Commit

Permalink
Preload (#139)
Browse files Browse the repository at this point in the history
* support preload

* fix
  • Loading branch information
mafintosh authored Nov 6, 2023
1 parent 6ecd0d3 commit 7ffc47c
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,31 @@ class TreeNode {
this.keys = keys
this.children = children
this.changed = false

this.preload()
}

preload () {
if (this.block === null) return

const core = getBackingCore(this.block.tree.core)
const bitfield = core.core.bitfield

const missing = []
for (let i = 0; i < this.keys.length; i++) {
const k = this.keys[i]
if (k.value) continue
if (k.seq >= core.length || bitfield.get(k.seq)) continue
missing.push(k.seq)
}
for (let i = 0; i < this.children.length; i++) {
const c = this.children[i]
if (c.value) continue
if (c.seq >= core.length || bitfield.get(c.seq)) continue
missing.push(c.seq)
}

if (missing.length) core.download({ blocks: missing })
}

async insertKey (key, child, node, encoding, cas) {
Expand Down Expand Up @@ -1416,6 +1441,12 @@ function defaultDiffer (currentSnap, previousSnap, opts) {
return currentSnap.createDiffStream(previousSnap, opts)
}

function getBackingCore (core) {
if (core._source) return core._source.originalCore
if (core.flush) return core.session
return core
}

function noop () {}

module.exports = Hyperbee

0 comments on commit 7ffc47c

Please sign in to comment.