Backporting a bunch of optimizations from db-only branch
This commit is contained in:
@@ -7,7 +7,7 @@ import { assertEquals } from "https://deno.land/std@0.165.0/testing/asserts.ts";
|
||||
import { PrefixedKvPrimitives } from "./prefixed_kv_primitives.ts";
|
||||
|
||||
async function test(db: KvPrimitives) {
|
||||
const datastore = new DataStore(new PrefixedKvPrimitives(db, ["ds"]), {
|
||||
const datastore = new DataStore(new PrefixedKvPrimitives(db, ["ds"]), false, {
|
||||
count: (arr: any[]) => arr.length,
|
||||
});
|
||||
await datastore.set(["user", "peter"], { name: "Peter" });
|
||||
|
||||
+25
-1
@@ -2,14 +2,18 @@ import { applyQueryNoFilterKV, evalQueryExpression } from "$sb/lib/query.ts";
|
||||
import { FunctionMap, KV, KvKey, KvQuery } from "$sb/types.ts";
|
||||
import { builtinFunctions } from "$sb/lib/builtin_query_functions.ts";
|
||||
import { KvPrimitives } from "./kv_primitives.ts";
|
||||
import { LimitedMap } from "../../common/limited_map.ts";
|
||||
|
||||
/**
|
||||
* This is the data store class you'll actually want to use, wrapping the primitives
|
||||
* in a more user-friendly way
|
||||
*/
|
||||
export class DataStore {
|
||||
private cache = new LimitedMap<any>(20);
|
||||
|
||||
constructor(
|
||||
readonly kv: KvPrimitives,
|
||||
private enableCache = false,
|
||||
private functionMap: FunctionMap = builtinFunctions,
|
||||
) {
|
||||
}
|
||||
@@ -50,6 +54,21 @@ export class DataStore {
|
||||
}
|
||||
|
||||
async query<T = any>(query: KvQuery): Promise<KV<T>[]> {
|
||||
let cacheKey: string | undefined;
|
||||
const cacheSecs = query.cacheSecs;
|
||||
// Should we do caching?
|
||||
if (cacheSecs && this.enableCache) {
|
||||
// Remove the cacheSecs from the query
|
||||
query = { ...query, cacheSecs: undefined };
|
||||
console.log("Going to cache query", query);
|
||||
cacheKey = JSON.stringify(query);
|
||||
const cachedResult = this.cache.get(cacheKey);
|
||||
if (cachedResult) {
|
||||
// Let's use the cached result
|
||||
return cachedResult;
|
||||
}
|
||||
}
|
||||
|
||||
const results: KV<T>[] = [];
|
||||
let itemCount = 0;
|
||||
// Accumulate results
|
||||
@@ -76,7 +95,12 @@ export class DataStore {
|
||||
}
|
||||
}
|
||||
// Apply order by, limit, and select
|
||||
return applyQueryNoFilterKV(query, results, this.functionMap);
|
||||
const finalResult = applyQueryNoFilterKV(query, results, this.functionMap);
|
||||
if (cacheKey) {
|
||||
// Store in the cache
|
||||
this.cache.set(cacheKey, finalResult, cacheSecs! * 1000);
|
||||
}
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
async queryDelete(query: KvQuery): Promise<void> {
|
||||
|
||||
@@ -23,6 +23,9 @@ export class DataStoreMQ implements MessageQueue {
|
||||
seq = 0;
|
||||
|
||||
async batchSend(queue: string, bodies: any[]): Promise<void> {
|
||||
if (bodies.length === 0) {
|
||||
return;
|
||||
}
|
||||
const messages: KV<MQMessage>[] = bodies.map((body) => {
|
||||
const id = `${Date.now()}-${String(++this.seq).padStart(6, "0")}`;
|
||||
const key = [...queuedPrefix, queue, id];
|
||||
@@ -54,6 +57,9 @@ export class DataStoreMQ implements MessageQueue {
|
||||
prefix: [...queuedPrefix, queue],
|
||||
limit: ["number", maxItems],
|
||||
});
|
||||
if (messages.length === 0) {
|
||||
return [];
|
||||
}
|
||||
// Put them in the processing queue
|
||||
await this.ds.batchSet(
|
||||
messages.map((m) => ({
|
||||
@@ -137,6 +143,9 @@ export class DataStoreMQ implements MessageQueue {
|
||||
}
|
||||
|
||||
async batchAck(queue: string, ids: string[]) {
|
||||
if (ids.length === 0) {
|
||||
return;
|
||||
}
|
||||
await this.ds.batchDelete(
|
||||
ids.map((id) => [...processingPrefix, queue, id]),
|
||||
);
|
||||
@@ -152,6 +161,9 @@ export class DataStoreMQ implements MessageQueue {
|
||||
prefix: processingPrefix,
|
||||
filter: ["<", ["attr", "ts"], ["number", now - timeout]],
|
||||
});
|
||||
if (messages.length === 0) {
|
||||
return;
|
||||
}
|
||||
await this.ds.batchDelete(messages.map((m) => m.key));
|
||||
const newMessages: KV<ProcessingMessage>[] = [];
|
||||
for (const { value: m } of messages) {
|
||||
|
||||
Reference in New Issue
Block a user