hypercore-storage 1.16.0 → 1.17.0
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/index.js +190 -101
- package/lib/block-dependency-stream.js +13 -13
- package/lib/close-error-stream.js +2 -2
- package/lib/keys.js +1 -1
- package/lib/streams.js +55 -21
- package/lib/tx.js +83 -58
- package/lib/view.js +34 -36
- package/package.json +5 -3
package/lib/view.js
CHANGED
|
@@ -3,7 +3,7 @@ const CloseErrorStream = require('./close-error-stream.js')
|
|
|
3
3
|
const b4a = require('b4a')
|
|
4
4
|
|
|
5
5
|
class OverlayStream extends Readable {
|
|
6
|
-
constructor
|
|
6
|
+
constructor(stream, start, end, reverse, changes, cleared) {
|
|
7
7
|
super()
|
|
8
8
|
|
|
9
9
|
this.start = start
|
|
@@ -22,12 +22,12 @@ class OverlayStream extends Readable {
|
|
|
22
22
|
this._stream.on('close', this._onclose.bind(this))
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
_drainMaybe
|
|
25
|
+
_drainMaybe() {
|
|
26
26
|
if (this._drained === true) return
|
|
27
27
|
this._drained = this._onreadable()
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
_onclose
|
|
30
|
+
_onclose() {
|
|
31
31
|
if (this.destroying) return
|
|
32
32
|
|
|
33
33
|
const err = getStreamError(this._stream)
|
|
@@ -49,7 +49,7 @@ class OverlayStream extends Readable {
|
|
|
49
49
|
this._stream = null
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
_onreadable
|
|
52
|
+
_onreadable() {
|
|
53
53
|
let data = this._stream.read()
|
|
54
54
|
if (data === null) return false
|
|
55
55
|
|
|
@@ -63,16 +63,16 @@ class OverlayStream extends Readable {
|
|
|
63
63
|
return drained
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
_read
|
|
66
|
+
_read(cb) {
|
|
67
67
|
this._drained = this._onreadable()
|
|
68
68
|
cb(null)
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
_predestroy
|
|
71
|
+
_predestroy() {
|
|
72
72
|
this.stream.destroy()
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
_push
|
|
75
|
+
_push(entry) {
|
|
76
76
|
const key = entry.key
|
|
77
77
|
|
|
78
78
|
while (this.range < this.cleared.length) {
|
|
@@ -125,20 +125,20 @@ class OverlayStream extends Readable {
|
|
|
125
125
|
return true
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
_inRange
|
|
128
|
+
_inRange(key) {
|
|
129
129
|
return b4a.compare(this.start, key) <= 0 && b4a.compare(key, this.end) < 0
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
class Overlay {
|
|
134
|
-
constructor
|
|
134
|
+
constructor() {
|
|
135
135
|
this.indexed = 0
|
|
136
136
|
this.changes = null
|
|
137
137
|
this.cleared = null
|
|
138
138
|
this.reverse = false
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
update
|
|
141
|
+
update(view, reverse) {
|
|
142
142
|
if (view.indexed === this.indexed) return
|
|
143
143
|
|
|
144
144
|
const changes = view.map === null ? [] : [...view.map.values()]
|
|
@@ -155,7 +155,7 @@ class Overlay {
|
|
|
155
155
|
this.reverse = reverse
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
createStream
|
|
158
|
+
createStream(stream, start, end, reverse) {
|
|
159
159
|
return new OverlayStream(
|
|
160
160
|
stream,
|
|
161
161
|
start,
|
|
@@ -168,7 +168,7 @@ class Overlay {
|
|
|
168
168
|
}
|
|
169
169
|
|
|
170
170
|
class View {
|
|
171
|
-
constructor
|
|
171
|
+
constructor() {
|
|
172
172
|
this.map = null
|
|
173
173
|
this.indexed = 0
|
|
174
174
|
this.changes = null
|
|
@@ -178,7 +178,7 @@ class View {
|
|
|
178
178
|
this.readers = 0
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
-
snapshot
|
|
181
|
+
snapshot() {
|
|
182
182
|
if (this._attached()) return this.snap.snapshot()
|
|
183
183
|
|
|
184
184
|
const snap = new View()
|
|
@@ -196,32 +196,32 @@ class View {
|
|
|
196
196
|
return snap
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
-
readStart
|
|
199
|
+
readStart() {
|
|
200
200
|
if (this.snap !== null) this.readers++
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
readStop
|
|
203
|
+
readStop() {
|
|
204
204
|
if (this.snap !== null && --this.readers === 0) this.snap.readers--
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
-
size
|
|
207
|
+
size() {
|
|
208
208
|
return this.changes === null ? 0 : this.changes.length
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
-
updated
|
|
211
|
+
updated() {
|
|
212
212
|
return this.changes === null
|
|
213
213
|
}
|
|
214
214
|
|
|
215
|
-
get
|
|
215
|
+
get(read, key) {
|
|
216
216
|
return this.changes === null ? read.get(key) : this._indexAndGet(read, key)
|
|
217
217
|
}
|
|
218
218
|
|
|
219
|
-
reset
|
|
219
|
+
reset() {
|
|
220
220
|
this.indexed = 0
|
|
221
221
|
this.snap = this.map = this.changes = this.cleared = this.overlay = null
|
|
222
222
|
}
|
|
223
223
|
|
|
224
|
-
iterator
|
|
224
|
+
iterator(db, start, end, reverse) {
|
|
225
225
|
if (dbClosing(db)) return new CloseErrorStream(new Error('RocksDB session is closed'))
|
|
226
226
|
|
|
227
227
|
const stream = db.iterator({ gte: start, lt: end, reverse })
|
|
@@ -234,20 +234,18 @@ class View {
|
|
|
234
234
|
return this.overlay.createStream(stream, start, end, reverse)
|
|
235
235
|
}
|
|
236
236
|
|
|
237
|
-
_indexAndGet
|
|
237
|
+
_indexAndGet(read, key) {
|
|
238
238
|
this._index()
|
|
239
239
|
const change = this.map.get(b4a.toString(key, 'hex'))
|
|
240
240
|
|
|
241
241
|
if (change === undefined) {
|
|
242
|
-
return this.cleared === null
|
|
243
|
-
? read.get(key)
|
|
244
|
-
: this._readAndMaybeDrop(read, key)
|
|
242
|
+
return this.cleared === null ? read.get(key) : this._readAndMaybeDrop(read, key)
|
|
245
243
|
}
|
|
246
244
|
|
|
247
245
|
return Promise.resolve(change[1])
|
|
248
246
|
}
|
|
249
247
|
|
|
250
|
-
async _readAndMaybeDrop
|
|
248
|
+
async _readAndMaybeDrop(read, key) {
|
|
251
249
|
const cleared = this.cleared // in case its cleared
|
|
252
250
|
const value = await read.get(key)
|
|
253
251
|
if (value === null) return null
|
|
@@ -261,15 +259,15 @@ class View {
|
|
|
261
259
|
return value
|
|
262
260
|
}
|
|
263
261
|
|
|
264
|
-
_attached
|
|
262
|
+
_attached() {
|
|
265
263
|
return this.snap !== null && this.changes === this.snap.changes
|
|
266
264
|
}
|
|
267
265
|
|
|
268
|
-
_frozen
|
|
266
|
+
_frozen() {
|
|
269
267
|
return this.changes === null || (this.snap !== null && this.changes !== this.snap.changes)
|
|
270
268
|
}
|
|
271
269
|
|
|
272
|
-
_index
|
|
270
|
+
_index() {
|
|
273
271
|
// if we are a snap and we are still attached (ie no mutations), simply copy the refs
|
|
274
272
|
if (this._attached()) {
|
|
275
273
|
this.snap._index()
|
|
@@ -290,7 +288,7 @@ class View {
|
|
|
290
288
|
}
|
|
291
289
|
}
|
|
292
290
|
|
|
293
|
-
_indexRange
|
|
291
|
+
_indexRange(range) {
|
|
294
292
|
const s = b4a.toString(range[0], 'hex')
|
|
295
293
|
const e = b4a.toString(range[2], 'hex')
|
|
296
294
|
|
|
@@ -302,7 +300,7 @@ class View {
|
|
|
302
300
|
this.cleared.push(range)
|
|
303
301
|
}
|
|
304
302
|
|
|
305
|
-
apply
|
|
303
|
+
apply(changes) {
|
|
306
304
|
if (this.snap !== null) throw new Error('Illegal to push changes to a snapshot')
|
|
307
305
|
|
|
308
306
|
if (this.readers !== 0 && this.changes !== null) {
|
|
@@ -321,7 +319,7 @@ class View {
|
|
|
321
319
|
}
|
|
322
320
|
}
|
|
323
321
|
|
|
324
|
-
static async flush
|
|
322
|
+
static async flush(changes, db) {
|
|
325
323
|
if (changes === null) return true
|
|
326
324
|
|
|
327
325
|
const w = db.write({ autoDestroy: true })
|
|
@@ -340,24 +338,24 @@ class View {
|
|
|
340
338
|
|
|
341
339
|
module.exports = View
|
|
342
340
|
|
|
343
|
-
function cmpChange
|
|
341
|
+
function cmpChange(a, b) {
|
|
344
342
|
const c = b4a.compare(a[0], b[0])
|
|
345
343
|
return c === 0 ? b4a.compare(a[2], b[2]) : c
|
|
346
344
|
}
|
|
347
345
|
|
|
348
|
-
function cmpChangeReverse
|
|
346
|
+
function cmpChangeReverse(a, b) {
|
|
349
347
|
return cmpChange(b, a)
|
|
350
348
|
}
|
|
351
349
|
|
|
352
|
-
function noop
|
|
350
|
+
function noop() {}
|
|
353
351
|
|
|
354
|
-
function reverseArray
|
|
352
|
+
function reverseArray(list) {
|
|
355
353
|
const r = new Array(list.length)
|
|
356
354
|
for (let i = 0; i < list.length; i++) r[r.length - 1 - i] = list[i]
|
|
357
355
|
return r
|
|
358
356
|
}
|
|
359
357
|
|
|
360
358
|
// TODO: expose from rocks instead
|
|
361
|
-
function dbClosing
|
|
359
|
+
function dbClosing(db) {
|
|
362
360
|
return db._state.closing || db._index === -1
|
|
363
361
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hypercore-storage",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.17.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"files": [
|
|
6
6
|
"index.js",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"migrations/0/*.js"
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
|
-
"
|
|
12
|
+
"format": "prettier --write .",
|
|
13
|
+
"test": "prettier --check . && node test/all.js",
|
|
13
14
|
"test:bare": "bare test/all.js",
|
|
14
15
|
"test:generate": "brittle -r test/all.js test/*.js"
|
|
15
16
|
},
|
|
@@ -43,7 +44,8 @@
|
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|
|
45
46
|
"brittle": "^3.7.0",
|
|
46
|
-
"
|
|
47
|
+
"prettier": "^3.6.2",
|
|
48
|
+
"prettier-config-holepunch": "^2.0.0",
|
|
47
49
|
"test-tmp": "^1.3.1"
|
|
48
50
|
}
|
|
49
51
|
}
|