Skip to content

Commit 31afceb

Browse files
authored
when clearing blocks, also clear the tree nodes if safe (#729)
* when clearing blocks, also clear the tree nodes if safe * if in range, its not in bitfield
1 parent b47eee0 commit 31afceb

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/bitfield.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,30 @@ module.exports = class Bitfield {
400400
return this.findLast(false, position)
401401
}
402402

403+
hasSet (start, length) {
404+
const end = start + length
405+
406+
let j = start & (BITS_PER_SEGMENT - 1)
407+
let i = (start - j) / BITS_PER_SEGMENT
408+
409+
while (i < this._segments.maxLength) {
410+
const s = this._segments.get(i)
411+
412+
let index = -1
413+
414+
if (s) index = s.findFirst(true, j)
415+
416+
if (index !== -1) return (i * BITS_PER_SEGMENT + index) < end
417+
418+
j = 0
419+
i++
420+
421+
if (i * BITS_PER_SEGMENT >= end) return false
422+
}
423+
424+
return false
425+
}
426+
403427
count (start, length, val) {
404428
let j = start & (BITS_PER_PAGE - 1)
405429
let i = (start - j) / BITS_PER_PAGE

lib/session-state.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,22 @@ module.exports = class SessionState {
476476
try {
477477
const tx = this.createWriteBatch()
478478

479+
const ite = flat.iterator(start * 2)
480+
while (!isRootIndex(ite.index, this.roots)) {
481+
const a = ite.index
482+
const b = ite.sibling()
483+
ite.parent()
484+
485+
const [left, right] = flat.spans(b)
486+
const s = left / 2
487+
const e = (right / 2) + 1
488+
const has = (s <= start && e <= end) ? false : this.core.bitfield.hasSet(s, e - s)
489+
if (has) break
490+
491+
tx.deleteTreeNode(a)
492+
tx.deleteTreeNode(b)
493+
}
494+
479495
if (this.isDefault()) {
480496
await storeBitfieldRange(this.storage, tx, start, end, false)
481497
if (start < this.core.header.hints.contiguousLength) {
@@ -1134,3 +1150,11 @@ function getCoreHead (storage) {
11341150
b.tryFlush()
11351151
return p
11361152
}
1153+
1154+
function isRootIndex (index, roots) {
1155+
for (const node of roots) {
1156+
if (node.index === index) return true
1157+
}
1158+
1159+
return false
1160+
}

0 commit comments

Comments
 (0)