Skip to content

Commit

Permalink
Use global cache passed through corestore if available (#163)
Browse files Browse the repository at this point in the history
* Use global cache passed through corestore

* Clean up + add test + rm maxCacheSize

* use npm deps

* style

* do not use getter in constructor

---------

Co-authored-by: Mathias Buus <[email protected]>
  • Loading branch information
HDegroote and mafintosh authored Jul 16, 2024
1 parent 382552a commit fe0485e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 28 deletions.
13 changes: 4 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = class Autobase extends ReadyResource {
this.keyPair = handlers.keyPair || null
this.valueEncoding = c.from(handlers.valueEncoding || 'binary')
this.store = store

this.globalCache = store.globalCache || null
this.encrypted = handlers.encrypted || !!handlers.encryptionKey
this.encryptionKey = handlers.encryptionKey || null

Expand Down Expand Up @@ -133,8 +133,6 @@ module.exports = class Autobase extends ReadyResource {
this.version = -1
this.interrupted = null

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 @@ -157,8 +155,7 @@ module.exports = class Autobase extends ReadyResource {
const sysCore = this._viewStore.get({ name: '_system', exclusive: true })

this.system = new SystemView(sysCore, {
checkout: 0,
maxCacheSize: this.maxCacheSize
checkout: 0
})

this.view = this._hasOpen ? this._handlers.open(this._viewStore, this) : null
Expand Down Expand Up @@ -327,8 +324,7 @@ module.exports = class Autobase extends ReadyResource {
}

const system = new SystemView(core, {
checkout: length,
maxCacheSize: this.maxCacheSize
checkout: length
})

await system.ready()
Expand Down Expand Up @@ -427,8 +423,7 @@ module.exports = class Autobase extends ReadyResource {

const base = this
const system = new SystemView(core, {
checkout: length,
maxCacheSize: this.maxCacheSize
checkout: length
})

await system.ready()
Expand Down
1 change: 1 addition & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class AutocoreSession extends EventEmitter {

this.activeRequests = []
this.valueEncoding = valueEncoding || null
this.globalCache = source.base.globalCache

this._source = source
this._index = source.sessions.push(this) - 1
Expand Down
9 changes: 4 additions & 5 deletions lib/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +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, maxCacheSize = 0 } = {}) {
constructor (core, { checkout = 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', maxCacheSize })
this.db = new Hyperbee(core, { keyEncoding: 'binary', extension: false, checkout, sessions: typeof core.session === 'function' })

this.version = -1 // set version in apply
this.members = 0
Expand Down Expand Up @@ -68,10 +68,9 @@ module.exports = class SystemView extends ReadyResource {
}
}

async checkout (length, { maxCacheSize = this.db.maxCacheSize } = {}) {
async checkout (length) {
const checkout = new SystemView(this.core.session(), {
checkout: length,
maxCacheSize
checkout: length
})

await checkout.ready()
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"core-coupler": "^1.0.0",
"debounceify": "^1.0.0",
"hyperbee": "^2.15.0",
"hypercore": "^10.37.1",
"hypercore": "^10.37.10",
"hypercore-crypto": "^3.4.0",
"hypercore-id-encoding": "^1.2.0",
"mutexify": "^1.4.0",
Expand All @@ -50,7 +50,8 @@
"devDependencies": {
"autobase-test-helpers": "^2.0.1",
"brittle": "^3.1.1",
"corestore": "^6.16.1",
"corestore": "^6.18.3",
"rache": "^1.0.0",
"random-access-memory": "^6.2.0",
"same-data": "^1.0.0",
"standard": "^17.0.0",
Expand Down
20 changes: 9 additions & 11 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const ram = require('random-access-memory')
const Corestore = require('corestore')
const b4a = require('b4a')
const crypto = require('hypercore-crypto')
const Rache = require('rache')

const Autobase = require('..')

Expand Down Expand Up @@ -1616,19 +1617,16 @@ 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 - sessions use globalCache from corestore if it is set', async t => {
const globalCache = new Rache()

test('basic - maxCacheSize has 0 default', async t => {
const [store] = await createStores(1, t)
const base = new Autobase(store.namespace('with-cache'))
const [store] = await createStores(1, t, { globalCache })
const base = createBase(store, null, t)
await base.ready()
t.is(base.maxCacheSize, 0, 'maxCacheSize default 0')

t.is(base.globalCache, globalCache, 'globalCache set on autobase itself')
t.is(base.view.globalCache, globalCache, 'passed to autocore sessions')
t.is(base.system.core.globalCache, globalCache, 'passed to system')
})

test('basic - interrupt', async t => {
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ async function createStores (n, t, opts = {}) {
const stores = []
for (let i = offset; i < n + offset; i++) {
const primaryKey = Buffer.alloc(32, i)
stores.push(new Corestore(await storage(), { primaryKey, encryptionKey }))
const globalCache = opts.globalCache || null
stores.push(new Corestore(await storage(), { primaryKey, encryptionKey, globalCache }))
}

t.teardown(() => Promise.all(stores.map(s => s.close())), { order: 2 })
Expand Down

0 comments on commit fe0485e

Please sign in to comment.