Skip to content

Commit

Permalink
Move to takes session (#634)
Browse files Browse the repository at this point in the history
* update moveTo

* update tests

* add missing length on boot

* fix issue and tests

* drop support for atomic moveTo

---------

Co-authored-by: Christophe Diederichs <chm-diederichs@protonmail.com>
  • Loading branch information
mafintosh and chm-diederichs authored Feb 10, 2025
1 parent 35a51ae commit 8c151c9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 92 deletions.
1 change: 1 addition & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ module.exports = class Core {

const treeInfo = {
fork: header.tree.fork,
length: header.tree.length,
signature: header.tree.signature,
roots: header.tree.length ? await tree.getRoots(header.tree.length) : [],
prologue
Expand Down
52 changes: 16 additions & 36 deletions lib/session-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = class SessionState {
this.signature = treeInfo.signature || null

const deps = this.storage.dependencies
this.dependencyLength = deps.length ? deps[deps.length - 1].length : Infinity
this.dependencyLength = deps.length ? deps[deps.length - 1].length : 0

if (treeInfo.length < this.dependencyLength) this.dependencyLength = treeInfo.length
if (treeInfo.roots.length) this.setRoots(treeInfo.roots)
Expand Down Expand Up @@ -237,24 +237,6 @@ module.exports = class SessionState {
this.storage.updateDependencyLength(src.dependencyLength)
}

// handle migration
if (src.core !== this.core) {
if (this.lingers === null) this.lingers = []
this.lingers.push(this.storage)

this.prologue = src.prologue
this.storage = await src.core.state.storage.resumeSession(this.name)
this.tree = new MerkleTree(this.storage)

for (let i = this.core.sessionStates.length - 1; i >= 0; i--) {
const state = this.core.sessionStates[i]
if (state === this) continue
if (state.name === this.name) state._moveToCore(src.core)
}

this._moveToCore(src.core)
}

const truncated = (bitfield && bitfield.truncated !== -1)
? bitfield.truncated
: src.dependencyLength
Expand Down Expand Up @@ -814,9 +796,9 @@ module.exports = class SessionState {
await this.mutex.lock()

try {
if (state.storage && (await state.storage.resumeSession(this.name)) !== null) {
throw STORAGE_CONFLICT('Batch has already been created')
}
// if (state.storage && (await state.storage.resumeSession(this.name)) !== null) {
// throw STORAGE_CONFLICT('Batch has already been created')
// }

const treeLength = this.length

Expand All @@ -831,12 +813,12 @@ module.exports = class SessionState {

// todo: validate treeInfo

if (!this.storage.atom) {
this.storage = await state.storage.createSession(this.name, treeInfo)
const resumed = await state.storage.resumeSession(this.name)

if (resumed) {
this.storage = resumed
} else {
const s = state.storage.atomize(this.storage.atom)
this.storage = await s.createSession(this.name, treeInfo)
await s.close()
this.storage = await state.storage.createSession(this.name, treeInfo)
}

this.tree = new MerkleTree(this.storage)
Expand All @@ -847,22 +829,20 @@ module.exports = class SessionState {
this.roots = await this.tree.getRoots(length)

if (truncation) {
const { dependency, tree, flushed } = truncation
const { dependency, tree } = truncation

if (dependency) this.storage.updateDependencyLength(this.dependencyLength)
this.ontruncate(tree, tree.length, treeLength, flushed)
this.ontruncate(tree, tree.length, treeLength, true)
}
}

if (!this.storage.atom) {
for (let i = this.core.sessionStates.length - 1; i >= 0; i--) {
const state = this.core.sessionStates[i]
if (state === this) continue
if (state.name === this.name) state._moveToCore(core)
}
for (let i = this.core.sessionStates.length - 1; i >= 0; i--) {
const state = this.core.sessionStates[i]
if (state === this) continue
if (state.name === this.name) state._moveToCore(core.core)
}

this._moveToCore(core)
this._moveToCore(core.core)
} finally {
this.mutex.unlock()
}
Expand Down
54 changes: 0 additions & 54 deletions test/atomic.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,60 +366,6 @@ test('atomic - overwrite and user data', async function (t) {
await core2.close()
})

test('atomic - move to', async function (t) {
const storage = await createStorage(t)

const core = new Hypercore(storage)
const core2 = new Hypercore(storage)

await core.ready()
await core2.ready()

await core.append('hello')
await core.append('world')

await core2.append('hello')

t.is(core.length, 2)
t.is(core2.length, 1)

const session = core.session({ name: 'moveable' })
await session.ready()

let truncates = 0
session.on('truncate', () => { truncates++ })

t.is(session.length, 2)
t.ok(session.core === core.core)
t.is(truncates, 0)

const atom = core.state.storage.createAtom()

const atomic = session.session({ atom })
await atomic.ready()

await atomic.state.moveTo(core2.core, 1)

t.is(atomic.length, 1)
t.is(session.length, 2)
t.ok(session.core === core.core)
t.is(truncates, 0)

t.is(atomic.length, 1)
await atom.flush()

t.is(session.length, 1)
t.ok(session.core !== core.core)
t.ok(session.core === core2.core)
t.is(truncates, 1)

await session.close()
await atomic.close()

await core.close()
await core2.close()
})

test('atomic - truncate', async function (t) {
const core = await create(t)

Expand Down
4 changes: 2 additions & 2 deletions test/move-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test('move - basic', async function (t) {

sess.once('migrate', key => { t.alike(key, core2.key) })

await sess.state.moveTo(core2.core, core2.length)
await sess.state.moveTo(core2, core2.length)
await sess.append('4')

await core2.commit(sess)
Expand Down Expand Up @@ -92,7 +92,7 @@ test('move - snapshots', async function (t) {

t.is(core2.length, 2)

await snap.state.moveTo(core2.core, core2.length)
await snap.state.moveTo(core2, core2.length)

t.is(snap.length, 3)

Expand Down

0 comments on commit 8c151c9

Please sign in to comment.