diff --git a/index.js b/index.js index 0267b93a..80134da3 100644 --- a/index.js +++ b/index.js @@ -300,7 +300,7 @@ module.exports = class Autobase extends ReadyResource { : { key: null, indexedLength: 0, indexersUpdated: false, fastForwarding: false, heads: null } } - interrupt (reason) { + _interrupt (reason) { assert(this._applyState.applying, 'Interrupt is only allowed in apply') this._interrupting = true if (reason) this.interrupted = reason @@ -1371,13 +1371,10 @@ module.exports = class Autobase extends ReadyResource { } // triggered from apply - async addWriter (key, { indexer = true, isIndexer = indexer } = {}) { // just compat for old version + async _addWriter (key, sys) { // just compat for old version assert(this._applyState.applying, 'System changes are only allowed in apply') - const sys = this._applyState.system - await sys.add(key, { isIndexer }) - - const writer = (await this._getWriterByKey(key, -1, 0, false, true, null)) || this._makeWriter(key, 0, true, false) + const writer = (await this._getWriterByKey(key, -1, 0, false, true, sys)) || this._makeWriter(key, 0, true, false) await writer.ready() if (!this.activeWriters.has(key)) { @@ -1390,27 +1387,18 @@ module.exports = class Autobase extends ReadyResource { this._queueBump() } - removeable (key, sys = this.system) { - if (sys.indexers.length !== 1) return true - return !b4a.equals(sys.indexers[0].key, key) - } - // triggered from apply - async removeWriter (key) { // just compat for old version - assert(this._applyState.applying, 'System changes are only allowed in apply') - - if (!this.removeable(key, this._applyState.system)) { - throw new Error('Not allowed to remove the last indexer') - } - - await this._applyState.system.remove(key) - + _removeWriter (key) { // just compat for old version const w = this.activeWriters.get(key) if (w) w.isRemoved = true this._queueBump() } + removeable (key) { + return this._applyState ? this._applyState.removeable(key) : false + } + _updateAckThreshold () { if (this._ackThreshold === 0) return if (this._ackTimer) this._ackTimer.bau() diff --git a/lib/apply-state.js b/lib/apply-state.js index f40f1d7b..f2715763 100644 --- a/lib/apply-state.js +++ b/lib/apply-state.js @@ -11,6 +11,32 @@ const safetyCatch = require('safety-catch') // must not be set too low (stretched with num of indexers) const SHOULD_WRITE_THRESHOLD = 16 +class AutobaseHostCalls { + constructor (state) { + this.state = state + this.base = state.base + this.system = state.system + } + + async addWriter (key, { indexer = true, isIndexer = indexer } = {}) { // just compat for old version + await this.system.add(key, { isIndexer }) + await this.base._addWriter(key, this.system) + } + + async removeWriter (key) { // just compat for old version + if (!this.state.removeable(key)) { + throw new Error('Not allowed to remove the last indexer') + } + + await this.system.remove(key) + this.base._removeWriter(key) + } + + interrupt (reason) { + this.base._interrupt(reason) + } +} + class CheckpointCore { constructor (view, core, signer) { this.view = view @@ -186,6 +212,7 @@ module.exports = class ApplyState extends ReadyResource { this.view = null this.views = [] this.systemRef = null + this.hostCalls = null this.updates = [] @@ -255,6 +282,11 @@ module.exports = class ApplyState extends ReadyResource { return false } + removeable (key) { + if (this.system.indexers.length !== 1) return true + return !b4a.equals(this.system.indexers[0].key, key) + } + isLocalIndexer () { return !!this.localCheckpoint } @@ -297,6 +329,7 @@ module.exports = class ApplyState extends ReadyResource { this.indexersUpdated = boot.indexersUpdated this.stretchedThreshold = this.system.indexers.length * SHOULD_WRITE_THRESHOLD this.quorum = sysCore.manifest ? sysCore.manifest.quorum : 0 + this.hostCalls = new AutobaseHostCalls(this) const added = new Set() @@ -769,7 +802,7 @@ module.exports = class ApplyState extends ReadyResource { if (applyBatch.length && this.base._hasApply === true) { this.applying = true - await this.base._handlers.apply(applyBatch, this.view, this.base) + await this.base._handlers.apply(applyBatch, this.view, this.hostCalls) this.applying = false } diff --git a/test/basic.js b/test/basic.js index 331546cc..a175a899 100644 --- a/test/basic.js +++ b/test/basic.js @@ -421,7 +421,6 @@ test('basic - throws', async t => { await t.exception(b.append('not writable')) await t.exception(a.view.append('append outside apply')) - await t.exception(() => a.addWriter(b.local.key)) }) test('basic - add 5 writers', async t => { diff --git a/test/fast-forward.js b/test/fast-forward.js index b41c7c71..1148b349 100644 --- a/test/fast-forward.js +++ b/test/fast-forward.js @@ -26,7 +26,7 @@ test('fast-forward - simple', async t => { const [a, b] = bases - for (let i = 0; i < 200; i++) { + for (let i = 0; i < 1000; i++) { await a.append('a' + i) }