2023-10-03 12:16:33 +00:00
|
|
|
import { datastore } from "$sb/syscalls.ts";
|
2023-10-03 13:24:07 +00:00
|
|
|
import { KV, KvKey, KvQuery, ObjectQuery, ObjectValue } from "$sb/types.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
import { QueryProviderEvent } from "$sb/app_event.ts";
|
|
|
|
import { builtins } from "./builtins.ts";
|
|
|
|
import { AttributeObject, determineType } from "./attributes.ts";
|
2024-01-15 15:43:12 +00:00
|
|
|
import { ttlCache } from "$sb/lib/memory_cache.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
|
|
|
|
const indexKey = "idx";
|
|
|
|
const pageKey = "ridx";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Key namespace:
|
|
|
|
* [indexKey, type, ...key, page] -> value
|
|
|
|
* [pageKey, page, ...key] -> true // for fast page clearing
|
|
|
|
* ["type", type] -> true // for fast type listing
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function batchSet(page: string, kvs: KV[]): Promise<void> {
|
|
|
|
const finalBatch: KV[] = [];
|
|
|
|
for (const { key, value } of kvs) {
|
|
|
|
finalBatch.push({
|
|
|
|
key: [indexKey, ...key, page],
|
|
|
|
value,
|
|
|
|
}, {
|
|
|
|
key: [pageKey, page, ...key],
|
|
|
|
value: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return datastore.batchSet(finalBatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears all keys for a given page
|
|
|
|
* @param page
|
|
|
|
*/
|
|
|
|
export async function clearPageIndex(page: string): Promise<void> {
|
|
|
|
const allKeys: KvKey[] = [];
|
|
|
|
for (
|
|
|
|
const { key } of await datastore.query({
|
|
|
|
prefix: [pageKey, page],
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
allKeys.push(key);
|
|
|
|
allKeys.push([indexKey, ...key.slice(2), page]);
|
|
|
|
}
|
|
|
|
await datastore.batchDel(allKeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the entire datastore for this indexKey plug
|
|
|
|
*/
|
|
|
|
export async function clearIndex(): Promise<void> {
|
|
|
|
const allKeys: KvKey[] = [];
|
|
|
|
for (
|
|
|
|
const { key } of await datastore.query({ prefix: [] })
|
|
|
|
) {
|
|
|
|
allKeys.push(key);
|
|
|
|
}
|
|
|
|
await datastore.batchDel(allKeys);
|
|
|
|
console.log("Deleted", allKeys.length, "keys from the index");
|
|
|
|
}
|
|
|
|
|
|
|
|
// ENTITIES API
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indexes entities in the data store
|
|
|
|
*/
|
2024-01-13 16:30:15 +00:00
|
|
|
export function indexObjects<T>(
|
2023-10-03 12:16:33 +00:00
|
|
|
page: string,
|
|
|
|
objects: ObjectValue<T>[],
|
|
|
|
): Promise<void> {
|
|
|
|
const kvs: KV<T>[] = [];
|
|
|
|
const allAttributes = new Map<string, string>(); // tag:name -> attributeType
|
|
|
|
for (const obj of objects) {
|
2024-01-11 12:20:50 +00:00
|
|
|
if (!obj.tag) {
|
|
|
|
console.error("Object has no tag", obj, "this shouldn't happen");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Index as all the tag + any additional tags specified
|
|
|
|
const allTags = [obj.tag, ...obj.tags || []];
|
|
|
|
for (const tag of allTags) {
|
2023-12-21 17:37:50 +00:00
|
|
|
// The object itself
|
2023-10-03 12:16:33 +00:00
|
|
|
kvs.push({
|
|
|
|
key: [tag, cleanKey(obj.ref, page)],
|
|
|
|
value: obj,
|
|
|
|
});
|
|
|
|
// Index attributes
|
2023-10-09 18:39:03 +00:00
|
|
|
const builtinAttributes = builtins[tag];
|
|
|
|
if (!builtinAttributes) {
|
2023-12-21 17:37:50 +00:00
|
|
|
// This is not a builtin tag, so we index all attributes (almost, see below)
|
|
|
|
attributeLabel: for (
|
2023-10-03 12:16:33 +00:00
|
|
|
const [attrName, attrValue] of Object.entries(
|
|
|
|
obj as Record<string, any>,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
if (attrName.startsWith("$")) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-12-21 17:37:50 +00:00
|
|
|
// Check for all tags attached to this object if they're builtins
|
|
|
|
// If so: if `attrName` is defined in the builtin, use the attributeType from there (mostly to preserve readOnly aspects)
|
2024-01-11 12:20:50 +00:00
|
|
|
for (const otherTag of allTags) {
|
2023-12-21 17:37:50 +00:00
|
|
|
const builtinAttributes = builtins[otherTag];
|
|
|
|
if (builtinAttributes && builtinAttributes[attrName]) {
|
|
|
|
allAttributes.set(
|
|
|
|
`${tag}:${attrName}`,
|
|
|
|
builtinAttributes[attrName],
|
|
|
|
);
|
|
|
|
continue attributeLabel;
|
|
|
|
}
|
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
allAttributes.set(`${tag}:${attrName}`, determineType(attrValue));
|
|
|
|
}
|
2023-10-09 18:39:03 +00:00
|
|
|
} else if (tag !== "attribute") {
|
|
|
|
// For builtin tags, only index custom ones
|
|
|
|
for (
|
|
|
|
const [attrName, attrValue] of Object.entries(
|
|
|
|
obj as Record<string, any>,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// console.log("Indexing", tag, attrName, attrValue);
|
|
|
|
// Skip builtins and internal attributes
|
|
|
|
if (builtinAttributes[attrName] || attrName.startsWith("$")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
allAttributes.set(`${tag}:${attrName}`, determineType(attrValue));
|
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (allAttributes.size > 0) {
|
2024-01-13 16:30:15 +00:00
|
|
|
[...allAttributes].forEach(([key, value]) => {
|
|
|
|
const [tagName, name] = key.split(":");
|
|
|
|
const attributeType = value.startsWith("!") ? value.substring(1) : value;
|
|
|
|
kvs.push({
|
|
|
|
key: ["attribute", cleanKey(key, page)],
|
|
|
|
value: {
|
2023-10-03 12:16:33 +00:00
|
|
|
ref: key,
|
2024-01-11 12:20:50 +00:00
|
|
|
tag: "attribute",
|
|
|
|
tagName,
|
2023-10-03 12:16:33 +00:00
|
|
|
name,
|
2023-12-21 17:37:50 +00:00
|
|
|
attributeType,
|
|
|
|
readOnly: value.startsWith("!"),
|
2023-10-03 12:16:33 +00:00
|
|
|
page,
|
2024-01-13 16:30:15 +00:00
|
|
|
} as T,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (kvs.length > 0) {
|
|
|
|
return batchSet(page, kvs);
|
|
|
|
} else {
|
|
|
|
return Promise.resolve();
|
2023-10-03 12:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanKey(ref: string, page: string) {
|
|
|
|
if (ref.startsWith(`${page}@`)) {
|
|
|
|
return ref.substring(page.length + 1);
|
|
|
|
} else {
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 15:43:12 +00:00
|
|
|
export function queryObjects<T>(
|
2023-10-03 12:16:33 +00:00
|
|
|
tag: string,
|
|
|
|
query: ObjectQuery,
|
2024-01-15 15:43:12 +00:00
|
|
|
ttlSecs?: number,
|
2023-10-03 12:16:33 +00:00
|
|
|
): Promise<ObjectValue<T>[]> {
|
2024-01-15 15:43:12 +00:00
|
|
|
return ttlCache(query, async () => {
|
|
|
|
return (await datastore.query({
|
|
|
|
...query,
|
|
|
|
prefix: [indexKey, tag],
|
|
|
|
distinct: true,
|
|
|
|
})).map(({ value }) => value);
|
|
|
|
}, ttlSecs);
|
2023-10-03 12:16:33 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 13:24:07 +00:00
|
|
|
export async function query(
|
|
|
|
query: KvQuery,
|
|
|
|
): Promise<KV[]> {
|
|
|
|
return (await datastore.query({
|
|
|
|
...query,
|
|
|
|
prefix: [indexKey, ...query.prefix ? query.prefix : []],
|
|
|
|
})).map(({ key, value }) => ({ key: key.slice(1), value }));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getObjectByRef<T>(
|
2023-10-03 12:16:33 +00:00
|
|
|
page: string,
|
|
|
|
tag: string,
|
|
|
|
ref: string,
|
|
|
|
): Promise<ObjectValue<T> | undefined> {
|
2023-10-03 13:24:07 +00:00
|
|
|
return datastore.get([indexKey, tag, cleanKey(ref, page), page]);
|
2023-10-03 12:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function objectSourceProvider({
|
|
|
|
query,
|
|
|
|
}: QueryProviderEvent): Promise<any[]> {
|
|
|
|
const tag = query.querySource!;
|
|
|
|
const results = await datastore.query({
|
|
|
|
...query,
|
|
|
|
prefix: [indexKey, tag],
|
2023-10-03 13:24:07 +00:00
|
|
|
distinct: true,
|
2023-10-03 12:16:33 +00:00
|
|
|
});
|
|
|
|
return results.map((r) => r.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function discoverSources() {
|
2023-12-22 10:27:07 +00:00
|
|
|
return (await datastore.query({
|
|
|
|
prefix: [indexKey, "tag"],
|
|
|
|
select: [{ name: "name" }],
|
|
|
|
distinct: true,
|
|
|
|
})).map((
|
2023-10-13 14:33:37 +00:00
|
|
|
{ value },
|
|
|
|
) => value.name);
|
2023-10-03 12:16:33 +00:00
|
|
|
}
|