Skip to content

Commit

Permalink
core.js commit method uses overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
chm-diederichs committed Oct 18, 2024
1 parent 26b771e commit d418634
Showing 1 changed file with 23 additions and 38 deletions.
61 changes: 23 additions & 38 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -881,61 +881,46 @@ module.exports = class Core {
}

async commit (state, { signature, keyPair = this.header.keyPair, length = state.tree.length, treeLength = state.flushedLength(), overwrite = false } = {}) {
await this.state.mutex.lock()

let sourceLocked = false
const update = this.state.createUpdate()

await this.state.mutex.lock()

try {
await state.mutex.lock()
const srcUpdate = state.createUpdate()
sourceLocked = true

if (!overwrite) {
if (this.tree.fork !== state.tree.fork) return null
if (this.tree.length > state.tree.length) return null // TODO: partial commit in the future if possible
if (this.tree.length > treeLength) {
for (const root of this.tree.roots) {
const batchRoot = await state.tree.get(root.index)
if (batchRoot.size !== root.size || !b4a.equals(batchRoot.hash, root.hash)) {
return null
}
}
}
}

const promises = []
const source = state.createUpdate()

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

const values = await Promise.all(promises)
if (this.tree.fork !== state.tree.fork) return null

const batch = await this.tree.reconcile(state.tree, length, treeLength)
if (batch.upgraded) batch.signature = signature || this.verifier.sign(batch, keyPair)
if (this.tree.length > state.tree.length) return null // TODO: partial commit in the future if possible

// upsert compat manifest
if (this.verifier === null && keyPair) this._setManifest(update, null, keyPair)
if (this.tree.length > treeLength) {
for (const root of this.tree.roots) {
const batchRoot = await state.tree.get(root.index)
if (batchRoot.size !== root.size || !b4a.equals(batchRoot.hash, root.hash)) {
return null
}
}
}

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

update.bitfield.setRange(treeLength, length, true)
update.flushTreeBatch(batch)
const batch = await this.state._overwrite(update, state, length, treeLength)
if (length > this.tree.length) batch.signature = signature || this.verifier.sign(batch, keyPair)

if (batch.length < this.state.flushedLength()) update.updateDependency(batch.length)
await update.flushTreeBatch(batch)

const bitfield = { start: treeLength, length: length - treeLength, drop: false }
const status = batch.upgraded ? 0b0001 : 0
// gc blocks from source
if (treeLength < batch.length) {
state.blocks.clear(source.batch, treeLength, length - treeLength)

update.coreUpdate({ status, bitfield, value: null, from: null })
source.updateDependency(batch.length)

// gc blocks from source
state.blocks.clear(srcUpdate.batch, treeLength, values.length)
srcUpdate.updateDependency(batch.length)
await state.flushUpdate(source)
}

await this.state.flushUpdate(update)
await state.flushUpdate(srcUpdate)

return {
length: batch.length,
Expand Down

0 comments on commit d418634

Please sign in to comment.