From 218d4ce7616e07b1d580c8b1022c52656a8058b7 Mon Sep 17 00:00:00 2001 From: HDegroote <75906619+HDegroote@users.noreply.github.com> Date: Mon, 24 Jun 2024 15:08:51 +0200 Subject: [PATCH] Expose maxCacheSize opt (for system db) (#156) * Expose maxCacheSize opt (for system db) * 0 default * Make SystemView's 'checkout' non-positional * Cleaner opts definition in SystemView * Bugfix: use correct maxCacheSize for SystemView.checkout * reformat multiline opts * checkout takes optional maxCacheSize --------- Co-authored-by: Christophe Diederichs --- index.js | 34 +++++++++++++++++++++++++++++----- lib/system.js | 13 +++++++++---- test/basic.js | 15 +++++++++++++++ 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index b6e040b8..4757fd1d 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 || 0 // 0 means the hyperbee default cache size will be used + const { ackInterval = DEFAULT_ACK_INTERVAL, ackThreshold = DEFAULT_ACK_THRESHOLD @@ -150,7 +152,13 @@ module.exports = class Autobase extends ReadyResource { this._waiting = new SignalPromise() - this.system = new SystemView(this._viewStore.get({ name: '_system', exclusive: true })) + const sysCore = this._viewStore.get({ name: '_system', exclusive: true }) + + this.system = new SystemView(sysCore, { + checkout: 0, + maxCacheSize: this.maxCacheSize + }) + this.view = this._hasOpen ? this._handlers.open(this._viewStore, this) : null this.ready().catch(safetyCatch) @@ -316,7 +324,11 @@ module.exports = class Autobase extends ReadyResource { return { bootstrap, system: null, heads: [] } } - const system = new SystemView(core, length) + const system = new SystemView(core, { + checkout: length, + maxCacheSize: this.maxCacheSize + }) + await system.ready() if (system.version > this.maxSupportedVersion) { @@ -406,7 +418,11 @@ 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, { + checkout: length, + maxCacheSize: this.maxCacheSize + }) + await system.ready() const indexerCores = [] @@ -1551,7 +1567,11 @@ 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(), { + checkout: length, + maxCacheSize: this.maxCacheSize + }) + await system.ready() if (system.version > this.maxSupportedVersion) { @@ -1670,7 +1690,11 @@ module.exports = class Autobase extends ReadyResource { return } - const system = new SystemView(core, length) + const system = new SystemView(core, { + checkout: length, + maxCacheSize: this.maxCacheSize + }) + await system.ready() const opened = [] diff --git a/lib/system.js b/lib/system.js index 30b1b711..6cc68418 100644 --- a/lib/system.js +++ b/lib/system.js @@ -12,12 +12,13 @@ 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, maxCacheSize = 0 } = {}) { 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 }) this.version = -1 // set version in apply this.members = 0 @@ -67,8 +68,12 @@ module.exports = class SystemView extends ReadyResource { } } - async checkout (length) { - const checkout = new SystemView(this.core.session(), length) + async checkout (length, { maxCacheSize = this.db.maxCacheSize } = {}) { + const checkout = new SystemView(this.core.session(), { + checkout: length, + maxCacheSize + }) + await checkout.ready() return checkout diff --git a/test/basic.js b/test/basic.js index be94550a..f63ffc0e 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1616,6 +1616,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 0 default', async t => { + const [store] = await createStores(1, t) + const base = new Autobase(store.namespace('with-cache')) + await base.ready() + t.is(base.maxCacheSize, 0, 'maxCacheSize default 0') +}) + // 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 })