init ingestion
This commit is contained in:
41
ingestion/rotor/src/registry/registry.js
Normal file
41
ingestion/rotor/src/registry/registry.js
Normal file
@@ -0,0 +1,41 @@
|
||||
// Function registry -- an in-memory cache of (workspace_id, slug) -> code.
|
||||
//
|
||||
// The console writes function code into Postgres; rotor loads it lazily on
|
||||
// first miss and refreshes on pub/sub invalidation.
|
||||
//
|
||||
// For this scaffold we keep it dumb: a Map you can preload via /api/admin
|
||||
// or set directly in tests. Replace `loader` with a real PG loader when the
|
||||
// console exists.
|
||||
|
||||
export class Registry {
|
||||
/**
|
||||
* @param {{ loader?: (workspaceId: string, slug: string) => Promise<string|null> }} opts
|
||||
*/
|
||||
constructor(opts = {}) {
|
||||
this.loader = opts.loader ?? (async () => null);
|
||||
/** @type {Map<string, { code: string, version: number }>} */
|
||||
this.cache = new Map();
|
||||
}
|
||||
|
||||
key(workspaceId, slug) { return `${workspaceId}:${slug}`; }
|
||||
|
||||
async get(workspaceId, slug) {
|
||||
const k = this.key(workspaceId, slug);
|
||||
if (this.cache.has(k)) return this.cache.get(k);
|
||||
|
||||
const code = await this.loader(workspaceId, slug);
|
||||
if (code == null) return null;
|
||||
|
||||
const entry = { code, version: 1 };
|
||||
this.cache.set(k, entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
set(workspaceId, slug, code, version = 1) {
|
||||
this.cache.set(this.key(workspaceId, slug), { code, version });
|
||||
}
|
||||
|
||||
invalidate(workspaceId, slug) {
|
||||
this.cache.delete(this.key(workspaceId, slug));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user