hypercore-storage 2.6.1 → 2.7.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.
Files changed (2) hide show
  1. package/index.js +48 -4
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -32,9 +32,14 @@ class Atom {
32
32
  this.view = new View()
33
33
  this.flushedPromise = null
34
34
  this.flushing = false
35
+ this.preflushes = []
35
36
  this.flushes = []
36
37
  }
37
38
 
39
+ preflush(fn) {
40
+ this.preflushes.push(fn)
41
+ }
42
+
38
43
  onflush(fn) {
39
44
  this.flushes.push(fn)
40
45
  }
@@ -57,8 +62,10 @@ class Atom {
57
62
  this.flushing = true
58
63
 
59
64
  try {
65
+ const plen = this.preflushes.length
66
+ for (let i = 0; i < plen; i++) this.preflushes[i]()
67
+
60
68
  await View.flush(this.view.changes, this.db)
61
- this.view.reset()
62
69
 
63
70
  const promises = []
64
71
  const len = this.flushes.length // in case of reentry
@@ -66,6 +73,7 @@ class Atom {
66
73
 
67
74
  await Promise.all(promises)
68
75
  } finally {
76
+ this.view.reset()
69
77
  this.flushing = false
70
78
  if (this.flushedPromise !== null) this._resolve()
71
79
  }
@@ -242,7 +250,7 @@ class HypercoreStorage {
242
250
  const existingSessions = await existingSessionsPromise
243
251
 
244
252
  const sessions = existingSessions || []
245
- const session = getBatch(sessions, name, false)
253
+ const session = getSession(sessions, name, false)
246
254
 
247
255
  if (session === null) return null
248
256
 
@@ -288,7 +296,7 @@ class HypercoreStorage {
288
296
  }
289
297
 
290
298
  const sessions = existingSessions || []
291
- const session = getBatch(sessions, name, true)
299
+ const session = getSession(sessions, name, true)
292
300
  const fresh = session.dataPointer === -1
293
301
 
294
302
  if (fresh) {
@@ -374,6 +382,29 @@ class HypercoreStorage {
374
382
  return deps
375
383
  }
376
384
 
385
+ async deleteSessions() {
386
+ const rx = this.read()
387
+ const existingSessionsPromise = rx.getSessions()
388
+ rx.tryFlush()
389
+
390
+ const existingSessions = await existingSessionsPromise
391
+
392
+ // Remove batches
393
+ const coreTx = this.write()
394
+ coreTx.setSessions([]) // Clear sessions record
395
+ await coreTx.flush()
396
+
397
+ const tx = this.db.write({ autoDestroy: true })
398
+
399
+ for (const { dataPointer } of existingSessions) {
400
+ const start = core.data(dataPointer)
401
+ const end = core.data(dataPointer + 1)
402
+ tx.tryDeleteRange(start, end)
403
+ }
404
+
405
+ await tx.flush()
406
+ }
407
+
377
408
  read() {
378
409
  return new CoreRX(this.core, this.db, this.view)
379
410
  }
@@ -427,6 +458,11 @@ class HypercoreStorage {
427
458
 
428
459
  return core
429
460
  }
461
+
462
+ static isParentStorage(storage, parent) {
463
+ const last = getLastDependency(storage)
464
+ return !!last && last.dataPointer === parent.core.dataPointer
465
+ }
430
466
  }
431
467
 
432
468
  class CorestoreStorage {
@@ -1080,6 +1116,10 @@ class CorestoreStorage {
1080
1116
  await this._exit()
1081
1117
  }
1082
1118
  }
1119
+
1120
+ static isParentStorage(storage, parent) {
1121
+ return HypercoreStorage.isParentStorage(storage, parent)
1122
+ }
1083
1123
  }
1084
1124
 
1085
1125
  module.exports = CorestoreStorage
@@ -1096,7 +1136,7 @@ function initStoreHead() {
1096
1136
  }
1097
1137
  }
1098
1138
 
1099
- function getBatch(sessions, name, alloc) {
1139
+ function getSession(sessions, name, alloc) {
1100
1140
  for (let i = 0; i < sessions.length; i++) {
1101
1141
  if (sessions[i].name === name) return sessions[i]
1102
1142
  }
@@ -1134,6 +1174,10 @@ function createColumnFamily(db, opts = {}) {
1134
1174
  return db.columnFamily(col)
1135
1175
  }
1136
1176
 
1177
+ function getLastDependency(storage) {
1178
+ return storage.dependencies.length ? storage.dependencies[storage.dependencies.length - 1] : null
1179
+ }
1180
+
1137
1181
  // TODO: remove in like 3-6 mo
1138
1182
  function tmpFixStorage(p) {
1139
1183
  // if CORESTORE file is written, new format
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "2.6.1",
3
+ "version": "2.7.1",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",