Implement CLI store using Deno store

This commit is contained in:
Zef Hemel
2023-08-04 21:17:36 +02:00
parent ba8625cbf1
commit bc561eb723
5 changed files with 69 additions and 82 deletions
+6 -2
View File
@@ -1,9 +1,12 @@
import { DenoKVStore } from "../../plugos/lib/kv_store.deno_kv.ts";
import { assertEquals } from "../../test_deps.ts";
import { pageIndexSyscalls } from "./index.ts";
Deno.test("Test KV index", async () => {
const ctx: any = {};
const calls = pageIndexSyscalls();
const kv = new DenoKVStore();
await kv.init("test.db");
const calls = pageIndexSyscalls(kv);
await calls["index.set"](ctx, "page", "test", "value");
assertEquals(await calls["index.get"](ctx, "page", "test"), "value");
await calls["index.delete"](ctx, "page", "test");
@@ -24,11 +27,12 @@ Deno.test("Test KV index", async () => {
}, { key: "random", value: "value3" }]);
let results = await calls["index.queryPrefix"](ctx, "attr:");
assertEquals(results.length, 4);
console.log("here");
await calls["index.clearPageIndexForPage"](ctx, "page");
results = await calls["index.queryPrefix"](ctx, "attr:");
assertEquals(results.length, 2);
await calls["index.clearPageIndex"](ctx);
results = await calls["index.queryPrefix"](ctx, "");
assertEquals(results.length, 0);
await calls["index.close"](ctx);
await kv.delete();
});
+37 -56
View File
@@ -1,13 +1,6 @@
/// <reference lib="deno.unstable" />
import { KVStore } from "../../plugos/lib/kv_store.ts";
import type { SysCallMapping } from "../../plugos/system.ts";
type Item = {
page: string;
key: string;
value: any;
};
export type KV = {
key: string;
value: any;
@@ -17,57 +10,50 @@ export type KV = {
// ["index", page, key] -> value
// ["indexByKey", key, page] -> value
const sep = "!";
/**
* Implements the index syscalls using Deno's KV store.
* @param dbFile
* @returns
*/
export function pageIndexSyscalls(dbFile?: string): SysCallMapping {
const kv = Deno.openKv(dbFile);
export function pageIndexSyscalls(kv: KVStore): SysCallMapping {
const apiObj: SysCallMapping = {
"index.set": async (_ctx, page: string, key: string, value: any) => {
const res = await (await kv).atomic()
.set(["index", page, key], value)
.set(["indexByKey", key, page], value)
.commit();
if (!res.ok) {
throw res;
}
"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,
}],
);
},
"index.batchSet": async (_ctx, page: string, kvs: KV[]) => {
// await items.bulkPut(kvs);
for (const { key, value } of kvs) {
await apiObj["index.set"](_ctx, page, key, value);
}
},
"index.delete": async (_ctx, page: string, key: string) => {
const res = await (await kv).atomic()
.delete(["index", page, key])
.delete(["indexByKey", key, page])
.commit();
if (!res.ok) {
throw res;
}
"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}`,
]);
},
"index.get": async (_ctx, page: string, key: string) => {
return (await (await kv).get(["index", page, key])).value;
"index.get": (_ctx, page: string, key: string) => {
return kv.get(`index${sep}${page}${sep}${key}`);
},
"index.queryPrefix": async (_ctx, prefix: string) => {
const results: { key: string; page: string; value: any }[] = [];
for await (
const result of (await kv).list({
start: ["indexByKey", prefix],
end: [
"indexByKey",
prefix.slice(0, -1) +
// This is a hack to get the next character in the ASCII table (e.g. "a" -> "b")
String.fromCharCode(prefix.charCodeAt(prefix.length - 1) + 1),
],
})
for (
const result of await kv.queryPrefix(`indexByKey!${prefix}`)
) {
const [_ns, key, page] = result.key.split(sep);
results.push({
key: result.key[1] as string,
page: result.key[2] as string,
key,
page,
value: result.value,
});
}
@@ -77,27 +63,22 @@ export function pageIndexSyscalls(dbFile?: string): SysCallMapping {
await apiObj["index.deletePrefixForPage"](ctx, page, "");
},
"index.deletePrefixForPage": async (_ctx, page: string, prefix: string) => {
for await (
const result of (await kv).list({
start: ["index", page, prefix],
end: ["index", page, prefix + "~"],
})
for (
const result of await kv.queryPrefix(
`index${sep}${page}${sep}${prefix}`,
)
) {
await apiObj["index.delete"](_ctx, page, result.key[2]);
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);
}
},
"index.clearPageIndex": async (ctx) => {
for await (
const result of (await kv).list({
prefix: ["index"],
})
) {
await apiObj["index.delete"](ctx, result.key[1], result.key[2]);
for (const result of await kv.queryPrefix(`index${sep}`)) {
const [_ns, page, key] = result.key.split(sep);
await apiObj["index.delete"](ctx, page, key);
}
},
"index.close": async () => {
(await kv).close();
},
};
return apiObj;
}