Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle dependency edge cases #63

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class HypercoreStorage {
}

snapshot () {
return new HypercoreStorage(this.store, this.db.snapshot(), this.core, this.view.snapshot(), this.atom)
return new HypercoreStorage(this.store, this.db.snapshot(), cloneCore(this.core), this.view.snapshot(), this.atom)
}

atomize (atom) {
Expand Down Expand Up @@ -212,13 +212,12 @@ class HypercoreStorage {
version: this.core.version,
corePointer: this.core.corePointer,
dataPointer: this.core.dataPointer,
dependencies: this._addDependency({ dataPointer: this.core.dataPointer, length })
dependencies: this._addDependency(null)
}

const coreTx = new CoreTX(core, this.db, atom.view, [])

if (length > 0) coreTx.setHead(head)
coreTx.setDependency(core.dependencies[core.dependencies.length - 1])

await coreTx.flush()

Expand All @@ -231,15 +230,15 @@ class HypercoreStorage {
for (let i = 0; i < this.core.dependencies.length; i++) {
const d = this.core.dependencies[i]

if (d.length > dep.length) {
if (dep !== null && d.length > dep.length) {
deps.push({ dataPointer: d.dataPointer, length: dep.length })
return deps
}

deps.push(d)
}

deps.push(dep)
if (dep !== null) deps.push(dep)
return deps
}

Expand Down Expand Up @@ -705,3 +704,17 @@ function createColumnFamily (db) {

return db.columnFamily(col)
}

function cloneCore (c) {
const copy = {
dataPointer: c.dataPointer,
corePointer: c.corePointer,
dependencies: []
mafintosh marked this conversation as resolved.
Show resolved Hide resolved
}

for (const { dataPointer, length } of c.dependencies) {
copy.dependencies.push({ dataPointer, length })
}

return copy
}
Loading