Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure don't go over sqlite max like pattern length #2703

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions core/autocomplete/cache.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Mutex } from "async-mutex";
import { open } from "sqlite";
import sqlite3 from "sqlite3";

Check warning on line 3 in core/autocomplete/cache.ts

View workflow job for this annotation

GitHub Actions / tsc-check

There should be at least one empty line between import groups
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({
Expand Down Expand Up @@ -37,7 +37,7 @@
// 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
Expand Down
6 changes: 3 additions & 3 deletions core/indexing/CodeSnippetsIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 (
Expand Down Expand Up @@ -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 = `
Expand Down
34 changes: 34 additions & 0 deletions core/indexing/refreshIndex.test.ts
Original file line number Diff line number Diff line change
@@ -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("");
});
});
21 changes: 21 additions & 0 deletions core/indexing/refreshIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading