-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09080d7
commit 3774694
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import dbOpenAmbient from 'better-sqlite3'; | ||
|
||
const HOME = process.env.HOME; | ||
|
||
/** @type {<T>(val: T | undefined) => T} */ | ||
export const NonNullish = val => { | ||
if (!val) throw Error('required'); | ||
return val; | ||
}; | ||
|
||
/** | ||
* @file look up vat incarnation from kernel DB | ||
* @see {getIncarnation} | ||
*/ | ||
|
||
const swingstorePath = `${HOME}/.agoric/data/agoric/swingstore.sqlite`; | ||
|
||
/** | ||
* SQL short-hand | ||
* | ||
* @param {import('better-sqlite3').Database} db | ||
*/ | ||
export const dbTool = db => { | ||
const prepare = (strings, ...params) => { | ||
const dml = strings.join('?'); | ||
return { stmt: db.prepare(dml), params }; | ||
}; | ||
const sql = (strings, ...args) => { | ||
const { stmt, params } = prepare(strings, ...args); | ||
return stmt.all(...params); | ||
}; | ||
sql.get = (strings, ...args) => { | ||
const { stmt, params } = prepare(strings, ...args); | ||
return stmt.get(...params); | ||
}; | ||
return sql; | ||
}; | ||
|
||
/** | ||
* @param {import('better-sqlite3').Database} db | ||
*/ | ||
const makeSwingstore = db => { | ||
const sql = dbTool(db); | ||
|
||
/** @param {string} key */ | ||
const kvGet = key => sql.get`select * from kvStore where key = ${key}`.value; | ||
/** @param {string} key */ | ||
const kvGetJSON = key => JSON.parse(kvGet(key)); | ||
|
||
/** @param {string} vatID */ | ||
const lookupVat = vatID => { | ||
return Object.freeze({ | ||
source: () => kvGetJSON(`${vatID}.source`), | ||
options: () => kvGetJSON(`${vatID}.options`), | ||
currentSpan: () => | ||
sql.get`select * from transcriptSpans where isCurrent = 1 and vatID = ${vatID}`, | ||
}); | ||
}; | ||
|
||
return Object.freeze({ | ||
/** @param {string} vatName */ | ||
findVat: vatName => { | ||
/** @type {string[]} */ | ||
const dynamicIDs = kvGetJSON('vat.dynamicIDs'); | ||
const targetVat = dynamicIDs.find(vatID => | ||
lookupVat(vatID).options().name.includes(vatName), | ||
); | ||
if (!targetVat) throw Error(`vat not found: ${vatName}`); | ||
return targetVat; | ||
}, | ||
/** @param {string} vatName */ | ||
findVats: vatName => { | ||
/** @type {string[]} */ | ||
const dynamicIDs = kvGetJSON('vat.dynamicIDs'); | ||
return dynamicIDs.filter(vatID => | ||
lookupVat(vatID).options().name.includes(vatName), | ||
); | ||
}, | ||
lookupVat, | ||
}); | ||
}; | ||
|
||
/** @param {string} vatName */ | ||
export const getDetailsMatchingVats = async vatName => { | ||
const kStore = makeSwingstore( | ||
dbOpenAmbient(swingstorePath, { readonly: true }), | ||
); | ||
|
||
const vatIDs = kStore.findVats(vatName); | ||
const infos = []; | ||
for (const vatID of vatIDs) { | ||
const vatInfo = kStore.lookupVat(vatID); | ||
const source = vatInfo.source(); | ||
// @ts-expect-error cast | ||
const { incarnation } = vatInfo.currentSpan(); | ||
infos.push({ vatName, vatID, incarnation, ...source }); | ||
} | ||
|
||
return infos; | ||
}; |