hypercore-storage 2.7.1 → 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
 
@@ -238,6 +239,10 @@ class HypercoreStorage {
238
239
  return createUserDataStream(this.core, this.db, this.view, opts)
239
240
  }
240
241
 
242
+ createMarkStream(opts) {
243
+ return createMarkStream(this.core, this.db, this.view, opts)
244
+ }
245
+
241
246
  createLocalStream(opts) {
242
247
  return createLocalStream(this.core, this.db, this.view, opts)
243
248
  }
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.1",
3
+ "version": "2.8.0",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",