Skip to content

Commit

Permalink
Expose maxCacheSize opt (for system db) (#156)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
HDegroote and chm-diederichs authored Jun 24, 2024
1 parent de082bc commit 218d4ce
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
34 changes: 29 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 || 0 // 0 means the hyperbee default cache size will be used

const {
ackInterval = DEFAULT_ACK_INTERVAL,
ackThreshold = DEFAULT_ACK_THRESHOLD
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = []
Expand Down
13 changes: 9 additions & 4 deletions lib/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down

0 comments on commit 218d4ce

Please sign in to comment.