hypercore-storage 0.0.27 → 0.0.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/memory-overlay.js +68 -13
- package/lib/messages.js +2 -2
- package/package.json +1 -1
package/lib/memory-overlay.js
CHANGED
|
@@ -9,7 +9,7 @@ class OverlayStream extends Readable {
|
|
|
9
9
|
|
|
10
10
|
this.tip = tip
|
|
11
11
|
this.storage = storage
|
|
12
|
-
this.
|
|
12
|
+
this.options = opts
|
|
13
13
|
|
|
14
14
|
this._reverse = !!opts.reverse
|
|
15
15
|
this._active = null
|
|
@@ -18,11 +18,7 @@ class OverlayStream extends Readable {
|
|
|
18
18
|
this._createStream = createStream
|
|
19
19
|
this._pendingDestroy = null
|
|
20
20
|
|
|
21
|
-
if (!opts.reverse)
|
|
22
|
-
this._active = createStream(storage, opts)
|
|
23
|
-
this._active.on('close', this._onclose.bind(this))
|
|
24
|
-
this._active.on('data', this._ondata.bind(this))
|
|
25
|
-
}
|
|
21
|
+
if (!opts.reverse) this._makeActiveStream()
|
|
26
22
|
}
|
|
27
23
|
|
|
28
24
|
_predestroy () {
|
|
@@ -31,7 +27,10 @@ class OverlayStream extends Readable {
|
|
|
31
27
|
|
|
32
28
|
_read (cb) {
|
|
33
29
|
if (this._active === null) {
|
|
34
|
-
if (this._yield())
|
|
30
|
+
if (this._yield()) {
|
|
31
|
+
cb(null)
|
|
32
|
+
return
|
|
33
|
+
}
|
|
35
34
|
}
|
|
36
35
|
|
|
37
36
|
if (this._active) {
|
|
@@ -69,13 +68,18 @@ class OverlayStream extends Readable {
|
|
|
69
68
|
return true
|
|
70
69
|
}
|
|
71
70
|
|
|
72
|
-
this.
|
|
73
|
-
this._active.on('close', this._onclose.bind(this))
|
|
74
|
-
this._active.on('data', this._ondata.bind(this))
|
|
71
|
+
this._makeActiveStream()
|
|
75
72
|
|
|
76
73
|
return false
|
|
77
74
|
}
|
|
78
75
|
|
|
76
|
+
_makeActiveStream () {
|
|
77
|
+
this._active = this._createStream(this.storage, this.options)
|
|
78
|
+
this._active.on('error', noop) // handled in onclose
|
|
79
|
+
this._active.on('close', this._onclose.bind(this))
|
|
80
|
+
this._active.on('data', this._ondata.bind(this))
|
|
81
|
+
}
|
|
82
|
+
|
|
79
83
|
_ondata (data) {
|
|
80
84
|
if (!this.push(data)) this._active.pause()
|
|
81
85
|
}
|
|
@@ -87,7 +91,10 @@ class OverlayStream extends Readable {
|
|
|
87
91
|
return
|
|
88
92
|
}
|
|
89
93
|
|
|
90
|
-
if (this._reverse)
|
|
94
|
+
if (this._reverse) {
|
|
95
|
+
this.push(null)
|
|
96
|
+
return
|
|
97
|
+
}
|
|
91
98
|
|
|
92
99
|
this._yield()
|
|
93
100
|
}
|
|
@@ -108,6 +115,9 @@ class MemoryOverlay {
|
|
|
108
115
|
this.bitfields = null
|
|
109
116
|
|
|
110
117
|
this.snapshotted = false
|
|
118
|
+
this.snapshotShared = null
|
|
119
|
+
this.activeSnapshots = 0
|
|
120
|
+
this.copyOnWrite = false
|
|
111
121
|
}
|
|
112
122
|
|
|
113
123
|
async registerBatch (name, length, overwrite) {
|
|
@@ -115,7 +125,44 @@ class MemoryOverlay {
|
|
|
115
125
|
}
|
|
116
126
|
|
|
117
127
|
snapshot () {
|
|
118
|
-
|
|
128
|
+
if (this.snapshotShared !== null) return this.snapshotShared.snapshot()
|
|
129
|
+
|
|
130
|
+
this.activeSnapshots++
|
|
131
|
+
this.copyOnWrite = true
|
|
132
|
+
|
|
133
|
+
const snap = new MemoryOverlay(this.storage.snapshot())
|
|
134
|
+
|
|
135
|
+
snap.snapshotted = true
|
|
136
|
+
snap.snapshotShared = this
|
|
137
|
+
|
|
138
|
+
if (this.head !== null) snap.head = this.head
|
|
139
|
+
if (this.auth !== null) snap.auth = this.auth
|
|
140
|
+
if (this.localKeyPair !== null) snap.localKeyPair = this.localKeyPair
|
|
141
|
+
if (this.encryptionKey !== null) snap.encryptionKey = this.encryptionKey
|
|
142
|
+
if (this.dataDependency !== null) snap.dataDependency = this.dataDependency
|
|
143
|
+
if (this.dataInfo !== null) snap.dataInfo = this.dataInfo
|
|
144
|
+
if (this.userData !== null) snap.userData = this.userData
|
|
145
|
+
if (this.blocks !== null) snap.blocks = this.blocks
|
|
146
|
+
if (this.treeNodes !== null) snap.treeNodes = this.treeNodes
|
|
147
|
+
if (this.bitfields !== null) snap.bitfields = this.bitfields
|
|
148
|
+
|
|
149
|
+
return snap
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
_copy () {
|
|
153
|
+
this.copyOnWrite = false
|
|
154
|
+
|
|
155
|
+
// clone state
|
|
156
|
+
if (this.head !== null) this.head = Object.assign(this.head)
|
|
157
|
+
if (this.auth !== null) this.auth = Object.assign(this.auth)
|
|
158
|
+
if (this.localKeyPair !== null) this.localKeyPair = Object.assign(this.localKeyPair)
|
|
159
|
+
if (this.encryptionKey !== null) this.encryptionKey = Object.assign(this.encryptionKey)
|
|
160
|
+
if (this.dataDependency !== null) this.dataDependency = Object.assign(this.dataDependency)
|
|
161
|
+
if (this.dataInfo !== null) this.dataInfo = Object.assign(this.dataInfo)
|
|
162
|
+
if (this.userData !== null) this.userData = mergeMap(new Map(), this.userData)
|
|
163
|
+
if (this.blocks !== null) this.blocks = mergeTip(new TipList(), this.blocks)
|
|
164
|
+
if (this.treeNodes !== null) this.treeNodes = mergeMap(new Map(), this.treeNodes)
|
|
165
|
+
if (this.bitfields !== null) this.bitfields = mergeTip(new TipList(), this.bitfields)
|
|
119
166
|
}
|
|
120
167
|
|
|
121
168
|
get dependencies () {
|
|
@@ -132,6 +179,7 @@ class MemoryOverlay {
|
|
|
132
179
|
}
|
|
133
180
|
|
|
134
181
|
createWriteBatch () {
|
|
182
|
+
if (this.copyOnWrite) this._copy()
|
|
135
183
|
return new MemoryOverlayWriteBatch(this)
|
|
136
184
|
}
|
|
137
185
|
|
|
@@ -166,7 +214,12 @@ class MemoryOverlay {
|
|
|
166
214
|
return (page && (!disk || index > disk.index)) ? { index, page } : disk
|
|
167
215
|
}
|
|
168
216
|
|
|
169
|
-
destroy () {
|
|
217
|
+
destroy () {
|
|
218
|
+
if (this.snapshotted) this.storage.destroy()
|
|
219
|
+
if (this.snapshotShared === null) return
|
|
220
|
+
if (--this.snapshotShared.activeSnapshots === 0) this.snapshotShared.copyOnWrite = false
|
|
221
|
+
this.snapshotShared = null
|
|
222
|
+
}
|
|
170
223
|
|
|
171
224
|
merge (overlay) {
|
|
172
225
|
if (overlay.head !== null) this.head = overlay.head
|
|
@@ -373,6 +426,8 @@ function mergeTip (a, b) {
|
|
|
373
426
|
return a
|
|
374
427
|
}
|
|
375
428
|
|
|
429
|
+
function noop () {}
|
|
430
|
+
|
|
376
431
|
function todo () {
|
|
377
432
|
throw ASSERTION('Not supported yet, but will be')
|
|
378
433
|
}
|
package/lib/messages.js
CHANGED
|
@@ -25,13 +25,13 @@ exports.KeyPair = {
|
|
|
25
25
|
|
|
26
26
|
exports.StorageInfo = {
|
|
27
27
|
preencode (state, m) {
|
|
28
|
-
c.uint.preencode(state,
|
|
28
|
+
c.uint.preencode(state, 0) // version
|
|
29
29
|
c.uint.preencode(state, 0) // flags, reserved
|
|
30
30
|
c.uint.preencode(state, m.free)
|
|
31
31
|
c.uint.preencode(state, m.total)
|
|
32
32
|
},
|
|
33
33
|
encode (state, m) {
|
|
34
|
-
c.uint.encode(state,
|
|
34
|
+
c.uint.encode(state, 0) // version
|
|
35
35
|
c.uint.encode(state, 0) // flags, reserved
|
|
36
36
|
c.uint.encode(state, m.free)
|
|
37
37
|
c.uint.encode(state, m.total)
|