Skip to content

Commit

Permalink
feat: tools for fiddling with kernel DB
Browse files Browse the repository at this point in the history
  • Loading branch information
FUDCo committed May 11, 2021
1 parent b5f6226 commit d14fa1e
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/SwingSet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@agoric/notifier": "^0.3.14",
"@agoric/promise-kit": "^0.2.13",
"@agoric/store": "^0.4.14",
"@agoric/swing-store-lmdb": "^0.4.11",
"@agoric/swing-store-simple": "^0.3.9",
"@agoric/tame-metering": "^1.3.9",
"@agoric/transform-metering": "^1.4.12",
Expand Down
62 changes: 62 additions & 0 deletions packages/SwingSet/tools/db-delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env -S node -r esm

import '@agoric/install-ses';
import process from 'process';
import { openSwingStore } from '@agoric/swing-store-lmdb';

const log = console.log;

function usage() {
log(`
Command line:
db-delete-key.js [FLAGS] STATEDIR KEY
FLAGS may be:
--range - delete all keys starting with \`\${KEY}.\`
`);
}

function fail(message, printUsage) {
if (message) {
log(message);
}
if (printUsage) {
usage();
}
process.exit(1);
}

function run() {
const argv = process.argv.slice(2);

let range = false;
while (argv[0] && argv[0].startsWith('-')) {
const flag = argv.shift();
switch (flag) {
case '--range':
range = true;
break;
default:
fail(`invalid flag ${flag}`, true);
break;
}
}
if (argv.length !== 2) {
fail('wrong number of args', true);
}
const stateDBDir = argv.shift();
const key = argv.shift();

const { storage, commit } = openSwingStore(stateDBDir);

if (range) {
for (const k of storage.getKeys(`${key}.`, `${key}/`)) {
storage.delete(k);
}
} else {
storage.delete(key);
}
commit();
}

run();
81 changes: 81 additions & 0 deletions packages/SwingSet/tools/db-get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env -S node -r esm

import '@agoric/install-ses';
import process from 'process';
import { openSwingStore } from '@agoric/swing-store-lmdb';

const log = console.log;

const out = process.stdout;
function p(str) {
out.write(str);
out.write('\n');
}

function usage() {
log(`
Command line:
db-get-key.js [FLAGS] STATEDIR KEY
FLAGS may be:
--raw - just output the value(s) without adornment or key info
--range - output values for all keys starting with \`\${KEY}.\`
`);
}

function fail(message, printUsage) {
if (message) {
log(message);
}
if (printUsage) {
usage();
}
process.exit(1);
}

function run() {
const argv = process.argv.slice(2);

let raw = false;
let range = false;
while (argv[0] && argv[0].startsWith('-')) {
const flag = argv.shift();
switch (flag) {
case '--raw':
raw = true;
break;
case '--range':
range = true;
break;
default:
fail(`invalid flag ${flag}`, true);
break;
}
}
if (argv.length !== 2) {
fail('wrong number of args', true);
}
const stateDBDir = argv.shift();
const key = argv.shift();

const { storage } = openSwingStore(stateDBDir);

function pkv(k) {
const value = storage.get(k);
if (raw) {
p(value);
} else {
p(`${k} :: ${value}`);
}
}

if (range) {
for (const k of storage.getKeys(`${key}.`, `${key}/`)) {
pkv(k);
}
} else {
pkv(key);
}
}

run();
42 changes: 42 additions & 0 deletions packages/SwingSet/tools/db-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env -S node -r esm

import '@agoric/install-ses';
import process from 'process';
import { openSwingStore } from '@agoric/swing-store-lmdb';

const log = console.log;

function usage() {
log(`
Command line:
db-set-key.js STATEDIR KEY VALUE
`);
}

function fail(message, printUsage) {
if (message) {
log(message);
}
if (printUsage) {
usage();
}
process.exit(1);
}

function run() {
const argv = process.argv.slice(2);

if (argv.length !== 3) {
fail('wrong number of args', true);
}
const stateDBDir = argv.shift();
const key = argv.shift();
const value = argv.shift();

const { storage, commit } = openSwingStore(stateDBDir);

storage.set(key, value);
commit();
}

run();
66 changes: 66 additions & 0 deletions packages/SwingSet/tools/replace-bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env -S node -r esm

import '@agoric/install-ses';
import process from 'process';
import { openSwingStore } from '@agoric/swing-store-lmdb';
import bundleSource from '@agoric/bundle-source';

const log = console.log;

function usage() {
log(`
Command line:
replace-bundle.js BUNDLEORVATNAME STATEDIR SOURCEPATH
`);
}

function fail(message, printUsage) {
if (message) {
log(message);
}
if (printUsage) {
usage();
}
process.exit(1);
}

async function run() {
const argv = process.argv.slice(2);
if (argv.length !== 3) {
fail('wrong number of args', true);
}

let bundleName = argv.shift();
let bundleBundle = true;
const stateDBDir = argv.shift();
const srcPath = argv.shift();

const { storage, commit } = openSwingStore(stateDBDir);
log(`will use ${srcPath} in ${stateDBDir} for ${bundleName}`);

if (bundleName === 'kernel') {
bundleName = 'kernelBundle';
} else {
const vatID = storage.get(`vat.name.${bundleName}`);
if (vatID) {
bundleName = `${vatID}.source`;
}
}
if (bundleName === 'kernelBundle') {
bundleBundle = false;
}

const oldBundleStr = storage.get(bundleName);
log(`old bundle is ${oldBundleStr.length} bytes`);
let bundle = await bundleSource(srcPath);
if (bundleBundle) {
bundle = { bundle };
}
const newBundleStr = JSON.stringify(bundle);
log(`new bundle is ${newBundleStr.length} bytes`);
storage.set(bundleName, newBundleStr);
commit();
log(`bundle ${bundleName} replaced`);
}

run().catch(console.error);

0 comments on commit d14fa1e

Please sign in to comment.