@danielsimonjr/memory-mcp 11.0.1 → 11.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -22
- package/dist/core/EntityManager.d.ts +10 -15
- package/dist/core/EntityManager.d.ts.map +1 -1
- package/dist/core/EntityManager.js +21 -54
- package/dist/core/GraphStorage.d.ts +0 -51
- package/dist/core/GraphStorage.d.ts.map +1 -1
- package/dist/core/GraphStorage.js +2 -79
- package/dist/core/GraphTraversal.d.ts +2 -7
- package/dist/core/GraphTraversal.d.ts.map +1 -1
- package/dist/core/GraphTraversal.js +2 -19
- package/dist/core/ManagerContext.d.ts +0 -4
- package/dist/core/ManagerContext.d.ts.map +1 -1
- package/dist/core/ManagerContext.js +2 -12
- package/dist/core/RelationManager.d.ts.map +1 -1
- package/dist/core/RelationManager.js +4 -5
- package/dist/core/SQLiteStorage.d.ts.map +1 -1
- package/dist/core/SQLiteStorage.js +2 -3
- package/dist/core/TransactionManager.d.ts +2 -207
- package/dist/core/TransactionManager.d.ts.map +1 -1
- package/dist/core/TransactionManager.js +6 -482
- package/dist/core/index.d.ts +1 -2
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +1 -3
- package/dist/features/ArchiveManager.d.ts +2 -14
- package/dist/features/ArchiveManager.d.ts.map +1 -1
- package/dist/features/ArchiveManager.js +3 -44
- package/dist/features/CompressionManager.d.ts +4 -14
- package/dist/features/CompressionManager.d.ts.map +1 -1
- package/dist/features/CompressionManager.js +9 -74
- package/dist/features/IOManager.d.ts +2 -6
- package/dist/features/IOManager.d.ts.map +1 -1
- package/dist/features/IOManager.js +10 -105
- package/dist/features/StreamingExporter.d.ts +4 -27
- package/dist/features/StreamingExporter.d.ts.map +1 -1
- package/dist/features/StreamingExporter.js +4 -65
- package/dist/features/index.d.ts +0 -2
- package/dist/features/index.d.ts.map +1 -1
- package/dist/features/index.js +0 -3
- package/dist/search/EmbeddingService.d.ts +9 -108
- package/dist/search/EmbeddingService.d.ts.map +1 -1
- package/dist/search/EmbeddingService.js +15 -187
- package/dist/search/FuzzySearch.js +1 -1
- package/dist/search/SavedSearchManager.d.ts.map +1 -1
- package/dist/search/SavedSearchManager.js +2 -3
- package/dist/search/SearchManager.d.ts +1 -42
- package/dist/search/SearchManager.d.ts.map +1 -1
- package/dist/search/SearchManager.js +0 -115
- package/dist/search/SemanticSearch.d.ts +1 -4
- package/dist/search/SemanticSearch.d.ts.map +1 -1
- package/dist/search/SemanticSearch.js +2 -12
- package/dist/search/TFIDFIndexManager.d.ts +0 -88
- package/dist/search/TFIDFIndexManager.d.ts.map +1 -1
- package/dist/search/TFIDFIndexManager.js +0 -217
- package/dist/search/index.d.ts +1 -18
- package/dist/search/index.d.ts.map +1 -1
- package/dist/search/index.js +1 -32
- package/dist/server/MCPServer.d.ts.map +1 -1
- package/dist/server/MCPServer.js +4 -1
- package/dist/server/responseCompressor.js +5 -5
- package/dist/server/toolDefinitions.d.ts.map +1 -1
- package/dist/server/toolDefinitions.js +5 -1
- package/dist/server/toolHandlers.d.ts +9 -5
- package/dist/server/toolHandlers.d.ts.map +1 -1
- package/dist/server/toolHandlers.js +23 -8
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types.d.ts +2 -579
- package/dist/types/types.d.ts.map +1 -1
- package/dist/utils/compressedCache.d.ts +0 -29
- package/dist/utils/compressedCache.d.ts.map +1 -1
- package/dist/utils/compressedCache.js +0 -39
- package/dist/utils/entityUtils.d.ts +1 -59
- package/dist/utils/entityUtils.d.ts.map +1 -1
- package/dist/utils/entityUtils.js +3 -113
- package/dist/utils/errors.d.ts +0 -18
- package/dist/utils/errors.d.ts.map +1 -1
- package/dist/utils/errors.js +0 -24
- package/dist/utils/index.d.ts +2 -6
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +2 -14
- package/dist/utils/logger.d.ts +0 -7
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +2 -9
- package/dist/utils/parallelUtils.d.ts +1 -5
- package/dist/utils/parallelUtils.d.ts.map +1 -1
- package/dist/utils/parallelUtils.js +1 -23
- package/dist/utils/schemas.d.ts +16 -16
- package/dist/utils/schemas.d.ts.map +1 -1
- package/dist/utils/schemas.js +12 -12
- package/dist/utils/taskScheduler.d.ts +0 -4
- package/dist/utils/taskScheduler.d.ts.map +1 -1
- package/dist/utils/taskScheduler.js +1 -21
- package/dist/workers/WorkerPool.d.ts +81 -0
- package/dist/workers/WorkerPool.d.ts.map +1 -0
- package/dist/workers/WorkerPool.js +121 -0
- package/dist/workers/index.d.ts +1 -1
- package/dist/workers/index.d.ts.map +1 -1
- package/dist/workers/levenshteinWorker.js +1 -1
- package/package.json +1 -4
- package/dist/__tests__/file-path.test.js +0 -119
- package/dist/__tests__/knowledge-graph.test.js +0 -318
- package/dist/core/GraphEventEmitter.d.ts +0 -202
- package/dist/core/GraphEventEmitter.d.ts.map +0 -1
- package/dist/core/GraphEventEmitter.js +0 -346
- package/dist/features/KeywordExtractor.d.ts +0 -61
- package/dist/features/KeywordExtractor.d.ts.map +0 -1
- package/dist/features/KeywordExtractor.js +0 -126
- package/dist/features/ObservationNormalizer.d.ts +0 -90
- package/dist/features/ObservationNormalizer.d.ts.map +0 -1
- package/dist/features/ObservationNormalizer.js +0 -193
- package/dist/memory.jsonl +0 -1
- package/dist/search/BM25Search.d.ts +0 -148
- package/dist/search/BM25Search.d.ts.map +0 -1
- package/dist/search/BM25Search.js +0 -339
- package/dist/search/EarlyTerminationManager.d.ts +0 -140
- package/dist/search/EarlyTerminationManager.d.ts.map +0 -1
- package/dist/search/EarlyTerminationManager.js +0 -279
- package/dist/search/EmbeddingCache.d.ts +0 -175
- package/dist/search/EmbeddingCache.d.ts.map +0 -1
- package/dist/search/EmbeddingCache.js +0 -246
- package/dist/search/HybridScorer.d.ts +0 -181
- package/dist/search/HybridScorer.d.ts.map +0 -1
- package/dist/search/HybridScorer.js +0 -257
- package/dist/search/HybridSearchManager.d.ts +0 -80
- package/dist/search/HybridSearchManager.d.ts.map +0 -1
- package/dist/search/HybridSearchManager.js +0 -187
- package/dist/search/IncrementalIndexer.d.ts +0 -201
- package/dist/search/IncrementalIndexer.d.ts.map +0 -1
- package/dist/search/IncrementalIndexer.js +0 -342
- package/dist/search/OptimizedInvertedIndex.d.ts +0 -163
- package/dist/search/OptimizedInvertedIndex.d.ts.map +0 -1
- package/dist/search/OptimizedInvertedIndex.js +0 -358
- package/dist/search/ParallelSearchExecutor.d.ts +0 -172
- package/dist/search/ParallelSearchExecutor.d.ts.map +0 -1
- package/dist/search/ParallelSearchExecutor.js +0 -309
- package/dist/search/QuantizedVectorStore.d.ts +0 -171
- package/dist/search/QuantizedVectorStore.d.ts.map +0 -1
- package/dist/search/QuantizedVectorStore.js +0 -307
- package/dist/search/QueryAnalyzer.d.ts +0 -76
- package/dist/search/QueryAnalyzer.d.ts.map +0 -1
- package/dist/search/QueryAnalyzer.js +0 -227
- package/dist/search/QueryCostEstimator.d.ts +0 -244
- package/dist/search/QueryCostEstimator.d.ts.map +0 -1
- package/dist/search/QueryCostEstimator.js +0 -652
- package/dist/search/QueryPlanCache.d.ts +0 -220
- package/dist/search/QueryPlanCache.d.ts.map +0 -1
- package/dist/search/QueryPlanCache.js +0 -379
- package/dist/search/QueryPlanner.d.ts +0 -58
- package/dist/search/QueryPlanner.d.ts.map +0 -1
- package/dist/search/QueryPlanner.js +0 -137
- package/dist/search/ReflectionManager.d.ts +0 -120
- package/dist/search/ReflectionManager.d.ts.map +0 -1
- package/dist/search/ReflectionManager.js +0 -231
- package/dist/search/SymbolicSearch.d.ts +0 -61
- package/dist/search/SymbolicSearch.d.ts.map +0 -1
- package/dist/search/SymbolicSearch.js +0 -163
- package/dist/search/TFIDFEventSync.d.ts +0 -85
- package/dist/search/TFIDFEventSync.d.ts.map +0 -1
- package/dist/search/TFIDFEventSync.js +0 -133
- package/dist/utils/BatchProcessor.d.ts +0 -271
- package/dist/utils/BatchProcessor.d.ts.map +0 -1
- package/dist/utils/BatchProcessor.js +0 -376
- package/dist/utils/MemoryMonitor.d.ts +0 -176
- package/dist/utils/MemoryMonitor.d.ts.map +0 -1
- package/dist/utils/MemoryMonitor.js +0 -305
- package/dist/utils/WorkerPoolManager.d.ts +0 -233
- package/dist/utils/WorkerPoolManager.d.ts.map +0 -1
- package/dist/utils/WorkerPoolManager.js +0 -420
- package/dist/utils/operationUtils.d.ts +0 -124
- package/dist/utils/operationUtils.d.ts.map +0 -1
- package/dist/utils/operationUtils.js +0 -175
- package/dist/vitest.config.js +0 -13
|
@@ -1,342 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Incremental Indexer
|
|
3
|
-
*
|
|
4
|
-
* Phase 12 Sprint 5: Queues embedding index updates and batch-processes them
|
|
5
|
-
* efficiently using flush thresholds and timer-based flushing.
|
|
6
|
-
*
|
|
7
|
-
* @module search/IncrementalIndexer
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* Default indexer options.
|
|
11
|
-
*/
|
|
12
|
-
export const DEFAULT_INDEXER_OPTIONS = {
|
|
13
|
-
flushThreshold: 50,
|
|
14
|
-
flushIntervalMs: 5000,
|
|
15
|
-
embeddingMode: 'document',
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* Incremental indexer for embedding vectors.
|
|
19
|
-
*
|
|
20
|
-
* Queues index updates and batch-processes them efficiently:
|
|
21
|
-
* - Auto-flush when queue reaches threshold
|
|
22
|
-
* - Timer-based flush for time-sensitive updates
|
|
23
|
-
* - Supports create, update, and delete operations
|
|
24
|
-
* - Graceful shutdown with final flush
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```typescript
|
|
28
|
-
* const indexer = new IncrementalIndexer(embeddingService, vectorStore, {
|
|
29
|
-
* flushThreshold: 100,
|
|
30
|
-
* flushIntervalMs: 10000,
|
|
31
|
-
* });
|
|
32
|
-
*
|
|
33
|
-
* // Queue operations
|
|
34
|
-
* indexer.queueCreate('entity1', 'Entity text content');
|
|
35
|
-
* indexer.queueUpdate('entity2', 'Updated text content');
|
|
36
|
-
* indexer.queueDelete('entity3');
|
|
37
|
-
*
|
|
38
|
-
* // Manual flush
|
|
39
|
-
* const result = await indexer.flush();
|
|
40
|
-
* console.log(`Processed ${result.processed} operations`);
|
|
41
|
-
*
|
|
42
|
-
* // Graceful shutdown
|
|
43
|
-
* await indexer.shutdown();
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
export class IncrementalIndexer {
|
|
47
|
-
embeddingService;
|
|
48
|
-
vectorStore;
|
|
49
|
-
options;
|
|
50
|
-
onProgress;
|
|
51
|
-
queue = [];
|
|
52
|
-
flushTimer = null;
|
|
53
|
-
isFlushing = false;
|
|
54
|
-
isShutdown = false;
|
|
55
|
-
/**
|
|
56
|
-
* Create a new incremental indexer.
|
|
57
|
-
*
|
|
58
|
-
* @param embeddingService - Service for generating embeddings
|
|
59
|
-
* @param vectorStore - Store for embedding vectors
|
|
60
|
-
* @param options - Indexer configuration options
|
|
61
|
-
*/
|
|
62
|
-
constructor(embeddingService, vectorStore, options) {
|
|
63
|
-
this.embeddingService = embeddingService;
|
|
64
|
-
this.vectorStore = vectorStore;
|
|
65
|
-
this.options = { ...DEFAULT_INDEXER_OPTIONS, ...options };
|
|
66
|
-
this.onProgress = options?.onProgress;
|
|
67
|
-
// Start the flush timer
|
|
68
|
-
this.startFlushTimer();
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Start the timer-based flush interval.
|
|
72
|
-
*/
|
|
73
|
-
startFlushTimer() {
|
|
74
|
-
if (this.flushTimer || this.isShutdown) {
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
this.flushTimer = setInterval(async () => {
|
|
78
|
-
if (this.queue.length > 0 && !this.isFlushing) {
|
|
79
|
-
await this.flush();
|
|
80
|
-
}
|
|
81
|
-
}, this.options.flushIntervalMs);
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Stop the flush timer.
|
|
85
|
-
*/
|
|
86
|
-
stopFlushTimer() {
|
|
87
|
-
if (this.flushTimer) {
|
|
88
|
-
clearInterval(this.flushTimer);
|
|
89
|
-
this.flushTimer = null;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Queue a create operation.
|
|
94
|
-
*
|
|
95
|
-
* @param entityName - Name of the entity to create
|
|
96
|
-
* @param text - Text content to embed
|
|
97
|
-
*/
|
|
98
|
-
queueCreate(entityName, text) {
|
|
99
|
-
if (this.isShutdown) {
|
|
100
|
-
throw new Error('Indexer is shutdown');
|
|
101
|
-
}
|
|
102
|
-
// Remove any existing operations for this entity
|
|
103
|
-
this.removeFromQueue(entityName);
|
|
104
|
-
this.queue.push({
|
|
105
|
-
type: 'create',
|
|
106
|
-
entityName,
|
|
107
|
-
text,
|
|
108
|
-
queuedAt: Date.now(),
|
|
109
|
-
});
|
|
110
|
-
this.checkAutoFlush();
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Queue an update operation.
|
|
114
|
-
*
|
|
115
|
-
* @param entityName - Name of the entity to update
|
|
116
|
-
* @param text - Updated text content to embed
|
|
117
|
-
*/
|
|
118
|
-
queueUpdate(entityName, text) {
|
|
119
|
-
if (this.isShutdown) {
|
|
120
|
-
throw new Error('Indexer is shutdown');
|
|
121
|
-
}
|
|
122
|
-
// Remove any existing operations for this entity
|
|
123
|
-
this.removeFromQueue(entityName);
|
|
124
|
-
this.queue.push({
|
|
125
|
-
type: 'update',
|
|
126
|
-
entityName,
|
|
127
|
-
text,
|
|
128
|
-
queuedAt: Date.now(),
|
|
129
|
-
});
|
|
130
|
-
this.checkAutoFlush();
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Queue a delete operation.
|
|
134
|
-
*
|
|
135
|
-
* @param entityName - Name of the entity to delete
|
|
136
|
-
*/
|
|
137
|
-
queueDelete(entityName) {
|
|
138
|
-
if (this.isShutdown) {
|
|
139
|
-
throw new Error('Indexer is shutdown');
|
|
140
|
-
}
|
|
141
|
-
// Remove any existing operations for this entity
|
|
142
|
-
this.removeFromQueue(entityName);
|
|
143
|
-
this.queue.push({
|
|
144
|
-
type: 'delete',
|
|
145
|
-
entityName,
|
|
146
|
-
queuedAt: Date.now(),
|
|
147
|
-
});
|
|
148
|
-
this.checkAutoFlush();
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Remove all queued operations for an entity.
|
|
152
|
-
*
|
|
153
|
-
* @param entityName - Entity name to remove from queue
|
|
154
|
-
*/
|
|
155
|
-
removeFromQueue(entityName) {
|
|
156
|
-
this.queue = this.queue.filter(op => op.entityName !== entityName);
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Check if auto-flush threshold is reached.
|
|
160
|
-
*/
|
|
161
|
-
checkAutoFlush() {
|
|
162
|
-
if (this.queue.length >= this.options.flushThreshold && !this.isFlushing) {
|
|
163
|
-
// Use setImmediate to avoid blocking
|
|
164
|
-
setImmediate(() => this.flush());
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Flush the queue and process all pending operations.
|
|
169
|
-
*
|
|
170
|
-
* @returns Result of the flush operation
|
|
171
|
-
*/
|
|
172
|
-
async flush() {
|
|
173
|
-
if (this.isFlushing) {
|
|
174
|
-
return {
|
|
175
|
-
processed: 0,
|
|
176
|
-
succeeded: 0,
|
|
177
|
-
failed: 0,
|
|
178
|
-
errors: [],
|
|
179
|
-
durationMs: 0,
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
this.isFlushing = true;
|
|
183
|
-
const startTime = Date.now();
|
|
184
|
-
const errors = [];
|
|
185
|
-
let succeeded = 0;
|
|
186
|
-
let failed = 0;
|
|
187
|
-
// Take the current queue
|
|
188
|
-
const operations = [...this.queue];
|
|
189
|
-
this.queue = [];
|
|
190
|
-
try {
|
|
191
|
-
// Separate operations by type
|
|
192
|
-
const createOps = operations.filter(op => op.type === 'create');
|
|
193
|
-
const updateOps = operations.filter(op => op.type === 'update');
|
|
194
|
-
const deleteOps = operations.filter(op => op.type === 'delete');
|
|
195
|
-
// Process deletes first (fast, O(1))
|
|
196
|
-
for (const op of deleteOps) {
|
|
197
|
-
try {
|
|
198
|
-
this.vectorStore.remove(op.entityName);
|
|
199
|
-
succeeded++;
|
|
200
|
-
}
|
|
201
|
-
catch (error) {
|
|
202
|
-
failed++;
|
|
203
|
-
errors.push({
|
|
204
|
-
entityName: op.entityName,
|
|
205
|
-
error: error instanceof Error ? error.message : String(error),
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
// Batch process creates and updates together
|
|
210
|
-
const embedOps = [...createOps, ...updateOps];
|
|
211
|
-
if (embedOps.length > 0) {
|
|
212
|
-
const texts = embedOps.map(op => op.text);
|
|
213
|
-
const entityNames = embedOps.map(op => op.entityName);
|
|
214
|
-
try {
|
|
215
|
-
// Check if the embedding service has the batch with progress method
|
|
216
|
-
let embeddings;
|
|
217
|
-
if (this.onProgress && 'embedBatchWithProgress' in this.embeddingService) {
|
|
218
|
-
embeddings = await this.embeddingService
|
|
219
|
-
.embedBatchWithProgress(texts, this.options.embeddingMode, this.onProgress);
|
|
220
|
-
}
|
|
221
|
-
else {
|
|
222
|
-
embeddings = await this.embeddingService.embedBatch(texts, this.options.embeddingMode);
|
|
223
|
-
}
|
|
224
|
-
// Store each embedding
|
|
225
|
-
for (let i = 0; i < embeddings.length; i++) {
|
|
226
|
-
try {
|
|
227
|
-
this.vectorStore.add(entityNames[i], embeddings[i]);
|
|
228
|
-
succeeded++;
|
|
229
|
-
}
|
|
230
|
-
catch (error) {
|
|
231
|
-
failed++;
|
|
232
|
-
errors.push({
|
|
233
|
-
entityName: entityNames[i],
|
|
234
|
-
error: error instanceof Error ? error.message : String(error),
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
catch (error) {
|
|
240
|
-
// Batch embedding failed, count all as failed
|
|
241
|
-
failed += embedOps.length;
|
|
242
|
-
for (const op of embedOps) {
|
|
243
|
-
errors.push({
|
|
244
|
-
entityName: op.entityName,
|
|
245
|
-
error: error instanceof Error ? error.message : String(error),
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
finally {
|
|
252
|
-
this.isFlushing = false;
|
|
253
|
-
}
|
|
254
|
-
return {
|
|
255
|
-
processed: operations.length,
|
|
256
|
-
succeeded,
|
|
257
|
-
failed,
|
|
258
|
-
errors,
|
|
259
|
-
durationMs: Date.now() - startTime,
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Get the current queue size.
|
|
264
|
-
*
|
|
265
|
-
* @returns Number of operations in the queue
|
|
266
|
-
*/
|
|
267
|
-
getQueueSize() {
|
|
268
|
-
return this.queue.length;
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Get the current queue contents (for debugging/monitoring).
|
|
272
|
-
*
|
|
273
|
-
* @returns Copy of the current queue
|
|
274
|
-
*/
|
|
275
|
-
getQueue() {
|
|
276
|
-
return [...this.queue];
|
|
277
|
-
}
|
|
278
|
-
/**
|
|
279
|
-
* Check if the indexer is currently flushing.
|
|
280
|
-
*
|
|
281
|
-
* @returns True if flushing
|
|
282
|
-
*/
|
|
283
|
-
isBusy() {
|
|
284
|
-
return this.isFlushing;
|
|
285
|
-
}
|
|
286
|
-
/**
|
|
287
|
-
* Check if the indexer is shutdown.
|
|
288
|
-
*
|
|
289
|
-
* @returns True if shutdown
|
|
290
|
-
*/
|
|
291
|
-
isShutdownComplete() {
|
|
292
|
-
return this.isShutdown;
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* Graceful shutdown with final flush.
|
|
296
|
-
*
|
|
297
|
-
* Stops the timer and flushes any remaining operations.
|
|
298
|
-
*
|
|
299
|
-
* @returns Result of the final flush
|
|
300
|
-
*/
|
|
301
|
-
async shutdown() {
|
|
302
|
-
this.isShutdown = true;
|
|
303
|
-
this.stopFlushTimer();
|
|
304
|
-
// Wait for any in-progress flush to complete
|
|
305
|
-
while (this.isFlushing) {
|
|
306
|
-
await new Promise(resolve => setTimeout(resolve, 10));
|
|
307
|
-
}
|
|
308
|
-
// Final flush
|
|
309
|
-
return this.flush();
|
|
310
|
-
}
|
|
311
|
-
/**
|
|
312
|
-
* Update indexer options.
|
|
313
|
-
*
|
|
314
|
-
* Note: Changes to flushIntervalMs will take effect on the next interval.
|
|
315
|
-
*
|
|
316
|
-
* @param options - New options to apply
|
|
317
|
-
*/
|
|
318
|
-
updateOptions(options) {
|
|
319
|
-
const { onProgress, ...rest } = options;
|
|
320
|
-
if (onProgress !== undefined) {
|
|
321
|
-
this.onProgress = onProgress;
|
|
322
|
-
}
|
|
323
|
-
if (Object.keys(rest).length > 0) {
|
|
324
|
-
this.options = { ...this.options, ...rest };
|
|
325
|
-
// Restart timer if interval changed
|
|
326
|
-
if (rest.flushIntervalMs !== undefined) {
|
|
327
|
-
this.stopFlushTimer();
|
|
328
|
-
this.startFlushTimer();
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
/**
|
|
333
|
-
* Clear the queue without processing.
|
|
334
|
-
*
|
|
335
|
-
* @returns Number of operations cleared
|
|
336
|
-
*/
|
|
337
|
-
clearQueue() {
|
|
338
|
-
const count = this.queue.length;
|
|
339
|
-
this.queue = [];
|
|
340
|
-
return count;
|
|
341
|
-
}
|
|
342
|
-
}
|
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Optimized Inverted Index
|
|
3
|
-
*
|
|
4
|
-
* Memory-efficient inverted index using integer IDs and Uint32Array
|
|
5
|
-
* for fast multi-term intersection queries.
|
|
6
|
-
*
|
|
7
|
-
* Phase 12 Sprint 3: Search Algorithm Optimization
|
|
8
|
-
*
|
|
9
|
-
* @module search/OptimizedInvertedIndex
|
|
10
|
-
*/
|
|
11
|
-
/**
|
|
12
|
-
* Statistics about memory usage.
|
|
13
|
-
*/
|
|
14
|
-
export interface IndexMemoryUsage {
|
|
15
|
-
/** Total bytes used by posting lists */
|
|
16
|
-
postingListBytes: number;
|
|
17
|
-
/** Total bytes used by ID map */
|
|
18
|
-
idMapBytes: number;
|
|
19
|
-
/** Total bytes used by term index */
|
|
20
|
-
termIndexBytes: number;
|
|
21
|
-
/** Total estimated memory usage in bytes */
|
|
22
|
-
totalBytes: number;
|
|
23
|
-
/** Number of unique terms */
|
|
24
|
-
termCount: number;
|
|
25
|
-
/** Number of documents indexed */
|
|
26
|
-
documentCount: number;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Result from a posting list lookup.
|
|
30
|
-
*/
|
|
31
|
-
export interface PostingListResult {
|
|
32
|
-
/** Term that was looked up */
|
|
33
|
-
term: string;
|
|
34
|
-
/** Document IDs containing the term (sorted) */
|
|
35
|
-
docIds: Uint32Array;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Optimized Inverted Index using integer document IDs.
|
|
39
|
-
*
|
|
40
|
-
* Memory Optimizations:
|
|
41
|
-
* 1. Uses integer IDs instead of string entity names
|
|
42
|
-
* 2. Stores posting lists as Uint32Array (4 bytes per ID vs ~20+ bytes per string)
|
|
43
|
-
* 3. Maintains sorted posting lists for efficient intersection
|
|
44
|
-
*
|
|
45
|
-
* Performance Optimizations:
|
|
46
|
-
* 1. Sorted array intersection is O(n+m) where n,m are posting list lengths
|
|
47
|
-
* 2. Early termination when one list is exhausted
|
|
48
|
-
* 3. Binary search available for unbalanced list sizes
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* ```typescript
|
|
52
|
-
* const index = new OptimizedInvertedIndex();
|
|
53
|
-
* index.addDocument('entity1', ['machine', 'learning', 'ai']);
|
|
54
|
-
* index.addDocument('entity2', ['deep', 'learning', 'neural']);
|
|
55
|
-
*
|
|
56
|
-
* // Find documents containing both 'machine' AND 'learning'
|
|
57
|
-
* const results = index.intersect(['machine', 'learning']);
|
|
58
|
-
* console.log(results); // ['entity1']
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
export declare class OptimizedInvertedIndex {
|
|
62
|
-
/** Map from entity name to integer ID */
|
|
63
|
-
private entityToId;
|
|
64
|
-
/** Map from integer ID to entity name */
|
|
65
|
-
private idToEntity;
|
|
66
|
-
/** Next available ID */
|
|
67
|
-
private nextId;
|
|
68
|
-
/** Inverted index: term -> sorted array of document IDs */
|
|
69
|
-
private postingLists;
|
|
70
|
-
/** Temporary posting lists (before finalization) */
|
|
71
|
-
private tempPostingLists;
|
|
72
|
-
/** Whether the index is finalized (posting lists converted to Uint32Array) */
|
|
73
|
-
private finalized;
|
|
74
|
-
/**
|
|
75
|
-
* Add a document to the index.
|
|
76
|
-
*
|
|
77
|
-
* @param entityName - Unique document identifier
|
|
78
|
-
* @param terms - Array of terms in the document (should be lowercase)
|
|
79
|
-
*/
|
|
80
|
-
addDocument(entityName: string, terms: string[]): void;
|
|
81
|
-
/**
|
|
82
|
-
* Remove a document from the index.
|
|
83
|
-
*
|
|
84
|
-
* @param entityName - Document to remove
|
|
85
|
-
* @returns True if document was found and removed
|
|
86
|
-
*/
|
|
87
|
-
removeDocument(entityName: string): boolean;
|
|
88
|
-
/**
|
|
89
|
-
* Finalize the index by converting posting lists to Uint32Array.
|
|
90
|
-
*
|
|
91
|
-
* This should be called after bulk indexing for optimal memory usage.
|
|
92
|
-
* The index can still be updated after finalization, but it will
|
|
93
|
-
* temporarily use more memory during updates.
|
|
94
|
-
*/
|
|
95
|
-
finalize(): void;
|
|
96
|
-
/**
|
|
97
|
-
* Convert finalized index back to mutable format.
|
|
98
|
-
*/
|
|
99
|
-
private unfinalize;
|
|
100
|
-
/**
|
|
101
|
-
* Get posting list for a term.
|
|
102
|
-
*
|
|
103
|
-
* @param term - Term to look up
|
|
104
|
-
* @returns Posting list result or null if term not found
|
|
105
|
-
*/
|
|
106
|
-
getPostingList(term: string): PostingListResult | null;
|
|
107
|
-
/**
|
|
108
|
-
* Perform intersection of posting lists for multiple terms.
|
|
109
|
-
*
|
|
110
|
-
* Returns entity names that contain ALL specified terms.
|
|
111
|
-
*
|
|
112
|
-
* @param terms - Array of terms to intersect
|
|
113
|
-
* @returns Array of entity names containing all terms
|
|
114
|
-
*/
|
|
115
|
-
intersect(terms: string[]): string[];
|
|
116
|
-
/**
|
|
117
|
-
* Perform union of posting lists for multiple terms.
|
|
118
|
-
*
|
|
119
|
-
* Returns entity names that contain ANY of the specified terms.
|
|
120
|
-
*
|
|
121
|
-
* @param terms - Array of terms to union
|
|
122
|
-
* @returns Array of entity names containing any term
|
|
123
|
-
*/
|
|
124
|
-
union(terms: string[]): string[];
|
|
125
|
-
/**
|
|
126
|
-
* Get entities containing a single term.
|
|
127
|
-
*
|
|
128
|
-
* @param term - Term to search for
|
|
129
|
-
* @returns Array of entity names containing the term
|
|
130
|
-
*/
|
|
131
|
-
search(term: string): string[];
|
|
132
|
-
/**
|
|
133
|
-
* Intersect two sorted Uint32Arrays.
|
|
134
|
-
*
|
|
135
|
-
* Uses merge-style intersection which is O(n+m).
|
|
136
|
-
*/
|
|
137
|
-
private intersectTwo;
|
|
138
|
-
/**
|
|
139
|
-
* Get memory usage statistics.
|
|
140
|
-
*/
|
|
141
|
-
getMemoryUsage(): IndexMemoryUsage;
|
|
142
|
-
/**
|
|
143
|
-
* Clear the entire index.
|
|
144
|
-
*/
|
|
145
|
-
clear(): void;
|
|
146
|
-
/**
|
|
147
|
-
* Get the number of documents in the index.
|
|
148
|
-
*/
|
|
149
|
-
get documentCount(): number;
|
|
150
|
-
/**
|
|
151
|
-
* Get the number of unique terms in the index.
|
|
152
|
-
*/
|
|
153
|
-
get termCount(): number;
|
|
154
|
-
/**
|
|
155
|
-
* Check if an entity is indexed.
|
|
156
|
-
*/
|
|
157
|
-
hasDocument(entityName: string): boolean;
|
|
158
|
-
/**
|
|
159
|
-
* Check if a term exists in the index.
|
|
160
|
-
*/
|
|
161
|
-
hasTerm(term: string): boolean;
|
|
162
|
-
}
|
|
163
|
-
//# sourceMappingURL=OptimizedInvertedIndex.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"OptimizedInvertedIndex.d.ts","sourceRoot":"","sources":["../../src/search/OptimizedInvertedIndex.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,wCAAwC;IACxC,gBAAgB,EAAE,MAAM,CAAC;IACzB,iCAAiC;IACjC,UAAU,EAAE,MAAM,CAAC;IACnB,qCAAqC;IACrC,cAAc,EAAE,MAAM,CAAC;IACvB,4CAA4C;IAC5C,UAAU,EAAE,MAAM,CAAC;IACnB,6BAA6B;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB,kCAAkC;IAClC,aAAa,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,8BAA8B;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,gDAAgD;IAChD,MAAM,EAAE,WAAW,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,sBAAsB;IACjC,yCAAyC;IACzC,OAAO,CAAC,UAAU,CAAkC;IAEpD,yCAAyC;IACzC,OAAO,CAAC,UAAU,CAAkC;IAEpD,wBAAwB;IACxB,OAAO,CAAC,MAAM,CAAa;IAE3B,2DAA2D;IAC3D,OAAO,CAAC,YAAY,CAAuC;IAE3D,oDAAoD;IACpD,OAAO,CAAC,gBAAgB,CAAoC;IAE5D,8EAA8E;IAC9E,OAAO,CAAC,SAAS,CAAkB;IAEnC;;;;;OAKG;IACH,WAAW,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI;IAiCtD;;;;;OAKG;IACH,cAAc,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO;IA6B3C;;;;;;OAMG;IACH,QAAQ,IAAI,IAAI;IAiBhB;;OAEG;IACH,OAAO,CAAC,UAAU;IAalB;;;;;OAKG;IACH,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,iBAAiB,GAAG,IAAI;IActD;;;;;;;OAOG;IACH,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE;IAqCpC;;;;;;;OAOG;IACH,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE;IAyBhC;;;;;OAKG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE;IAY9B;;;;OAIG;IACH,OAAO,CAAC,YAAY;IAoBpB;;OAEG;IACH,cAAc,IAAI,gBAAgB;IAwClC;;OAEG;IACH,KAAK,IAAI,IAAI;IASb;;OAEG;IACH,IAAI,aAAa,IAAI,MAAM,CAE1B;IAED;;OAEG;IACH,IAAI,SAAS,IAAI,MAAM,CAItB;IAED;;OAEG;IACH,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO;IAIxC;;OAEG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;CAK/B"}
|