Merge pull request #2703 from continuedev/dallin/sqlite-max-like-pattern

Ensure don't go over sqlite max like pattern length
This commit is contained in:
Nate Sesti 2024-10-29 22:25:14 -07:00 • committed by GitHub
commit 268b6eae34
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 61 additions and 6 deletions

View file

@ -1,14 +1,14 @@
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";
export class AutocompleteLruCache {
private static capacity = 1000;
private mutex = new Mutex();
constructor(private db: DatabaseConnection) {}
constructor(private db: DatabaseConnection) { }
static async get(): Promise<AutocompleteLruCache> {
const db = await open({
@ -37,7 +37,7 @@ export class AutocompleteLruCache {
// 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",
prefix,
truncateSqliteLikePattern(prefix),
);
// Validate that the cached compeltion is a valid completion for the prefix

View file

@ -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 = `

View file

@ -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("");
});
});

View file

@ -469,3 +469,24 @@ export class GlobalCacheCodeBaseIndex implements CodebaseIndex {
);
}
}
const SQLITE_MAX_LIKE_PATTERN_LENGTH = 50000;
export function truncateToLastNBytes(input: string, maxBytes: number): string {
let bytes = 0;
let startIndex = 0;
for (let i = input.length - 1; i >= 0; i--) {
bytes += new TextEncoder().encode(input[i]).length;
if (bytes > maxBytes) {
startIndex = i + 1;
break;
}
}
return input.substring(startIndex, input.length);
}
export function truncateSqliteLikePattern(input: string) {
return truncateToLastNBytes(input, SQLITE_MAX_LIKE_PATTERN_LENGTH);
}