Skip to content

Commit

Permalink
feat(swing-store-lmdb)!: enable configuration of LMDB database size l…
Browse files Browse the repository at this point in the history
…imit

BREAKING CHANGE: This includes a renaming of the constructors to acknowledge
that the different SwingStore constructors are not polymorphic.
  • Loading branch information
FUDCo committed Jun 3, 2021
1 parent 94f7fbf commit 6f7cefa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
22 changes: 13 additions & 9 deletions packages/swing-store-lmdb/src/lmdbSwingStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,27 @@ 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) {
fs.rmdirSync(dirPath, { recursive: true });
}
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
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions packages/swing-store-lmdb/test/test-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import test from 'ava';
import { getAllState } from '@agoric/swing-store-simple';

import {
initSwingStore,
openSwingStore,
initLMDBSwingStore,
openLMDBSwingStore,
isSwingStore,
} from '../src/lmdbSwingStore';

Expand Down Expand Up @@ -54,15 +54,15 @@ 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();
const before = getAllState(store);
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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6f7cefa

Please sign in to comment.