diff --git a/index.js b/index.js index f630d21e..c11953a0 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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 @@ -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 @@ -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() @@ -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() diff --git a/lib/core.js b/lib/core.js index 8de4c6a9..6a172e91 100644 --- a/lib/core.js +++ b/lib/core.js @@ -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 diff --git a/lib/system.js b/lib/system.js index 6cc68418..e2a7cacd 100644 --- a/lib/system.js +++ b/lib/system.js @@ -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 @@ -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() diff --git a/package.json b/package.json index fcf73bd4..a5442991 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/test/basic.js b/test/basic.js index 53ac873c..e7f2b3e3 100644 --- a/test/basic.js +++ b/test/basic.js @@ -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('..') @@ -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 => { diff --git a/test/helpers/index.js b/test/helpers/index.js index 5942a883..a3993215 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -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 })