Skip to content

Commit

Permalink
commit acquires lock directly
Browse files Browse the repository at this point in the history
  • Loading branch information
chm-diederichs committed Oct 2, 2024
1 parent f794da1 commit 628e35b
Showing 1 changed file with 35 additions and 38 deletions.
73 changes: 35 additions & 38 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,32 +390,6 @@ class SessionState {
this.mutex.unlock()
}
}

async upgrade (start, end, batch, values, keyPair) {
await this.mutex.lock()

const update = this.createUpdate()

try {
// upsert compat manifest
if (this.core.verifier === null && keyPair) this.core._setManifest(update, null, keyPair)

this.blocks.putBatch(update.batch, start, values)

update.bitfield.setRange(start, end, true)
update.flushTreeBatch(batch)

const bitfield = { start, length: end - start, drop: false }
const status = batch.upgraded ? 0b0001 : 0

update.coreUpdate({ status, bitfield, value: null, from: null })

await this.flushUpdate(update)
} finally {
this._clearActiveBatch()
this.mutex.unlock()
}
}
}

module.exports = class Core {
Expand Down Expand Up @@ -859,24 +833,47 @@ module.exports = class Core {
}
}

const promises = []
await this.state.mutex.lock()

const reader = state.storage.createReadBatch()
for (let i = treeLength; i < length; i++) promises.push(reader.getBlock(i))
reader.tryFlush()
const update = this.state.createUpdate()

const values = await Promise.all(promises)
try {
const promises = []

const batch = await this.tree.reconcile(state.tree, length, treeLength)
if (batch.upgraded) batch.signature = signature || this.verifier.sign(batch, keyPair)
const reader = state.storage.createReadBatch()
for (let i = treeLength; i < length; i++) promises.push(reader.getBlock(i))
reader.tryFlush()

await this.state.upgrade(treeLength, length, batch, values, keyPair)
const values = await Promise.all(promises)

state.treeLength = batch.length
const batch = await this.tree.reconcile(state.tree, length, treeLength)
if (batch.upgraded) batch.signature = signature || this.verifier.sign(batch, keyPair)

return {
length: batch.length,
byteLength: batch.byteLength
// upsert compat manifest
if (this.verifier === null && keyPair) this._setManifest(update, null, keyPair)

this.state.blocks.putBatch(update.batch, treeLength, values)

update.bitfield.setRange(treeLength, length, true)
update.flushTreeBatch(batch)

const bitfield = { start: treeLength, length: length - treeLength, drop: false }
const status = batch.upgraded ? 0b0001 : 0

update.coreUpdate({ status, bitfield, value: null, from: null })

await this.state.flushUpdate(update)

state.treeLength = batch.length

return {
length: batch.length,
byteLength: batch.byteLength
}
} finally {
this.state._clearActiveBatch()
this.updating = false
this.state.mutex.unlock()
}
}

Expand Down

0 comments on commit 628e35b

Please sign in to comment.