From 474c52aec15e16664b7f0cd03b8eb39072355e37 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Sun, 6 Apr 2025 17:39:29 -0700 Subject: [PATCH] truncate prefixes --- .../autocomplete/util/AutocompleteLruCache.ts | 42 ++++++++++++------- core/indexing/refreshIndex.ts | 19 +++++---- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/core/autocomplete/util/AutocompleteLruCache.ts b/core/autocomplete/util/AutocompleteLruCache.ts index a93b23c5a..adac906ff 100644 --- a/core/autocomplete/util/AutocompleteLruCache.ts +++ b/core/autocomplete/util/AutocompleteLruCache.ts @@ -39,20 +39,28 @@ export class AutocompleteLruCache { // If the query is "co" and we have "c" -> "ontinue" in the cache, // we should return "ntinue" as the completion. // Have to make sure we take the key with shortest length - const result = await this.db.get( - "SELECT key, value FROM cache WHERE ? LIKE key || '%' ORDER BY LENGTH(key) DESC LIMIT 1", - truncateSqliteLikePattern(prefix), - ); - - // Validate that the cached compeltion is a valid completion for the prefix - if (result && result.value.startsWith(prefix.slice(result.key.length))) { - await this.db.run( - "UPDATE cache SET timestamp = ? WHERE key = ?", - Date.now(), - prefix, + const truncatedPrefix = truncateSqliteLikePattern(prefix); + try { + const result = await this.db.get( + "SELECT key, value FROM cache WHERE ? LIKE key || '%' ORDER BY LENGTH(key) DESC LIMIT 1", + truncatedPrefix, ); - // And then truncate so we aren't writing something that's already there - return result.value.slice(prefix.length - result.key.length); + // Validate that the cached completion is a valid completion for the prefix + if ( + result && + result.value.startsWith(truncatedPrefix.slice(result.key.length)) + ) { + await this.db.run( + "UPDATE cache SET timestamp = ? WHERE key = ?", + Date.now(), + truncatedPrefix, + ); + // And then truncate so we aren't writing something that's already there + return result.value.slice(truncatedPrefix.length - result.key.length); + } + } catch (e) { + // catches e.g. old SQLITE LIKE OR GLOB PATTERN TOO COMPLEX + console.error(e); } return undefined; @@ -60,13 +68,15 @@ export class AutocompleteLruCache { async put(prefix: string, completion: string) { const release = await this.mutex.acquire(); + + const truncatedPrefix = truncateSqliteLikePattern(prefix); try { await this.db.run("BEGIN TRANSACTION"); try { const result = await this.db.get( "SELECT key FROM cache WHERE key = ?", - prefix, + truncatedPrefix, ); if (result) { @@ -74,7 +84,7 @@ export class AutocompleteLruCache { "UPDATE cache SET value = ?, timestamp = ? WHERE key = ?", completion, Date.now(), - prefix, + truncatedPrefix, ); } else { const count = await this.db.get( @@ -89,7 +99,7 @@ export class AutocompleteLruCache { await this.db.run( "INSERT INTO cache (key, value, timestamp) VALUES (?, ?, ?)", - prefix, + truncatedPrefix, completion, Date.now(), ); diff --git a/core/indexing/refreshIndex.ts b/core/indexing/refreshIndex.ts index ef21149b1..f76c6a7f6 100644 --- a/core/indexing/refreshIndex.ts +++ b/core/indexing/refreshIndex.ts @@ -158,18 +158,21 @@ async function getAddRemoveForTag( const updateLastUpdated: PathAndCacheKey[] = []; // First, group items by path and find latest timestamp for each - const pathGroups = new Map - }>(); + const pathGroups = new Map< + string, + { + latest: { lastUpdated: number; cacheKey: string }; + allVersions: Array<{ cacheKey: string }>; + } + >(); for (const item of saved) { const { lastUpdated, path, cacheKey } = item; - + if (!pathGroups.has(path)) { pathGroups.set(path, { latest: { lastUpdated, cacheKey }, - allVersions: [{ cacheKey }] + allVersions: [{ cacheKey }], }); } else { const group = pathGroups.get(path)!; @@ -530,6 +533,6 @@ export function truncateToLastNBytes(input: string, maxBytes: number): string { return input.substring(startIndex, input.length); } -export function truncateSqliteLikePattern(input: string) { - return truncateToLastNBytes(input, SQLITE_MAX_LIKE_PATTERN_LENGTH); +export function truncateSqliteLikePattern(input: string, safety: number = 100) { + return truncateToLastNBytes(input, SQLITE_MAX_LIKE_PATTERN_LENGTH - safety); }