hypercore-storage 2.7.0 → 2.8.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 CHANGED
@@ -21,6 +21,7 @@ const {
21
21
  createBitfieldStream,
22
22
  createUserDataStream,
23
23
  createTreeNodeStream,
24
+ createMarkStream,
24
25
  createLocalStream
25
26
  } = require('./lib/streams.js')
26
27
 
@@ -32,9 +33,14 @@ class Atom {
32
33
  this.view = new View()
33
34
  this.flushedPromise = null
34
35
  this.flushing = false
36
+ this.preflushes = []
35
37
  this.flushes = []
36
38
  }
37
39
 
40
+ preflush(fn) {
41
+ this.preflushes.push(fn)
42
+ }
43
+
38
44
  onflush(fn) {
39
45
  this.flushes.push(fn)
40
46
  }
@@ -57,8 +63,10 @@ class Atom {
57
63
  this.flushing = true
58
64
 
59
65
  try {
66
+ const plen = this.preflushes.length
67
+ for (let i = 0; i < plen; i++) this.preflushes[i]()
68
+
60
69
  await View.flush(this.view.changes, this.db)
61
- this.view.reset()
62
70
 
63
71
  const promises = []
64
72
  const len = this.flushes.length // in case of reentry
@@ -66,6 +74,7 @@ class Atom {
66
74
 
67
75
  await Promise.all(promises)
68
76
  } finally {
77
+ this.view.reset()
69
78
  this.flushing = false
70
79
  if (this.flushedPromise !== null) this._resolve()
71
80
  }
@@ -230,6 +239,10 @@ class HypercoreStorage {
230
239
  return createUserDataStream(this.core, this.db, this.view, opts)
231
240
  }
232
241
 
242
+ createMarkStream(opts) {
243
+ return createMarkStream(this.core, this.db, this.view, opts)
244
+ }
245
+
233
246
  createLocalStream(opts) {
234
247
  return createLocalStream(this.core, this.db, this.view, opts)
235
248
  }
@@ -450,6 +463,11 @@ class HypercoreStorage {
450
463
 
451
464
  return core
452
465
  }
466
+
467
+ static isParentStorage(storage, parent) {
468
+ const last = getLastDependency(storage)
469
+ return !!last && last.dataPointer === parent.core.dataPointer
470
+ }
453
471
  }
454
472
 
455
473
  class CorestoreStorage {
@@ -1103,6 +1121,10 @@ class CorestoreStorage {
1103
1121
  await this._exit()
1104
1122
  }
1105
1123
  }
1124
+
1125
+ static isParentStorage(storage, parent) {
1126
+ return HypercoreStorage.isParentStorage(storage, parent)
1127
+ }
1106
1128
  }
1107
1129
 
1108
1130
  module.exports = CorestoreStorage
@@ -1157,6 +1179,10 @@ function createColumnFamily(db, opts = {}) {
1157
1179
  return db.columnFamily(col)
1158
1180
  }
1159
1181
 
1182
+ function getLastDependency(storage) {
1183
+ return storage.dependencies.length ? storage.dependencies[storage.dependencies.length - 1] : null
1184
+ }
1185
+
1160
1186
  // TODO: remove in like 3-6 mo
