forked from ethereumjs/merkle-patricia-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckpoint-interface.js
185 lines (162 loc) · 4.1 KB
/
checkpoint-interface.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const levelup = require('levelup')
const memdown = require('memdown')
const async = require('async')
const inherits = require('util').inherits
const Readable = require('readable-stream').Readable
const levelws = require('level-ws')
const callTogether = require('./util').callTogether
module.exports = checkpointInterface
function checkpointInterface (trie) {
this._scratch = null
trie._checkpoints = []
Object.defineProperty(trie, 'isCheckpoint', {
get: function () {
return !!trie._checkpoints.length
}
})
// new methods
trie.checkpoint = checkpoint
trie.commit = commit
trie.revert = revert
trie._enterCpMode = _enterCpMode
trie._exitCpMode = _exitCpMode
trie.createScratchReadStream = createScratchReadStream
// overwrites
trie.copy = copy.bind(trie, trie.copy.bind(trie))
}
/**
* Creates a checkpoint that can later be reverted to or committed. After this is called, no changes to the trie will be permanently saved until `commit` is called
* @method checkpoint
*/
function checkpoint () {
var self = this
var wasCheckpoint = self.isCheckpoint
self._checkpoints.push(self.root)
if (!wasCheckpoint && self.isCheckpoint) {
self._enterCpMode()
}
}
/**
* commits a checkpoint to disk
* @method commit
* @param {Function} cb the callback
*/
function commit (cb) {
var self = this
cb = callTogether(cb, self.sem.leave)
self.sem.take(function () {
if (self.isCheckpoint) {
self._checkpoints.pop()
if (!self.isCheckpoint) {
self._exitCpMode(true, cb)
} else {
cb()
}
} else {
throw new Error('trying to commit when not checkpointed')
}
})
}
/**
* Reverts the trie to the state it was at when `checkpoint` was first called.
* @method revert
* @param {Function} cb the callback
*/
function revert (cb) {
var self = this
cb = callTogether(cb, self.sem.leave)
self.sem.take(function () {
if (self.isCheckpoint) {
self.root = self._checkpoints.pop()
if (!self.isCheckpoint) {
self._exitCpMode(false, cb)
return
}
}
cb()
})
}
// enter into checkpoint mode
function _enterCpMode () {
this._scratch = levelup('', {
db: memdown
})
this._getDBs = [this._scratch].concat(this._getDBs)
this.__putDBs = this._putDBs
this._putDBs = [this._scratch]
this._putRaw = this.putRaw
this.putRaw = putRaw
}
// exit from checkpoint mode
function _exitCpMode (commitState, cb) {
var self = this
var scratch = this._scratch
this._scratch = null
this._getDBs = this._getDBs.slice(1)
this._putDBs = this.__putDBs
this.putRaw = this._putRaw
function flushScratch (db, cb) {
if (!db.createWriteStream) {
db = levelws(db)
}
self.createScratchReadStream(scratch)
.pipe(db.createWriteStream())
.on('close', cb)
}
if (commitState) {
async.map(this._putDBs, flushScratch, cb)
} else {
cb()
}
}
// adds the interface when copying the trie
function copy (_super) {
var trie = _super()
checkpointInterface.call(trie, trie)
trie._scratch = this._scratch
// trie._checkpoints = this._checkpoints.slice()
return trie
}
function putRaw (key, val, cb) {
function dbPut (db, cb2) {
db.put(key, val, {
keyEncoding: 'binary',
valueEncoding: 'binary'
}, cb2)
}
async.each(this.__putDBs, dbPut, cb)
}
function createScratchReadStream (scratch) {
var trie = this.copy()
scratch = scratch || this._scratch
// only read from the scratch
trie._getDBs = [scratch]
trie._scratch = scratch
return new ScratchReadStream(trie)
}
// ScratchReadStream
// this is used to minimally dump the scratch into the db
inherits(ScratchReadStream, Readable)
function ScratchReadStream (trie) {
this.trie = trie
this.next = null
Readable.call(this, {
objectMode: true
})
}
ScratchReadStream.prototype._read = function () {
var self = this
if (!self._started) {
self._started = true
self.trie._findDbNodes(function (nodeRef, node, key, next) {
self.push({
key: nodeRef,
value: node.serialize()
})
next()
}, function () {
// close stream
self.push(null)
})
}
}