dakera-engine 0.10.2

Vector search engine for the Dakera AI memory platform
Documentation
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! Auto-Index Selection
//!
//! Automatically selects the optimal index type based on data characteristics:
//! - Dataset size (number of vectors)
//! - Dimensionality
//! - Memory constraints
//! - Query latency requirements
//! - Accuracy requirements
//!
//! # Index Selection Matrix
//!
//! | Vectors | Dimensions | Memory | Latency | Recommended Index |
//! |---------|------------|--------|---------|-------------------|
//! | < 10K   | Any        | Any    | Any     | Flat (brute force) |
//! | 10K-100K| < 256      | Low    | Medium  | IVF + SQ8         |
//! | 10K-100K| < 256      | Normal | Low     | HNSW              |
//! | 100K-1M | Any        | Low    | Medium  | IVF-PQ            |
//! | 100K-1M | Any        | Normal | Low     | HNSW              |
//! | > 1M    | Any        | Low    | Any     | IVF-PQ            |
//! | > 1M    | Any        | Normal | Low     | HNSW + SQ8        |

use serde::{Deserialize, Serialize};

/// Memory constraint level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum MemoryConstraint {
    /// Minimize memory usage at all costs
    Minimal,
    /// Balance memory and performance
    #[default]
    Balanced,
    /// Prioritize performance over memory
    Unlimited,
}

/// Query latency requirement
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum LatencyRequirement {
    /// Sub-millisecond queries required
    UltraLow,
    /// < 10ms queries required
    #[default]
    Low,
    /// < 100ms queries acceptable
    Medium,
    /// Latency not critical
    Relaxed,
}

/// Accuracy requirement for search results
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum AccuracyRequirement {
    /// Exact nearest neighbors (recall = 1.0)
    Exact,
    /// Very high recall (> 0.99)
    VeryHigh,
    /// High recall (> 0.95)
    #[default]
    High,
    /// Moderate recall (> 0.90)
    Moderate,
    /// Lower recall acceptable (> 0.80)
    Relaxed,
}

/// Data characteristics for index selection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataCharacteristics {
    /// Expected number of vectors
    pub num_vectors: usize,
    /// Vector dimensionality
    pub dimensions: usize,
    /// Whether vectors will be frequently updated
    pub frequent_updates: bool,
    /// Whether vectors will be frequently deleted
    pub frequent_deletes: bool,
}

/// Requirements for the index
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct IndexRequirements {
    /// Memory constraint level
    pub memory: MemoryConstraint,
    /// Query latency requirement
    pub latency: LatencyRequirement,
    /// Accuracy requirement
    pub accuracy: AccuracyRequirement,
}

/// Recommended index type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RecommendedIndex {
    /// Flat index (brute force) - exact but slow for large datasets
    Flat,
    /// HNSW - fast queries, moderate memory, good for < 1M vectors
    Hnsw,
    /// IVF - balanced performance, works well with clustering
    Ivf,
    /// IVF with Product Quantization - memory efficient for large datasets
    IvfPq,
    /// IVF with Scalar Quantization - good balance of memory and accuracy
    IvfSq,
    /// HNSW with Scalar Quantization - fast queries with reduced memory
    HnswSq,
    /// SPFresh - optimized for cold storage with streaming updates
    SpFresh,
}

impl std::fmt::Display for RecommendedIndex {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RecommendedIndex::Flat => write!(f, "Flat (Brute Force)"),
            RecommendedIndex::Hnsw => write!(f, "HNSW"),
            RecommendedIndex::Ivf => write!(f, "IVF"),
            RecommendedIndex::IvfPq => write!(f, "IVF-PQ"),
            RecommendedIndex::IvfSq => write!(f, "IVF-SQ"),
            RecommendedIndex::HnswSq => write!(f, "HNSW-SQ"),
            RecommendedIndex::SpFresh => write!(f, "SPFresh"),
        }
    }
}

