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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright (c) 2025 SynaDB Contributors
// Licensed under the SynaDB License. See LICENSE file for details.
//! # SynaDB
//!
//! An AI-native embedded database written in Rust.
//!
//! SynaDB synthesizes the embedded simplicity of SQLite, the columnar analytical
//! speed of DuckDB, and the schema flexibility of MongoDB. It exposes a C-ABI for
//! polyglot integration and is optimized for AI/ML workloads including vector search,
//! tensor operations, model versioning, and experiment tracking.
//!
//! ## Features
//!
//! ### Core Database
//! - **Append-only log structure** - Fast sequential writes, immutable history
//! - **Schema-free** - Store heterogeneous data types without migrations
//! - **Delta & LZ4 compression** - Minimize storage for time-series data
//! - **Crash recovery** - Automatic index rebuild on open
//! - **Thread-safe** - Concurrent read/write access with mutex-protected writes
//!
//! ### Vector Search
//! - **[`vector::VectorStore`]** - Embedding storage with similarity search
//! - **[`mmap_vector::MmapVectorStore`]** - Ultra-high-throughput vector storage (7x faster than VectorStore)
//! - **[`hnsw::HnswIndex`]** - O(log N) approximate nearest neighbor search
//! - **[`gwi::GravityWellIndex`]** - Novel O(N) build time index (faster than HNSW)
//! - **[`cascade::CascadeIndex`]** - Three-stage hybrid index (LSH + bucket tree + graph)
//! - **[`sparse_vector_store::SparseVectorStore`]** - Inverted index for lexical embeddings (SPLADE, BM25, TF-IDF)
//! - **[`distance`]** - Cosine, Euclidean, and Dot Product metrics
//!
//! ### AI/ML Platform
//! - **[`tensor::TensorEngine`]** - Batch tensor operations with chunked storage
//! - **[`model_registry::ModelRegistry`]** - Version models with SHA-256 checksum verification
//! - **[`experiment::ExperimentTracker`]** - Log parameters, metrics, and artifacts
//!
//! ### Performance
//! - **[`mmap::MmapReader`]** - Memory-mapped I/O for zero-copy reads
//! - **[`gpu::GpuContext`]** - GPU Direct memory access (optional `gpu` feature)
//! - **[`faiss_index`](faiss_index/index.html)** - FAISS integration for billion-scale search (optional `faiss` feature)
//!
//! ### Decay-Aware Storage (Experimental)
//! - **[`davo`]** - DAVO: Decay-Aware Value Optimization (optional `davo` feature)
//! - [`davo::DAVOAtom`] - Values with decay metadata
//! - [`davo::FreshnessIndexV2`] - O(k + log N) staleness queries via Forward Decay
//! - [`davo::DecayPredictor`] - Bayesian learning of optimal decay rates
//!
//! ### FFI
//! - **[`ffi`]** - C-ABI interface for Python, Node.js, C++, or any FFI-capable language
//!
//! ## Architecture Philosophy
//!
//! SynaDB uses a **modular architecture** where each component is optimized for its workload:
//!
//! | Component | Purpose |
//! |-----------|---------|
//! | [`SynaDB`] | Core key-value store with history |
//! | [`vector::VectorStore`] | Embedding storage with HNSW search |
//! | [`mmap_vector::MmapVectorStore`] | High-throughput vector ingestion |
//! | [`gwi::GravityWellIndex`] | Fast-build vector index |
//! | [`cascade::CascadeIndex`] | Hybrid three-stage index |
//! | [`sparse_vector_store::SparseVectorStore`] | Lexical search (SPLADE, BM25) |
//! | [`tensor::TensorEngine`] | Batch tensor operations |
//! | [`model_registry::ModelRegistry`] | Model versioning with checksums |
//! | [`experiment::ExperimentTracker`] | Experiment tracking |
//! | [`davo`] | Decay-aware value optimization (Experimental, `davo` feature) |
//!
//! ## Quick Start
//!
//! ### Core Database
//!
//! ```rust,no_run
//! use synadb::{SynaDB, Atom, Result};
//!
//! fn main() -> Result<()> {
//! let mut db = SynaDB::new("my_data.db")?;
//!
//! // Write different data types
//! db.append("temperature", Atom::Float(23.5))?;
//! db.append("count", Atom::Int(42))?;
//! db.append("name", Atom::Text("sensor-1".to_string()))?;
//!
//! // Read values back
//! if let Some(temp) = db.get("temperature")? {
//! println!("Temperature: {:?}", temp);
//! }
//!
//! // Build history for ML
//! db.append("temperature", Atom::Float(24.1))?;
//! db.append("temperature", Atom::Float(24.8))?;
//!
//! // Extract as tensor
//! let history = db.get_history_floats("temperature")?;
//! println!("History: {:?}", history); // [23.5, 24.1, 24.8]
//!
//! Ok(())
//! }
//! ```
//!
//! ### Vector Store
//!
//! ```rust,no_run
//! use synadb::vector::{VectorStore, VectorConfig, SearchResult};
//! use synadb::distance::DistanceMetric;
//!
//! fn main() -> synadb::Result<()> {
//! let config = VectorConfig {
//! dimensions: 768,
//! metric: DistanceMetric::Cosine,
//! ..Default::default()
//! };
//! let mut store = VectorStore::new("vectors.db", config)?;
//!
//! // Insert embeddings
//! let embedding = vec![0.1f32; 768];
//! store.insert("doc1", &embedding)?;
//!
//! // Search for similar vectors
//! let query = vec![0.1f32; 768];
//! let results = store.search(&query, 10)?;
//! for r in results {
//! println!("{}: {:.4}", r.key, r.score);
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ### High-Throughput Vector Ingestion
//!
//! ```rust,no_run
//! use synadb::mmap_vector::{MmapVectorStore, MmapVectorConfig};
//! use synadb::distance::DistanceMetric;
//!
//! fn main() -> synadb::Result<()> {
//! let config = MmapVectorConfig {
//! dimensions: 768,
//! metric: DistanceMetric::Cosine,
//! initial_capacity: 100_000,
//! ..Default::default()
//! };
//! let mut store = MmapVectorStore::new("vectors.mmap", config)?;
//!
//! // Insert vectors one at a time or in batches
//! let embedding = vec![0.1f32; 768];
//! store.insert("doc_0", &embedding)?;
//!
//! // Batch insert (7x faster than VectorStore)
//! let keys: Vec<&str> = vec!["doc_1", "doc_2", "doc_3"];
//! let vecs: Vec<Vec<f32>> = vec![vec![0.1f32; 768]; 3];
//! let vec_refs: Vec<&[f32]> = vecs.iter().map(|v| v.as_slice()).collect();
//! store.insert_batch(&keys, &vec_refs)?;
//!
//! // Build index for fast search
//! store.build_index()?;
//!
//! Ok(())
//! }
//! ```
//!
//! ### Model Registry
//!
//! ```rust,no_run
//! use synadb::model_registry::{ModelRegistry, ModelStage};
//! use std::collections::HashMap;
//!
//! fn main() -> synadb::Result<()> {
//! let mut registry = ModelRegistry::new("models.db")?;
//!
//! // Save model with metadata
//! let model_data = vec![0u8; 1024];
//! let mut metadata = HashMap::new();
//! metadata.insert("accuracy".to_string(), "0.95".to_string());
//! let version = registry.save_model("classifier", &model_data, metadata)?;
//! println!("Saved v{} with checksum {}", version.version, version.checksum);
//!
//! // Load with checksum verification
//! let (data, info) = registry.load_model("classifier", None)?;
//!
//! // Promote to production
//! registry.set_stage("classifier", version.version, ModelStage::Production)?;
//!
//! Ok(())
//! }
//! ```
//!
//! ### Experiment Tracking
//!
//! ```rust,no_run
//! use synadb::experiment::{ExperimentTracker, RunStatus};
//!
//! fn main() -> synadb::Result<()> {
//! let mut tracker = ExperimentTracker::new("experiments.db")?;
//!
//! // Start a run
//! let run_id = tracker.start_run("mnist", vec!["baseline".to_string()])?;
//!
//! // Log parameters and metrics
//! tracker.log_param(&run_id, "learning_rate", "0.001")?;
//! for epoch in 0..100 {
//! let loss = 1.0 / (epoch + 1) as f64;
//! tracker.log_metric(&run_id, "loss", loss, Some(epoch as u64))?;
//! }
//!
//! // End run
//! tracker.end_run(&run_id, RunStatus::Completed)?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Feature Flags
//!
//! | Feature | Description |
//! |---------|-------------|
//! | `gpu` | Enable GPU Direct memory access (requires CUDA) |
//! | `faiss` | Enable FAISS integration for billion-scale search |
//! | `davo` | Enable DAVO: Decay-Aware Value Optimization (Experimental) |
//! | `async` | Enable async runtime for parallel operations |
//!
//! ## Storage Architecture
//!
//! SynaDB uses an append-only log structure. Each entry consists of:
//! - A fixed-size [`LogHeader`] (15 bytes) with timestamp, lengths, and flags
//! - The key as UTF-8 bytes
//! - The value serialized with bincode
//!
//! An in-memory index maps keys to file offsets for O(1) lookups.
// Re-export commonly used types
pub use ;
pub use ;
pub use ;
// Re-export tensor types for high-throughput operations
pub use ;
// Re-export memory-mapped reader for zero-copy access
pub use MmapReader;
// Re-export mmap vector store for ultra-high-throughput writes
pub use ;
// Re-export GPU types for direct memory access
pub use ;
// Re-export Cascade Index for fast O(N) build time
pub use ;
// Re-export vector store types
pub use ;
// Re-export HNSW index types
pub use ;
// Re-export Gravity Well Index
pub use GravityWellIndex;
// Re-export Sparse Vector for lexical embeddings
pub use SparseVector;
// Re-export Sparse Vector Store for inverted index retrieval
pub use ;
// Re-export model registry types
pub use ;
// Re-export experiment tracking types
pub use ;
// Re-export distance metrics
pub use DistanceMetric;