Skip to content

Commit

Permalink
rocksdb: use storage API for updating dependency (#610)
Browse files Browse the repository at this point in the history
* add test for snapshot on named batch

* move dependency mutation to storage api
  • Loading branch information
chm-diederichs authored Dec 2, 2024
1 parent 13bc2d4 commit 6c11bab
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ module.exports = class Core {

await state.flushWriteBatch(storage)

if (dependency) state.refreshDependencies(dependency)
if (dependency) state.storage.updateDependencies(dependency.length)
}

if (this.header.hints.contiguousLength === treeLength) {
Expand Down
37 changes: 6 additions & 31 deletions lib/session-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,6 @@ module.exports = class SessionState {
return dependency
}

refreshDependencies (dependency) {
assert(!this.isDefault(), 'Default state should have no dependencies')

const deps = this.storage.dependencies

for (let i = deps.length - 1; i >= 0; i--) {
if (deps[i].data === dependency.data) {
const updated = deps.slice(0, i)
updated.push(dependency)

this.storage.dependencies = updated
return
}
}

throw new Error('Dependency not found')
}

_clearActiveBatch (err) {
if (!this._activeBatch) return
this._activeBatch.destroy()
Expand Down Expand Up @@ -271,7 +253,7 @@ module.exports = class SessionState {

await this.flushWriteBatch(storage)

if (dependency) this.refreshDependencies(dependency)
if (dependency) this.storage.updateDependencies(dependency.length)
this.tree.onupdate(treeUpdate)

this.ontruncate(tree, tree.length, batch.treeLength)
Expand All @@ -292,7 +274,7 @@ module.exports = class SessionState {

await this.flushWriteBatch(storage)

if (dependency) this.refreshDependencies(dependency)
if (dependency) this.storage.updateDependencies(dependency.length)
this.tree.onupdate(treeUpdate)

this.ontruncate(tree, batch.ancestors, batch.treeLength)
Expand Down Expand Up @@ -341,7 +323,7 @@ module.exports = class SessionState {

await this.flushWriteBatch(storage)

if (dependency) this.refreshDependencies(dependency)
if (dependency) this.storage.updateDependencies(dependency.length)

if (this.isDefault()) {
const length = end - start
Expand Down Expand Up @@ -635,18 +617,11 @@ async function storeBitfieldRange (storage, writer, from, to, value) {
}

function updateDependency (state, length) {
const dependencies = state.storage.dependencies
if (!dependencies.length) return null // skip default state and overlays of default

let target = dependencies[dependencies.length - 1]

for (const dep of dependencies) {
if (dep.length < length) break
target = dep
}
const dependency = state.storage.findDependency(length)
if (dependency === null) return null // skip default state and overlays of default

return {
data: target.data,
data: dependency.data,
length
}
}
Expand Down
33 changes: 33 additions & 0 deletions test/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,36 @@ test('snapshots are consistent', async function (t) {

await snapshot.close()
})

test('snapshot over named batch persists after truncate', async function (t) {
t.plan(8)

const core = await create(t)

await core.append('block #0.0')
await core.append('block #1.0')
await core.append('block #2.0')

const session = core.session({ name: 'session' })
const snapshot = session.snapshot({ valueEncoding: 'utf-8' })

t.is(snapshot.length, 3)

t.is(await snapshot.get(1), 'block #1.0')

await core.truncate(1)
await core.append('block #1.1')

t.is(core.fork, 1, 'clone updated')
t.is(core.length, 2, 'core updated')

// t.is(snapshot.fork, 0, 'snapshot remains')
t.is(snapshot.length, 3, 'snapshot remains')

t.is(await snapshot.get(0), 'block #0.0')
t.is(await snapshot.get(1), 'block #1.0')
t.is(await snapshot.get(2), 'block #2.0')

await core.close()
await snapshot.close()
})

0 comments on commit 6c11bab

Please sign in to comment.