/// Index recommendation with reasoning
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexRecommendation {
    /// Primary recommended index type
    pub primary: RecommendedIndex,
    /// Alternative index types that could work
    pub alternatives: Vec<RecommendedIndex>,
    /// Explanation of why this index was chosen
    pub reasoning: String,
    /// Estimated memory usage in bytes
    pub estimated_memory_bytes: usize,
    /// Estimated query latency in milliseconds
    pub estimated_latency_ms: f32,
    /// Estimated recall/accuracy
    pub estimated_recall: f32,
    /// Suggested configuration parameters
    pub suggested_params: IndexParams,
}

/// Suggested index parameters
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct IndexParams {
    // HNSW params
    pub hnsw_m: Option<usize>,
    pub hnsw_ef_construction: Option<usize>,
    pub hnsw_ef_search: Option<usize>,

    // IVF params
    pub ivf_num_clusters: Option<usize>,
    pub ivf_num_probes: Option<usize>,

    // PQ params
    pub pq_num_subquantizers: Option<usize>,
    pub pq_bits_per_subquantizer: Option<usize>,

    // SQ params
    pub sq_bits: Option<usize>,
}

/// Auto-index selector
pub struct AutoIndexSelector;

impl AutoIndexSelector {
    /// Select the optimal index based on data characteristics and requirements
    pub fn select(
        data: &DataCharacteristics,
        requirements: &IndexRequirements,
    ) -> IndexRecommendation {
        let num_vectors = data.num_vectors;
        let _dimensions = data.dimensions;

        // Decision logic based on dataset size
        let (primary, alternatives, base_reasoning) = if num_vectors < 10_000 {
            // Small dataset: flat index is fine
            if requirements.accuracy == AccuracyRequirement::Exact {
                (
                    RecommendedIndex::Flat,
                    vec![RecommendedIndex::Hnsw],
                    "Small dataset (<10K vectors) - flat index provides exact results efficiently",
                )
            } else {
                (
                    RecommendedIndex::Hnsw,
                    vec![RecommendedIndex::Flat, RecommendedIndex::Ivf],
                    "Small dataset (<10K vectors) - HNSW provides fast approximate search",
                )
            }
        } else if num_vectors < 100_000 {
            // Medium dataset
            match requirements.memory {
                MemoryConstraint::Minimal => {
                    (
                        RecommendedIndex::IvfSq,
                        vec![RecommendedIndex::IvfPq, RecommendedIndex::Ivf],
                        "Medium dataset (10K-100K) with memory constraints - IVF-SQ balances memory and accuracy",
                    )
                }
                MemoryConstraint::Balanced => {
                    match requirements.latency {
                        LatencyRequirement::UltraLow | LatencyRequirement::Low => {
                            (
                                RecommendedIndex::Hnsw,
                                vec![RecommendedIndex::HnswSq, RecommendedIndex::Ivf],
                                "Medium dataset (10K-100K) with low latency requirement - HNSW provides fast queries",
                            )
                        }
                        _ => {
                            (
                                RecommendedIndex::Ivf,
                                vec![RecommendedIndex::Hnsw, RecommendedIndex::IvfPq],
                                "Medium dataset (10K-100K) - IVF provides good balance of speed and memory",
                            )
                        }
                    }
                }
                MemoryConstraint::Unlimited => {
                    (
                        RecommendedIndex::Hnsw,
                        vec![RecommendedIndex::Ivf],
                        "Medium dataset (10K-100K) with no memory constraints - HNSW provides best query performance",
                    )
                }
            }
        } else if num_vectors < 1_000_000 {
            // Large dataset
            match requirements.memory {
                MemoryConstraint::Minimal => {
                    (
                        RecommendedIndex::IvfPq,
                        vec![RecommendedIndex::IvfSq, RecommendedIndex::SpFresh],
                        "Large dataset (100K-1M) with memory constraints - IVF-PQ provides best compression",
                    )
                }
                MemoryConstraint::Balanced => {
                    if data.frequent_updates || data.frequent_deletes {
                        (
                            RecommendedIndex::SpFresh,
                            vec![RecommendedIndex::Hnsw, RecommendedIndex::Ivf],
                            "Large dataset (100K-1M) with frequent updates - SPFresh handles streaming updates well",
                        )
                    } else {
                        match requirements.latency {
                            LatencyRequirement::UltraLow | LatencyRequirement::Low => {
                                (
                                    RecommendedIndex::HnswSq,
                                    vec![RecommendedIndex::Hnsw, RecommendedIndex::IvfPq],
                                    "Large dataset (100K-1M) with low latency requirement - HNSW-SQ balances speed and memory",
                                )
                            }
                            _ => {
                                (
                                    RecommendedIndex::IvfPq,
                                    vec![RecommendedIndex::IvfSq, RecommendedIndex::Hnsw],
                                    "Large dataset (100K-1M) - IVF-PQ provides good compression with acceptable latency",
                                )
                            }
                        }
                    }
                }
                MemoryConstraint::Unlimited => {
                    (
                        RecommendedIndex::Hnsw,
                        vec![RecommendedIndex::HnswSq],
                        "Large dataset (100K-1M) with no memory constraints - HNSW provides best query performance",
                    )
                }
            }
        } else {
            // Very large dataset (> 1M)
            match requirements.memory {
                MemoryConstraint::Minimal => {
                    (
                        RecommendedIndex::IvfPq,
                        vec![RecommendedIndex::SpFresh],
                        "Very large dataset (>1M) with memory constraints - IVF-PQ is essential for memory efficiency",
                    )
                }
                MemoryConstraint::Balanced => {
                    if data.frequent_updates || data.frequent_deletes {
                        (
                            RecommendedIndex::SpFresh,
                            vec![RecommendedIndex::IvfPq],
                            "Very large dataset (>1M) with frequent updates - SPFresh handles streaming workloads",
                        )
                    } else {
                        (
                            RecommendedIndex::IvfPq,
                            vec![RecommendedIndex::HnswSq, RecommendedIndex::SpFresh],
                            "Very large dataset (>1M) - IVF-PQ provides best memory/performance tradeoff",
                        )
                    }
                }
                MemoryConstraint::Unlimited => {
                    (
                        RecommendedIndex::HnswSq,
                        vec![RecommendedIndex::Hnsw, RecommendedIndex::IvfPq],
                        "Very large dataset (>1M) with no memory constraints - HNSW-SQ provides fast queries with reduced memory",
                    )
                }
            }
        };

        // Calculate estimates
        let (estimated_memory, estimated_latency, estimated_recall) =
            Self::estimate_performance(&primary, data, requirements);

        // Generate suggested parameters
        let suggested_params = Self::suggest_params(&primary, data, requirements);

        IndexRecommendation {
            primary,
            alternatives,
            reasoning: base_reasoning.to_string(),
            estimated_memory_bytes: estimated_memory,
            estimated_latency_ms: estimated_latency,
            estimated_recall,
            suggested_params,
        }
    }

