Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use global cache passed through corestore if available #163

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,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
mafintosh marked this conversation as resolved.
Show resolved Hide resolved
this.encrypted = handlers.encrypted || !!handlers.encryptionKey
this.encryptionKey = handlers.encryptionKey || null

Expand Down Expand Up @@ -131,8 +131,6 @@ 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 @@ -155,8 +153,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 @@ -325,8 +322,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 @@ -419,8 +415,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
2 changes: 2 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class AutocoreSession extends EventEmitter {
this._index = source.sessions.push(this) - 1
this._snapshot = snapshot

this.globalCache = this.base.globalCache

this.ready().catch(safetyCatch)
}

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": "holepunchto/hypercore#rache-global-approach",
"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": "holepunchto/corestore#global-cache-opt",
"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')
})

// todo: this test is hard, probably have to rely on ff to recover
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
Loading