1
0
silverbullet/server/syscalls/page_index.ts

118 lines
3.2 KiB
TypeScript
Raw Normal View History

import { Knex } from "knex";
2022-03-27 09:31:12 +00:00
import { SysCallMapping } from "../../plugos/system";
2022-03-28 13:25:05 +00:00
import {
ensureTable,
storeReadSyscalls,
storeWriteSyscalls,
} from "../../plugos/syscall/store.knex_node";
type IndexItem = {
page: string;
key: string;
value: any;
};
export type KV = {
key: string;
value: any;
};
2022-03-28 13:25:05 +00:00
/*
Keyspace design:
for page lookups:
p~page~key
for global lookups:
k~key~page
*/
function pageKey(page: string, key: string) {
return `p~${page}~${key}`;
}
function unpackPageKey(dbKey: string): { page: string; key: string } {
const [, page, key] = dbKey.split("~");
return { page, key };
}
function globalKey(page: string, key: string) {
return `k~${key}~${page}`;
}
function unpackGlobalKey(dbKey: string): { page: string; key: string } {
const [, key, page] = dbKey.split("~");
return { page, key };
}
export async function ensurePageIndexTable(db: Knex<any, unknown>) {
await ensureTable(db, "page_index");
}
export function pageIndexSyscalls(db: Knex<any, unknown>): SysCallMapping {
const readCalls = storeReadSyscalls(db, "page_index");
const writeCalls = storeWriteSyscalls(db, "page_index");
2022-03-25 11:03:06 +00:00
const apiObj: SysCallMapping = {
set: async (ctx, page: string, key: string, value: any) => {
2022-03-28 13:25:05 +00:00
await writeCalls.set(ctx, pageKey(page, key), value);
await writeCalls.set(ctx, globalKey(page, key), value);
},
2022-03-25 11:03:06 +00:00
batchSet: async (ctx, page: string, kvs: KV[]) => {
for (let { key, value } of kvs) {
2022-03-25 11:03:06 +00:00
await apiObj.set(ctx, page, key, value);
}
},
2022-03-25 11:03:06 +00:00
delete: async (ctx, page: string, key: string) => {
2022-03-28 13:25:05 +00:00
await writeCalls.delete(ctx, pageKey(page, key));
await writeCalls.delete(ctx, globalKey(page, key));
},
get: async (ctx, page: string, key: string) => {
return readCalls.get(ctx, pageKey(page, key));
},
2022-03-25 11:03:06 +00:00
scanPrefixForPage: async (ctx, page: string, prefix: string) => {
2022-03-28 13:25:05 +00:00
return (await readCalls.queryPrefix(ctx, pageKey(page, prefix))).map(
({ key, value }: { key: string; value: any }) => {
const { key: pageKey } = unpackPageKey(key);
return {
page,
key: pageKey,
value,
};
}
);
},
2022-03-25 11:03:06 +00:00
scanPrefixGlobal: async (ctx, prefix: string) => {
2022-03-28 13:25:05 +00:00
return (await readCalls.queryPrefix(ctx, `k~${prefix}`)).map(
({ key, value }: { key: string; value: any }) => {
const { page, key: pageKey } = unpackGlobalKey(key);
return {
page,
key: pageKey,
value,
};
}
);
},
clearPageIndexForPage: async (ctx, page: string) => {
await apiObj.deletePrefixForPage(ctx, page, "");
},
2022-03-25 11:03:06 +00:00
deletePrefixForPage: async (ctx, page: string, prefix: string) => {
2022-03-28 13:25:05 +00:00
// Collect all global keys for this page to delete
let keysToDelete = (
await readCalls.queryPrefix(ctx, pageKey(page, prefix))
).map(({ key }: { key: string; value: string }) =>
globalKey(page, unpackPageKey(key).key)
);
// Delete all page keys
await writeCalls.deletePrefix(ctx, pageKey(page, prefix));
await writeCalls.batchDelete(ctx, keysToDelete);
},
2022-03-28 13:25:05 +00:00
clearPageIndex: async (ctx) => {
await writeCalls.deleteAll(ctx);
},
};
return apiObj;
}