Skip to content

Commit 31cdf36

Browse files
core.js commit method uses overwrite
1 parent c9f96ce commit 31cdf36

File tree

1 file changed

+23
-38
lines changed

1 file changed

+23
-38
lines changed

lib/core.js

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -939,61 +939,46 @@ module.exports = class Core {
939939
}
940940

941941
async commit (state, { signature, keyPair = this.header.keyPair, length = state.tree.length, treeLength = state.flushedLength(), overwrite = false } = {}) {
942-
await this.state.mutex.lock()
943-
944942
let sourceLocked = false
945-
const update = this.state.createUpdate()
943+
944+
await this.state.mutex.lock()
946945

947946
try {
948947
await state.mutex.lock()
949-
const srcUpdate = state.createUpdate()
950948
sourceLocked = true
951949

952-
if (!overwrite) {
953-
if (this.tree.fork !== state.tree.fork) return null
954-
if (this.tree.length > state.tree.length) return null // TODO: partial commit in the future if possible
955-
if (this.tree.length > treeLength) {
956-
for (const root of this.tree.roots) {
957-
const batchRoot = await state.tree.get(root.index)
958-
if (batchRoot.size !== root.size || !b4a.equals(batchRoot.hash, root.hash)) {
959-
return null
960-
}
961-
}
962-
}
963-
}
964-
965-
const promises = []
950+
const source = state.createUpdate()
966951

967-
const reader = state.storage.createReadBatch()
968-
for (let i = treeLength; i < length; i++) promises.push(reader.getBlock(i))
969-
reader.tryFlush()
970-
971-
const values = await Promise.all(promises)
952+
if (this.tree.fork !== state.tree.fork) return null
972953

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

976-
// upsert compat manifest
977-
if (this.verifier === null && keyPair) this._setManifest(update, null, keyPair)
956+
if (this.tree.length > treeLength) {
957+
for (const root of this.tree.roots) {
958+
const batchRoot = await state.tree.get(root.index)
959+
if (batchRoot.size !== root.size || !b4a.equals(batchRoot.hash, root.hash)) {
960+
return null
961+
}
962+
}
963+
}
978964

979-
this.state.blocks.putBatch(update.batch, treeLength, values)
965+
const update = this.state.createUpdate()
980966

981-
update.bitfield.setRange(treeLength, length, true)
982-
update.flushTreeBatch(batch)
967+
const batch = await this.state._overwrite(update, state, length, treeLength)
968+
if (length > this.tree.length) batch.signature = signature || this.verifier.sign(batch, keyPair)
983969

984-
if (batch.length < this.state.flushedLength()) update.updateDependency(batch.length)
970+
await update.flushTreeBatch(batch)
985971

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

989-
update.coreUpdate({ status, bitfield, value: null, from: null })
976+
source.updateDependency(batch.length)
990977

991-
// gc blocks from source
992-
state.blocks.clear(srcUpdate.batch, treeLength, values.length)
993-
srcUpdate.updateDependency(batch.length)
978+
await state.flushUpdate(source)
979+
}
994980

995981
await this.state.flushUpdate(update)
996-
await state.flushUpdate(srcUpdate)
997982

998983
return {
999984
length: batch.length,

0 commit comments

Comments
 (0)