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,137 @@
1
+ /**
2
+ * Storage Backend Factory
3
+ *
4
+ * Auto-detects the best storage backend for the current environment:
5
+ * - Bun/Node.js: BunStorageBackend (file system)
6
+ * - Modern browsers: OPFSBackend (Origin Private File System)
7
+ * - Fallback: MemoryBackend (in-memory)
8
+ */
9
+
10
+ import type { StorageBackend, StorageOptions } from './StorageBackend';
11
+ import { BunStorageBackend } from './BunStorageBackend';
12
+ import { MemoryBackend } from './MemoryBackend';
13
+ import { OPFSBackend } from './OPFSBackend';
14
+
15
+ export type StorageType = 'auto' | 'bun' | 'opfs' | 'memory';
16
+
17
+ export interface CreateStorageOptions extends StorageOptions {
18
+ /** Force a specific storage type */
19
+ type?: StorageType;
20
+ }
21
+
22
+ /**
23
+ * Detect the current runtime environment
24
+ */
25
+ function detectEnvironment(): 'bun' | 'browser' | 'unknown' {
26
+ // Check for Bun
27
+ if (typeof Bun !== 'undefined') {
28
+ return 'bun';
29
+ }
30
+
31
+ // Check for browser
32
+ if (typeof window !== 'undefined' && typeof navigator !== 'undefined') {
33
+ return 'browser';
34
+ }
35
+
36
+ return 'unknown';
37
+ }
38
+
39
+ /**
40
+ * Create the optimal storage backend for the current environment
41
+ *
42
+ * @param options Configuration options
43
+ * @returns Initialized storage backend
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * // Auto-detect best backend
48
+ * const storage = await createStorageBackend();
49
+ *
50
+ * // Force specific backend
51
+ * const bunStorage = await createStorageBackend({ type: 'bun', path: './data' });
52
+ * const memStorage = await createStorageBackend({ type: 'memory' });
53
+ * ```
54
+ */
55
+ export async function createStorageBackend(options?: CreateStorageOptions): Promise<StorageBackend> {
56
+ const type = options?.type ?? 'auto';
57
+
58
+ // Force specific backend type
59
+ if (type === 'bun') {
60
+ const backend = new BunStorageBackend(options?.path ?? './vectordb_data');
61
+ await backend.init();
62
+ return backend;
63
+ }
64
+
65
+ if (type === 'opfs') {
66
+ if (!OPFSBackend.isAvailable()) {
67
+ throw new Error('OPFS not available in this environment');
68
+ }
69
+ const backend = new OPFSBackend();
70
+ await backend.init();
71
+ return backend;
72
+ }
73
+
74
+ if (type === 'memory') {
75
+ return new MemoryBackend();
76
+ }
77
+
78
+ // Auto-detect
79
+ const env = detectEnvironment();
80
+
81
+ if (env === 'bun') {
82
+ const backend = new BunStorageBackend(options?.path ?? './vectordb_data');
83
+ await backend.init();
84
+ return backend;
85
+ }
86
+
87
+ if (env === 'browser') {
88
+ // Try OPFS first (modern browsers)
89
+ if (OPFSBackend.isAvailable()) {
90
+ try {
91
+ const backend = new OPFSBackend();
92
+ await backend.init();
93
+ return backend;
94
+ } catch {
95
+ // Fall through to memory backend
96
+ }
97
+ }
98
+ }
99
+
100
+ // Fallback to memory backend
101
+ return new MemoryBackend();
102
+ }
103
+
104
+ /**
105
+ * Get the recommended storage type for the current environment
106
+ */
107
+ export function getRecommendedStorageType(): StorageType {
108
+ const env = detectEnvironment();
109
+
110
+ if (env === 'bun') {
111
+ return 'bun';
112
+ }
113
+
114
+ if (env === 'browser' && OPFSBackend.isAvailable()) {
115
+ return 'opfs';
116
+ }
117
+
118
+ return 'memory';
119
+ }
120
+
121
+ /**
122
+ * Check if a specific storage type is available
123
+ */
124
+ export function isStorageTypeAvailable(type: StorageType): boolean {
125
+ switch (type) {
126
+ case 'bun':
127
+ return typeof Bun !== 'undefined';
128
+ case 'opfs':
129
+ return OPFSBackend.isAvailable();
130
+ case 'memory':
131
+ return true;
132
+ case 'auto':
133
+ return true;
134
+ default:
135
+ return false;
136
+ }
137
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Storage Module
3
+ *
4
+ * Provides pluggable storage backends for vector index persistence.
5
+ * Supports multiple platforms:
6
+ * - Bun/Node.js: File system with Bun.file APIs
7
+ * - Modern browsers: OPFS (Origin Private File System)
8
+ * - Testing/fallback: In-memory storage
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { createStorageBackend, WriteAheadLog } from 'bun-vector-search/storage';
13
+ *
14
+ * // Auto-detect best storage for current environment
15
+ * const storage = await createStorageBackend();
16
+ *
17
+ * // Use WAL for incremental writes
18
+ * const wal = new WriteAheadLog('./index');
19
+ * await wal.appendVector(1, new Float32Array([1, 2, 3]));
20
+ * await wal.flush();
21
+ * ```
22
+ */
23
+
24
+ // Storage backend interface and types
25
+ export type { StorageBackend, StorageOptions } from './StorageBackend';
26
+
27
+ // Storage backend implementations
28
+ export { BunStorageBackend } from './BunStorageBackend';
29
+ export { MemoryBackend } from './MemoryBackend';
30
+ export { OPFSBackend } from './OPFSBackend';
31
+
32
+ // Factory function and utilities
33
+ export {
34
+ createStorageBackend,
35
+ getRecommendedStorageType,
36
+ isStorageTypeAvailable,
37
+ type StorageType,
38
+ type CreateStorageOptions,
39
+ } from './createStorageBackend';
40
+
41
+ // Write-ahead log for incremental updates
42
+ export {
43
+ WriteAheadLog,
44
+ WALOperationType,
45
+ type WALEntry,
46
+ } from './WriteAheadLog';
47
+
48
+ // Batch write coalescing for reduced I/O
49
+ export {
50
+ BatchWriter,
51
+ createBatchWriter,
52
+ type BatchWriterOptions,
53
+ } from './BatchWriter';