    /// Estimate performance characteristics for an index type
    fn estimate_performance(
        index_type: &RecommendedIndex,
        data: &DataCharacteristics,
        requirements: &IndexRequirements,
    ) -> (usize, f32, f32) {
        let n = data.num_vectors;
        let d = data.dimensions;
        let base_vector_size = n * d * 4; // 4 bytes per f32

        match index_type {
            RecommendedIndex::Flat => {
                // Flat: stores all vectors, O(n) search
                let memory = base_vector_size;
                let latency = (n as f32 / 100_000.0) * 10.0; // ~10ms per 100K vectors
                (memory, latency, 1.0) // Exact recall
            }
            RecommendedIndex::Hnsw => {
                // HNSW: vectors + graph structure (~2x overhead)
                let memory = base_vector_size * 2;
                let latency = ((n as f32).log2() * 0.1).max(0.5); // O(log n)
                let recall = match requirements.accuracy {
                    AccuracyRequirement::Exact => 0.999,
                    AccuracyRequirement::VeryHigh => 0.995,
                    _ => 0.98,
                };
                (memory, latency, recall)
            }
            RecommendedIndex::Ivf => {
                // IVF: vectors + centroids
                let num_clusters = (n as f32).sqrt() as usize;
                let memory = base_vector_size + num_clusters * d * 4;
                let latency = (n as f32 / num_clusters as f32 / 10_000.0) * 5.0;
                (memory, latency.max(1.0), 0.95)
            }
            RecommendedIndex::IvfPq => {
                // IVF-PQ: heavily compressed
                let compression_ratio = 32; // Typically 32x compression
                let memory = base_vector_size / compression_ratio;
                let latency = 5.0 + (n as f32 / 1_000_000.0) * 2.0;
                (memory, latency, 0.90)
            }
            RecommendedIndex::IvfSq => {
                // IVF-SQ: 4x compression
                let memory = base_vector_size / 4;
                let latency = 3.0 + (n as f32 / 500_000.0) * 2.0;
                (memory, latency, 0.95)
            }
            RecommendedIndex::HnswSq => {
                // HNSW-SQ: HNSW with quantized vectors
                let memory = (base_vector_size / 4) * 2; // Quantized + graph
                let latency = ((n as f32).log2() * 0.15).max(0.5);
                (memory, latency, 0.96)
            }
            RecommendedIndex::SpFresh => {
                // SPFresh: optimized for cold storage
                let memory = base_vector_size / 2; // Partial in-memory
                let latency = 10.0 + (n as f32 / 100_000.0) * 1.0;
                (memory, latency, 0.92)
            }
        }
    }

