2022-04-05 15:02:17 +00:00
|
|
|
import { Knex } from "knex";
|
|
|
|
import { SysCallMapping } from "../../plugos/system";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-04-05 15:02:17 +00:00
|
|
|
import { ensureTable, storeSyscalls } from "../../plugos/syscalls/store.knex_node";
|
2022-03-28 13:25:05 +00:00
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
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 {
|
2022-04-03 16:42:12 +00:00
|
|
|
const storeCalls = storeSyscalls(db, "page_index");
|
2022-03-25 11:03:06 +00:00
|
|
|
const apiObj: SysCallMapping = {
|
2022-04-03 16:42:12 +00:00
|
|
|
"index.set": async (ctx, page: string, key: string, value: any) => {
|
|
|
|
await storeCalls["store.set"](ctx, pageKey(page, key), value);
|
|
|
|
await storeCalls["store.set"](ctx, globalKey(page, key), value);
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"index.batchSet": async (ctx, page: string, kvs: KV[]) => {
|
2022-03-20 08:56:28 +00:00
|
|
|
for (let { key, value } of kvs) {
|
2022-04-03 16:42:12 +00:00
|
|
|
await apiObj["index.set"](ctx, page, key, value);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"index.delete": async (ctx, page: string, key: string) => {
|
|
|
|
await storeCalls["store.delete"](ctx, pageKey(page, key));
|
|
|
|
await storeCalls["store.delete"](ctx, globalKey(page, key));
|
2022-03-28 13:25:05 +00:00
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"index.get": async (ctx, page: string, key: string) => {
|
|
|
|
return storeCalls["store.get"](ctx, pageKey(page, key));
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"index.scanPrefixForPage": async (ctx, page: string, prefix: string) => {
|
|
|
|
return (
|
|
|
|
await storeCalls["store.queryPrefix"](ctx, pageKey(page, prefix))
|
|
|
|
).map(({ key, value }: { key: string; value: any }) => {
|
|
|
|
const { key: pageKey } = unpackPageKey(key);
|
|
|
|
return {
|
|
|
|
page,
|
|
|
|
key: pageKey,
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
});
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"index.scanPrefixGlobal": async (ctx, prefix: string) => {
|
|
|
|
return (await storeCalls["store.queryPrefix"](ctx, `k~${prefix}`)).map(
|
2022-03-28 13:25:05 +00:00
|
|
|
({ key, value }: { key: string; value: any }) => {
|
|
|
|
const { page, key: pageKey } = unpackGlobalKey(key);
|
|
|
|
return {
|
|
|
|
page,
|
|
|
|
key: pageKey,
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"index.clearPageIndexForPage": async (ctx, page: string) => {
|
|
|
|
await apiObj["index.deletePrefixForPage"](ctx, page, "");
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"index.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 = (
|
2022-04-03 16:42:12 +00:00
|
|
|
await storeCalls["store.queryPrefix"](ctx, pageKey(page, prefix))
|
2022-03-28 13:25:05 +00:00
|
|
|
).map(({ key }: { key: string; value: string }) =>
|
|
|
|
globalKey(page, unpackPageKey(key).key)
|
|
|
|
);
|
|
|
|
// Delete all page keys
|
2022-04-03 16:42:12 +00:00
|
|
|
await storeCalls["store.deletePrefix"](ctx, pageKey(page, prefix));
|
|
|
|
await storeCalls["store.batchDelete"](ctx, keysToDelete);
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"index.clearPageIndex": async (ctx) => {
|
|
|
|
await storeCalls["store.deleteAll"](ctx);
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
return apiObj;
|
|
|
|
}
|