Skip to content

Commit 3b51e45

Browse files
committed
trie -> new CP mechanism: added a thin-layer checkpointing API to DB
1 parent 84d4f8c commit 3b51e45

File tree

2 files changed

+31
-42
lines changed

2 files changed

+31
-42
lines changed

packages/trie/src/checkpointTrie.ts

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,7 @@ export class CheckpointTrie extends BaseTrie {
2020
* After this is called, all changes can be reverted until `commit` is called.
2121
*/
2222
checkpoint() {
23-
const wasCheckpoint = this.isCheckpoint
24-
this.db.checkpoints.push({ root: this.root, operations: [] })
25-
26-
// Entering checkpoint mode is not necessary for nested checkpoints
27-
if (!wasCheckpoint && this.isCheckpoint) {
28-
this._enterCpMode()
29-
}
23+
this.db.checkpoint(this.root)
3024
}
3125

3226
/**
@@ -40,13 +34,7 @@ export class CheckpointTrie extends BaseTrie {
4034
}
4135

4236
await this.lock.wait()
43-
44-
this.db.checkpoints.pop()
45-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
46-
if (!this.isCheckpoint) {
47-
await this._exitCpMode(true)
48-
}
49-
37+
this.db.commit()
5038
this.lock.signal()
5139
}
5240

@@ -56,16 +44,13 @@ export class CheckpointTrie extends BaseTrie {
5644
* parent checkpoint as current.
5745
*/
5846
async revert(): Promise<void> {
47+
if (!this.isCheckpoint) {
48+
throw new Error('trying to revert when not checkpointed')
49+
}
50+
5951
await this.lock.wait()
6052
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
61-
if (this.isCheckpoint) {
62-
const { root } = this.db.checkpoints.pop()!
63-
this.root = root
64-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
65-
if (!this.isCheckpoint) {
66-
await this._exitCpMode(false)
67-
}
68-
}
53+
this.root = this.db.revert()
6954
this.lock.signal()
7055
}
7156

@@ -81,24 +66,4 @@ export class CheckpointTrie extends BaseTrie {
8166
}
8267
return trie
8368
}
84-
85-
/**
86-
* Enter into checkpoint mode.
87-
* @private
88-
*/
89-
_enterCpMode() {}
90-
91-
/**
92-
* Exit from checkpoint mode.
93-
* @private
94-
*/
95-
async _exitCpMode(commitState: boolean): Promise<void> {
96-
return new Promise((resolve) => {
97-
/*if (commitState) {
98-
} else {
99-
}*/
100-
commitState = !!commitState // Temporary dummy line for linting
101-
resolve()
102-
})
103-
}
10469
}

packages/trie/src/db.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ export class DB {
4040
this._leveldb = leveldb || level()
4141
}
4242

43+
/**
44+
* Adds a new checkpoint to the stack
45+
* @param root
46+
*/
47+
checkpoint(root: Buffer) {
48+
this.checkpoints.push({ root, operations: [] })
49+
}
50+
51+
/**
52+
* Commits the latest checkpoint
53+
*/
54+
commit() {
55+
const { root } = this.checkpoints.pop()!
56+
return root
57+
}
58+
59+
/**
60+
* Reverts the latest checkpoint
61+
*/
62+
revert() {
63+
const { root } = this.checkpoints.pop()!
64+
return root
65+
}
66+
4367
/**
4468
* Retrieves a raw value from leveldb.
4569
* @param key

0 commit comments

Comments
 (0)