    /// Suggest optimal parameters for the index type
    fn suggest_params(
        index_type: &RecommendedIndex,
        data: &DataCharacteristics,
        requirements: &IndexRequirements,
    ) -> IndexParams {
        let mut params = IndexParams::default();
        let n = data.num_vectors;
        let d = data.dimensions;

        match index_type {
            RecommendedIndex::Hnsw | RecommendedIndex::HnswSq => {
                // HNSW parameters
                params.hnsw_m = Some(match requirements.accuracy {
                    AccuracyRequirement::Exact | AccuracyRequirement::VeryHigh => 32,
                    AccuracyRequirement::High => 16,
                    _ => 12,
                });
                params.hnsw_ef_construction = Some(params.hnsw_m.unwrap_or(16) * 10);
                params.hnsw_ef_search = Some(match requirements.latency {
                    LatencyRequirement::UltraLow => 50,
                    LatencyRequirement::Low => 100,
                    LatencyRequirement::Medium => 200,
                    LatencyRequirement::Relaxed => 400,
                });

                if matches!(index_type, RecommendedIndex::HnswSq) {
                    params.sq_bits = Some(8);
                }
            }
            RecommendedIndex::Ivf | RecommendedIndex::IvfPq | RecommendedIndex::IvfSq => {
                // IVF parameters
                let num_clusters = ((n as f32).sqrt() as usize).clamp(16, 65536);
                params.ivf_num_clusters = Some(num_clusters);
                params.ivf_num_probes = Some(
                    match requirements.accuracy {
                        AccuracyRequirement::Exact | AccuracyRequirement::VeryHigh => {
                            num_clusters / 4
                        }
                        AccuracyRequirement::High => num_clusters / 8,
                        AccuracyRequirement::Moderate => num_clusters / 16,
                        AccuracyRequirement::Relaxed => num_clusters / 32,
                    }
                    .max(1),
                );

                if matches!(index_type, RecommendedIndex::IvfPq) {
                    // PQ parameters
                    let num_subquantizers = (d / 4).clamp(1, 64);
                    params.pq_num_subquantizers = Some(num_subquantizers);
                    params.pq_bits_per_subquantizer = Some(8);
                }

                if matches!(index_type, RecommendedIndex::IvfSq) {
                    params.sq_bits = Some(8);
                }
            }
            RecommendedIndex::SpFresh => {
                // SPFresh uses IVF-like clustering
                let num_clusters = ((n as f32).sqrt() as usize).clamp(16, 4096);
                params.ivf_num_clusters = Some(num_clusters);
                params.ivf_num_probes = Some((num_clusters / 10).max(1));
            }
            RecommendedIndex::Flat => {
                // No parameters needed for flat index
            }
        }

        params
    }

