hypercore-storage 1.0.0 → 1.0.2

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.
Files changed (3) hide show
  1. package/index.js +18 -18
  2. package/lib/view.js +38 -19
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -47,12 +47,12 @@ class Atom {
47
47
  }
48
48
 
49
49
  class HypercoreStorage {
50
- constructor (store, db, core, view, atomic) {
50
+ constructor (store, db, core, view, atom) {
51
51
  this.store = store
52
52
  this.db = db
53
53
  this.core = core
54
54
  this.view = view
55
- this.atomic = atomic
55
+ this.atom = atom
56
56
 
57
57
  this.view.readStart()
58
58
  }
@@ -95,15 +95,15 @@ class HypercoreStorage {
95
95
  }
96
96
 
97
97
  snapshot () {
98
- return new HypercoreStorage(this.store, this.db.snapshot(), this.core, this.view.snapshot(), this.atomic)
98
+ return new HypercoreStorage(this.store, this.db.snapshot(), this.core, this.view.snapshot(), this.atom)
99
99
  }
100
100
 
101
101
  atomize (atom) {
102
- return new HypercoreStorage(this.store, this.db.session(), this.core, atom.view, true)
102
+ return new HypercoreStorage(this.store, this.db.session(), this.core, atom.view, atom)
103
103
  }
104
104
 
105
- atom () {
106
- return this.store.atom()
105
+ createAtom () {
106
+ return this.store.createAtom()
107
107
  }
108
108
 
109
109
  createBlockStream (opts) {
@@ -149,11 +149,11 @@ class HypercoreStorage {
149
149
  const dependency = await dependencyPromise
150
150
  if (dependency) core.dependencies = this._addDependency(dependency)
151
151
 
152
- return new HypercoreStorage(this.store, this.db.session(), core, this.atomic ? this.view : new View(), this.atomic)
152
+ return new HypercoreStorage(this.store, this.db.session(), core, this.atom ? this.view : new View(), this.atom)
153
153
  }
154
154
 
155
- async createSession (name, head, atom) {
156
- const rx = this.read(atom)
155
+ async createSession (name, head) {
156
+ const rx = this.read()
157
157
 
158
158
  const existingSessionsPromise = rx.getSessions()
159
159
  const existingHeadPromise = rx.getHead()
@@ -174,7 +174,7 @@ class HypercoreStorage {
174
174
  session.dataPointer = await this.store._allocData()
175
175
  }
176
176
 
177
- const tx = this.write(atom)
177
+ const tx = this.write()
178
178
 
179
179
  tx.setSessions(sessions)
180
180
 
@@ -193,7 +193,7 @@ class HypercoreStorage {
193
193
 
194
194
  await tx.flush()
195
195
 
196
- return new HypercoreStorage(this.store, this.db.session(), core, atom ? atom.view : this.atomic ? this.view : new View(), !!atom || this.atomic)
196
+ return new HypercoreStorage(this.store, this.db.session(), core, this.atom ? this.view : new View(), this.atom)
197
197
  }
198
198
 
199
199
  async createAtomicSession (atom, head) {
@@ -233,12 +233,12 @@ class HypercoreStorage {
233
233
  return deps
234
234
  }
235
235
 
236
- read (atom) {
237
- return new CoreRX(this.core, this.db, atom ? atom.view : this.view)
236
+ read () {
237
+ return new CoreRX(this.core, this.db, this.view)
238
238
  }
239
239
 
240
- write (atom) {
241
- return new CoreTX(this.core, this.db, atom ? atom.view : this.atomic ? this.view : null, [])
240
+ write () {
241
+ return new CoreTX(this.core, this.db, this.atom ? this.view : null, [])
242
242
  }
243
243
 
244
244
  close () {
@@ -429,7 +429,7 @@ class CorestoreStorage {
429
429
  return head === null ? initStoreHead() : head
430
430
  }
431
431
 
432
- atom () {
432
+ createAtom () {
433
433
  return new Atom(this.db)
434
434
  }
435
435
 
@@ -585,7 +585,7 @@ class CorestoreStorage {
585
585
  dataPointer = dependency.dataPointer
586
586
  }
587
587
 
588
- const result = new HypercoreStorage(this, this.db.session(), core, EMPTY, false)
588
+ const result = new HypercoreStorage(this, this.db.session(), core, EMPTY, null)
589
589
 
590
590
  if (result.core.version === 0) await this._migrateCore(result, discoveryKey, create)
591
591
  return result
@@ -635,7 +635,7 @@ class CorestoreStorage {
635
635
 
636
636
  tx.apply()
637
637
 
638
- return new HypercoreStorage(this, this.db.session(), ptr, EMPTY, false)
638
+ return new HypercoreStorage(this, this.db.session(), ptr, EMPTY, null)
639
639
  }
640
640
 
641
641
  async create (data) {
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, ranges) {
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.ranges = ranges
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.ranges.length) {
77
- const r = this.ranges[this.range]
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, r[0]) < 0 : b4a.compare(r[2], key) <= 0) {
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(r[0], key) <= 0 && b4a.compare(key, r[2]) < 0) {
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.ranges = null
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 ranges = view.ranges === null ? [] : view.ranges.slice(0)
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
- ranges.sort(cmp)
140
+ cleared.sort(cmp)
141
141
 
142
142
  this.indexed = view.indexed
143
143
  this.changes = changes
144
- this.ranges = ranges
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.ranges : reverseArray(this.ranges)
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.ranges = null
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.ranges = this.ranges
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.ranges = null
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
- if (change === undefined) return read.get(key)
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.ranges = this.snap.ranges
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.ranges === null) this.ranges = []
270
- this.ranges.push(range)
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.ranges = this.ranges === null ? null : this.ranges.slice(0)
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",