hypercore-storage 0.0.30 → 0.0.32
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 +45 -2
- package/lib/memory-overlay.js +4 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -489,7 +489,34 @@ class HypercoreStorage {
|
|
|
489
489
|
|
|
490
490
|
snapshot () {
|
|
491
491
|
assert(this.destroyed === false)
|
|
492
|
-
|
|
492
|
+
const s = new HypercoreStorage(this.root, this.discoveryKey, this.corePointer, this.dataPointer, this.db.snapshot())
|
|
493
|
+
|
|
494
|
+
for (const dep of this.dependencies) s.dependencies.push(dep)
|
|
495
|
+
|
|
496
|
+
return s
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
findDependency (length) {
|
|
500
|
+
for (let i = this.dependencies.length - 1; i >= 0; i--) {
|
|
501
|
+
const dep = this.dependencies[i]
|
|
502
|
+
if (dep.length < length) return dep
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
return null
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
updateDependencies (length) {
|
|
509
|
+
const deps = this.dependencies
|
|
510
|
+
|
|
511
|
+
for (let i = deps.length - 1; i >= 0; i--) {
|
|
512
|
+
if (deps[i].length < length) {
|
|
513
|
+
deps[i].length = length
|
|
514
|
+
this.dependencies = deps.slice(0, i + 1)
|
|
515
|
+
return
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
throw new Error('Dependency not found')
|
|
493
520
|
}
|
|
494
521
|
|
|
495
522
|
createReadBatch (opts) {
|
|
@@ -513,7 +540,7 @@ class HypercoreStorage {
|
|
|
513
540
|
createUserDataStream (opts = {}) {
|
|
514
541
|
assert(this.destroyed === false)
|
|
515
542
|
|
|
516
|
-
const r =
|
|
543
|
+
const r = encodeUserDataRange(this.dataPointer, DATA.USER_DATA, this.dbSnapshot, opts)
|
|
517
544
|
const s = this.db.iterator(r)
|
|
518
545
|
s._readableState.map = mapStreamUserData
|
|
519
546
|
return s
|
|
@@ -584,6 +611,8 @@ function mapStreamUserData (data) {
|
|
|
584
611
|
|
|
585
612
|
const key = c.string.decode(state)
|
|
586
613
|
|
|
614
|
+
if (data.value.byteLength === 0) return null
|
|
615
|
+
|
|
587
616
|
return { key, value: data.value }
|
|
588
617
|
}
|
|
589
618
|
|
|
@@ -655,6 +684,20 @@ function encodeIndexRange (pointer, type, snapshot, opts) {
|
|
|
655
684
|
return bounded
|
|
656
685
|
}
|
|
657
686
|
|
|
687
|
+
function encodeUserDataRange (pointer, type, snapshot, opts) {
|
|
688
|
+
const bounded = { snapshot, gt: null, gte: null, lte: null, lt: null, reverse: !!opts.reverse, limit: toLimit(opts.limit) }
|
|
689
|
+
|
|
690
|
+
if (opts.gt || opts.gt === 0) bounded.gt = encodeUserDataIndex(pointer, type, opts.gt)
|
|
691
|
+
else if (opts.gte) bounded.gte = encodeUserDataIndex(pointer, type, opts.gte)
|
|
692
|
+
else bounded.gte = encodeDataIndex(pointer, type, 0)
|
|
693
|
+
|
|
694
|
+
if (opts.lt || opts.lt === 0) bounded.lt = encodeUserDataIndex(pointer, type, opts.lt)
|
|
695
|
+
else if (opts.lte) bounded.lte = encodeUserDataIndex(pointer, type, opts.lte)
|
|
696
|
+
else bounded.lte = encodeDataIndex(pointer, type, Infinity)
|
|
697
|
+
|
|
698
|
+
return bounded
|
|
699
|
+
}
|
|
700
|
+
|
|
658
701
|
function toLimit (n) {
|
|
659
702
|
return n === 0 ? 0 : (n || Infinity)
|
|
660
703
|
}
|
package/lib/memory-overlay.js
CHANGED