continue-custom/core/indexing/chunk/ChunkCodebaseIndex.ts
Rob Leidle dfbc6aabcb
Convert ChunkCodebaseIndexer's sqlite inserts into a single bulk insert to improve performance (#1943)
This change greatly improves the performance when inserting chunks.

Co-authored-by: Rob Leidle <rleidle@tesla.com>
2024-08-06 16:14:05 -07:00

212 lines
6.6 KiB
TypeScript

import { RunResult } from "sqlite3";
import { IContinueServerClient } from "../../continueServer/interface.js";
import { Chunk, IndexTag, IndexingProgressUpdate } from "../../index.js";
import { getBasename } from "../../util/index.js";
import { getLanguageForFile } from "../../util/treeSitter.js";
import { DatabaseConnection, SqliteDb, tagToString } from "../refreshIndex.js";
import {
IndexResultType,
MarkCompleteCallback,
PathAndCacheKey,
RefreshIndexResults,
type CodebaseIndex,
} from "../types.js";
import { chunkDocument } from "./chunk.js";
export class ChunkCodebaseIndex implements CodebaseIndex {
relativeExpectedTime: number = 1;
static artifactId = "chunks";
artifactId: string = ChunkCodebaseIndex.artifactId;
constructor(
private readonly readFile: (filepath: string) => Promise<string>,
private readonly continueServerClient: IContinueServerClient,
private readonly maxChunkSize: number,
) {
this.readFile = readFile;
}
async *update(
tag: IndexTag,
results: RefreshIndexResults,
markComplete: MarkCompleteCallback,
repoName: string | undefined,
): AsyncGenerator<IndexingProgressUpdate, any, unknown> {
const db = await SqliteDb.get();
await this.createTables(db);
const tagString = tagToString(tag);
// Check the remote cache
if (this.continueServerClient.connected) {
try {
const keys = results.compute.map(({ cacheKey }) => cacheKey);
const resp = await this.continueServerClient.getFromIndexCache(
keys,
"chunks",
repoName,
);
for (const [cacheKey, chunks] of Object.entries(resp.files)) {
await this.insertChunks(db, tagString, chunks);
}
results.compute = results.compute.filter(
(item) => !resp.files[item.cacheKey],
);
} catch (e) {
console.error("Failed to fetch from remote cache: ", e);
}
}
let accumulatedProgress = 0;
yield {
desc: `Chunking ${results.compute.length} ${this.formatListPlurality("file", results.compute.length)}`,
status: "indexing",
progress: accumulatedProgress,
};
const chunks = await this.computeChunks(results.compute);
await this.insertChunks(db, tagString, chunks);
// Add tag
for (const item of results.addTag) {
await db.run(
`
INSERT INTO chunk_tags (chunkId, tag)
SELECT id, ? FROM chunks
WHERE cacheKey = ? AND path = ?
`,
[tagString, item.cacheKey, item.path],
);
markComplete([item], IndexResultType.AddTag);
accumulatedProgress += 1 / results.addTag.length / 4;
yield {
progress: accumulatedProgress,
desc: `Adding ${getBasename(item.path)}`,
status: "indexing",
};
}
// Remove tag
for (const item of results.removeTag) {
await db.run(
`
DELETE FROM chunk_tags
WHERE tag = ?
AND chunkId IN (
SELECT id FROM chunks
WHERE cacheKey = ? AND path = ?
)
`,
[tagString, item.cacheKey, item.path],
);
markComplete([item], IndexResultType.RemoveTag);
accumulatedProgress += 1 / results.removeTag.length / 4;
yield {
progress: accumulatedProgress,
desc: `Removing ${getBasename(item.path)}`,
status: "indexing",
};
}
// Delete
for (const item of results.del) {
const deleted = await db.run("DELETE FROM chunks WHERE cacheKey = ?", [
item.cacheKey,
]);
// Delete from chunk_tags
await db.run("DELETE FROM chunk_tags WHERE chunkId = ?", [
deleted.lastID,
]);
markComplete([item], IndexResultType.Delete);
accumulatedProgress += 1 / results.del.length / 4;
yield {
progress: accumulatedProgress,
desc: `Removing ${getBasename(item.path)}`,
status: "indexing",
};
}
}
private async createTables(db: DatabaseConnection) {
await db.exec("PRAGMA journal_mode=WAL;");
await db.exec(`CREATE TABLE IF NOT EXISTS chunks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
cacheKey TEXT NOT NULL,
path TEXT NOT NULL,
idx INTEGER NOT NULL,
startLine INTEGER NOT NULL,
endLine INTEGER NOT NULL,
content TEXT NOT NULL
)`);
await db.exec(`CREATE TABLE IF NOT EXISTS chunk_tags (
id INTEGER PRIMARY KEY AUTOINCREMENT,
tag TEXT NOT NULL,
chunkId INTEGER NOT NULL,
FOREIGN KEY (chunkId) REFERENCES chunks (id)
)`);
}
private async packToChunks(pack: PathAndCacheKey): Promise<Chunk[]> {
const contents = await this.readFile(pack.path);
if (!contents.length) {
return [];
}
const chunks: Chunk[] = [];
const chunkParams = {
filepath: pack.path,
contents,
maxChunkSize: this.maxChunkSize,
digest: pack.cacheKey,
};
for await (const c of chunkDocument(chunkParams)) {
chunks.push(c);
}
return chunks;
}
private async computeChunks(paths: PathAndCacheKey[]): Promise<Chunk[]> {
const chunkLists = await Promise.all(paths.map(this.packToChunks.bind(this)));
return chunkLists.flat();
}
private async insertChunks(db: DatabaseConnection, tagString: string, chunks: Chunk[]) {
await new Promise<void>((resolve, reject) => {
db.db.serialize(() => {
db.db.exec("BEGIN", (err: Error | null) => {
if (err) {
reject(new Error("error creating transaction", { cause: err }));
}
});
const chunksSQL = "INSERT INTO chunks (cacheKey, path, idx, startLine, endLine, content) VALUES (?, ?, ?, ?, ?, ?)";
chunks.map(c => {
db.db.run(chunksSQL, [c.digest, c.filepath, c.index, c.startLine, c.endLine, c.content], (result: RunResult, err: Error) => {
if (err) {
reject(new Error("error inserting into chunks table", { cause: err }));
}
});
const chunkTagsSQL = "INSERT INTO chunk_tags (chunkId, tag) VALUES (last_insert_rowid(), ?)";
db.db.run(chunkTagsSQL, [ tagString ], (result: RunResult, err: Error) => {
if (err) {
reject(new Error("error inserting into chunk_tags table", { cause: err }));
}
});
});
db.db.exec("COMMIT", (err: Error | null) => {
if (err) {
reject(new Error("error while committing insert chunks transaction", { cause: err }));
}
});
resolve();
});
});
}
private formatListPlurality(word: string, length: number): string {
return length <= 1 ? word : `${word}s`;
}
}