hypercore-storage 1.0.0 → 1.0.1
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/view.js +38 -19
- package/package.json +1 -1
package/lib/view.js
CHANGED
|
@@ -2,14 +2,14 @@ const { Readable, getStreamError } = require('streamx')
|
|
|
2
2
|
const b4a = require('b4a')
|
|
3
3
|
|
|
4
4
|
class OverlayStream extends Readable {
|
|
5
|
-
constructor (stream, start, end, reverse, changes,
|
|
5
|
+
constructor (stream, start, end, reverse, changes, cleared) {
|
|
6
6
|
super()
|
|
7
7
|
|
|
8
8
|
this.start = start
|
|
9
9
|
this.end = end
|
|
10
10
|
this.reverse = reverse
|
|
11
11
|
this.changes = changes
|
|
12
|
-
this.
|
|
12
|
+
this.cleared = cleared
|
|
13
13
|
this.change = 0
|
|
14
14
|
this.range = 0
|
|
15
15
|
|
|
@@ -73,17 +73,17 @@ class OverlayStream extends Readable {
|
|
|
73
73
|
_push (entry) {
|
|
74
74
|
const key = entry.key
|
|
75
75
|
|
|
76
|
-
while (this.range < this.
|
|
77
|
-
const
|
|
76
|
+
while (this.range < this.cleared.length) {
|
|
77
|
+
const c = this.cleared[this.range]
|
|
78
78
|
|
|
79
79
|
// we moved past the range
|
|
80
|
-
if (this.reverse ? b4a.compare(key,
|
|
80
|
+
if (this.reverse ? b4a.compare(key, c[0]) < 0 : b4a.compare(c[2], key) <= 0) {
|
|
81
81
|
this.range++
|
|
82
82
|
continue
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
// we didnt move past and are in, drop
|
|
86
|
-
if (b4a.compare(
|
|
86
|
+
if (b4a.compare(c[0], key) <= 0 && b4a.compare(key, c[2]) < 0) {
|
|
87
87
|
return true
|
|
88
88
|
}
|
|
89
89
|
|
|
@@ -124,7 +124,7 @@ class Overlay {
|
|
|
124
124
|
constructor () {
|
|
125
125
|
this.indexed = 0
|
|
126
126
|
this.changes = null
|
|
127
|
-
this.
|
|
127
|
+
this.cleared = null
|
|
128
128
|
this.reverse = false
|
|
129
129
|
}
|
|
130
130
|
|
|
@@ -132,16 +132,16 @@ class Overlay {
|
|
|
132
132
|
if (view.indexed === this.indexed) return
|
|
133
133
|
|
|
134
134
|
const changes = view.map === null ? [] : [...view.map.values()]
|
|
135
|
-
const
|
|
135
|
+
const cleared = view.cleared === null ? [] : view.cleared.slice(0)
|
|
136
136
|
|
|
137
137
|
const cmp = reverse ? cmpChangeReverse : cmpChange
|
|
138
138
|
|
|
139
139
|
changes.sort(cmp)
|
|
140
|
-
|
|
140
|
+
cleared.sort(cmp)
|
|
141
141
|
|
|
142
142
|
this.indexed = view.indexed
|
|
143
143
|
this.changes = changes
|
|
144
|
-
this.
|
|
144
|
+
this.cleared = cleared
|
|
145
145
|
this.reverse = reverse
|
|
146
146
|
}
|
|
147
147
|
|
|
@@ -152,7 +152,7 @@ class Overlay {
|
|
|
152
152
|
end,
|
|
153
153
|
reverse,
|
|
154
154
|
this.reverse === reverse ? this.changes : reverseArray(this.changes),
|
|
155
|
-
this.reverse === reverse ? this.
|
|
155
|
+
this.reverse === reverse ? this.cleared : reverseArray(this.cleared)
|
|
156
156
|
)
|
|
157
157
|
}
|
|
158
158
|
}
|
|
@@ -162,7 +162,7 @@ class View {
|
|
|
162
162
|
this.map = null
|
|
163
163
|
this.indexed = 0
|
|
164
164
|
this.changes = null
|
|
165
|
-
this.
|
|
165
|
+
this.cleared = null
|
|
166
166
|
this.overlay = null
|
|
167
167
|
this.snap = null
|
|
168
168
|
this.readers = 0
|
|
@@ -176,7 +176,7 @@ class View {
|
|
|
176
176
|
snap.map = this.map
|
|
177
177
|
snap.indexed = this.indexed
|
|
178
178
|
snap.changes = this.changes
|
|
179
|
-
snap.
|
|
179
|
+
snap.cleared = this.cleared
|
|
180
180
|
|
|
181
181
|
if (this._frozen()) return snap
|
|
182
182
|
|
|
@@ -208,7 +208,7 @@ class View {
|
|
|
208
208
|
|
|
209
209
|
reset () {
|
|
210
210
|
this.indexed = 0
|
|
211
|
-
this.snap = this.map = this.changes = this.
|
|
211
|
+
this.snap = this.map = this.changes = this.cleared = null
|
|
212
212
|
}
|
|
213
213
|
|
|
214
214
|
iterator (db, start, end, reverse) {
|
|
@@ -225,10 +225,29 @@ class View {
|
|
|
225
225
|
_indexAndGet (read, key) {
|
|
226
226
|
this._index()
|
|
227
227
|
const change = this.map.get(b4a.toString(key, 'hex'))
|
|
228
|
-
|
|
228
|
+
|
|
229
|
+
if (change === undefined) {
|
|
230
|
+
return this.cleared === null
|
|
231
|
+
? read.get(key)
|
|
232
|
+
: this._readAndMaybeDrop(read, key)
|
|
233
|
+
}
|
|
234
|
+
|
|
229
235
|
return Promise.resolve(change[1])
|
|
230
236
|
}
|
|
231
237
|
|
|
238
|
+
async _readAndMaybeDrop (read, key) {
|
|
239
|
+
const value = await read.get(key)
|
|
240
|
+
if (value === null) return null
|
|
241
|
+
|
|
242
|
+
for (let i = 0; i < this.cleared.length; i++) {
|
|
243
|
+
const c = this.cleared[i]
|
|
244
|
+
// check if in range
|
|
245
|
+
if (b4a.compare(c[0], key) <= 0 && b4a.compare(key, c[2]) < 0) return null
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return value
|
|
249
|
+
}
|
|
250
|
+
|
|
232
251
|
_attached () {
|
|
233
252
|
return this.snap !== null && this.changes === this.snap.changes
|
|
234
253
|
}
|
|
@@ -242,7 +261,7 @@ class View {
|
|
|
242
261
|
if (this._attached()) {
|
|
243
262
|
this.snap._index()
|
|
244
263
|
this.map = this.snap.map
|
|
245
|
-
this.
|
|
264
|
+
this.cleared = this.snap.cleared
|
|
246
265
|
this.indexed = this.snap.indexed
|
|
247
266
|
return
|
|
248
267
|
}
|
|
@@ -266,8 +285,8 @@ class View {
|
|
|
266
285
|
if (s <= key && key < e) this.map.set(key, [c[0], null, null])
|
|
267
286
|
}
|
|
268
287
|
|
|
269
|
-
if (this.
|
|
270
|
-
this.
|
|
288
|
+
if (this.cleared === null) this.cleared = []
|
|
289
|
+
this.cleared.push(range)
|
|
271
290
|
}
|
|
272
291
|
|
|
273
292
|
apply (changes) {
|
|
@@ -275,7 +294,7 @@ class View {
|
|
|
275
294
|
|
|
276
295
|
if (this.readers !== 0 && this.changes !== null) {
|
|
277
296
|
this.changes = this.changes.slice(0)
|
|
278
|
-
this.
|
|
297
|
+
this.cleared = this.cleared === null ? null : this.cleared.slice(0)
|
|
279
298
|
this.map = this.map === null ? null : new Map([...this.map])
|
|
280
299
|
}
|
|
281
300
|
|