Skip to content

Commit

Permalink
moveTo acquires lock
Browse files Browse the repository at this point in the history
  • Loading branch information
chm-diederichs committed Dec 18, 2024
1 parent bcf8e43 commit fae5047
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions lib/session-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,48 +543,54 @@ module.exports = class SessionState {
async moveTo (core, length, { atom } = {}) {
const state = core.state

if (state.storage && (await state.storage.openBatch(this.name)) !== null) {
throw STORAGE_CONFLICT('Batch has already been created')
}
const lock = await this.mutex.lock(atom)

const head = this.core.sessionStates.pop()
if (head !== this) this.core.sessionStates[(head.index = this.index)] = head
try {
if (state.storage && (await state.storage.openBatch(this.name)) !== null) {
throw STORAGE_CONFLICT('Batch has already been created')
}

this.core = core
this.index = this.core.sessionStates.push(this) - 1
const head = this.core.sessionStates.pop()
if (head !== this) this.core.sessionStates[(head.index = this.index)] = head

if (atom) atom.enter()
this.core = core
this.index = this.core.sessionStates.push(this) - 1

const treeLength = this.tree.length
if (atom) atom.enter()

if (!this.isSnapshot()) {
const truncation = length < this.tree.length ? await truncateAndFlush(this, length, atom) : null
const treeLength = this.tree.length

const treeInfo = truncation ? truncation.tree : await state._getTreeHeadAt(this.tree.length)
const prologue = state.tree.prologue
if (!this.isSnapshot()) {
const truncation = length < this.tree.length ? await truncateAndFlush(this, length, atom) : null

// todo: validate treeInfo
const treeInfo = truncation ? truncation.tree : await state._getTreeHeadAt(this.tree.length)
const prologue = state.tree.prologue

const registration = state.storage.registerBatch(this.name, treeInfo)
// todo: validate treeInfo

if (atom) atom.exit()
const registration = state.storage.registerBatch(this.name, treeInfo)

if (truncation) await truncation.flushing
this.storage = await registration
if (atom) atom.exit()

this.tree = await MerkleTree.open(this.storage, treeInfo.length, { prologue })
if (truncation) await truncation.flushing
this.storage = await registration

if (truncation) {
await truncation.flushing
const { dependency, treeUpdate, tree } = truncation
this.tree = await MerkleTree.open(this.storage, treeInfo.length, { prologue })

if (dependency) this.storage.updateDependencies(dependency.length)
this.tree.onupdate(treeUpdate)
this.ontruncate(tree, tree.length, treeLength)
if (truncation) {
await truncation.flushing
const { dependency, treeUpdate, tree } = truncation

if (dependency) this.storage.updateDependencies(dependency.length)
this.tree.onupdate(treeUpdate)
this.ontruncate(tree, tree.length, treeLength)
}
}
}

for (const s of this.sessions) s.transferSession(this.core)
for (const s of this.sessions) s.transferSession(this.core)
} finally {
lock.release()
}
}

async createSession (name, length, overwrite, draft) {
Expand Down

0 comments on commit fae5047

Please sign in to comment.