From 290a30039b6644f669d46eaf5d61d2ed898ccc89 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Sat, 26 Oct 2024 23:32:55 -0700 Subject: [PATCH 1/4] truncate like pattern to 50000 bytes --- core/autocomplete/cache.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/core/autocomplete/cache.ts b/core/autocomplete/cache.ts index 4886feb23..8f126e409 100644 --- a/core/autocomplete/cache.ts +++ b/core/autocomplete/cache.ts @@ -4,11 +4,19 @@ import sqlite3 from "sqlite3"; import { DatabaseConnection } from "../indexing/refreshIndex.js"; import { getTabAutocompleteCacheSqlitePath } from "../util/paths.js"; +const SQLITE_MAX_LIKE_PATTERN_LENGTH = 50000; + +function truncateToBytes(input: string, maxBytes: number): string { + let bytes = 0; + return input.split("").filter(char => (bytes += new TextEncoder().encode(char).length) <= maxBytes).join(""); +} + + export class AutocompleteLruCache { private static capacity = 1000; private mutex = new Mutex(); - constructor(private db: DatabaseConnection) {} + constructor(private db: DatabaseConnection) { } static async get(): Promise { const db = await open({ @@ -35,9 +43,10 @@ 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 truncatedPrefix = truncateToBytes(prefix, SQLITE_MAX_LIKE_PATTERN_LENGTH); const result = await this.db.get( "SELECT key, value FROM cache WHERE ? LIKE key || '%' ORDER BY LENGTH(key) DESC LIMIT 1", - prefix, + truncatedPrefix, ); // Validate that the cached compeltion is a valid completion for the prefix From 334705c0a3c4fd081c22faf13f9f5478a57600d4 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Sat, 26 Oct 2024 23:41:01 -0700 Subject: [PATCH 2/4] rearrange truncateSqliteLikePattern --- core/autocomplete/cache.ts | 13 ++----------- core/indexing/CodeSnippetsIndex.ts | 6 +++--- core/indexing/refreshIndex.ts | 15 +++++++++++++-- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/core/autocomplete/cache.ts b/core/autocomplete/cache.ts index 8f126e409..c04770a4c 100644 --- a/core/autocomplete/cache.ts +++ b/core/autocomplete/cache.ts @@ -1,17 +1,9 @@ import { Mutex } from "async-mutex"; import { open } from "sqlite"; import sqlite3 from "sqlite3"; -import { DatabaseConnection } from "../indexing/refreshIndex.js"; +import { DatabaseConnection, truncateSqliteLikePattern } from "../indexing/refreshIndex.js"; import { getTabAutocompleteCacheSqlitePath } from "../util/paths.js"; -const SQLITE_MAX_LIKE_PATTERN_LENGTH = 50000; - -function truncateToBytes(input: string, maxBytes: number): string { - let bytes = 0; - return input.split("").filter(char => (bytes += new TextEncoder().encode(char).length) <= maxBytes).join(""); -} - - export class AutocompleteLruCache { private static capacity = 1000; private mutex = new Mutex(); @@ -43,10 +35,9 @@ 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 truncatedPrefix = truncateToBytes(prefix, SQLITE_MAX_LIKE_PATTERN_LENGTH); const result = await this.db.get( "SELECT key, value FROM cache WHERE ? LIKE key || '%' ORDER BY LENGTH(key) DESC LIMIT 1", - truncatedPrefix, + truncateSqliteLikePattern(prefix), ); // Validate that the cached compeltion is a valid completion for the prefix diff --git a/core/indexing/CodeSnippetsIndex.ts b/core/indexing/CodeSnippetsIndex.ts index a8b41cafe..70872aff9 100644 --- a/core/indexing/CodeSnippetsIndex.ts +++ b/core/indexing/CodeSnippetsIndex.ts @@ -14,7 +14,7 @@ import { getParserForFile, getQueryForFile, } from "../util/treeSitter"; -import { DatabaseConnection, SqliteDb, tagToString } from "./refreshIndex"; +import { DatabaseConnection, SqliteDb, tagToString, truncateSqliteLikePattern } from "./refreshIndex"; import { IndexResultType, MarkCompleteCallback, @@ -28,7 +28,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { relativeExpectedTime: number = 1; artifactId = "codeSnippets"; - constructor(private readonly ide: IDE) {} + constructor(private readonly ide: IDE) { } private static async _createTables(db: DatabaseConnection) { await db.exec(`CREATE TABLE IF NOT EXISTS code_snippets ( @@ -383,7 +383,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { const db = await SqliteDb.get(); await CodeSnippetsCodebaseIndex._createTables(db); - const likePatterns = workspaceDirs.map((dir) => `${dir}%`); + const likePatterns = workspaceDirs.map((dir) => truncateSqliteLikePattern(`${dir}%`)); const placeholders = likePatterns.map(() => "?").join(" OR path LIKE "); const query = ` diff --git a/core/indexing/refreshIndex.ts b/core/indexing/refreshIndex.ts index cf59899f1..d82a690ca 100644 --- a/core/indexing/refreshIndex.ts +++ b/core/indexing/refreshIndex.ts @@ -406,7 +406,7 @@ export async function getComputeDeleteAddRemove( for await (const _ of globalCacheIndex.update( tag, results, - async () => {}, + async () => { }, repoName, )) { } @@ -417,7 +417,7 @@ export async function getComputeDeleteAddRemove( export class GlobalCacheCodeBaseIndex implements CodebaseIndex { relativeExpectedTime: number = 1; - constructor(private db: DatabaseConnection) {} + constructor(private db: DatabaseConnection) { } artifactId = "globalCache"; @@ -469,3 +469,14 @@ export class GlobalCacheCodeBaseIndex implements CodebaseIndex { ); } } + +const SQLITE_MAX_LIKE_PATTERN_LENGTH = 50000; + +function truncateToBytes(input: string, maxBytes: number): string { + let bytes = 0; + return input.split("").filter(char => (bytes += new TextEncoder().encode(char).length) <= maxBytes).join(""); +} + +export function truncateSqliteLikePattern(input: string) { + return truncateToBytes(input, SQLITE_MAX_LIKE_PATTERN_LENGTH); +} \ No newline at end of file From c9d38cfee15c39f951a4c710803cafdce3ce8c12 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 28 Oct 2024 15:33:32 -0700 Subject: [PATCH 3/4] truncate first bytes --- core/indexing/refreshIndex.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/core/indexing/refreshIndex.ts b/core/indexing/refreshIndex.ts index d82a690ca..a2072c60e 100644 --- a/core/indexing/refreshIndex.ts +++ b/core/indexing/refreshIndex.ts @@ -472,11 +472,21 @@ export class GlobalCacheCodeBaseIndex implements CodebaseIndex { const SQLITE_MAX_LIKE_PATTERN_LENGTH = 50000; -function truncateToBytes(input: string, maxBytes: number): string { +function truncateFirstNBytes(input: string, maxBytes: number): string { let bytes = 0; - return input.split("").filter(char => (bytes += new TextEncoder().encode(char).length) <= maxBytes).join(""); + let startIndex = 0; + + for (let i = 0; i < input.length; i++) { + bytes += new TextEncoder().encode(input[i]).length; + if (bytes > maxBytes) { + startIndex = i; + break; + } + } + + return input.substring(startIndex); } export function truncateSqliteLikePattern(input: string) { - return truncateToBytes(input, SQLITE_MAX_LIKE_PATTERN_LENGTH); + return truncateFirstNBytes(input, SQLITE_MAX_LIKE_PATTERN_LENGTH); } \ No newline at end of file From ae636520ca5ce39ed8741a1d2a8847804f429d4a Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 29 Oct 2024 22:23:59 -0700 Subject: [PATCH 4/4] reverse and test --- core/indexing/refreshIndex.test.ts | 34 ++++++++++++++++++++++++++++++ core/indexing/refreshIndex.ts | 16 +++++++------- 2 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 core/indexing/refreshIndex.test.ts diff --git a/core/indexing/refreshIndex.test.ts b/core/indexing/refreshIndex.test.ts new file mode 100644 index 000000000..7eeb2c5d8 --- /dev/null +++ b/core/indexing/refreshIndex.test.ts @@ -0,0 +1,34 @@ +import { truncateToLastNBytes } from "./refreshIndex"; + +describe("truncateToLastNBytes", () => { + it("should return full string if maxBytes greater than string byte length", () => { + const input = "Hello World"; + const result = truncateToLastNBytes(input, 100); + expect(result).toBe("Hello World"); + }); + + it("should truncate ASCII string correctly", () => { + const input = "Hello World"; + const result = truncateToLastNBytes(input, 5); + expect(result).toBe("World"); + }); + + it("should handle empty string", () => { + const input = ""; + const result = truncateToLastNBytes(input, 5); + expect(result).toBe(""); + }); + + it("should handle UTF-8 characters correctly", () => { + const input = "👋 Hello"; + // 👋 is 4 bytes, space is 1 byte + const result = truncateToLastNBytes(input, 5); + expect(result).toBe("Hello"); + }); + + it("should handle maxBytes of 0", () => { + const input = "Hello World"; + const result = truncateToLastNBytes(input, 0); + expect(result).toBe(""); + }); +}); diff --git a/core/indexing/refreshIndex.ts b/core/indexing/refreshIndex.ts index a2072c60e..ba2dbd820 100644 --- a/core/indexing/refreshIndex.ts +++ b/core/indexing/refreshIndex.ts @@ -406,7 +406,7 @@ export async function getComputeDeleteAddRemove( for await (const _ of globalCacheIndex.update( tag, results, - async () => { }, + async () => {}, repoName, )) { } @@ -417,7 +417,7 @@ export async function getComputeDeleteAddRemove( export class GlobalCacheCodeBaseIndex implements CodebaseIndex { relativeExpectedTime: number = 1; - constructor(private db: DatabaseConnection) { } + constructor(private db: DatabaseConnection) {} artifactId = "globalCache"; @@ -472,21 +472,21 @@ export class GlobalCacheCodeBaseIndex implements CodebaseIndex { const SQLITE_MAX_LIKE_PATTERN_LENGTH = 50000; -function truncateFirstNBytes(input: string, maxBytes: number): string { +export function truncateToLastNBytes(input: string, maxBytes: number): string { let bytes = 0; let startIndex = 0; - for (let i = 0; i < input.length; i++) { + for (let i = input.length - 1; i >= 0; i--) { bytes += new TextEncoder().encode(input[i]).length; if (bytes > maxBytes) { - startIndex = i; + startIndex = i + 1; break; } } - return input.substring(startIndex); + return input.substring(startIndex, input.length); } export function truncateSqliteLikePattern(input: string) { - return truncateFirstNBytes(input, SQLITE_MAX_LIKE_PATTERN_LENGTH); -} \ No newline at end of file + return truncateToLastNBytes(input, SQLITE_MAX_LIKE_PATTERN_LENGTH); +}