Skip to content

Commit

Permalink
feat: swing-store-lmdb: add isSwingStore() query
Browse files Browse the repository at this point in the history
refs #953
  • Loading branch information
warner committed Apr 18, 2020
1 parent 561da92 commit fce7168
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
25 changes: 25 additions & 0 deletions packages/swing-store-lmdb/lmdbSwingStore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import path from 'path';

import lmdb from 'node-lmdb';

Expand Down Expand Up @@ -234,3 +235,27 @@ export function openSwingStore(dirPath) {
}
return makeSwingStore(dirPath, false);
}

/**
* Is this directory a compatible swing store?
*
* @param dirPath Path to a directory in which database files might be present.
* This directory need not actually exist
*
* @return boolean
* If the directory is present and contains the files created by initSwingStore
* or openSwingStore, returns true. Else returns false.
*
*/
export function isSwingStore(dirPath) {
if (`${dirPath}` !== dirPath) {
throw new Error('dirPath must be a string');
}
if (fs.existsSync(dirPath)) {
const storeFile = path.resolve(dirPath, 'data.mdb');
if (fs.existsSync(storeFile)) {
return true;
}
}
return false;
}
23 changes: 22 additions & 1 deletion packages/swing-store-lmdb/test/test-state.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import fs from 'fs';
import path from 'path';

import { test } from 'tape-promise/tape';
import { getAllState } from '@agoric/swing-store-simple';

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

function testStorage(t, storage) {
t.notOk(storage.has('missing'));
Expand Down Expand Up @@ -37,15 +42,31 @@ function testStorage(t, storage) {
}

test('storageInLMDB', t => {
fs.rmdirSync('testdb', { recursive: true });
t.equal(isSwingStore('testdb'), false);
const { storage, commit, close } = initSwingStore('testdb');
testStorage(t, storage);
commit();
const before = getAllState(storage);
close();
t.equal(isSwingStore('testdb'), true);

const { storage: after } = openSwingStore('testdb');
t.deepEqual(getAllState(after), before, 'check state after reread');
t.equal(isSwingStore('testdb'), true);
t.end();
});

test('rejectSimple', t => {
const simpleDir = 'testdb-simple';
fs.mkdirSync(simpleDir, { recursive: true });
fs.writeFileSync(
path.resolve(simpleDir, 'swingset-kernel-state.jsonlines'),
'some data\n',
);
t.equal(isSwingStore(simpleDir), false);
t.end();
});

test.onFinish(() => fs.rmdirSync('testdb', { recursive: true }));
test.onFinish(() => fs.rmdirSync('testdb-simple', { recursive: true }));

0 comments on commit fce7168

Please sign in to comment.