Refactor, cleanup

This commit is contained in:
Zef Hemel
2025-06-12 20:30:01 +02:00
parent b942ebabad
commit 8d3dc4cc4e
120 changed files with 1139 additions and 1101 deletions
+2 -2
View File
@@ -5,8 +5,8 @@ import type { KvPrimitives } from "../data/kv_primitives.ts";
import { assertEquals } from "@std/assert";
import { PrefixedKvPrimitives } from "../data/prefixed_kv_primitives.ts";
import { DataStore } from "$lib/data/datastore.ts";
import { LuaEnv, LuaStackFrame } from "../../web/space_lua/runtime.ts";
import { parseExpressionString } from "../../web/space_lua/parse.ts";
import { LuaEnv, LuaStackFrame } from "../space_lua/runtime.ts";
import { parseExpressionString } from "../space_lua/parse.ts";
async function test(db: KvPrimitives) {
const datastore = new DataStore(new PrefixedKvPrimitives(db, ["ds"]));
+2 -2
View File
@@ -1,8 +1,8 @@
import {
type LuaCollectionQuery,
queryLua,
} from "../../web/space_lua/query_collection.ts";
import { LuaEnv, LuaStackFrame } from "../../web/space_lua/runtime.ts";
} from "../space_lua/query_collection.ts";
import { LuaEnv, LuaStackFrame } from "../space_lua/runtime.ts";
import type { KV, KvKey } from "../../plug-api/types.ts";
import type { KvPrimitives, KvQueryOptions } from "./kv_primitives.ts";
+4 -4
View File
@@ -75,6 +75,10 @@ export class IndexedDBKvPrimitives implements KvPrimitives {
}
}
close() {
this.db.close();
}
private buildKey(key: KvKey): string {
for (const k of key) {
if (k.includes(sep)) {
@@ -87,8 +91,4 @@ export class IndexedDBKvPrimitives implements KvPrimitives {
private extractKey(key: string): KvKey {
return key.split(sep);
}
close() {
this.db.close();
}
}
+5
View File
@@ -6,10 +6,15 @@ export type KvQueryOptions = {
export interface KvPrimitives {
batchGet(keys: KvKey[]): Promise<(any | undefined)[]>;
batchSet(entries: KV[]): Promise<void>;
batchDelete(keys: KvKey[]): Promise<void>;
query(options: KvQueryOptions): AsyncIterableIterator<KV>;
// Completely clear all data from this datastore
clear(): Promise<void>;
close(): void;
}
+34 -33
View File
@@ -28,6 +28,27 @@ export class MemoryKvPrimitives implements KvPrimitives {
}
}
}
static fromJSON(json: Record<string, any>): MemoryKvPrimitives {
const result = new MemoryKvPrimitives();
for (const key of Object.keys(json)) {
result.store.set(key, json[key]);
}
return result;
}
/**
* Create a new MemoryKvPrimitives instance from a file and initialize it
*/
static async fromFile(
filePath: string,
options: { throttleMs?: number } = {},
): Promise<MemoryKvPrimitives> {
const instance = new MemoryKvPrimitives(filePath, options);
await instance.init();
return instance;
}
clear(): Promise<void> {
this.store.clear();
return Promise.resolve();
@@ -62,23 +83,6 @@ export class MemoryKvPrimitives implements KvPrimitives {
}
}
/**
* Persist the current state to disk
*/
private async persistToDisk(): Promise<void> {
if (!this.filePath) return;
try {
const jsonData = this.toJSON();
await Deno.writeTextFile(
this.filePath,
JSON.stringify(jsonData, null, 2),
);
} catch (error) {
console.error(`Failed to persist KV store to ${this.filePath}:`, error);
}
}
batchGet(keys: KvKey[]): Promise<any[]> {
return Promise.resolve(
keys.map((key) => this.store.get(key.join(memoryKeySeparator))),
@@ -125,14 +129,6 @@ export class MemoryKvPrimitives implements KvPrimitives {
return result;
}
static fromJSON(json: Record<string, any>): MemoryKvPrimitives {
const result = new MemoryKvPrimitives();
for (const key of Object.keys(json)) {
result.store.set(key, json[key]);
}
return result;
}
async *query(options: KvQueryOptions): AsyncIterableIterator<KV> {
const prefix = options.prefix?.join(memoryKeySeparator);
const sortedKeys = [...this.store.keys()].sort();
@@ -155,14 +151,19 @@ export class MemoryKvPrimitives implements KvPrimitives {
}
/**
* Create a new MemoryKvPrimitives instance from a file and initialize it
* Persist the current state to disk
*/
static async fromFile(
filePath: string,
options: { throttleMs?: number } = {},
): Promise<MemoryKvPrimitives> {
const instance = new MemoryKvPrimitives(filePath, options);
await instance.init();
return instance;
private async persistToDisk(): Promise<void> {
if (!this.filePath) return;
try {
const jsonData = this.toJSON();
await Deno.writeTextFile(
this.filePath,
JSON.stringify(jsonData, null, 2),
);
} catch (error) {
console.error(`Failed to persist KV store to ${this.filePath}:`, error);
}
}
}
+4 -5
View File
@@ -6,8 +6,8 @@ import type {
MQSubscribeOptions,
} from "../../plug-api/types.ts";
import type { DataStore } from "./datastore.ts";
import { parseExpressionString } from "../../web/space_lua/parse.ts";
import { LuaEnv } from "../../web/space_lua/runtime.ts";
import { parseExpressionString } from "../space_lua/parse.ts";
import { LuaEnv } from "../space_lua/runtime.ts";
export type ProcessingMessage = MQMessage & {
ts: number;
@@ -20,15 +20,14 @@ const dlqPrefix = ["mq", "dlq"];
export class DataStoreMQ {
// queue -> set of run() functions
localSubscriptions = new Map<string, Set<() => void>>();
// Internal sequencer for messages, only really necessary when batch sending tons of messages within a millisecond
seq = 0;
constructor(
private ds: DataStore,
) {
}
// Internal sequencer for messages, only really necessary when batch sending tons of messages within a millisecond
seq = 0;
async batchSend(queue: string, bodies: any[]): Promise<void> {
if (bodies.length === 0) {
return;
+1
View File
@@ -35,6 +35,7 @@ export class PrefixedKvPrimitives implements KvPrimitives {
yield { key: this.stripPrefix(result.key), value: result.value };
}
}
close(): void {
this.wrapped.close();
}