1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//! # EdgeVec
//!
//! High-performance embedded vector database for Browser, Node, and Edge.
//!
//! ## Current Status
//!
//! **PHASE 3: Implementation (Week 7 Complete)**
//!
//! **Status:** Week 7 Complete — Persistence Hardened
//!
//! Core vector storage, HNSW graph indexing, and full durability (WAL + Snapshots) are implemented and verified.
//!
//! ## Implemented Features
//!
//! - **HNSW Graph**: Full insertion and search implementation with heuristic optimization.
//! - **Vector Storage**: Contiguous memory layout for fast access.
//! - **Scalar Quantization (SQ8)**: 4x memory reduction (f32 -> u8) with high accuracy.
//! - **Durability**: Write-Ahead Log (WAL) with CRC32 checksums, crash recovery, and atomic snapshots.
//! - **Metrics**: L2 (Euclidean), Cosine, and Dot Product distance functions.
//!
//! ## Development Protocol
//!
//! `EdgeVec` follows a military-grade development protocol:
//!
//! 1. **Architecture Phase** — Design docs must be approved before planning
//! 2. **Planning Phase** — Roadmap must be approved before coding
//! 3. **Implementation Phase** — Weekly tasks must be approved before coding
//! 4. **All gates require `HOSTILE_REVIEWER` approval**
//!
//! ## Example
//!
//! ```rust
//! use edgevec::{HnswConfig, HnswIndex, Metric, VectorStorage};
//!
//! // 1. Create Config
//! let config = HnswConfig::new(128);
//!
//! // 2. Initialize Storage and Index
//! let mut storage = VectorStorage::new(&config, None);
//! let mut index = HnswIndex::new(config, &storage).expect("failed to create index");
//!
//! // 3. Insert Vectors
//! let vector = vec![0.5; 128];
//! let id = index.insert(&vector, &mut storage).expect("failed to insert");
//!
//! // 4. Search
//! let query = vec![0.5; 128];
//! let results = index.search(&query, 10, &storage).expect("failed to search");
//!
//! assert!(!results.is_empty());
//! assert_eq!(results[0].vector_id, id);
//! ```
//!
//! ## Persistence Example
//!
//! ```rust,no_run
//! use edgevec::{HnswConfig, HnswIndex, VectorStorage};
//! use edgevec::persistence::{write_snapshot, read_snapshot, MemoryBackend};
//!
//! // Create index and storage
//! let config = HnswConfig::new(128);
//! let mut storage = VectorStorage::new(&config, None);
//! let mut index = HnswIndex::new(config, &storage).expect("failed to create");
//!
//! // Save snapshot using storage backend
//! let mut backend = MemoryBackend::new();
//! write_snapshot(&index, &storage, &mut backend).expect("failed to save");
//!
//! // Load snapshot
//! let (loaded_index, loaded_storage) = read_snapshot(&backend).expect("failed to load");
//! ```
//!
//! ## Next Steps (Phase 5)
//!
//! 1. **Documentation**: Finalize API docs.
//! 2. **NPM Package**: Release to npm registry.
//! 3. **Performance**: Final tuning and benchmarks.
//!
//! ## Documentation
//!
//! - [Genesis Workflow](docs/GENESIS_WORKFLOW.md)
//! - [Agent Commands](.cursor/commands/README.md)
//! - [Supreme Rules](.cursorrules)
/// Persistence and file format definitions.
/// Unified error handling.
/// Batch insertion API.
/// HNSW Graph implementation.
/// Distance metrics.
/// Vector storage.
/// WASM bindings.
/// Quantization support.
/// SIMD capability detection and runtime optimization.
/// Metadata storage for vector annotations.
/// Filter expression parsing and evaluation.
/// Flat (brute-force) index for binary vectors.
/// Index implementations (FlatIndex, etc.).
/// Sparse vector support for hybrid search.
// =============================================================================
// Index Type Selection
// =============================================================================
/// Index type for vector search.
///
/// EdgeVec supports two index types with different performance characteristics:
///
/// | Index Type | Insert | Search (1M) | Recall | Best For |
/// |------------|--------|-------------|--------|----------|
/// | **Flat** | O(1) ~1μs | O(n) ~5-10ms | 100% (exact) | Real-time apps, <1M vectors |
/// | **HNSW** | O(log n) ~2ms | O(log n) ~2ms | 90-95% | Large datasets, batch insert |
///
/// # Example (Rust)
///
/// ```rust
/// use edgevec::{IndexType, HnswConfig, BinaryFlatIndex};
///
/// // Create a flat index for insert-heavy workloads
/// let flat = BinaryFlatIndex::new(1024);
///
/// // Create an HNSW index for large-scale search
/// let config = HnswConfig::new(1024);
/// let index_type = IndexType::Hnsw(config);
/// ```
/// Hybrid search combining dense and sparse retrieval.
pub use BatchInsertable;
pub use BatchError;
pub use ;
pub use ;
pub use Metric;
// Re-export IndexType (defined in this crate root)
// No `use` statement needed since it's already defined above
pub use ChunkedWriter;
pub use ;
pub use ;
pub use VectorStorage;
pub use ;
pub use ;
/// The crate version string.
pub const VERSION: &str = env!;
/// Returns the crate version string.
///
/// # Returns
///
/// The crate version string.
///
/// # Example
///
/// ```rust
/// let version = edgevec::version();
/// assert!(!version.is_empty());
/// ```