    /// Quick recommendation based on just vector count and dimensions
    pub fn quick_select(num_vectors: usize, dimensions: usize) -> RecommendedIndex {
        let data = DataCharacteristics {
            num_vectors,
            dimensions,
            frequent_updates: false,
            frequent_deletes: false,
        };
        let requirements = IndexRequirements::default();
        Self::select(&data, &requirements).primary
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_small_dataset() {
        let data = DataCharacteristics {
            num_vectors: 1000,
            dimensions: 128,
            frequent_updates: false,
            frequent_deletes: false,
        };
        let requirements = IndexRequirements::default();

        let rec = AutoIndexSelector::select(&data, &requirements);
        assert!(matches!(
            rec.primary,
            RecommendedIndex::Hnsw | RecommendedIndex::Flat
        ));
    }

    #[test]
    fn test_large_dataset_memory_constrained() {
        let data = DataCharacteristics {
            num_vectors: 5_000_000,
            dimensions: 768,
            frequent_updates: false,
            frequent_deletes: false,
        };
        let requirements = IndexRequirements {
            memory: MemoryConstraint::Minimal,
            latency: LatencyRequirement::Medium,
            accuracy: AccuracyRequirement::Moderate,
        };

        let rec = AutoIndexSelector::select(&data, &requirements);
        assert_eq!(rec.primary, RecommendedIndex::IvfPq);
        assert!(rec.estimated_memory_bytes < data.num_vectors * data.dimensions * 4 / 10);
    }

    #[test]
    fn test_streaming_workload() {
        let data = DataCharacteristics {
            num_vectors: 500_000,
            dimensions: 256,
            frequent_updates: true,
            frequent_deletes: true,
        };
        let requirements = IndexRequirements {
            memory: MemoryConstraint::Balanced,
            latency: LatencyRequirement::Medium,
            accuracy: AccuracyRequirement::High,
        };

        let rec = AutoIndexSelector::select(&data, &requirements);
        assert_eq!(rec.primary, RecommendedIndex::SpFresh);
    }

    #[test]
    fn test_exact_search() {
        let data = DataCharacteristics {
            num_vectors: 5000,
            dimensions: 64,
            frequent_updates: false,
            frequent_deletes: false,
        };
        let requirements = IndexRequirements {
            memory: MemoryConstraint::Unlimited,
            latency: LatencyRequirement::Relaxed,
            accuracy: AccuracyRequirement::Exact,
        };

        let rec = AutoIndexSelector::select(&data, &requirements);
        assert_eq!(rec.primary, RecommendedIndex::Flat);
        assert_eq!(rec.estimated_recall, 1.0);
    }

    #[test]
    fn test_quick_select() {
        // Small: HNSW or Flat
        let small = AutoIndexSelector::quick_select(5000, 128);
        assert!(matches!(
            small,
            RecommendedIndex::Hnsw | RecommendedIndex::Flat
        ));

        // Medium: HNSW
        let medium = AutoIndexSelector::quick_select(50_000, 256);
        assert!(matches!(
            medium,
            RecommendedIndex::Hnsw | RecommendedIndex::Ivf
        ));

        // Large: usually IVF-PQ or HNSW-SQ
        let large = AutoIndexSelector::quick_select(2_000_000, 512);
        assert!(matches!(
            large,
            RecommendedIndex::IvfPq | RecommendedIndex::HnswSq
        ));
    }

    #[test]
    fn test_suggested_params() {
        let data = DataCharacteristics {
            num_vectors: 100_000,
            dimensions: 128,
            frequent_updates: false,
            frequent_deletes: false,
        };
        let requirements = IndexRequirements::default();

        let rec = AutoIndexSelector::select(&data, &requirements);

        // Should have suggested parameters
        if matches!(rec.primary, RecommendedIndex::Hnsw) {
            assert!(rec.suggested_params.hnsw_m.is_some());
            assert!(rec.suggested_params.hnsw_ef_construction.is_some());
            assert!(rec.suggested_params.hnsw_ef_search.is_some());
        }
    }
}