hypercore-storage 0.0.37 → 0.0.38

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
@@ -162,7 +162,7 @@ class WriteBatch {
162
162
 
163
163
  flush () {
164
164
  if (this.atom) return this.atom.flush()
165
- return this.write.flush()
165
+ return flushAndDestroy(this.write)
166
166
  }
167
167
  }
168
168
 
@@ -259,11 +259,11 @@ class ReadBatch {
259
259
  }
260
260
 
261
261
  flush () {
262
- return this.read.flush()
262
+ return flushAndDestroy(this.read)
263
263
  }
264
264
 
265
265
  tryFlush () {
266
- this.read.tryFlush()
266
+ tryFlushAndDestroy(this.read)
267
267
  }
268
268
  }
269
269
 
@@ -378,10 +378,12 @@ class Atom {
378
378
  try {
379
379
  await batch.flush()
380
380
  } catch (err) {
381
+ batch.destroy()
381
382
  reject(err)
382
383
  return
383
384
  }
384
385
 
386
+ batch.destroy()
385
387
  resolve()
386
388
  }
387
389
 
@@ -411,7 +413,7 @@ class Atom {
411
413
 
412
414
  module.exports = class CoreStorage {
413
415
  constructor (dir) {
414
- this.db = new RocksDB(dir)
416
+ this.db = typeof dir === 'object' ? dir : new RocksDB(dir)
415
417
  this.mutex = new RW()
416
418
  }
417
419
 
@@ -433,7 +435,7 @@ module.exports = class CoreStorage {
433
435
  try {
434
436
  const b = this.db.write()
435
437
  b.tryPut(b4a.from([TL.LOCAL_SEED]), seed)
436
- await b.flush()
438
+ await flushAndDestroy(b)
437
439
 
438
440
  return true
439
441
  } finally {
@@ -488,7 +490,7 @@ module.exports = class CoreStorage {
488
490
  async clear () {
489
491
  const b = this.db.write()
490
492
  b.tryDeleteRange(b4a.from([TL.STORAGE_INFO]), INF)
491
- await b.flush()
493
+ await flushAndDestroy(b)
492
494
  }
493
495
 
494
496
  async has (discoveryKey) {
@@ -556,6 +558,7 @@ class HypercoreStorage {
556
558
  this.root = root
557
559
  this.db = root.db
558
560
  this.dbSnapshot = snapshot
561
+ this.dbRead = snapshot || this.db
559
562
  this.mutex = root.mutex
560
563
 
561
564
  this.discoveryKey = discoveryKey
@@ -617,7 +620,8 @@ class HypercoreStorage {
617
620
  batch.setBatchPointer(name, storage.dataPointer)
618
621
  if (head.rootHash) batch.setCoreHead(head) // if no root hash its the empty core - no head yet
619
622
 
620
- await write.flush()
623
+ if (atom) await atom.flush()
624
+ else await flushAndDestroy(write)
621
625
 
622
626
  storage.dependencies = await addDependencies(this.db, storage.dataPointer, head.length)
623
627
  return storage
@@ -644,7 +648,7 @@ class HypercoreStorage {
644
648
 
645
649
  snapshot () {
646
650
  assert(this.destroyed === false)
647
- const s = new HypercoreStorage(this.root, this.discoveryKey, this.corePointer, this.dataPointer, this.db.snapshot())
651
+ const s = new HypercoreStorage(this.root, this.discoveryKey, this.corePointer, this.dataPointer, this.dbRead.snapshot())
648
652
 
649
653
  for (const { data, length } of this.dependencies) s.dependencies.push({ data, length })
650
654
 
@@ -677,8 +681,7 @@ class HypercoreStorage {
677
681
  createReadBatch (opts) {
678
682
  assert(this.destroyed === false)
679
683
 
680
- const snapshot = this.dbSnapshot
681
- return new ReadBatch(this, this.db.read({ snapshot }))
684
+ return new ReadBatch(this, this.dbRead.read())
682
685
  }
683
686
 
684
687
  createWriteBatch (atom) {
@@ -689,6 +692,24 @@ class HypercoreStorage {
689
692
  return new WriteBatch(this, this.db.write(), null)
690
693
  }
691
694
 
695
+ // helper for atomic flows
696
+ async createWriteBatchAndLock (atomizer, lock) {
697
+ assert(this.destroyed === false)
698
+ if (atomizer) atomizer.enter()
699
+
700
+ try {
701
+ await lock.lock()
702
+ } catch (err) {
703
+ if (atomizer) atomizer.exit()
704
+ throw err
705
+ }
706
+
707
+ const batch = this.createWriteBatch(atomizer)
708
+ if (atomizer) atomizer.exit()
709
+
710
+ return batch
711
+ }
712
+
692
713
  createBlockStream (opts = {}) {
693
714
  assert(this.destroyed === false)
694
715
  return createStream(this, createBlockStream, opts)
@@ -697,8 +718,8 @@ class HypercoreStorage {
697
718
  createUserDataStream (opts = {}) {
698
719
  assert(this.destroyed === false)
699
720
 
700
- const r = encodeUserDataRange(this.dataPointer, DATA.USER_DATA, this.dbSnapshot, opts)
701
- const s = this.db.iterator(r)
721
+ const r = encodeUserDataRange(this.dataPointer, DATA.USER_DATA, opts)
722
+ const s = this.dbRead.iterator(r)
702
723
  s._readableState.map = mapStreamUserData
703
724
  return s
704
725
  }
@@ -706,8 +727,8 @@ class HypercoreStorage {
706
727
  createTreeNodeStream (opts = {}) {
707
728
  assert(this.destroyed === false)
708
729
 
709
- const r = encodeIndexRange(this.dataPointer, DATA.TREE, this.dbSnapshot, opts)
710
- const s = this.db.iterator(r)
730
+ const r = encodeIndexRange(this.dataPointer, DATA.TREE, opts)
731
+ const s = this.dbRead.iterator(r)
711
732
  s._readableState.map = mapStreamTreeNode
712
733
  return s
713
734
  }
@@ -715,8 +736,8 @@ class HypercoreStorage {
715
736
  createBitfieldPageStream (opts = {}) {
716
737
  assert(this.destroyed === false)
717
738
 
718
- const r = encodeIndexRange(this.dataPointer, DATA.BITFIELD, this.dbSnapshot, opts)
719
- const s = this.db.iterator(r)
739
+ const r = encodeIndexRange(this.dataPointer, DATA.BITFIELD, opts)
740
+ const s = this.dbRead.iterator(r)
720
741
  s._readableState.map = mapStreamBitfieldPage
721
742
  return s
722
743
  }
@@ -724,7 +745,7 @@ class HypercoreStorage {
724
745
  async peekLastTreeNode () {
725
746
  assert(this.destroyed === false)
726
747
 
727
- const last = await this.db.peek(encodeIndexRange(this.dataPointer, DATA.TREE, this.dbSnapshot, { reverse: true }))
748
+ const last = await this.dbRead.peek(encodeIndexRange(this.dataPointer, DATA.TREE, { reverse: true }))
728
749
  if (last === null) return null
729
750
  return c.decode(m.TreeNode, last.value)
730
751
  }
@@ -732,7 +753,7 @@ class HypercoreStorage {
732
753
  async peekLastBitfieldPage () {
733
754
  assert(this.destroyed === false)
734
755
 
735
- const last = await this.db.peek(encodeIndexRange(this.dataPointer, DATA.BITFIELD, this.dbSnapshot, { reverse: true }))
756
+ const last = await this.dbRead.peek(encodeIndexRange(this.dataPointer, DATA.BITFIELD, { reverse: true }))
736
757
  if (last === null) return null
737
758
  return mapStreamBitfieldPage(last)
738
759
  }
@@ -741,19 +762,19 @@ class HypercoreStorage {
741
762
  if (this.destroyed) return
742
763
  this.destroyed = true
743
764
 
744
- if (this.dbSnapshot) this.dbSnapshot.destroy()
765
+ if (this.dbSnapshot) this.dbSnapshot.close().catch(noop)
745
766
  this.dbSnapshot = null
746
767
  }
747
768
  }
748
769
 
749
770
  function createStream (storage, createStreamType, opts) {
750
771
  return storage.dependencies.length === 0
751
- ? createStreamType(storage.db, storage.dbSnapshot, storage.dataPointer, opts)
772
+ ? createStreamType(storage.db, storage.dataPointer, opts)
752
773
  : new DependencyStream(storage, createStreamType, opts)
753
774
  }
754
775
 
755
- function createBlockStream (db, snap, data, opts) {
756
- const r = encodeIndexRange(data, DATA.BLOCK, snap, opts)
776
+ function createBlockStream (db, data, opts) {
777
+ const r = encodeIndexRange(data, DATA.BLOCK, opts)
757
778
  const s = db.iterator(r)
758
779
  s._readableState.map = mapStreamBlock
759
780
  return s
@@ -827,8 +848,8 @@ function ensureSlab (size) {
827
848
  return SLAB
828
849
  }
829
850
 
830
- function encodeIndexRange (pointer, type, snapshot, opts) {
831
- const bounded = { snapshot, gt: null, gte: null, lte: null, lt: null, reverse: !!opts.reverse, limit: toLimit(opts.limit) }
851
+ function encodeIndexRange (pointer, type, opts) {
852
+ const bounded = { gt: null, gte: null, lte: null, lt: null, reverse: !!opts.reverse, limit: toLimit(opts.limit) }
832
853
 
833
854
  if (opts.gt || opts.gt === 0) bounded.gt = encodeDataIndex(pointer, type, opts.gt)
834
855
  else if (opts.gte) bounded.gte = encodeDataIndex(pointer, type, opts.gte)
@@ -841,8 +862,8 @@ function encodeIndexRange (pointer, type, snapshot, opts) {
841
862
  return bounded
842
863
  }
843
864
 
844
- function encodeUserDataRange (pointer, type, snapshot, opts) {
845
- const bounded = { snapshot, gt: null, gte: null, lte: null, lt: null, reverse: !!opts.reverse, limit: toLimit(opts.limit) }
865
+ function encodeUserDataRange (pointer, type, opts) {
866
+ const bounded = { gt: null, gte: null, lte: null, lt: null, reverse: !!opts.reverse, limit: toLimit(opts.limit) }
846
867
 
847
868
  if (opts.gt || opts.gt === 0) bounded.gt = encodeUserDataIndex(pointer, type, opts.gt)
848
869
  else if (opts.gte) bounded.gte = encodeUserDataIndex(pointer, type, opts.gte)
@@ -965,3 +986,24 @@ function initialiseCoreData (db, { userData } = {}) {
965
986
  }
966
987
  }
967
988
  }
989
+
990
+ function noop () {}
991
+
992
+ async function tryFlushAndDestroy (batch) {
993
+ try {
994
+ await batch.flush()
995
+ } catch {}
996
+
997
+ batch.destroy()
998
+ }
999
+
1000
+ async function flushAndDestroy (batch) {
1001
+ try {
1002
+ await batch.flush()
1003
+ } catch (err) {
1004
+ batch.destroy()
1005
+ throw err
1006
+ }
1007
+
1008
+ batch.destroy()
1009
+ }
@@ -93,7 +93,7 @@ module.exports = class DependencyStream extends Readable {
93
93
  _nextStream () {
94
94
  const { data, gte, lt } = this._streams[this._next++]
95
95
 
96
- const stream = this.createStream(this.storage.db, this.storage.dbSnapshot, data, {
96
+ const stream = this.createStream(this.storage.dbRead, data, {
97
97
  reverse: this._reverse,
98
98
  limit: this._limit,
99
99
  gte,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "0.0.37",
3
+ "version": "0.0.38",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",
@@ -22,7 +22,7 @@
22
22
  "queue-tick": "^1.0.1",
23
23
  "read-write-mutexify": "^2.1.0",
24
24
  "resolve-reject-promise": "^1.0.0",
25
- "rocksdb-native": "^2.2.0",
25
+ "rocksdb-native": "^3.0.0",
26
26
  "streamx": "^2.20.1"
27
27
  },
28
28
  "devDependencies": {