From 5b9b757caf5fb761deb789172adb331ca5642c20 Mon Sep 17 00:00:00 2001 From: HDegroote <75906619+HDegroote@users.noreply.github.com> Date: Tue, 18 Jun 2024 10:40:46 +0200 Subject: [PATCH] Expose maxCacheSize opt (for system db) --- index.js | 16 +++++++++++----- lib/system.js | 4 ++-- test/basic.js | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 5cb08e9e..386c8550 100644 --- a/index.js +++ b/index.js @@ -131,6 +131,8 @@ module.exports = class Autobase extends ReadyResource { this.system = null this.version = -1 + this.maxCacheSize = handlers.maxCacheSize || null + const { ackInterval = DEFAULT_ACK_INTERVAL, ackThreshold = DEFAULT_ACK_THRESHOLD @@ -150,7 +152,11 @@ module.exports = class Autobase extends ReadyResource { this._waiting = new SignalPromise() - this.system = new SystemView(this._viewStore.get({ name: '_system', exclusive: true })) + this.system = new SystemView( + this._viewStore.get({ name: '_system', exclusive: true }), + 0, + { maxCacheSize: this.maxCacheSize } + ) this.view = this._hasOpen ? this._handlers.open(this._viewStore, this) : null this.ready().catch(safetyCatch) @@ -316,7 +322,7 @@ module.exports = class Autobase extends ReadyResource { return { bootstrap, system: null, heads: [] } } - const system = new SystemView(core, length) + const system = new SystemView(core, length, { maxCacheSize: this.maxCacheSize }) await system.ready() if (system.version > this.maxSupportedVersion) { @@ -406,7 +412,7 @@ module.exports = class Autobase extends ReadyResource { const core = this.store.get({ key, encryptionKey, isBlockKey: true }).batch({ checkout: length, session: false }) const base = this - const system = new SystemView(core, length) + const system = new SystemView(core, length, { maxCacheSize: this.maxCacheSize }) await system.ready() const indexerCores = [] @@ -1535,7 +1541,7 @@ module.exports = class Autobase extends ReadyResource { await core.get(length - 1, { timeout }) } - const system = new SystemView(core.session(), length) + const system = new SystemView(core.session(), length, { maxCacheSize: this.maxCacheSize }) await system.ready() if (system.version > this.maxSupportedVersion) { @@ -1654,7 +1660,7 @@ module.exports = class Autobase extends ReadyResource { return } - const system = new SystemView(core, length) + const system = new SystemView(core, length, { maxCacheSize: this.maxCacheSize }) await system.ready() const opened = [] diff --git a/lib/system.js b/lib/system.js index c8d4d216..4598f273 100644 --- a/lib/system.js +++ b/lib/system.js @@ -12,12 +12,12 @@ const DIGEST = subs.sub(b4a.from([0])) const MEMBERS = subs.sub(b4a.from([1])) module.exports = class SystemView extends ReadyResource { - constructor (core, checkout = 0) { + constructor (core, checkout = 0, opts = {}) { super() this.core = core // sessions is a workaround for batches not having sessions atm... - this.db = new Hyperbee(core, { keyEncoding: 'binary', extension: false, checkout, sessions: typeof core.session === 'function' }) + this.db = new Hyperbee(core, { keyEncoding: 'binary', extension: false, checkout, sessions: typeof core.session === 'function', maxCacheSize: opts.maxCacheSize }) this.version = -1 // set version in apply this.members = 0 diff --git a/test/basic.js b/test/basic.js index 7e992ae6..646f5bce 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1581,6 +1581,21 @@ test('basic - writer adds a writer while being removed', async t => { t.is(binfo.isRemoved, true) }) +test('basic - maxCacheSize opt', async t => { + const [store] = await createStores(1, t) + const base = new Autobase(store.namespace('with-cache'), null, { maxCacheSize: 10 }) + await base.ready() + t.is(base.maxCacheSize, 10, 'maxCacheSize set') + t.is(base.system.db.maxCacheSize, 10, 'maxCacheSize applied to sys db') +}) + +test('basic - maxCacheSize has null default', async t => { + const [store] = await createStores(1, t) + const base = new Autobase(store.namespace('with-cache')) + await base.ready() + t.is(base.maxCacheSize, null, 'maxCacheSize default null') +}) + // todo: this test is hard, probably have to rely on ff to recover test.skip('basic - writer adds a writer while being removed', async t => { const { bases } = await create(4, t, { apply: applyWithRemove })