hypercore 10.32.4 → 10.32.5

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
@@ -813,8 +813,8 @@ module.exports = class Hypercore extends EventEmitter {
813
813
  return true
814
814
  }
815
815
 
816
- batch ({ checkout = -1, autoClose = true, session = true, restore = false } = {}) {
817
- return new Batch(session ? this.session() : this, checkout, autoClose, restore)
816
+ batch ({ checkout = -1, autoClose = true, session = true, restore = false, clear = false } = {}) {
817
+ return new Batch(session ? this.session() : this, checkout, autoClose, restore, clear)
818
818
  }
819
819
 
820
820
  async seek (bytes, opts) {
package/lib/batch.js CHANGED
@@ -4,7 +4,7 @@ const c = require('compact-encoding')
4
4
  const b4a = require('b4a')
5
5
 
6
6
  module.exports = class HypercoreBatch extends EventEmitter {
7
- constructor (session, checkoutLength, autoClose, restore) {
7
+ constructor (session, checkoutLength, autoClose, restore, clear) {
8
8
  super()
9
9
 
10
10
  this.session = session
@@ -25,8 +25,10 @@ module.exports = class HypercoreBatch extends EventEmitter {
25
25
  this._sessionByteLength = 0
26
26
  this._sessionBatch = null
27
27
  this._flushing = null
28
+ this._clear = clear
28
29
 
29
- this.opening = this.ready().catch(noop)
30
+ this.opening = this._open()
31
+ this.opening.catch(noop)
30
32
  }
31
33
 
32
34
  get id () {
@@ -69,23 +71,27 @@ module.exports = class HypercoreBatch extends EventEmitter {
69
71
  return this.session.manifest
70
72
  }
71
73
 
72
- async ready () {
74
+ ready () {
75
+ return this.opening
76
+ }
77
+
78
+ async _open () {
73
79
  await this.session.ready()
74
- if (this.opened) return
80
+
81
+ if (this._clear) this._checkoutLength = this.core.tree.length
75
82
 
76
83
  if (this._checkoutLength !== -1) {
77
84
  const batch = await this.session.core.tree.restoreBatch(this._checkoutLength)
78
85
  batch.treeLength = this._checkoutLength
79
- if (this.opened) return
80
86
  this._sessionLength = batch.length
81
87
  this._sessionByteLength = batch.byteLength
82
88
  this._sessionBatch = batch
89
+ if (this._clear) await this.core.clearBatch()
83
90
  } else {
84
91
  const last = this.restore ? this.session.core.bitfield.findFirst(false, this.session.length) : 0
85
92
 
86
93
  if (last > this.session.length) {
87
94
  const batch = await this.session.core.tree.restoreBatch(last)
88
- if (this.opened) return
89
95
  this._sessionLength = batch.length
90
96
  this._sessionByteLength = batch.byteLength - this.session.padding * batch.length
91
97
  this._sessionBatch = batch
package/lib/core.js CHANGED
@@ -440,6 +440,40 @@ module.exports = class Core {
440
440
  }
441
441
  }
442
442
 
443
+ async clearBatch () {
444
+ await this._mutex.lock()
445
+
446
+ try {
447
+ const len = this.bitfield.findFirst(false, this.tree.length)
448
+ if (len <= this.tree.length) return
449
+
450
+ const batch = await this.tree.truncate(this.tree.length, this.tree.fork)
451
+
452
+ batch.signature = this.tree.signature // same sig
453
+
454
+ const entry = {
455
+ userData: null,
456
+ treeNodes: batch.nodes,
457
+ treeUpgrade: batch,
458
+ bitfield: {
459
+ drop: true,
460
+ start: batch.ancestors,
461
+ length: len - batch.ancestors
462
+ }
463
+ }
464
+
465
+ await this.oplog.append([entry], false)
466
+
467
+ this.bitfield.setRange(batch.ancestors, len - batch.ancestors, false)
468
+ batch.commit()
469
+
470
+ // TODO: (see below todo)
471
+ await this._flushOplog()
472
+ } finally {
473
+ this._mutex.unlock()
474
+ }
475
+ }
476
+
443
477
  async clear (start, end, cleared) {
444
478
  await this._mutex.lock()
445
479
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.32.4",
3
+ "version": "10.32.5",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {