verso-db 0.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.
Files changed (68) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/LICENSE +21 -0
  3. package/README.md +252 -0
  4. package/dist/BinaryHeap.d.ts +25 -0
  5. package/dist/BinaryHeap.d.ts.map +1 -0
  6. package/dist/Collection.d.ts +156 -0
  7. package/dist/Collection.d.ts.map +1 -0
  8. package/dist/HNSWIndex.d.ts +357 -0
  9. package/dist/HNSWIndex.d.ts.map +1 -0
  10. package/dist/MaxBinaryHeap.d.ts +63 -0
  11. package/dist/MaxBinaryHeap.d.ts.map +1 -0
  12. package/dist/Storage.d.ts +54 -0
  13. package/dist/Storage.d.ts.map +1 -0
  14. package/dist/VectorDB.d.ts +44 -0
  15. package/dist/VectorDB.d.ts.map +1 -0
  16. package/dist/backends/DistanceBackend.d.ts +5 -0
  17. package/dist/backends/DistanceBackend.d.ts.map +1 -0
  18. package/dist/backends/JsDistanceBackend.d.ts +37 -0
  19. package/dist/backends/JsDistanceBackend.d.ts.map +1 -0
  20. package/dist/encoding/DeltaEncoder.d.ts +61 -0
  21. package/dist/encoding/DeltaEncoder.d.ts.map +1 -0
  22. package/dist/errors.d.ts +58 -0
  23. package/dist/errors.d.ts.map +1 -0
  24. package/dist/index.d.ts +64 -0
  25. package/dist/index.d.ts.map +1 -0
  26. package/dist/index.js +3732 -0
  27. package/dist/presets.d.ts +91 -0
  28. package/dist/presets.d.ts.map +1 -0
  29. package/dist/quantization/ScalarQuantizer.d.ts +114 -0
  30. package/dist/quantization/ScalarQuantizer.d.ts.map +1 -0
  31. package/dist/storage/BatchWriter.d.ts +104 -0
  32. package/dist/storage/BatchWriter.d.ts.map +1 -0
  33. package/dist/storage/BunStorageBackend.d.ts +58 -0
  34. package/dist/storage/BunStorageBackend.d.ts.map +1 -0
  35. package/dist/storage/MemoryBackend.d.ts +44 -0
  36. package/dist/storage/MemoryBackend.d.ts.map +1 -0
  37. package/dist/storage/OPFSBackend.d.ts +59 -0
  38. package/dist/storage/OPFSBackend.d.ts.map +1 -0
  39. package/dist/storage/StorageBackend.d.ts +66 -0
  40. package/dist/storage/StorageBackend.d.ts.map +1 -0
  41. package/dist/storage/WriteAheadLog.d.ts +111 -0
  42. package/dist/storage/WriteAheadLog.d.ts.map +1 -0
  43. package/dist/storage/createStorageBackend.d.ts +40 -0
  44. package/dist/storage/createStorageBackend.d.ts.map +1 -0
  45. package/dist/storage/index.d.ts +30 -0
  46. package/dist/storage/index.d.ts.map +1 -0
  47. package/package.json +98 -0
  48. package/src/BinaryHeap.ts +131 -0
  49. package/src/Collection.ts +695 -0
  50. package/src/HNSWIndex.ts +1839 -0
  51. package/src/MaxBinaryHeap.ts +175 -0
  52. package/src/Storage.ts +435 -0
  53. package/src/VectorDB.ts +109 -0
  54. package/src/backends/DistanceBackend.ts +17 -0
  55. package/src/backends/JsDistanceBackend.ts +227 -0
  56. package/src/encoding/DeltaEncoder.ts +217 -0
  57. package/src/errors.ts +110 -0
  58. package/src/index.ts +138 -0
  59. package/src/presets.ts +229 -0
  60. package/src/quantization/ScalarQuantizer.ts +383 -0
  61. package/src/storage/BatchWriter.ts +336 -0
  62. package/src/storage/BunStorageBackend.ts +161 -0
  63. package/src/storage/MemoryBackend.ts +120 -0
  64. package/src/storage/OPFSBackend.ts +250 -0
  65. package/src/storage/StorageBackend.ts +74 -0
  66. package/src/storage/WriteAheadLog.ts +326 -0
  67. package/src/storage/createStorageBackend.ts +137 -0
  68. package/src/storage/index.ts +53 -0
