Delete duplicate entries and REPLACE in compute
Also moves the insert snippets explicitly to CodeSnippetsIndex instead of depending on compute
This commit is contained in:
parent
e7baa29b29
commit
e74abcda35
1 changed files with 57 additions and 8 deletions
|
|
@ -36,6 +36,21 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
|
|||
startLine INTEGER NOT NULL,
|
||||
endLine INTEGER NOT NULL
|
||||
)`);
|
||||
// Delete duplicate entries in code_snippets
|
||||
await db.exec(`
|
||||
DELETE FROM code_snippets
|
||||
WHERE id NOT IN (
|
||||
SELECT MIN(id)
|
||||
FROM code_snippets
|
||||
GROUP BY path, cacheKey, content, title, startLine, endLine
|
||||
)
|
||||
`);
|
||||
|
||||
// Add unique constraint if it doesn't exist
|
||||
await db.exec(`
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_code_snippets_unique
|
||||
ON code_snippets (path, cacheKey, content, title, startLine, endLine)
|
||||
`);
|
||||
|
||||
await db.exec(`CREATE TABLE IF NOT EXISTS code_snippets_tags (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
|
@ -44,6 +59,21 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
|
|||
FOREIGN KEY (snippetId) REFERENCES code_snippets (id)
|
||||
)`);
|
||||
|
||||
// Delete code_snippets associated with duplicate code_snippets_tags entries
|
||||
await db.exec(`
|
||||
DELETE FROM code_snippets
|
||||
WHERE id IN (
|
||||
SELECT snippetId
|
||||
FROM code_snippets_tags
|
||||
WHERE (snippetId, tag) IN (
|
||||
SELECT snippetId, tag
|
||||
FROM code_snippets_tags
|
||||
GROUP BY snippetId, tag
|
||||
HAVING COUNT(*) > 1
|
||||
)
|
||||
)
|
||||
`);
|
||||
|
||||
// Delete duplicate entries
|
||||
await db.exec(`
|
||||
DELETE FROM code_snippets_tags
|
||||
|
|
@ -109,12 +139,13 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
|
|||
);
|
||||
} catch (e) {
|
||||
// If can't parse, assume malformatted code
|
||||
console.error(`Error parsing ${compute.path}:`, e);
|
||||
}
|
||||
|
||||
// Add snippets to sqlite
|
||||
for (const snippet of snippets) {
|
||||
const { lastID } = await db.run(
|
||||
"INSERT INTO code_snippets (path, cacheKey, content, title, startLine, endLine) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
"REPLACE INTO code_snippets (path, cacheKey, content, title, startLine, endLine) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
[
|
||||
compute.path,
|
||||
compute.cacheKey,
|
||||
|
|
@ -126,7 +157,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
|
|||
);
|
||||
|
||||
await db.run(
|
||||
"INSERT INTO code_snippets_tags (snippetId, tag) VALUES (?, ?)",
|
||||
"REPLACE INTO code_snippets_tags (snippetId, tag) VALUES (?, ?)",
|
||||
[lastID, tagString],
|
||||
);
|
||||
}
|
||||
|
|
@ -152,15 +183,33 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
|
|||
}
|
||||
|
||||
for (let i = 0; i < results.addTag.length; i++) {
|
||||
const snippetsWithPath = await db.all(
|
||||
"SELECT * FROM code_snippets WHERE cacheKey = ?",
|
||||
[results.addTag[i].cacheKey],
|
||||
);
|
||||
const addTag = results.addTag[i];
|
||||
let snippets: (ChunkWithoutID & { title: string })[] = [];
|
||||
try {
|
||||
snippets = await this.getSnippetsInFile(
|
||||
addTag.path,
|
||||
await this.ide.readFile(addTag.path),
|
||||
);
|
||||
} catch (e) {
|
||||
// If can't parse, assume malformatted code
|
||||
console.error(`Error parsing ${addTag.path}:`, e);
|
||||
}
|
||||
|
||||
for (const snippet of snippetsWithPath) {
|
||||
for (const snippet of snippets) {
|
||||
const { lastID } = await db.run(
|
||||
"INSERT INTO code_snippets (path, cacheKey, content, title, startLine, endLine) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
[
|
||||
addTag.path,
|
||||
addTag.cacheKey,
|
||||
snippet.content,
|
||||
snippet.title,
|
||||
snippet.startLine,
|
||||
snippet.endLine,
|
||||
],
|
||||
);
|
||||
await db.run(
|
||||
"INSERT INTO code_snippets_tags (snippetId, tag) VALUES (?, ?)",
|
||||
[snippet.id, tagString],
|
||||
[lastID, tagString],
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue