Skip to content

Commit 48624a8

Browse files
SessionState tracks most recent truncation (#682)
* session state tracks most recent truncation * add tests for lastTruncation * clear lastTruncation each flush * track from and to
1 parent f1a9f89 commit 48624a8

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

lib/session-state.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = class SessionState {
3636
this.signature = treeInfo.signature || null
3737

3838
this.snapshotCompatLength = this.isSnapshot() ? Math.min(this.length, this.core.state.length) : -1
39+
this.lastTruncation = null
3940

4041
this.active = 0
4142

@@ -230,10 +231,13 @@ module.exports = class SessionState {
230231
this._activeTx = null
231232

232233
try {
233-
return await tx.flush()
234+
if (!(await tx.flush())) return false
234235
} finally {
235236
this._clearActiveBatch()
236237
}
238+
239+
this.lastTruncation = null
240+
return true
237241
}
238242

239243
_precommit () {
@@ -246,6 +250,7 @@ module.exports = class SessionState {
246250
try {
247251
const bitfield = this._pendingBitfield
248252
this._pendingBitfield = null
253+
this.lastTruncation = null
249254
await this.parent._oncommit(this, bitfield)
250255
} finally {
251256
this.commiting = false
@@ -576,6 +581,8 @@ module.exports = class SessionState {
576581
ontruncate (tree, to, from, flushed) {
577582
const bitfield = { start: to, length: from - to, drop: true }
578583

584+
this.lastTruncation = { from, to }
585+
579586
if (!flushed) this._updateBitfield(bitfield)
580587
else if (this.isDefault()) this.core.ontruncate(tree, bitfield)
581588

test/core.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ test('core - append and truncate', async function (t) {
5656
b4a.from('ooo')
5757
])
5858

59+
t.is(core.state.lastTruncation, null)
60+
5961
await core.state.truncate(3, 1)
6062

63+
t.is(core.state.lastTruncation.from, 4)
64+
t.is(core.state.lastTruncation.to, 3)
65+
6166
t.is(core.state.length, 3)
6267
t.is(core.state.byteLength, 12)
6368
t.is(core.state.fork, 1)
@@ -71,30 +76,52 @@ test('core - append and truncate', async function (t) {
7176

7277
await core.state.truncate(3, 2)
7378

79+
t.is(core.state.lastTruncation.from, 7)
80+
t.is(core.state.lastTruncation.to, 3)
81+
7482
t.is(core.state.length, 3)
7583
t.is(core.state.byteLength, 12)
7684
t.is(core.state.fork, 2)
7785

7886
await core.state.truncate(2, 3)
87+
t.is(core.state.lastTruncation.from, 3)
88+
t.is(core.state.lastTruncation.to, 2)
7989

8090
await core.state.append([b4a.from('a')])
91+
t.is(core.state.lastTruncation, null)
92+
8193
await core.state.truncate(2, 4)
94+
t.is(core.state.lastTruncation.from, 3)
95+
t.is(core.state.lastTruncation.to, 2)
8296

8397
await core.state.append([b4a.from('a')])
98+
t.is(core.state.lastTruncation, null)
99+
84100
await core.state.truncate(2, 5)
101+
t.is(core.state.lastTruncation.from, 3)
102+
t.is(core.state.lastTruncation.to, 2)
85103

86104
await core.state.append([b4a.from('a')])
105+
t.is(core.state.lastTruncation, null)
106+
87107
await core.state.truncate(2, 6)
108+
t.is(core.state.lastTruncation.from, 3)
109+
t.is(core.state.lastTruncation.to, 2)
88110

89111
await core.state.append([b4a.from('a')])
112+
t.is(core.state.lastTruncation, null)
113+
90114
await core.state.truncate(2, 7)
115+
t.is(core.state.lastTruncation.from, 3)
116+
t.is(core.state.lastTruncation.to, 2)
91117

92118
// check that it was persisted
93119
const coreReopen = await reopen()
94120

95121
t.is(coreReopen.state.length, 2)
96122
t.is(coreReopen.state.byteLength, 10)
97123
t.is(coreReopen.state.fork, 7)
124+
t.is(coreReopen.state.lastTruncation, null)
98125
// t.is(coreReopen.header.hints.reorgs.length, 4)
99126
})
100127

0 commit comments

Comments
 (0)