2023-08-04 19:17:36 +00:00
|
|
|
import { KVStore } from "../../plugos/lib/kv_store.ts";
|
2023-08-04 16:56:55 +00:00
|
|
|
import type { SysCallMapping } from "../../plugos/system.ts";
|
|
|
|
|
|
|
|
export type KV = {
|
|
|
|
key: string;
|
|
|
|
value: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Keyspace:
|
|
|
|
// ["index", page, key] -> value
|
|
|
|
// ["indexByKey", key, page] -> value
|
|
|
|
|
2023-08-04 19:17:36 +00:00
|
|
|
const sep = "!";
|
|
|
|
|
2023-08-04 16:56:55 +00:00
|
|
|
/**
|
|
|
|
* Implements the index syscalls using Deno's KV store.
|
|
|
|
* @param dbFile
|
|
|
|
* @returns
|
|
|
|
*/
|
2023-08-04 19:17:36 +00:00
|
|
|
export function pageIndexSyscalls(kv: KVStore): SysCallMapping {
|
2023-08-04 16:56:55 +00:00
|
|
|
const apiObj: SysCallMapping = {
|
2023-08-04 19:17:36 +00:00
|
|
|
"index.set": (_ctx, page: string, key: string, value: any) => {
|
|
|
|
return kv.batchSet(
|
|
|
|
[{
|
|
|
|
key: `index${sep}${page}${sep}${key}`,
|
|
|
|
value,
|
|
|
|
}, {
|
|
|
|
key: `indexByKey${sep}${key}${sep}${page}`,
|
|
|
|
value,
|
|
|
|
}],
|
|
|
|
);
|
2023-08-04 16:56:55 +00:00
|
|
|
},
|
|
|
|
"index.batchSet": async (_ctx, page: string, kvs: KV[]) => {
|
|
|
|
for (const { key, value } of kvs) {
|
|
|
|
await apiObj["index.set"](_ctx, page, key, value);
|
|
|
|
}
|
|
|
|
},
|
2023-08-04 19:17:36 +00:00
|
|
|
"index.delete": (_ctx, page: string, key: string) => {
|
|
|
|
console.log("delete", page, key);
|
|
|
|
return kv.batchDelete([
|
|
|
|
`index${sep}${page}${sep}${key}`,
|
|
|
|
`indexByKey${sep}${key}${sep}${page}`,
|
|
|
|
]);
|
2023-08-04 16:56:55 +00:00
|
|
|
},
|
2023-08-04 19:17:36 +00:00
|
|
|
"index.get": (_ctx, page: string, key: string) => {
|
|
|
|
return kv.get(`index${sep}${page}${sep}${key}`);
|
2023-08-04 16:56:55 +00:00
|
|
|
},
|
|
|
|
"index.queryPrefix": async (_ctx, prefix: string) => {
|
|
|
|
const results: { key: string; page: string; value: any }[] = [];
|
2023-08-04 19:17:36 +00:00
|
|
|
for (
|
|
|
|
const result of await kv.queryPrefix(`indexByKey!${prefix}`)
|
2023-08-04 16:56:55 +00:00
|
|
|
) {
|
2023-08-04 19:17:36 +00:00
|
|
|
const [_ns, key, page] = result.key.split(sep);
|
2023-08-04 16:56:55 +00:00
|
|
|
results.push({
|
2023-08-04 19:17:36 +00:00
|
|
|
key,
|
|
|
|
page,
|
2023-08-04 16:56:55 +00:00
|
|
|
value: result.value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
"index.clearPageIndexForPage": async (ctx, page: string) => {
|
|
|
|
await apiObj["index.deletePrefixForPage"](ctx, page, "");
|
|
|
|
},
|
|
|
|
"index.deletePrefixForPage": async (_ctx, page: string, prefix: string) => {
|
2023-08-04 19:17:36 +00:00
|
|
|
for (
|
|
|
|
const result of await kv.queryPrefix(
|
|
|
|
`index${sep}${page}${sep}${prefix}`,
|
|
|
|
)
|
2023-08-04 16:56:55 +00:00
|
|
|
) {
|
2023-08-04 19:17:36 +00:00
|
|
|
console.log("GOt back this key to delete", result.key);
|
|
|
|
const [_ns, page, key] = result.key.split(sep);
|
|
|
|
await apiObj["index.delete"](_ctx, page, key);
|
2023-08-04 16:56:55 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"index.clearPageIndex": async (ctx) => {
|
2023-08-04 19:17:36 +00:00
|
|
|
for (const result of await kv.queryPrefix(`index${sep}`)) {
|
|
|
|
const [_ns, page, key] = result.key.split(sep);
|
|
|
|
await apiObj["index.delete"](ctx, page, key);
|
2023-08-04 16:56:55 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return apiObj;
|
|
|
|
}
|