Skip to content

Commit 16219b2

Browse files
committed
feat: sqlite optimize and auto_vacuum
1 parent 4a4356c commit 16219b2

File tree

2 files changed

+4
-23
lines changed

2 files changed

+4
-23
lines changed

src/services/code-index/vector-store/__tests__/local-vector-store.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// src/services/code-index/vector-store/__tests__/local-vector-store.spec.ts
1+
// npx vitest run services/code-index/vector-store/__tests__/local-vector-store.spec.ts
22
import { describe, it, expect, beforeEach, vi, afterEach } from "vitest"
33
import { LocalVectorStore } from "../local-vector-store"
44
import type { Payload, VectorStoreSearchResult } from "../../interfaces"

src/services/code-index/vector-store/local-vector-store.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ export class LocalVectorStore implements IVectorStore {
2121

2222
private readonly UPDATE_BATCH_SIZE = 1000
2323
private readonly SEARCH_BATCH_SIZE = 10000
24-
private deletedFileCount = 0
25-
private lastCleanupTime: Date | null = null
26-
private readonly RESIZE_FILE_THRESHOLD = 50
27-
private readonly RESIZE_TIME_THRESHOLD_MS = 60 * 60 * 1000
2824

2925
constructor(workspacePath: string, vectorSize: number, dbDirectory: string) {
3026
this.vectorSize = vectorSize
@@ -57,11 +53,13 @@ export class LocalVectorStore implements IVectorStore {
5753
if (!this.db) return
5854

5955
this.db.exec(`
60-
PRAGMA journal_mode = WAL;
56+
PRAGMA journal_mode = MEMORY;
6157
PRAGMA synchronous = NORMAL;
6258
PRAGMA cache_size = 100000;
6359
PRAGMA locking_mode = NORMAL;
6460
PRAGMA temp_store = MEMORY;
61+
PRAGMA auto_vacuum = INCREMENTAL;
62+
PRAGMA optimize;
6563
`)
6664
// Create tables if they don't exist
6765
await this.db.exec(`
@@ -133,7 +131,6 @@ export class LocalVectorStore implements IVectorStore {
133131
this.cachedCollectionId = collection.id != null ? Number(collection.id) : null
134132
return true
135133
}
136-
137134
this.cachedCollectionId = collection.id != null ? Number(collection.id) : null
138135
return false
139136
} catch (error) {
@@ -413,19 +410,6 @@ export class LocalVectorStore implements IVectorStore {
413410
return this.deletePointsByMultipleFilePaths([filePath])
414411
}
415412

416-
private async checkAndResize(): Promise<void> {
417-
const now = new Date()
418-
const shouldResize =
419-
this.deletedFileCount >= this.RESIZE_FILE_THRESHOLD ||
420-
(this.lastCleanupTime && now.getTime() - this.lastCleanupTime.getTime() > this.RESIZE_TIME_THRESHOLD_MS)
421-
422-
if (shouldResize) {
423-
await this.resizeCollection()
424-
this.deletedFileCount = 0
425-
this.lastCleanupTime = now
426-
}
427-
}
428-
429413
async deletePointsByMultipleFilePaths(filePaths: string[]): Promise<void> {
430414
if (filePaths.length === 0) {
431415
return
@@ -460,8 +444,6 @@ export class LocalVectorStore implements IVectorStore {
460444
}
461445

462446
await db.exec("COMMIT")
463-
this.deletedFileCount += filePaths.length
464-
await this.checkAndResize()
465447
} catch (error) {
466448
await db.exec("ROLLBACK")
467449
console.error("Failed to delete points by file paths:", error)
@@ -489,7 +471,6 @@ export class LocalVectorStore implements IVectorStore {
489471
if (collectionId != null) {
490472
await db.prepare("DELETE FROM vectors WHERE collection_id = ?").run(collectionId)
491473
await db.prepare("DELETE FROM files WHERE collection_id = ?").run(collectionId)
492-
await this.resizeCollection()
493474
}
494475
} catch (error) {
495476
console.error("Failed to clear collection:", error)

0 commit comments

Comments
 (0)