Skip to content

Commit

Permalink
chore: this time for sure
Browse files Browse the repository at this point in the history
  • Loading branch information
FUDCo committed Mar 16, 2023
1 parent d95d3c5 commit cb0f1bf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
6 changes: 6 additions & 0 deletions packages/swing-store/src/snapStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ export function makeSnapStore(
return `snapshot.${rec.vatID}.current`;
}

/**
* @param {string} vatID
* @param {number} endPos
* @param {string} [hash]
* @param {number} [inUse]
*/
function snapshotRec(vatID, endPos, hash, inUse) {
return { vatID, endPos, hash, inUse };
}
Expand Down
12 changes: 7 additions & 5 deletions packages/swing-store/src/swingStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export function makeSwingStoreExporter(dirPath, exportMode = 'current') {
sqlBeginTransaction.run();

// ensureTxn can be a dummy, we just started one
const ensureTxn = () => 0;
const ensureTxn = () => {};
const snapStore = makeSnapStore(db, ensureTxn, makeSnapStoreIO());
const transcriptStore = makeTranscriptStore(db, ensureTxn, () => {});

Expand Down Expand Up @@ -882,11 +882,11 @@ export function initSwingStore(dirPath = null, options = {}) {
return makeSwingStore(dirPath, true, options);
}

function parseExportKey(key) {
function parseVatArtifactExportKey(key) {
const parts = key.split('.');
const [vatID, rawPos] = parts;
const [_type, vatID, rawPos] = parts;
// prettier-ignore
parts.length === 2 ||
parts.length === 3 ||
Fail`expected artifact name of the form '{type}.{vatID}.{pos}', saw ${q(key)}`;
const isCurrent = rawPos === 'current';
let pos;
Expand Down Expand Up @@ -943,7 +943,7 @@ export async function importSwingStore(exporter, dirPath = null, options = {}) {
} else if (tag === 'transcript' || tag === 'snapshot') {
// 'transcript' and 'snapshot' keys contain artifact description info.
assert(value); // make TypeScript shut up
const { vatID, isCurrent, pos } = parseExportKey(subKey);
const { vatID, isCurrent, pos } = parseVatArtifactExportKey(key);
if (isCurrent) {
const vatInfo = vatArtifacts.get(vatID) || {};
if (tag === 'snapshot') {
Expand All @@ -960,6 +960,8 @@ export async function importSwingStore(exporter, dirPath = null, options = {}) {
} else {
artifactMetadata.set(artifactKey(tag, vatID, pos), JSON.parse(value));
}
} else {
Fail`unknown artifact type tag ${q(tag)} on import`;
}
}

Expand Down
51 changes: 23 additions & 28 deletions packages/swing-store/src/transcriptStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function insistTranscriptPosition(position) {
export function makeTranscriptStore(
db,
ensureTxn,
noteExport,
noteExport = () => {},
{ keepTranscripts = true } = {},
) {
db.exec(`
Expand All @@ -69,7 +69,7 @@ export function makeTranscriptStore(

// Transcripts are broken up into "spans", delimited by heap snapshots. If we
// take heap snapshots after deliveries 100 and 200, and have not yet
// performed delivery 201, we'll have two non-current (i.e., isCurrent=0)
// performed delivery 201, we'll have two non-current (i.e., isCurrent=null)
// spans (one with startPos=0, endPos=100, the second with startPos=100,
// endPos=200), and a single empty isCurrent==1 span with startPos=200 and
// endPos=200. After we perform delivery 201, the single isCurrent=1 span
Expand All @@ -88,7 +88,8 @@ export function makeTranscriptStore(
endPos INTEGER, -- exclusive
hash TEXT, -- cumulative hash of this item and previous cumulative hash
isCurrent INTEGER,
PRIMARY KEY (vatID, startPos)
PRIMARY KEY (vatID, startPos),
UNIQUE (vatID, isCurrent)
)
`);
db.exec(`
Expand Down Expand Up @@ -131,16 +132,16 @@ export function makeTranscriptStore(
}

/**
* Compute a cumulative hash that includes a new transcript item. This is
* computed by hashing together the hash from the previous item in its span
* together with the new item's own text.
* Compute a new cumulative hash for a span that includes a new transcript
* item. This is computed by hashing together the hash from the previous item
* in its span together with the new item's own text.
*
* @param {string} priorHash The previous item's hash
* @param {string} item The item itself
*
* @returns {string} The hash of the combined parameters.
*/
function computeItemHash(priorHash, item) {
function updateSpanHash(priorHash, item) {
const itemHash = createSHA256(item).finish();
return createSHA256(priorHash).add(itemHash).finish();
}
Expand Down Expand Up @@ -190,25 +191,22 @@ export function makeTranscriptStore(
return `transcript.${rec.vatID}.${rec.startPos}.${rec.endPos}`;
}

function historicSpanMetadataKey(rec) {
function spanMetadataKey(rec) {
if (rec.isCurrent) {
return `transcript.${rec.vatID}.current`;
} else {
return `transcript.${rec.vatID}.${rec.startPos}`;
}
}

function currentSpanMetadataKey(rec) {
return `transcript.${rec.vatID}.current`;
}

function spanRec(vatID, startPos, endPos, hash, isCurrent) {
isCurrent = isCurrent ? 1 : 0;
return { vatID, startPos, endPos, hash, isCurrent };
}

const sqlEndCurrentSpan = db.prepare(`
UPDATE transcriptSpans
SET isCurrent = 0
SET isCurrent = null
WHERE isCurrent = 1 AND vatID = ?
`);

Expand All @@ -227,11 +225,11 @@ export function makeTranscriptStore(
ensureTxn();
const { hash, startPos, endPos } = getCurrentSpanBounds(vatID);
const rec = spanRec(vatID, startPos, endPos, hash, 0);
noteExport(historicSpanMetadataKey(rec), JSON.stringify(rec));
noteExport(spanMetadataKey(rec), JSON.stringify(rec));
sqlEndCurrentSpan.run(vatID);
sqlWriteSpan.run(vatID, endPos, endPos, initialHash, 1);
const newRec = spanRec(vatID, endPos, endPos, initialHash, 1);
noteExport(currentSpanMetadataKey(newRec), JSON.stringify(newRec));
noteExport(spanMetadataKey(newRec), JSON.stringify(newRec));
if (!keepTranscripts) {
sqlDeleteOldItems.run(vatID, endPos);
}
Expand All @@ -248,12 +246,11 @@ export function makeTranscriptStore(
`);

const sqlGetVatSpans = db.prepare(`
SELECT startPos
SELECT vatID, startPos, isCurrent
FROM transcriptSpans
WHERE vatID = ?
ORDER BY startPos
`);
sqlGetVatSpans.pluck(true);

/**
* Delete all transcript data for a given vat (for use when, e.g., a vat is terminated)
Expand All @@ -263,11 +260,9 @@ export function makeTranscriptStore(
function deleteVatTranscripts(vatID) {
ensureTxn();
const deletions = sqlGetVatSpans.all(vatID);
for (const startPos of deletions) {
const exportRec = spanRec(vatID, startPos);
noteExport(historicSpanMetadataKey(exportRec), undefined);
for (const rec of deletions) {
noteExport(spanMetadataKey(rec), undefined);
}
noteExport(currentSpanMetadataKey({ vatID }), undefined);
sqlDeleteVatItems.run(vatID);
sqlDeleteVatSpans.run(vatID);
}
Expand Down Expand Up @@ -309,8 +304,8 @@ export function makeTranscriptStore(
*
* @yields {[key: string, value: string]}
* @returns {Iterable<[key: string, value: string]>} An iterator over pairs of
* [historicSpanMetadataKey, rec], where `rec` is a JSON-encoded metadata record for the
* span named by `historicSpanMetadataKey`.
* [spanMetadataKey, rec], where `rec` is a JSON-encoded metadata record for the
* span named by `spanMetadataKey`.
*/
function* getExportRecords(includeHistorical = true) {
const sql = includeHistorical
Expand All @@ -319,7 +314,7 @@ export function makeTranscriptStore(
for (const rec of sql.iterate()) {
const { vatID, startPos, endPos, hash, isCurrent } = rec;
const exportRec = spanRec(vatID, startPos, endPos, hash, isCurrent);
yield [historicSpanMetadataKey(rec), JSON.stringify(exportRec)];
yield [spanMetadataKey(rec), JSON.stringify(exportRec)];
}
}

Expand Down Expand Up @@ -455,10 +450,10 @@ export function makeTranscriptStore(
const { startPos, endPos, hash } = getCurrentSpanBounds(vatID);
sqlAddItem.run(vatID, item, endPos);
const newEndPos = endPos + 1;
const newHash = computeItemHash(hash, item);
const newHash = updateSpanHash(hash, item);
sqlUpdateSpan.run(newEndPos, newHash, vatID);
const rec = spanRec(vatID, startPos, newEndPos, newHash, 1);
noteExport(currentSpanMetadataKey(rec), JSON.stringify(rec));
noteExport(spanMetadataKey(rec), JSON.stringify(rec));
};

/**
Expand Down Expand Up @@ -495,7 +490,7 @@ export function makeTranscriptStore(
let pos = startPos;
for await (const item of lineStream) {
sqlAddItem.run(vatID, item, pos);
hash = computeItemHash(hash, item);
hash = updateSpanHash(hash, item);
pos += 1;
}
pos === endPos || Fail`artifact ${name} is not available`;
Expand All @@ -506,7 +501,7 @@ export function makeTranscriptStore(
info.startPos,
info.endPos,
info.hash,
info.isCurrent,
info.isCurrent ? 1 : null,
);
}

Expand Down
1 change: 0 additions & 1 deletion packages/swing-store/test/test-deletion.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ test('delete transcripts with export callback', async t => {

t.deepEqual(exportLog, [
['transcript.v1.0', null],
['transcript.v1.3', null],
['transcript.v1.current', null],
]);

Expand Down

0 comments on commit cb0f1bf

Please sign in to comment.