Skip to content

Commit

Permalink
feat: improve chunking desc on large projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick-Erichsen committed Aug 13, 2024
1 parent 3692aee commit 3844208
Showing 1 changed file with 49 additions and 18 deletions.
67 changes: 49 additions & 18 deletions core/indexing/chunk/ChunkCodebaseIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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,
Expand All @@ -12,6 +11,7 @@ import {
type CodebaseIndex,
} from "../types.js";
import { chunkDocument, shouldChunk } from "./chunk.js";
import * as path from "path";

export class ChunkCodebaseIndex implements CodebaseIndex {
relativeExpectedTime: number = 1;
Expand Down Expand Up @@ -61,8 +61,11 @@ export class ChunkCodebaseIndex implements CodebaseIndex {
let accumulatedProgress = 0;

if (results.compute.length > 0) {
const folderPath = results.compute[0].path;
const folderName = path.basename(path.dirname(folderPath));

yield {
desc: `Chunking ${results.compute.length} ${this.formatListPlurality("file", results.compute.length)}`,
desc: `Chunking files in ${folderName}`,
status: "indexing",
progress: accumulatedProgress,
};
Expand Down Expand Up @@ -171,35 +174,63 @@ export class ChunkCodebaseIndex implements CodebaseIndex {
}

private async computeChunks(paths: PathAndCacheKey[]): Promise<Chunk[]> {
const chunkLists = await Promise.all(paths.map(p => this.packToChunks(p)));
const chunkLists = await Promise.all(
paths.map((p) => this.packToChunks(p)),
);
return chunkLists.flat();
}

private async insertChunks(db: DatabaseConnection, tagString: string, chunks: Chunk[]) {
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 }));
}
});
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 }));
reject(
new Error("error while committing insert chunks transaction", {
cause: err,
}),
);
} else {
resolve();
}
Expand Down

0 comments on commit 3844208

Please sign in to comment.