Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions packages/loopover-miner/lib/policy-doc-cache.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { normalizeLocalStoreDbPath, openLocalStoreDb, resolveLocalStoreDbPath } from "./local-store.js";
import { normalizeLocalStoreDbPath, openLocalStoreAdapter, resolveLocalStoreDbPath } from "./local-store.js";
import { applySchemaMigrations } from "./schema-version.js";

// Local ETag cache for discovery's small policy-doc fetches (#4842). `discover` refetches each target repo's
Expand Down Expand Up @@ -30,10 +30,14 @@ function normalizeUrl(url) {
/**
* Opens the 100% local/client-side miner policy-doc ETag cache. The database only lives on this machine; this
* module never uploads, syncs, or phones home with its contents. (#4842)
*
* Opened through the #7175 SqliteDriver seam (`openLocalStoreAdapter`): CRUD goes through `driver.query`,
* while schema creation/migrations still use the underlying DatabaseSync until those helpers are migrated.
* Public API stays synchronous so callers need no async cascade in this part-1 slice.
*/
export function initPolicyDocCacheStore(dbPath = resolvePolicyDocCacheDbPath()) {
const resolvedPath = normalizeDbPath(dbPath);
const db = openLocalStoreDb(resolvedPath);
const { db, driver } = openLocalStoreAdapter(resolvedPath);
db.exec(`
CREATE TABLE IF NOT EXISTS policy_doc_cache (
url TEXT PRIMARY KEY,
Expand All @@ -45,22 +49,23 @@ export function initPolicyDocCacheStore(dbPath = resolvePolicyDocCacheDbPath())
// Schema-version convention (#4832): stamp the baseline and run any post-baseline migrations (none yet).
applySchemaMigrations(db, []);

const getStatement = db.prepare("SELECT etag, content FROM policy_doc_cache WHERE url = ?");
const putStatement = db.prepare(`
const getSql = "SELECT etag, content FROM policy_doc_cache WHERE url = ?";
const putSql = `
INSERT INTO policy_doc_cache (url, etag, content, updated_at)
VALUES (?, ?, ?, ?)
ON CONFLICT(url) DO UPDATE SET
etag = excluded.etag,
content = excluded.content,
updated_at = excluded.updated_at
`);
`;

return {
dbPath: resolvedPath,
/** The last-known `{ etag, content }` for a policy-doc URL, or null when it has never been cached. Both columns
* are `TEXT NOT NULL`, so a present row always carries string values. */
get(url) {
const row = getStatement.get(normalizeUrl(url));
const { rows } = driver.query(getSql, [normalizeUrl(url)]);
const row = rows[0];
return row ? { etag: row.etag, content: row.content } : null;
},
/** Record the fresh ETag + body so the next run can revalidate it with a conditional GET. */
Expand All @@ -69,7 +74,7 @@ export function initPolicyDocCacheStore(dbPath = resolvePolicyDocCacheDbPath())
if (typeof etag !== "string" || !etag.trim()) throw new Error("invalid_policy_doc_etag");
if (typeof content !== "string") throw new Error("invalid_policy_doc_content");
const updatedAt = new Date().toISOString();
putStatement.run(normalizedUrl, etag, content, updatedAt);
driver.query(putSql, [normalizedUrl, etag, content, updatedAt]);
return { url: normalizedUrl, etag, content, updatedAt };
},
close() {
Expand Down