hypercore-storage 1.0.5 → 1.0.7

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
@@ -104,7 +104,7 @@ class HypercoreStorage {
104
104
  }
105
105
 
106
106
  snapshot () {
107
- return new HypercoreStorage(this.store, this.db.snapshot(), this.core, this.view.snapshot(), this.atom)
107
+ return new HypercoreStorage(this.store, this.db.snapshot(), cloneCore(this.core), this.view.snapshot(), this.atom)
108
108
  }
109
109
 
110
110
  atomize (atom) {
@@ -212,13 +212,12 @@ class HypercoreStorage {
212
212
  version: this.core.version,
213
213
  corePointer: this.core.corePointer,
214
214
  dataPointer: this.core.dataPointer,
215
- dependencies: this._addDependency({ dataPointer: this.core.dataPointer, length })
215
+ dependencies: this._addDependency(null)
216
216
  }
217
217
 
218
218
  const coreTx = new CoreTX(core, this.db, atom.view, [])
219
219
 
220
220
  if (length > 0) coreTx.setHead(head)
221
- coreTx.setDependency(core.dependencies[core.dependencies.length - 1])
222
221
 
223
222
  await coreTx.flush()
224
223
 
@@ -231,7 +230,7 @@ class HypercoreStorage {
231
230
  for (let i = 0; i < this.core.dependencies.length; i++) {
232
231
  const d = this.core.dependencies[i]
233
232
 
234
- if (d.length > dep.length) {
233
+ if (dep !== null && d.length > dep.length) {
235
234
  deps.push({ dataPointer: d.dataPointer, length: dep.length })
236
235
  return deps
237
236
  }
@@ -239,7 +238,7 @@ class HypercoreStorage {
239
238
  deps.push(d)
240
239
  }
241
240
 
242
- deps.push(dep)
241
+ if (dep !== null) deps.push(dep)
243
242
  return deps
244
243
  }
245
244
 
@@ -705,3 +704,17 @@ function createColumnFamily (db) {
705
704
 
706
705
  return db.columnFamily(col)
707
706
  }
707
+
708
+ function cloneCore (c) {
709
+ const copy = {
710
+ dataPointer: c.dataPointer,
711
+ corePointer: c.corePointer,
712
+ dependencies: []
713
+ }
714
+
715
+ for (const { dataPointer, length } of c.dependencies) {
716
+ copy.dependencies.push({ dataPointer, length })
717
+ }
718
+
719
+ return copy
720
+ }
@@ -8,6 +8,7 @@ module.exports = class BlockStream extends Readable {
8
8
  this.core = core
9
9
  this.db = db
10
10
  this.updates = updates
11
+ this.start = start
11
12
  this.end = end
12
13
  this.reverse = reverse === true
13
14
 
@@ -23,26 +24,18 @@ module.exports = class BlockStream extends Readable {
23
24
  _update () {
24
25
  if (this._consumed > this.core.dependencies.length) return
25
26
 
26
- const index = this.reverse ? this.core.dependencies.length - this._consumed : this._consumed
27
- const offset = index === 0 ? 0 : this.core.dependencies[index - 1].length
27
+ const deps = this.core.dependencies
28
28
 
29
- let end = 0
30
- let ptr = 0
29
+ const index = findDependencyIndex(deps, this.end, this._consumed++, this.reverse)
31
30
 
32
- if (this._consumed < this.core.dependencies.length) {
33
- const dep = this.core.dependencies[this._consumed]
34
- end = this.end === -1 ? dep.length : Math.min(this.end, dep.length)
35
- ptr = dep.dataPointer
36
- } else {
37
- end = this.end === -1 ? Infinity : this.end
38
- ptr = this.core.dataPointer
39
- }
40
-
41
- this._consumed++
31
+ const curr = deps[index]
32
+ const prev = deps[index - 1]
42
33
 
43
- if (end === offset) return
34
+ const start = prev && prev.length > this.start ? prev.length : this.start
35
+ const end = curr && curr.length < this.end ? curr.length : this.end
36
+ const ptr = curr ? curr.dataPointer : this.core.dataPointer
44
37
 
45
- this._makeStream(core.block(ptr, offset), core.block(ptr, end))
38
+ this._makeStream(core.block(ptr, start), core.block(ptr, end))
46
39
  }
47
40
 
48
41
  _predestroy () {
@@ -105,3 +98,13 @@ module.exports = class BlockStream extends Readable {
105
98
  }
106
99
 
107
100
  function noop () {}
101
+
102
+ function findDependencyIndex (deps, end, offset, reverse) {
103
+ if (!reverse) return offset
104
+
105
+ for (let i = deps.length - offset; i > 0; i--) {
106
+ if (deps[i - 1].length <= end) return i
107
+ }
108
+
109
+ return 0
110
+ }
package/lib/tx.js CHANGED
@@ -16,6 +16,7 @@ const CORE_HINTS = schema.getEncoding('@core/hints')
16
16
 
17
17
  class CoreTX {
18
18
  constructor (core, db, view, changes) {
19
+ if (db.snapshotted) throw new Error('Cannot open core tx on snapshot')
19
20
  this.core = core
20
21
  this.db = db
21
22
  this.view = view
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",