hypercore-storage 0.0.21 → 0.0.22

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 +17 -13
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -426,23 +426,26 @@ class HypercoreStorage {
426
426
  return this.dbSnapshot !== null
427
427
  }
428
428
 
429
- async registerBatch (name, length, overwrite) {
430
- // todo: make sure opened
429
+ async openBatch (name) {
431
430
  const existing = await this.db.get(encodeBatch(this.corePointer, CORE.BATCHES, name))
432
- const storage = new HypercoreStorage(this.root, this.discoveryKey, this.corePointer, this.dataPointer, null)
431
+ if (!existing) return null
433
432
 
434
- if (existing && !overwrite) {
435
- const dataPointer = c.decode(m.DataPointer, existing)
436
- storage.dataPointer = dataPointer
437
- storage.dependencies = await addDependencies(this.db, storage.dataPointer, length)
438
- return storage
439
- }
433
+ const storage = new HypercoreStorage(this.root, this.discoveryKey, this.corePointer, this.dataPointer, this.dbSnapshot)
434
+ const dataPointer = c.decode(m.DataPointer, existing)
440
435
 
436
+ storage.dataPointer = dataPointer
437
+ storage.dependencies = await addDependencies(this.db, storage.dataPointer, -1)
438
+
439
+ return storage
440
+ }
441
+
442
+ async registerBatch (name, head) {
441
443
  await this.mutex.write.lock()
442
444
 
445
+ const storage = new HypercoreStorage(this.root, this.discoveryKey, this.corePointer, this.dataPointer, null)
446
+
443
447
  try {
444
448
  const info = await getStorageInfo(this.db)
445
-
446
449
  const write = this.db.write()
447
450
 
448
451
  storage.dataPointer = info.free++
@@ -453,12 +456,13 @@ class HypercoreStorage {
453
456
 
454
457
  initialiseCoreData(batch)
455
458
 
456
- batch.setDataDependency({ data: this.dataPointer, length })
459
+ batch.setDataDependency({ data: this.dataPointer, length: head.length })
457
460
  batch.setBatchPointer(name, storage.dataPointer)
461
+ if (head.rootHash) batch.setCoreHead(head) // if no root hash its the empty core - no head yet
458
462
 
459
463
  await write.flush()
460
464
 
461
- storage.dependencies = await addDependencies(this.db, storage.dataPointer, length)
465
+ storage.dependencies = await addDependencies(this.db, storage.dataPointer, head.length)
462
466
  return storage
463
467
  } finally {
464
468
  this.mutex.write.unlock()
@@ -709,7 +713,7 @@ async function addDependencies (db, dataPointer, treeLength) {
709
713
  let dep = await db.get(encodeDataIndex(dataPointer, DATA.DEPENDENCY))
710
714
  while (dep) {
711
715
  const { data, length } = c.decode(m.DataDependency, dep)
712
- if (length <= treeLength) dependencies.push({ data, length })
716
+ if (treeLength === -1 || length <= treeLength) dependencies.push({ data, length })
713
717
 
714
718
  dep = await db.get(encodeDataIndex(data, DATA.DEPENDENCY))
715
719
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "0.0.21",
3
+ "version": "0.0.22",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",