Skip to content

Commit

Permalink
Expose maxCacheSize opt (for system db)
Browse files Browse the repository at this point in the history
  • Loading branch information
HDegroote committed Jun 18, 2024
1 parent c442b53 commit 5b9b757
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = []
Expand Down
4 changes: 2 additions & 2 deletions lib/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down

0 comments on commit 5b9b757

Please sign in to comment.