Skip to content

Commit

Permalink
feat: swing-store-simple: add isSwingStore() query
Browse files Browse the repository at this point in the history
refs #953
  • Loading branch information
warner committed Apr 17, 2020
1 parent 0b94ecc commit 17b93f6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/swing-store-simple/simpleSwingStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,27 @@ export function setAllState(storage, stuff) {
storage.set(k, stuff[k]);
}
}

/**
* 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, 'swingset-kernel-state.jsonlines');
if (fs.existsSync(storeFile)) {
return true;
}
}
return false;
}
15 changes: 15 additions & 0 deletions packages/swing-store-simple/test/test-state.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import fs from 'fs';
import path from 'path';

import { test } from 'tape-promise/tape';
import {
initSwingStore,
openSwingStore,
getAllState,
isSwingStore,
} from '../simpleSwingStore';

function testStorage(t, storage) {
Expand Down Expand Up @@ -45,15 +47,28 @@ test('storageInMemory', t => {
});

test('storageInFile', t => {
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('rejectLMDB', t => {
const notSimpleDir = 'testdb-lmdb';
fs.mkdirSync(notSimpleDir, { recursive: true });
fs.writeFileSync(path.resolve(notSimpleDir, 'data.mdb'), 'some data\n');
fs.writeFileSync(path.resolve(notSimpleDir, 'lock.mdb'), 'lock stuff\n');
t.equal(isSwingStore(notSimpleDir), false);
t.end();
});

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

0 comments on commit 17b93f6

Please sign in to comment.