1161
1187
  function tmpFixStorage(p) {
1162
1188
  // if CORESTORE file is written, new format
package/lib/keys.js CHANGED
@@ -21,6 +21,7 @@ const DATA_TREE = 4
21
21
  const DATA_BITFIELD = 5
22
22
  const DATA_USER_DATA = 6
23
23
  const DATA_LOCAL = 7
24
+ const DATA_MARK = 8
24
25
 
25
26
  const slab = { buffer: b4a.allocUnsafe(65536), start: 0, end: 0 }
26
27
 
@@ -224,6 +225,16 @@ core.userDataEnd = function (ptr) {
224
225
  return state.buffer.subarray(start, state.start)
225
226
  }
226
227
 
228
+ core.mark = function (ptr, index) {
229
+ const state = alloc()
230
+ const start = state.start
231
+ UINT.encode(state, TL_DATA)
232
+ UINT.encode(state, ptr)
233
+ UINT.encode(state, DATA_MARK)
234
+ UINT.encode(state, index)
235
+ return state.buffer.subarray(start, state.start)
236
+ }
237
+
227
238
  core.local = function (ptr, key) {
228
239
  if (key.byteLength > 2048) {
229
240
  throw new Error('local keys has an upper limit of 2048 bytes atm')
@@ -273,6 +284,14 @@ core.userDataKey = function (buffer) {
273
284
  return STRING.decode(state)
274
285
  }
275
286
 
287
+ core.markKey = function (buffer) {
288
+ const state = { buffer, start: 0, end: buffer.byteLength }
289
+ UINT.decode(state) // ns
290
+ UINT.decode(state) // ptr
291
+ UINT.decode(state) // type
292
+ return UINT.decode(state)
293
+ }
294
+
276
295
  core.localKey = function (buffer) {
277
296
  const state = { buffer, start: 0, end: buffer.byteLength }
278
297
  UINT.decode(state) // ns
package/lib/streams.js CHANGED
@@ -15,6 +15,7 @@ module.exports = {
15
15
  createAliasStream,
16
16
  createDiscoveryKeyStream,
17
17
  createTreeNodeStream,
18
+ createMarkStream,
18
19
  createLocalStream
19
20
  }
20
21
 
@@ -117,6 +118,20 @@ function createUserDataStream(
117
118
  return ite
118
119
  }
119
120
 
121
+ function createMarkStream(
122
+ ptr,
123
+ db,
124
+ view,
125
+ { gt = -1, gte = gt + 1, lte = -1, lt = lte === -1 ? -1 : lte + 1, reverse = false } = {}
126
+ ) {
127
+ const s = core.mark(ptr.dataPointer, gte)
128
+ const e = core.mark(ptr.dataPointer, lt === -1 ? Infinity : lt)
129
+ const ite = view.iterator(db, s, e, reverse)
130
+
131
+ ite._readableState.map = mapMark
132
+ return ite
133
+ }
134
+
120
135
  function createLocalStream(
121
136
  ptr,
122
137
  db,
@@ -139,6 +154,11 @@ function mapBitfield(data) {
139
154
  return { index, page: data.value }
140
155
  }
141
156
 
157
+ function mapMark(data) {
158
+ const index = core.markKey(data.key)
159
+ return { index, page: data.value }
160
+ }
161
+
142
162
  function mapLocal(data) {
143
163
  const key = core.localKey(data.key)
144
164
  return { key, value: data.value }
package/lib/tx.js CHANGED
@@ -108,6 +108,22 @@ class CoreTX {
108
108
  this.changes.push([core.userData(this.core.dataPointer, key), null, null])
109
109
  }
110
110
 
111
+ putMark(index, data) {
112
+ this.changes.push([core.mark(this.core.dataPointer, index), data, null])
113
+ }
114
+
115
+ deleteMark(index) {
116
+ this.changes.push([core.mark(this.core.dataPointer, index), null, null])
117
+ }
118
+
119
+ deleteMarkRange(start, end) {
120
+ this.changes.push([
121
+ core.mark(this.core.dataPointer, start),
122
+ null,
123
+ core.mark(this.core.dataPointer, end === -1 ? Infinity : end)
124
+ ])
125
+ }
126
+
111
127
  putLocal(key, value) {
112
128
  this.changes.push([core.local(this.core.dataPointer, key), value, null])
113
129
  }
@@ -213,6 +229,10 @@ class CoreRX {
213
229
  return this.view.get(this.read, core.userData(this.core.dataPointer, key))
214
230
  }
215
231
 
232
+ getMark(index) {
233
+ return this.view.get(this.read, core.mark(this.core.dataPointer, index))
234
+ }
235
+
216
236
  getLocal(key) {
217
237
  return this.view.get(this.read, core.local(this.core.dataPointer, key))
218
238
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "2.7.0",
3
+ "version": "2.8.0",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",