Skip to content

Commit

Permalink
seek still needs session
Browse files Browse the repository at this point in the history
  • Loading branch information
chm-diederichs committed Jan 8, 2025
1 parent 8d3cc90 commit 22395fc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,8 @@ class Hypercore extends EventEmitter {
if (this.opened === false) await this.opening
if (!isValidIndex(bytes)) throw ASSERTION('seek is invalid')

const tree = (opts && opts.tree) || this.state.createTreeBatch()
const s = tree.seek(bytes, this.padding)
const tree = (opts && opts.tree) || this.state.core.tree
const s = tree.seek(this.state, bytes, this.padding)

const offset = await s.update()
if (offset) return offset
Expand Down
13 changes: 9 additions & 4 deletions lib/merkle-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class MerkleTreeBatch {
}

seek (bytes, padding) {
return new ByteSeeker(this, bytes, padding)
return new ByteSeeker(this, this, bytes, padding)
}

byteRange (index, readBatch = null) {
Expand All @@ -283,7 +283,7 @@ class MerkleTreeBatch {
return getByteOffset(this, index, readBatch)
}

async restoreBatch (length, session) {
async restoreBatch (length) {
if (length === this.length) return this

const roots = unslabNodes(await this.tree.getRoots(length))
Expand Down Expand Up @@ -386,8 +386,9 @@ class ReorgBatch extends MerkleTreeBatch {
}

class ByteSeeker {
constructor (tree, bytes, padding = 0) {
constructor (tree, session, bytes, padding = 0) {
this.tree = tree
this.session = session
this.bytes = bytes
this.padding = padding

Expand All @@ -400,7 +401,7 @@ class ByteSeeker {
async _seek (bytes) {
if (!bytes) return [0, 0]

for (const node of this.tree.roots) { // all async ticks happen once we find the root so safe
for (const node of this.session.roots) { // all async ticks happen once we find the root so safe
const size = getUnpaddedSize(node, this.padding, null)

if (bytes === size) return [flat.rightSpan(node.index) + 2, 0]
Expand Down Expand Up @@ -601,6 +602,10 @@ class MerkleTree {
return true
}

seek (session, bytes, padding) {
return new ByteSeeker(this, session, bytes, padding)
}

get (index, error = true, readBatch = null) {
if (readBatch) return readBatch.getTreeNode(index, error)

Expand Down

0 comments on commit 22395fc

Please sign in to comment.