More dead code elimination
This commit is contained in:
@@ -3,13 +3,12 @@ import { IndexedDBKvPrimitives } from "./indexeddb_kv_primitives.ts";
|
||||
import { MemoryKvPrimitives } from "./memory_kv_primitives.ts";
|
||||
import type { KvPrimitives } from "./kv_primitives.ts";
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { PrefixedKvPrimitives } from "./prefixed_kv_primitives.ts";
|
||||
import { DataStore } from "./datastore.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"]));
|
||||
const datastore = new DataStore(db);
|
||||
await datastore.set(["user", "peter"], { name: "Peter" });
|
||||
await datastore.set(["user", "hank"], { name: "Hank" });
|
||||
const env = new LuaEnv();
|
||||
|
||||
@@ -2,7 +2,6 @@ import { DataStoreMQ } from "./mq.datastore.ts";
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { MemoryKvPrimitives } from "./memory_kv_primitives.ts";
|
||||
import { DataStore } from "./datastore.ts";
|
||||
import { PrefixedKvPrimitives } from "./prefixed_kv_primitives.ts";
|
||||
import { FakeTime } from "@std/testing/time";
|
||||
|
||||
import type { MQMessage } from "../../plug-api/types/datastore.ts";
|
||||
@@ -13,7 +12,7 @@ Deno.test("DataStore MQ", async () => {
|
||||
|
||||
try {
|
||||
const mq = new DataStoreMQ(
|
||||
new DataStore(new PrefixedKvPrimitives(db, ["mq"])),
|
||||
new DataStore(db),
|
||||
);
|
||||
|
||||
let messages: MQMessage[];
|
||||
@@ -81,7 +80,7 @@ Deno.test("DataStore MQ - Scale test with multiple subscribers", async () => {
|
||||
|
||||
try {
|
||||
const mq = new DataStoreMQ(
|
||||
new DataStore(new PrefixedKvPrimitives(db, ["mq"])),
|
||||
new DataStore(db),
|
||||
);
|
||||
|
||||
const queueName = "scale-test";
|
||||
|
||||
@@ -84,6 +84,9 @@ export class QueueWorker {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic message queue implementation on top of a DataStore
|
||||
*/
|
||||
export class DataStoreMQ {
|
||||
// Internal sequencer for messages, only really necessary when batch sending tons of messages within a millisecond
|
||||
seq = 0;
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
import type { KvPrimitives, KvQueryOptions } from "./kv_primitives.ts";
|
||||
|
||||
import type { KV, KvKey } from "../../plug-api/types/datastore.ts";
|
||||
|
||||
/**
|
||||
* Turns any KvPrimitives into a KvPrimitives that automatically prefixes all keys (and removes them again when reading)
|
||||
*/
|
||||
export class PrefixedKvPrimitives implements KvPrimitives {
|
||||
constructor(private wrapped: KvPrimitives, private prefix: KvKey) {
|
||||
}
|
||||
|
||||
clear(): Promise<void> {
|
||||
return this.wrapped.clear();
|
||||
}
|
||||
|
||||
batchGet(keys: KvKey[]): Promise<any[]> {
|
||||
return this.wrapped.batchGet(keys.map((key) => this.applyPrefix(key)));
|
||||
}
|
||||
|
||||
batchSet(entries: KV[]): Promise<void> {
|
||||
return this.wrapped.batchSet(
|
||||
entries.map(({ key, value }) => ({ key: this.applyPrefix(key), value })),
|
||||
);
|
||||
}
|
||||
|
||||
batchDelete(keys: KvKey[]): Promise<void> {
|
||||
return this.wrapped.batchDelete(keys.map((key) => this.applyPrefix(key)));
|
||||
}
|
||||
|
||||
async *query(options: KvQueryOptions): AsyncIterableIterator<KV> {
|
||||
for await (
|
||||
const result of this.wrapped.query({
|
||||
prefix: this.applyPrefix(options.prefix),
|
||||
})
|
||||
) {
|
||||
yield { key: this.stripPrefix(result.key), value: result.value };
|
||||
}
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.wrapped.close();
|
||||
}
|
||||
|
||||
private applyPrefix(key?: KvKey): KvKey {
|
||||
return [...this.prefix, ...(key ? key : [])];
|
||||
}
|
||||
|
||||
private stripPrefix(key: KvKey): KvKey {
|
||||
return key.slice(this.prefix.length);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user