Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,32 +272,27 @@ class Hyperbee extends EventEmitter {
}

async _rollback(config) {
const expected = this.unbatch

let n = expected
let length = this.root === EMPTY ? 0 : this.root.seq + 1
let seq = this.root.seq
let context = this.context
let count = this.unbatch

while (n > 0 && length > 0 && expected === this.unbatch) {
const seq = length - 1
while (count) {
const blk = await context.getBlock(seq, 0, config)

if (!blk.previous) {
length = 0
break
}

if (!blk.previous) break
seq = blk.previous.seq
context = await context.getContext(blk.previous.core, config)
length = blk.previous.seq + 1
n--
count--
}

if (expected === this.unbatch) {
this.context = context
this.root = length === 0 ? EMPTY : context.createTreeNode(0, length - 1, 0, false, null)
this.unbatch = 0
this.emit('update')
if (count) {
// There was an attempt to rollback more batches than exist.
this.root = EMPTY
} else {
this.root = context.createTreeNode(0, seq, 0, false, null)
}
this.context = context
this.unbatch = 0
this.emit('update')
}

update(root = null) {
Expand Down