From 6f7cefa6eae92ff00594a576a7ccad4c5c5c2bcc Mon Sep 17 00:00:00 2001 From: Chip Morningstar Date: Wed, 2 Jun 2021 17:30:51 -0700 Subject: [PATCH] feat(swing-store-lmdb)!: enable configuration of LMDB database size limit BREAKING CHANGE: This includes a renaming of the constructors to acknowledge that the different SwingStore constructors are not polymorphic. --- .../swing-store-lmdb/src/lmdbSwingStore.js | 22 +++++++++++-------- packages/swing-store-lmdb/test/test-state.js | 12 +++++----- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/packages/swing-store-lmdb/src/lmdbSwingStore.js b/packages/swing-store-lmdb/src/lmdbSwingStore.js index 2f99d005df1..a7087b2d932 100644 --- a/packages/swing-store-lmdb/src/lmdbSwingStore.js +++ b/packages/swing-store-lmdb/src/lmdbSwingStore.js @@ -19,14 +19,15 @@ const encoder = new util.TextEncoder(); */ /** - * Do the work of `initSwingStore` and `openSwingStore`. + * Do the work of `initLMDBSwingStore` and `openLMDBSwingStore`. * * @param {string} dirPath Path to a directory in which database files may be kept. * @param {boolean} forceReset If true, initialize the database to an empty state + * @param {Object} options Configuration options * * @returns {SwingStore} */ -function makeSwingStore(dirPath, forceReset = false) { +function makeLMDBSwingStore(dirPath, forceReset, options) { let txn = null; if (forceReset) { @@ -34,10 +35,11 @@ function makeSwingStore(dirPath, forceReset = false) { } fs.mkdirSync(`${dirPath}/streams`, { recursive: true }); + const { mapSize = 2 * 1024 * 1024 * 1024 } = options; let lmdbEnv = new lmdb.Env(); lmdbEnv.open({ path: dirPath, - mapSize: 2 * 1024 * 1024 * 1024, // XXX need to tune this + mapSize, // Turn off useWritemap on the Mac. The userWritemap option is currently // required for LMDB to function correctly on Linux running under WSL, but // we don't yet have a convenient recipe to probe our environment at @@ -406,12 +408,13 @@ function makeSwingStore(dirPath, forceReset = false) { * This directory need not actually exist yet (if it doesn't it will be * created) but it is reserved (by the caller) for the exclusive use of this * swing store instance. + * @param {Object?} options Optional configuration options * * @returns {SwingStore} */ -export function initSwingStore(dirPath) { +export function initLMDBSwingStore(dirPath, options = {}) { assert.typeof(dirPath, 'string'); - return makeSwingStore(dirPath, true); + return makeLMDBSwingStore(dirPath, true, options); } /** @@ -422,12 +425,13 @@ export function initSwingStore(dirPath) { * This directory need not actually exist yet (if it doesn't it will be * created) but it is reserved (by the caller) for the exclusive use of this * swing store instance. + * @param {Object?} options Optional configuration options * * @returns {SwingStore} */ -export function openSwingStore(dirPath) { +export function openLMDBSwingStore(dirPath, options = {}) { assert.typeof(dirPath, 'string'); - return makeSwingStore(dirPath, false); + return makeLMDBSwingStore(dirPath, false, options); } /** @@ -437,8 +441,8 @@ export function openSwingStore(dirPath) { * This directory need not actually exist * * @returns {boolean} - * If the directory is present and contains the files created by initSwingStore - * or openSwingStore, returns true. Else returns false. + * If the directory is present and contains the files created by initLMDBSwingStore + * or openLMDBSwingStore, returns true. Else returns false. * */ export function isSwingStore(dirPath) { diff --git a/packages/swing-store-lmdb/test/test-state.js b/packages/swing-store-lmdb/test/test-state.js index 64137a2b7ab..50593863ad9 100644 --- a/packages/swing-store-lmdb/test/test-state.js +++ b/packages/swing-store-lmdb/test/test-state.js @@ -9,8 +9,8 @@ import test from 'ava'; import { getAllState } from '@agoric/swing-store-simple'; import { - initSwingStore, - openSwingStore, + initLMDBSwingStore, + openLMDBSwingStore, isSwingStore, } from '../src/lmdbSwingStore'; @@ -54,7 +54,7 @@ test('storageInLMDB under SES', t => { t.teardown(() => fs.rmdirSync(dbDir, { recursive: true })); fs.rmdirSync(dbDir, { recursive: true }); t.is(isSwingStore(dbDir), false); - const store = initSwingStore(dbDir); + const store = initLMDBSwingStore(dbDir); const { commit, close } = store; testKVStore(t, store); commit(); @@ -62,7 +62,7 @@ test('storageInLMDB under SES', t => { close(); t.is(isSwingStore(dbDir), true); - const store2 = openSwingStore(dbDir); + const store2 = openLMDBSwingStore(dbDir); const { close: close2 } = store2; t.deepEqual(getAllState(store2), before, 'check state after reread'); t.is(isSwingStore(dbDir), true); @@ -74,7 +74,7 @@ test('streamStore read/write', t => { t.teardown(() => fs.rmdirSync(dbDir, { recursive: true })); fs.rmdirSync(dbDir, { recursive: true }); t.is(isSwingStore(dbDir), false); - const { streamStore, commit, close } = initSwingStore(dbDir); + const { streamStore, commit, close } = initLMDBSwingStore(dbDir); const start = streamStore.STREAM_START; let s1pos = start; @@ -117,7 +117,7 @@ test('streamStore mode interlock', t => { t.teardown(() => fs.rmdirSync(dbDir, { recursive: true })); fs.rmdirSync(dbDir, { recursive: true }); t.is(isSwingStore(dbDir), false); - const { streamStore, commit, close } = initSwingStore(dbDir); + const { streamStore, commit, close } = initLMDBSwingStore(dbDir); const start = streamStore.STREAM_START; const s1pos = streamStore.writeStreamItem('st1', 'first', start);