@@ -0,0 +1,91 @@
1
+ /**
2
+ * HNSW Parameter Presets
3
+ *
4
+ * These presets are optimized based on extensive benchmarking to achieve
5
+ * the target recall@10 >= 95% for different dataset sizes and dimensions.
6
+ *
7
+ * Key parameters:
8
+ * - M: Maximum connections per node (higher = better recall, more memory)
9
+ * - efConstruction: Beam width during index building (higher = better quality, slower build)
10
+ * - efSearch: Beam width during search (higher = better recall, slower search)
11
+ */
12
+ export interface HNSWPreset {
13
+ name: string;
14
+ description: string;
15
+ M: number;
16
+ efConstruction: number;
17
+ efSearch: number;
18
+ expectedRecall: number;
19
+ targetDimensions: string;
20
+ targetDatasetSize: string;
21
+ }
22
+ /**
23
+ * Preset for low-dimensional vectors (128D or less)
24
+ * Suitable for: Image features, word2vec, GloVe embeddings
25
+ */
26
+ export declare const PRESET_LOW_DIM: HNSWPreset;
27
+ /**
28
+ * Preset for medium-dimensional vectors (256-512D)
29
+ * Suitable for: Sentence embeddings, smaller transformer outputs
30
+ */
31
+ export declare const PRESET_MEDIUM_DIM: HNSWPreset;
32
+ /**
33
+ * Preset for high-dimensional vectors (768D+)
34
+ * Suitable for: BERT, GPT embeddings, Cohere, OpenAI embeddings
35
+ * This is the recommended preset for RAG applications
36
+ *
37
+ * Benchmarked on Cohere Wikipedia 1024D (495K vectors):
38
+ * - efSearch=128: 99.2% recall, 168 QPS, 10.72ms P99
39
+ */
40
+ export declare const PRESET_HIGH_DIM: HNSWPreset;
41
+ /**
42
+ * Preset for very high-dimensional vectors (1536D+)
43
+ * Suitable for: OpenAI text-embedding-ada-002, text-embedding-3-large
44
+ *
45
+ * Scaled from PRESET_HIGH_DIM benchmarks (higher M for higher dimensions)
46
+ */
47
+ export declare const PRESET_VERY_HIGH_DIM: HNSWPreset;
48
+ /**
49
+ * Preset for small datasets (<10K vectors)
50
+ * Prioritizes recall over speed since brute-force is viable
51
+ */
52
+ export declare const PRESET_SMALL_DATASET: HNSWPreset;
53
+ /**
54
+ * Preset for large datasets (100K-1M vectors)
55
+ * Balances recall with build time and memory
56
+ *
57
+ * Benchmarked on Cohere Wikipedia 1024D (495K vectors):
58
+ * - efSearch=128: 99.2% recall, 168 QPS
59
+ */
60
+ export declare const PRESET_LARGE_DATASET: HNSWPreset;
61
+ /**
62
+ * Preset for maximum recall (prioritizes accuracy over speed)
63
+ * Use when recall is critical and latency is acceptable
64
+ */
65
+ export declare const PRESET_MAX_RECALL: HNSWPreset;
66
+ /**
67
+ * Preset for minimum latency (prioritizes speed over recall)
68
+ * Use when latency is critical and 90% recall is acceptable
69
+ */
70
+ export declare const PRESET_LOW_LATENCY: HNSWPreset;
71
+ /**
72
+ * All available presets
73
+ */
74
+ export declare const PRESETS: Record<string, HNSWPreset>;
75
+ /**
76
+ * Get recommended preset based on dimension and dataset size
77
+ *
78
+ * For high-dimensional vectors (768D+), dimension takes priority over dataset size
79
+ * because recall degrades significantly without higher M values.
80
+ */
81
+ export declare function getRecommendedPreset(dimension: number, datasetSize?: number): HNSWPreset;
82
+ /**
83
+ * Get preset by name
84
+ */
85
+ export declare function getPreset(name: string): HNSWPreset | undefined;
86
+ /**
87
+ * RAG-specific preset recommendation
88
+ * For typical RAG applications using popular embedding models
89
+ */
90
+ export declare function getRAGPreset(embeddingModel: string): HNSWPreset;
91
+ //# sourceMappingURL=presets.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"presets.d.ts","sourceRoot":"","sources":["../src/presets.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,CAAC,EAAE,MAAM,CAAC;IACV,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB,EAAE,MAAM,CAAC;IACzB,iBAAiB,EAAE,MAAM,CAAC;CAC3B;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,EAAE,UAS5B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,iBAAiB,EAAE,UAS/B,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,EAAE,UAS7B,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,EAAE,UASlC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,oBAAoB,EAAE,UASlC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,EAAE,UASlC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,iBAAiB,EAAE,UAS/B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,kBAAkB,EAAE,UAShC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAS9C,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,UAAU,CAkBxF;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,CAE9D;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,cAAc,EAAE,MAAM,GAAG,UAAU,CAyB/D"}
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Scalar Quantizer for Int8 vector compression
3
+ *
4
+ * Provides 4x memory reduction by converting Float32 vectors to Int8.
5
+ * Expected performance:
6
+ * - Memory: 4x reduction (32 bits → 8 bits per dimension)
7
+ * - Speed: ~3.5x faster distance calculations (smaller data, better cache)
8
+ * - Recall: <2% loss compared to float32
9
+ *
10
+ * Uses range quantization: maps [min, max] to [-128, 127]
11
+ */
12
+ export interface QuantizationParams {
13
+ min: Float32Array;
14
+ max: Float32Array;
15
+ scale: Float32Array;
16
+ offset: Float32Array;
17
+ }
18
+ export declare class ScalarQuantizer {
19
+ private dimension;
20
+ private params;
21
+ private trained;
22
+ constructor(dimension: number);
23
+ /**
24
+ * Train the quantizer on a set of vectors to determine optimal range
25
+ */
26
+ train(vectors: Float32Array[]): void;
27
+ /**
28
+ * Get training status
29
+ */
30
+ isTrained(): boolean;
31
+ /**
32
+ * Get quantization parameters
33
+ */
34
+ getParams(): QuantizationParams | null;
35
+ /**
36
+ * Set quantization parameters (for loading saved quantizer)
37
+ */
38
+ setParams(params: QuantizationParams): void;
39
+ /**
40
+ * Quantize a single float32 vector to int8
41
+ */
42
+ quantize(vector: Float32Array): Int8Array;
43
+ /**
44
+ * Quantize multiple vectors
45
+ */
46
+ quantizeBatch(vectors: Float32Array[]): Int8Array[];
47
+ /**
48
+ * Dequantize an int8 vector back to float32 (for rescoring)
49
+ */
50
+ dequantize(vector: Int8Array): Float32Array;
51
+ /**
52
+ * Serialize quantization parameters for saving
53
+ */
54
+ serialize(): ArrayBuffer;
55
+ /**
56
+ * Load quantization parameters
57
+ */
58
+ static deserialize(buffer: ArrayBuffer): ScalarQuantizer;
59
+ }
60
+ /**
61
+ * Fast Int8 distance calculations
62
+ * These are optimized for quantized vectors and provide significant speedup
63
+ */
64
+ /**
65
+ * Compute dot product between two Int8 vectors
66
+ * Uses 8-wide unrolling for better ILP (instruction-level parallelism)
67
+ */
68
+ export declare function dotProductInt8(a: Int8Array, b: Int8Array): number;
69
+ /**
70
+ * Compute L2 squared distance between two Int8 vectors
71
+ * Uses 8-wide unrolling for better ILP
72
+ */
73
+ export declare function l2SquaredInt8(a: Int8Array, b: Int8Array): number;
74
+ /**
75
+ * Compute approximate cosine distance for Int8 vectors
76
+ * Note: This is approximate because quantization changes magnitude
77
+ * Uses 8-wide unrolling with separate accumulators for better ILP
78
+ */
79
+ export declare function cosineDistanceInt8(a: Int8Array, b: Int8Array): number;
80
+ /**
81
+ * QuantizedVectorStore - Efficient storage for quantized vectors
82
+ */
83
+ export declare class QuantizedVectorStore {
84
+ private quantizer;
85
+ private vectors;
86
+ private originalVectors;
87
+ private keepOriginals;
88
+ constructor(dimension: number, keepOriginals?: boolean);
89
+ /**
90
+ * Train the quantizer and add vectors
91
+ */
92
+ addVectors(vectors: Float32Array[]): void;
93
+ /**
94
+ * Get quantized vector by index
95
+ */
96
+ getQuantized(index: number): Int8Array;
97
+ /**
98
+ * Get original float32 vector by index (for rescoring)
99
+ */
100
+ getOriginal(index: number): Float32Array | null;
101
+ /**
102
+ * Get number of vectors
103
+ */
104
+ size(): number;
105
+ /**
106
+ * Calculate memory usage in bytes
107
+ */
108
+ memoryUsage(): {
109
+ quantized: number;
110
+ original: number;
111
+ total: number;
112
+ };
113
+ }
114
+ //# sourceMappingURL=ScalarQuantizer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ScalarQuantizer.d.ts","sourceRoot":"","sources":["../../src/quantization/ScalarQuantizer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,MAAM,WAAW,kBAAkB;IACjC,GAAG,EAAE,YAAY,CAAC;IAClB,GAAG,EAAE,YAAY,CAAC;IAClB,KAAK,EAAE,YAAY,CAAC;IACpB,MAAM,EAAE,YAAY,CAAC;CACtB;AAED,qBAAa,eAAe;IAC1B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,MAAM,CAAmC;IACjD,OAAO,CAAC,OAAO,CAAkB;gBAErB,SAAS,EAAE,MAAM;IAI7B;;OAEG;IACH,KAAK,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,IAAI;IAsCpC;;OAEG;IACH,SAAS,IAAI,OAAO;IAIpB;;OAEG;IACH,SAAS,IAAI,kBAAkB,GAAG,IAAI;IAItC;;OAEG;IACH,SAAS,CAAC,MAAM,EAAE,kBAAkB,GAAG,IAAI;IAK3C;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,YAAY,GAAG,SAAS;IAkBzC;;OAEG;IACH,aAAa,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,SAAS,EAAE;IAQnD;;OAEG;IACH,UAAU,CAAC,MAAM,EAAE,SAAS,GAAG,YAAY;IAiB3C;;OAEG;IACH,SAAS,IAAI,WAAW;IAuBxB;;OAEG;IACH,MAAM,CAAC,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,eAAe;CAqBzD;AAED;;;GAGG;AAEH;;;GAGG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,MAAM,CAyBjE;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,MAAM,CAkChE;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,MAAM,CAwCrE;AAED;;GAEG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,SAAS,CAAkB;IACnC,OAAO,CAAC,OAAO,CAAc;IAC7B,OAAO,CAAC,eAAe,CAAwB;IAC/C,OAAO,CAAC,aAAa,CAAU;gBAEnB,SAAS,EAAE,MAAM,EAAE,aAAa,UAAO;IAOnD;;OAEG;IACH,UAAU,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,IAAI;IAazC;;OAEG;IACH,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS;IAItC;;OAEG;IACH,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI;IAK/C;;OAEG;IACH,IAAI,IAAI,MAAM;IAId;;OAEG;IACH,WAAW,IAAI;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE;CAOtE"}
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Batch Write Coalescing for Storage Backends
3
+ *
4
+ * Buffers writes in memory and flushes them in batches to reduce I/O operations.
5
+ * This is especially beneficial for:
6
+ * - OPFS where each write has overhead
7
+ * - IndexedDB where transactions have cost
8
+ * - Network storage where round-trips are expensive
9
+ *
10
+ * Features:
11
+ * - Configurable flush thresholds (count and size)
12
+ * - Automatic flushing when thresholds are reached
13
+ * - Explicit flush for durability guarantees
14
+ * - Coalesces multiple writes to the same key
15
+ */
16
+ import type { StorageBackend } from './StorageBackend';
17
+ export interface BatchWriterOptions {
18
+ /** Maximum number of pending writes before auto-flush (default: 100) */
19
+ maxPendingWrites?: number;
20
+ /** Maximum total size of pending data in bytes before auto-flush (default: 1MB) */
21
+ maxPendingBytes?: number;
22
+ /** Auto-flush interval in milliseconds (0 = disabled, default: 0) */
23
+ autoFlushInterval?: number;
24
+ }
25
+ /**
26
+ * BatchWriter - Coalesces multiple writes into batched flushes
27
+ */
28
+ export declare class BatchWriter {
29
+ private backend;
30
+ private pendingWrites;
31
+ private pendingAppends;
32
+ private pendingBytes;
33
+ private maxPendingWrites;
34
+ private maxPendingBytes;
35
+ private autoFlushInterval;
36
+ private flushTimer;
37
+ private isFlushing;
38
+ private flushPromise;
39
+ constructor(backend: StorageBackend, options?: BatchWriterOptions);
40
+ /**
41
+ * Write data to a key (buffered)
42
+ * Multiple writes to the same key will coalesce to the last value
43
+ */
44
+ write(key: string, data: ArrayBuffer | Uint8Array): Promise<void>;
45
+ /**
46
+ * Append data to a key (buffered)
47
+ * Multiple appends to the same key will be concatenated
48
+ */
49
+ append(key: string, data: ArrayBuffer | Uint8Array): Promise<void>;
50
+ /**
51
+ * Delete a key (buffered)
52
+ * Clears any pending writes/appends for this key
53
+ */
54
+ delete(key: string): Promise<void>;
55
+ /**
56
+ * Read data from a key
57
+ * Returns pending data if available, otherwise reads from backend
58
+ */
59
+ read(key: string): Promise<ArrayBuffer | null>;
60
+ /**
61
+ * Check if thresholds are exceeded and flush if needed
62
+ */
63
+ private checkThresholds;
64
+ /**
65
+ * Flush all pending writes to the backend
66
+ */
67
+ flush(): Promise<void>;
68
+ private doFlush;
69
+ /**
70
+ * Get statistics about pending writes
71
+ */
72
+ getStats(): {
73
+ pendingWrites: number;
74
+ pendingAppends: number;
75
+ pendingBytes: number;
76
+ maxPendingWrites: number;
77
+ maxPendingBytes: number;
78
+ };
79
+ /**
80
+ * Check if there are pending writes
81
+ */
82
+ hasPendingWrites(): boolean;
83
+ /**
84
+ * Start auto-flush timer
85
+ */
86
+ private startAutoFlush;
87
+ /**
88
+ * Stop auto-flush timer
89
+ */
90
+ stopAutoFlush(): void;
91
+ /**
92
+ * Close the batch writer, flushing any pending writes
93
+ */
94
+ close(): Promise<void>;
95
+ /**
96
+ * Get the underlying storage backend
97
+ */
98
+ getBackend(): StorageBackend;
99
+ }
100
+ /**
101
+ * Create a batch writer that wraps an existing storage backend
102
+ */
103
+ export declare function createBatchWriter(backend: StorageBackend, options?: BatchWriterOptions): BatchWriter;
104
+ //# sourceMappingURL=BatchWriter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BatchWriter.d.ts","sourceRoot":"","sources":["../../src/storage/BatchWriter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,MAAM,WAAW,kBAAkB;IACjC,wEAAwE;IACxE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,mFAAmF;IACnF,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,qEAAqE;IACrE,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAQD;;GAEG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,OAAO,CAAiB;IAChC,OAAO,CAAC,aAAa,CAAwC;IAC7D,OAAO,CAAC,cAAc,CAAwC;IAC9D,OAAO,CAAC,YAAY,CAAa;IACjC,OAAO,CAAC,gBAAgB,CAAS;IACjC,OAAO,CAAC,eAAe,CAAS;IAChC,OAAO,CAAC,iBAAiB,CAAS;IAClC,OAAO,CAAC,UAAU,CAA8C;IAChE,OAAO,CAAC,UAAU,CAAkB;IACpC,OAAO,CAAC,YAAY,CAA8B;gBAEtC,OAAO,EAAE,cAAc,EAAE,OAAO,GAAE,kBAAuB;IAWrE;;;OAGG;IACG,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IA4BvE;;;OAGG;IACG,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IA2BxE;;;OAGG;IACG,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAoBxC;;;OAGG;IACG,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;IAuCpD;;OAEG;YACW,eAAe;IAQ7B;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;YAsBd,OAAO;IAqCrB;;OAEG;IACH,QAAQ,IAAI;QACV,aAAa,EAAE,MAAM,CAAC;QACtB,cAAc,EAAE,MAAM,CAAC;QACvB,YAAY,EAAE,MAAM,CAAC;QACrB,gBAAgB,EAAE,MAAM,CAAC;QACzB,eAAe,EAAE,MAAM,CAAC;KACzB;IAUD;;OAEG;IACH,gBAAgB,IAAI,OAAO;IAI3B;;OAEG;IACH,OAAO,CAAC,cAAc;IAUtB;;OAEG;IACH,aAAa,IAAI,IAAI;IAOrB;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAK5B;;OAEG;IACH,UAAU,IAAI,cAAc;CAG7B;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,cAAc,EACvB,OAAO,CAAC,EAAE,kBAAkB,GAC3B,WAAW,CAEb"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Bun Storage Backend
3
+ *
4
+ * High-performance file system storage using Bun's native APIs.
5
+ * Features:
6
+ * - Auto-initializing (no manual init() required)
7
+ * - Automatic memory mapping for large files
8
+ * - Efficient async I/O with O(1) append
9
+ * - Native TypedArray support
10
+ */
11
+ import type { StorageBackend } from './StorageBackend';
12
+ export declare class BunStorageBackend implements StorageBackend {
13
+ readonly type = "bun";
14
+ private basePath;
15
+ private dirCache;
16
+ /**
17
+ * Create a new Bun storage backend
18
+ * @param basePath Base directory for all storage operations
19
+ */
20
+ constructor(basePath?: string);
21
+ /**
22
+ * Ensure directory exists, using cache to avoid redundant mkdir calls
23
+ */
24
+ private ensureDir;
25
+ /**
26
+ * Get the full path for a key
27
+ */
28
+ private getFullPath;
29
+ /**
30
+ * Ensure base directory exists (optional - operations auto-initialize)
31
+ * @deprecated No longer required - write/append create directories automatically
32
+ */
33
+ init(): Promise<void>;
34
+ read(key: string): Promise<ArrayBuffer | null>;
35
+ write(key: string, data: ArrayBuffer | Uint8Array): Promise<void>;
36
+ append(key: string, data: ArrayBuffer | Uint8Array): Promise<void>;
37
+ delete(key: string): Promise<void>;
38
+ exists(key: string): Promise<boolean>;
39
+ list(prefix?: string): Promise<string[]>;
40
+ mkdir(dirPath: string): Promise<void>;
41
+ /**
42
+ * Delete all data in the storage directory
43
+ */
44
+ clear(): Promise<void>;
45
+ /**
46
+ * Get file size without reading the entire file
47
+ */
48
+ size(key: string): Promise<number>;
49
+ /**
50
+ * Read a file as a stream (for large files)
51
+ */
52
+ stream(key: string): ReadableStream<Uint8Array> | null;
53
+ /**
54
+ * Get the base path
55
+ */
56
+ getBasePath(): string;
57
+ }
58
+ //# sourceMappingURL=BunStorageBackend.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BunStorageBackend.d.ts","sourceRoot":"","sources":["../../src/storage/BunStorageBackend.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,qBAAa,iBAAkB,YAAW,cAAc;IACtD,QAAQ,CAAC,IAAI,SAAS;IACtB,OAAO,CAAC,QAAQ,CAAS;IAGzB,OAAO,CAAC,QAAQ,CAA0B;IAE1C;;;OAGG;gBACS,QAAQ,GAAE,MAA0B;IAIhD;;OAEG;YACW,SAAS;IAMvB;;OAEG;IACH,OAAO,CAAC,WAAW;IAInB;;;OAGG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAIrB,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;IAW9C,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IASjE,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAWlE,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IASlC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAMrC,IAAI,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAexC,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAK3C;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAQ5B;;OAEG;IACG,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAWxC;;OAEG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,cAAc,CAAC,UAAU,CAAC,GAAG,IAAI;IAMtD;;OAEG;IACH,WAAW,IAAI,MAAM;CAGtB"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * In-Memory Storage Backend
3
+ *
4
+ * Stores all data in memory using a Map. Useful for:
5
+ * - Testing without file system
6
+ * - Small datasets that fit in memory
7
+ * - Browser environments without OPFS/IndexedDB
8
+ * - Temporary caches
9
+ */
10
+ import type { StorageBackend } from './StorageBackend';
11
+ export declare class MemoryBackend implements StorageBackend {
12
+ readonly type = "memory";
13
+ private storage;
14
+ private directories;
15
+ constructor();
16
+ read(key: string): Promise<ArrayBuffer | null>;
17
+ write(key: string, data: ArrayBuffer | Uint8Array): Promise<void>;
18
+ append(key: string, data: ArrayBuffer | Uint8Array): Promise<void>;
19
+ delete(key: string): Promise<void>;
20
+ exists(key: string): Promise<boolean>;
21
+ list(prefix?: string): Promise<string[]>;
22
+ mkdir(path: string): Promise<void>;
23
+ /**
24
+ * Clear all data
25
+ */
26
+ clear(): void;
27
+ /**
28
+ * Get memory usage statistics
29
+ */
30
+ getStats(): {
31
+ keyCount: number;
32
+ totalBytes: number;
33
+ };
34
+ /**
35
+ * Export all data as a serializable object
36
+ * Useful for debugging or saving state
37
+ */
38
+ export(): Record<string, ArrayBuffer>;
39
+ /**
40
+ * Import data from a serializable object
41
+ */
42
+ import(data: Record<string, ArrayBuffer>): void;
43
+ }
44
+ //# sourceMappingURL=MemoryBackend.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MemoryBackend.d.ts","sourceRoot":"","sources":["../../src/storage/MemoryBackend.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,qBAAa,aAAc,YAAW,cAAc;IAClD,QAAQ,CAAC,IAAI,YAAY;IACzB,OAAO,CAAC,OAAO,CAAuC;IACtD,OAAO,CAAC,WAAW,CAA0B;;IAMvC,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;IAO9C,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAMjE,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAelE,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAIrC,IAAI,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAUxC,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IASxC;;OAEG;IACH,KAAK,IAAI,IAAI;IAMb;;OAEG;IACH,QAAQ,IAAI;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE;IAWpD;;;OAGG;IACH,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC;IAQrC;;OAEG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,GAAG,IAAI;CAKhD"}
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Origin Private File System (OPFS) Storage Backend
3
+ *
4
+ * High-performance browser storage using the File System Access API.
5
+ * OPFS provides file-like semantics with significantly better performance
6
+ * than IndexedDB for large binary data (up to 10x faster).
7
+ *
8
+ * Features:
9
+ * - Auto-initializing (no manual init() required)
10
+ * - High performance for large binary data
11
+ *
12
+ * Browser Support:
13
+ * - Chrome 86+
14
+ * - Safari 15.2+
15
+ * - Firefox 111+
16
+ * - Edge 86+
17
+ *
18
+ * Note: This module is designed for browser environments only.
19
+ * It will not work in Node.js/Bun server environments.
20
+ */
21
+ import type { StorageBackend } from './StorageBackend';
22
+ export declare class OPFSBackend implements StorageBackend {
23
+ readonly type = "opfs";
24
+ private root;
25
+ private initialized;
26
+ /**
27
+ * Initialize the OPFS backend (optional - auto-initializes on first operation)
28
+ * @deprecated No longer required - operations auto-initialize
29
+ */
30
+ init(): Promise<void>;
31
+ /**
32
+ * Ensure backend is initialized (lazy init on first use)
33
+ */
34
+ private ensureInitialized;
35
+ /**
36
+ * Get a file handle, creating parent directories as needed
37
+ */
38
+ private getFileHandle;
39
+ /**
40
+ * Get a directory handle, creating if needed
41
+ */
42
+ private getDirectoryHandle;
43
+ read(key: string): Promise<ArrayBuffer | null>;
44
+ write(key: string, data: ArrayBuffer | Uint8Array): Promise<void>;
45
+ append(key: string, data: ArrayBuffer | Uint8Array): Promise<void>;
46
+ delete(key: string): Promise<void>;
47
+ exists(key: string): Promise<boolean>;
48
+ list(prefix?: string): Promise<string[]>;
49
+ mkdir(path: string): Promise<void>;
50
+ /**
51
+ * Clear all data in OPFS
52
+ */
53
+ clear(): Promise<void>;
54
+ /**
55
+ * Check if OPFS is available in the current environment
56
+ */
57
+ static isAvailable(): boolean;
58
+ }
59
+ //# sourceMappingURL=OPFSBackend.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OPFSBackend.d.ts","sourceRoot":"","sources":["../../src/storage/OPFSBackend.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,qBAAa,WAAY,YAAW,cAAc;IAChD,QAAQ,CAAC,IAAI,UAAU;IACvB,OAAO,CAAC,IAAI,CAA0C;IACtD,OAAO,CAAC,WAAW,CAAkB;IAErC;;;OAGG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAW3B;;OAEG;YACW,iBAAiB;IAM/B;;OAEG;YACW,aAAa;IA0B3B;;OAEG;YACW,kBAAkB;IAkB1B,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;IAc9C,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAkBjE,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAkClE,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAyBlC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAOrC,IAAI,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IA0BxC,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAKxC;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAS5B;;OAEG;IACH,MAAM,CAAC,WAAW,IAAI,OAAO;CAG9B"}
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Storage Backend Interface
3
+ *
4
+ * Provides a unified interface for persisting vector index data across different platforms:
5
+ * - Bun/Node.js: File system storage with Bun.file APIs
6
+ * - Browser: OPFS (Origin Private File System) or IndexedDB
7
+ * - Testing: In-memory storage
8
+ *
9
+ * All operations are async to support both sync and async underlying storage.
10
+ */
11
+ export interface StorageBackend {
12
+ /**
13
+ * Read data from storage
14
+ * @param key Unique key/path for the data
15
+ * @returns ArrayBuffer of data or null if not found
16
+ */
17
+ read(key: string): Promise<ArrayBuffer | null>;
18
+ /**
19
+ * Write data to storage (overwrites existing)
20
+ * @param key Unique key/path for the data
21
+ * @param data Data to write
22
+ */
23
+ write(key: string, data: ArrayBuffer | Uint8Array): Promise<void>;
24
+ /**
25
+ * Append data to existing file (for WAL)
26
+ * @param key Unique key/path for the data
27
+ * @param data Data to append
28
+ */
29
+ append(key: string, data: ArrayBuffer | Uint8Array): Promise<void>;
30
+ /**
31
+ * Delete data from storage
32
+ * @param key Unique key/path to delete
33
+ */
34
+ delete(key: string): Promise<void>;
35
+ /**
36
+ * Check if key exists in storage
37
+ * @param key Unique key/path to check
38
+ */
39
+ exists(key: string): Promise<boolean>;
40
+ /**
41
+ * List all keys with given prefix
42
+ * @param prefix Optional prefix to filter keys
43
+ */
44
+ list(prefix?: string): Promise<string[]>;
45
+ /**
46
+ * Create a directory (for file-based backends)
47
+ * @param path Directory path to create
48
+ */
49
+ mkdir(path: string): Promise<void>;
50
+ /**
51
+ * Get storage type identifier
52
+ */
53
+ readonly type: string;
54
+ }
55
+ /**
56
+ * Options for creating a storage backend
57
+ */
58
+ export interface StorageOptions {
59
+ /** Base path for file-based storage */
60
+ path?: string;
61
+ /** Database name for IndexedDB */
62
+ dbName?: string;
63
+ /** Store name for IndexedDB */
64
+ storeName?: string;
65
+ }
66
+ //# sourceMappingURL=StorageBackend.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StorageBackend.d.ts","sourceRoot":"","sources":["../../src/storage/StorageBackend.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,MAAM,WAAW,cAAc;IAC7B;;;;OAIG;IACH,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAAC;IAE/C;;;;OAIG;IACH,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAElE;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnE;;;OAGG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnC;;;OAGG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAEtC;;;OAGG;IACH,IAAI,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAEzC;;;OAGG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnC;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,uCAAuC;IACvC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,kCAAkC;IAClC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,+BAA+B;IAC/B,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB"}
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Write-Ahead Log (WAL) for Incremental Index Updates
3
+ *
4
+ * Provides durability and efficient incremental writes for HNSW index operations.
5
+ * Instead of rewriting the entire index on each update, operations are appended
6
+ * to a log file. The log can be compacted into a full snapshot periodically.
7
+ *
8
+ * Benefits:
9
+ * - Fast appends (no full serialization)
10
+ * - Crash recovery (replay log after restart)
11
+ * - Reduced I/O for frequent updates
12
+ */
13
+ export declare enum WALOperationType {
14
+ ADD_VECTOR = 1,
15
+ ADD_NEIGHBORS = 2,
16
+ UPDATE_ENTRY_POINT = 3,
17
+ CHECKPOINT = 4
18
+ }
19
+ export interface WALEntry {
20
+ type: WALOperationType;
21
+ timestamp: number;
22
+ data: ArrayBuffer;
23
+ }
24
+ /**
25
+ * WriteAheadLog - Append-only log for incremental index updates
26
+ */
27
+ export declare class WriteAheadLog {
28
+ private logPath;
29
+ private pendingEntries;
30
+ private flushThreshold;
31
+ private lastFlushTime;
32
+ private entryCount;
33
+ /**
34
+ * Create a new WAL
35
+ * @param basePath Base path for the index (WAL will be basePath.wal)
36
+ * @param flushThreshold Number of entries before auto-flush (default: 100)
37
+ */
38
+ constructor(basePath: string, flushThreshold?: number);
39
+ /**
40
+ * Get the WAL file path
41
+ */
42
+ getPath(): string;
43
+ /**
44
+ * Check if WAL file exists
45
+ */
46
+ exists(): Promise<boolean>;
47
+ /**
48
+ * Append a vector addition operation to the log
49
+ */
50
+ appendVector(id: number, vector: Float32Array): Promise<void>;
51
+ /**
52
+ * Append a neighbor update operation to the log
53
+ */
54
+ appendNeighbors(nodeId: number, layer: number, neighbors: number[]): Promise<void>;
55
+ /**
56
+ * Append entry point update to the log
57
+ */
58
+ appendEntryPointUpdate(entryPointId: number, maxLevel: number): Promise<void>;
59
+ /**
60
+ * Write a checkpoint marker to the log
61
+ */
62
+ checkpoint(): Promise<void>;
63
+ /**
64
+ * Serialize a WAL entry to bytes
65
+ */
66
+ private serializeEntry;
67
+ /**
68
+ * Flush pending entries to disk
69
+ * Uses O(1) append instead of O(n) read-modify-write
70
+ */
71
+ flush(): Promise<void>;
72
+ /**
73
+ * Read all entries from the WAL file
74
+ */
75
+ readEntries(): Promise<WALEntry[]>;
76
+ /**
77
+ * Parse a vector addition entry
78
+ */
79
+ static parseVectorEntry(data: ArrayBuffer): {
80
+ id: number;
81
+ vector: Float32Array;
82
+ };
83
+ /**
84
+ * Parse a neighbor update entry
85
+ */
86
+ static parseNeighborsEntry(data: ArrayBuffer): {
87
+ nodeId: number;
88
+ layer: number;
89
+ neighbors: number[];
90
+ };
91
+ /**
92
+ * Parse an entry point update entry
93
+ */
94
+ static parseEntryPointEntry(data: ArrayBuffer): {
95
+ entryPointId: number;
96
+ maxLevel: number;
97
+ };
98
+ /**
99
+ * Get entry count since last compact
100
+ */
101
+ getEntryCount(): number;
102
+ /**
103
+ * Clear the WAL (after successful compaction)
104
+ */
105
+ clear(): Promise<void>;
106
+ /**
107
+ * Delete the WAL file
108
+ */
109
+ delete(): Promise<void>;
110
+ }
111
+ //# sourceMappingURL=WriteAheadLog.d.ts.map