Skip to content

Commit

Permalink
feat: add exporter.getHostKV() API
Browse files Browse the repository at this point in the history
Add a new API to the exporter, `exporter.getHostKV(key)`, so that the
cosmic-swingset state-sync exporter process can query `host.height`
without accidentally creating a read-write transaction too.

refs #8523
  • Loading branch information
warner committed Dec 5, 2023
1 parent 1b5e57f commit 017686e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/swing-store/src/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,33 @@ export function makeSwingStoreExporter(dirPath, options = {}) {
assertComplete(internal, artifactMode);
}

const sqlKVGet = db.prepare(`
SELECT value
FROM kvStore
WHERE key = ?
`);
sqlKVGet.pluck(true);

/**
* Obtain the value stored for a given host key. This is for the
* benefit of clients who need to briefly query the DB to ensure
* they are exporting the right thing, and need to avoid modifying
* anything (or creating a read-write DB lock) in the process.
*
* @param {string} key The key whose value is sought.
*
* @returns {string | undefined} the (string) value for the given key, or
* undefined if there is no such value.
*
* @throws if key is not a string, or the key is not in the host
* section
*/
function getHostKV(key) {
typeof key === 'string' || Fail`key must be a string`;
key.startsWith('host.') || Fail`key must start with host.`;
return sqlKVGet.get(key);
}

const sqlGetAllKVData = db.prepare(`
SELECT key, value
FROM kvStore
Expand Down Expand Up @@ -173,6 +200,7 @@ export function makeSwingStoreExporter(dirPath, options = {}) {
}

return harden({
getHostKV,
getExportData,
getArtifactNames,
getArtifact,
Expand Down
3 changes: 3 additions & 0 deletions packages/swing-store/test/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export function buildData() {
*/
export function makeExporter(exportData, artifacts) {
return {
getHostKV(_key) {
return undefined;
},
async *getExportData() {
for (const [key, value] of exportData.entries()) {
/** @type { import('../src/exporter.js').KVPair } */
Expand Down
6 changes: 6 additions & 0 deletions packages/swing-store/test/test-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const exportTest = test.macro(async (t, mode) => {
const ss1 = initSwingStore(dbDir, options);
const ks = ss1.kernelStorage;

ss1.hostStorage.kvStore.set('host.h1', 'hostvalue1');

// build a DB with four spans (one in an old incarnation, two
// historical but current incarnation, only one inUse) and two
// snapshots (only one inUSe)
Expand Down Expand Up @@ -86,6 +88,10 @@ const exportTest = test.macro(async (t, mode) => {
}
const exporter = makeSwingStoreExporter(dbDir, { artifactMode });

// hostKV
t.is(exporter.getHostKV('host.h1'), 'hostvalue1');
t.is(exporter.getHostKV('host.hmissing'), undefined);

// exportData
{
const exportData = new Map();
Expand Down

0 comments on commit 017686e

Please sign in to comment.