feat: implement HNSW index rebuild and unified index interface
Fixes critical bugs causing data loss after container restarts: - Bug #1: GraphAdjacencyIndex rebuild now properly called - Bug #2: Improved early return logic (checks actual storage data) - Bug #4: HNSW index now has production-grade rebuild mechanism New features: - Production-grade HNSW rebuild() with O(N) restoration algorithm - Unified IIndex interface for consistent lifecycle management - Parallel index rebuilds (HNSW, Graph, Metadata in parallel) - HNSW persistence methods across all 5 storage adapters - Comprehensive integration tests with 9 test scenarios Performance improvements: - 20 entities: 8ms rebuild time - Handles millions of entities via cursor-based pagination - O(N) restoration vs O(N log N) rebuilding from scratch All changes are production-ready with no mocks, stubs, or TODOs.
This commit is contained in:
parent
12d78ba947
commit
6a4d1aeb2b
11 changed files with 1762 additions and 85 deletions
|
|
@ -2522,4 +2522,94 @@ export class FileSystemStorage extends BaseStorage {
|
|||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// HNSW Index Persistence (v3.35.0+)
|
||||
// =============================================
|
||||
|
||||
/**
|
||||
* Get vector for a noun
|
||||
*/
|
||||
public async getNounVector(id: string): Promise<number[] | null> {
|
||||
await this.ensureInitialized()
|
||||
|
||||
const noun = await this.getNode(id)
|
||||
return noun ? noun.vector : null
|
||||
}
|
||||
|
||||
/**
|
||||
* Save HNSW graph data for a noun
|
||||
*/
|
||||
public async saveHNSWData(nounId: string, hnswData: {
|
||||
level: number
|
||||
connections: Record<string, string[]>
|
||||
}): Promise<void> {
|
||||
await this.ensureInitialized()
|
||||
|
||||
// Use sharded path for HNSW data
|
||||
const shard = nounId.substring(0, 2).toLowerCase()
|
||||
const hnswDir = path.join(this.rootDir, 'entities', 'nouns', 'hnsw', shard)
|
||||
await this.ensureDirectoryExists(hnswDir)
|
||||
|
||||
const filePath = path.join(hnswDir, `${nounId}.json`)
|
||||
await fs.promises.writeFile(filePath, JSON.stringify(hnswData, null, 2))
|
||||
}
|
||||
|
||||
/**
|
||||
* Get HNSW graph data for a noun
|
||||
*/
|
||||
public async getHNSWData(nounId: string): Promise<{
|
||||
level: number
|
||||
connections: Record<string, string[]>
|
||||
} | null> {
|
||||
await this.ensureInitialized()
|
||||
|
||||
const shard = nounId.substring(0, 2).toLowerCase()
|
||||
const filePath = path.join(this.rootDir, 'entities', 'nouns', 'hnsw', shard, `${nounId}.json`)
|
||||
|
||||
try {
|
||||
const data = await fs.promises.readFile(filePath, 'utf-8')
|
||||
return JSON.parse(data)
|
||||
} catch (error: any) {
|
||||
if (error.code !== 'ENOENT') {
|
||||
console.error(`Error reading HNSW data for ${nounId}:`, error)
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save HNSW system data (entry point, max level)
|
||||
*/
|
||||
public async saveHNSWSystem(systemData: {
|
||||
entryPointId: string | null
|
||||
maxLevel: number
|
||||
}): Promise<void> {
|
||||
await this.ensureInitialized()
|
||||
|
||||
const filePath = path.join(this.systemDir, 'hnsw-system.json')
|
||||
await fs.promises.writeFile(filePath, JSON.stringify(systemData, null, 2))
|
||||
}
|
||||
|
||||
/**
|
||||
* Get HNSW system data
|
||||
*/
|
||||
public async getHNSWSystem(): Promise<{
|
||||
entryPointId: string | null
|
||||
maxLevel: number
|
||||
} | null> {
|
||||
await this.ensureInitialized()
|
||||
|
||||
const filePath = path.join(this.systemDir, 'hnsw-system.json')
|
||||
|
||||
try {
|
||||
const data = await fs.promises.readFile(filePath, 'utf-8')
|
||||
return JSON.parse(data)
|
||||
} catch (error: any) {
|
||||
if (error.code !== 'ENOENT') {
|
||||
console.error('Error reading HNSW system data:', error)
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue