sochdb-vector 2.0.6

Streaming elimination vector search engine for SochDB - CPU-first ANN with RDF + BPS
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// Copyright 2025 SochDB Authors
//
// Licensed under the Apache License, Version 2.0

//! Shard-First ANN Topology
//!
//! This module provides coarse clustering for query routing to minimize
//! fan-out across shards while maintaining high recall.
//!
//! # Problem
//!
//! Hash-based sharding forces broad fan-out:
//! - Query must hit all S shards → S× work
//! - Throughput limited by slowest shard
//! - Network/coordination overhead grows with S
//!
//! # Solution
//!
//! Cluster-based routing:
//! 1. Build K coarse centroids covering all vectors
//! 2. Assign each vector to nearest centroid
//! 3. Route queries to nearest 1-3 centroids only
//! 4. Balance within clusters for hot/cold patterns
//!
//! # Fan-out Analysis
//!
//! For S shards and K clusters:
//! - Hash routing: query all S shards, work = O(S × N/S) = O(N)
//! - Cluster routing: query ~3 shards, work = O(3 × N/S) = O(3N/S)
//! - Speedup: S/3 (e.g., 256 shards → 85× less work)
//!
//! # Trade-offs
//!
//! - Slight recall loss at cluster boundaries (~1-2%)
//! - Centroid computation adds O(K × D) per query
//! - Need rebalancing on insert skew

use std::collections::HashMap;
use std::sync::RwLock;

/// Shard identifier.
pub type ShardId = u32;

/// Cluster identifier.
pub type ClusterId = u32;

/// Coarse centroid for cluster routing.
#[derive(Debug, Clone)]
pub struct Centroid {
    /// Cluster ID.
    pub id: ClusterId,
    /// Centroid vector.
    pub vector: Vec<f32>,
    /// Assigned shards.
    pub shards: Vec<ShardId>,
    /// Vector count in this cluster.
    pub count: usize,
}

impl Centroid {
    /// Create a new centroid.
    pub fn new(id: ClusterId, vector: Vec<f32>) -> Self {
        Self {
            id,
            vector,
            shards: Vec::new(),
            count: 0,
        }
    }

    /// Compute squared L2 distance to query.
    #[inline]
    pub fn distance_squared(&self, query: &[f32]) -> f32 {
        self.vector
            .iter()
            .zip(query.iter())
            .map(|(&a, &b)| {
                let d = a - b;
                d * d
            })
            .sum()
    }
}

/// Routing decision for a query.
#[derive(Debug, Clone)]
pub struct RoutingDecision {
    /// Target shards to query.
    pub shards: Vec<ShardId>,
    /// Distances to cluster centroids.
    pub distances: Vec<f32>,
    /// Number of clusters considered.
    pub clusters_probed: usize,
}

impl RoutingDecision {
    /// Estimated work reduction vs full scan.
    pub fn work_reduction(&self, total_shards: usize) -> f32 {
        if self.shards.is_empty() {
            return 1.0;
        }
        self.shards.len() as f32 / total_shards as f32
    }
}

/// Configuration for shard topology.
#[derive(Debug, Clone)]
pub struct TopologyConfig {
    /// Number of clusters.
    pub num_clusters: usize,
    /// Number of shards per cluster.
    pub shards_per_cluster: usize,
    /// Number of clusters to probe per query.
    pub probe_clusters: usize,
    /// Rebalance threshold (imbalance ratio).
    pub rebalance_threshold: f32,
}

impl Default for TopologyConfig {
    fn default() -> Self {
        Self {
            num_clusters: 16,
            shards_per_cluster: 16,
            probe_clusters: 2,
            rebalance_threshold: 2.0,
        }
    }
}

/// Shard topology with cluster-based routing.
pub struct ShardTopology {
    /// Cluster centroids.
    centroids: Vec<Centroid>,
    /// Shard to cluster mapping.
    shard_to_cluster: HashMap<ShardId, ClusterId>,
    /// Configuration.
    config: TopologyConfig,
    /// Total shards.
    total_shards: usize,
    /// Statistics lock.
    stats: RwLock<TopologyStats>,
}

/// Topology statistics.
#[derive(Debug, Clone, Default)]
pub struct TopologyStats {
    /// Total queries routed.
    pub queries_routed: u64,
    /// Total shards probed.
    pub shards_probed: u64,
    /// Average fan-out.
    pub avg_fanout: f32,
    /// Cluster load distribution.
    pub cluster_loads: Vec<u64>,
}

impl ShardTopology {
    /// Create a new topology with given centroids.
    pub fn new(centroids: Vec<Centroid>, config: TopologyConfig) -> Self {
        let total_shards = centroids.iter().map(|c| c.shards.len()).sum();

        let mut shard_to_cluster = HashMap::new();
        for centroid in &centroids {
            for &shard in &centroid.shards {
                shard_to_cluster.insert(shard, centroid.id);
            }
        }

        let cluster_loads = vec![0; centroids.len()];

        Self {
            centroids,
            shard_to_cluster,
            config,
            total_shards,
            stats: RwLock::new(TopologyStats {
                cluster_loads,
                ..Default::default()
            }),
        }
    }

    /// Build topology from vectors using k-means clustering.
    pub fn build_from_vectors(vectors: &[Vec<f32>], config: TopologyConfig) -> Self {
        if vectors.is_empty() {
            return Self::empty(config);
        }

        let dimension = vectors[0].len();
        let num_clusters = config.num_clusters.min(vectors.len());

        // Simple k-means initialization (random sampling)
        let mut centroids: Vec<Centroid> = (0..num_clusters)
            .map(|i| {
                let idx = (i * vectors.len()) / num_clusters;
                Centroid::new(i as ClusterId, vectors[idx].clone())
            })
            .collect();

        // K-means iterations
        for _ in 0..10 {
            // Assign vectors to nearest centroid
            let mut assignments: Vec<Vec<usize>> = vec![Vec::new(); num_clusters];

            for (vec_idx, vector) in vectors.iter().enumerate() {
                let nearest = Self::find_nearest_centroid(vector, &centroids);
                assignments[nearest].push(vec_idx);
            }

            // Update centroids
            for (cluster_idx, assigned) in assignments.iter().enumerate() {
                if assigned.is_empty() {
                    continue;
                }

                let mut new_centroid = vec![0.0f32; dimension];
                for &vec_idx in assigned {
                    for (i, &v) in vectors[vec_idx].iter().enumerate() {
                        new_centroid[i] += v;
                    }
                }

                let count = assigned.len() as f32;
                for v in &mut new_centroid {
                    *v /= count;
                }

                centroids[cluster_idx].vector = new_centroid;
                centroids[cluster_idx].count = assigned.len();
            }
        }

        // Assign shards to clusters (round-robin for now)
        let _total_shards = config.num_clusters * config.shards_per_cluster;
        for (i, centroid) in centroids.iter_mut().enumerate() {
            let start_shard = i * config.shards_per_cluster;
            let end_shard = start_shard + config.shards_per_cluster;
            centroid.shards = (start_shard..end_shard).map(|s| s as ShardId).collect();
        }

        Self::new(centroids, config)
    }

    /// Create empty topology.
    pub fn empty(config: TopologyConfig) -> Self {
        Self {
            centroids: Vec::new(),
            shard_to_cluster: HashMap::new(),
            config,
            total_shards: 0,
            stats: RwLock::new(TopologyStats::default()),
        }
    }

    /// Route a query to target shards.
    pub fn route(&self, query: &[f32]) -> RoutingDecision {
        if self.centroids.is_empty() {
            return RoutingDecision {
                shards: Vec::new(),
                distances: Vec::new(),
                clusters_probed: 0,
            };
        }

        // Find nearest clusters
        let mut cluster_dists: Vec<(ClusterId, f32)> = self
            .centroids
            .iter()
            .map(|c| (c.id, c.distance_squared(query)))
            .collect();

        cluster_dists.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());

        // Take top probe_clusters
        let probe_count = self.config.probe_clusters.min(cluster_dists.len());
        let probed: Vec<_> = cluster_dists[..probe_count].to_vec();

        // Collect shards from probed clusters
        let mut shards = Vec::new();
        let mut distances = Vec::new();

        for (cluster_id, dist) in &probed {
            if let Some(centroid) = self.centroids.get(*cluster_id as usize) {
                shards.extend_from_slice(&centroid.shards);
                distances.push(*dist);
            }
        }

        // Update stats
        if let Ok(mut stats) = self.stats.write() {
            stats.queries_routed += 1;
            stats.shards_probed += shards.len() as u64;
            stats.avg_fanout = stats.shards_probed as f32 / stats.queries_routed as f32;

            for (cluster_id, _) in &probed {
                if (*cluster_id as usize) < stats.cluster_loads.len() {
                    stats.cluster_loads[*cluster_id as usize] += 1;
                }
            }
        }

        RoutingDecision {
            shards,
            distances,
            clusters_probed: probe_count,
        }
    }

    /// Find which cluster a shard belongs to.
    pub fn shard_cluster(&self, shard: ShardId) -> Option<ClusterId> {
        self.shard_to_cluster.get(&shard).copied()
    }

    /// Get all shards.
    pub fn all_shards(&self) -> Vec<ShardId> {
        self.shard_to_cluster.keys().copied().collect()
    }

    /// Get cluster by ID.
    pub fn cluster(&self, id: ClusterId) -> Option<&Centroid> {
        self.centroids.get(id as usize)
    }

    /// Number of clusters.
    pub fn num_clusters(&self) -> usize {
        self.centroids.len()
    }

    /// Total shards.
    pub fn num_shards(&self) -> usize {
        self.total_shards
    }

    /// Check if rebalancing is needed.
    pub fn needs_rebalance(&self) -> bool {
        if self.centroids.len() < 2 {
            return false;
        }

        let counts: Vec<usize> = self.centroids.iter().map(|c| c.count).collect();
        let max_count = *counts.iter().max().unwrap_or(&1) as f32;
        let min_count = *counts.iter().min().unwrap_or(&1).max(&1) as f32;

        max_count / min_count > self.config.rebalance_threshold
    }

    /// Get topology statistics.
    pub fn stats(&self) -> TopologyStats {
        self.stats.read().unwrap().clone()
    }

    /// Find nearest centroid index.
    fn find_nearest_centroid(vector: &[f32], centroids: &[Centroid]) -> usize {
        centroids
            .iter()
            .enumerate()
            .min_by(|(_, a), (_, b)| {
                a.distance_squared(vector)
                    .partial_cmp(&b.distance_squared(vector))
                    .unwrap()
            })
            .map(|(i, _)| i)
            .unwrap_or(0)
    }
}

/// Router for shard-aware ANN search.
pub struct ShardRouter {
    /// Topology.
    topology: ShardTopology,
    /// Enable adaptive probing.
    #[allow(dead_code)]
    adaptive: bool,
}

impl ShardRouter {
    /// Create a new router.
    pub fn new(topology: ShardTopology) -> Self {
        Self {
            topology,
            adaptive: true,
        }
    }

    /// Route query with adaptive probe depth.
    pub fn route_adaptive(&self, query: &[f32], target_recall: f32) -> RoutingDecision {
        // Adjust probe depth based on target recall
        let base_probe = self.topology.config.probe_clusters;

        let _probe = if target_recall > 0.99 {
            // High recall: probe more clusters
            (base_probe * 2).min(self.topology.num_clusters())
        } else if target_recall > 0.95 {
            base_probe
        } else {
            // Low recall acceptable: probe fewer
            (base_probe / 2).max(1)
        };

        // Temporarily adjust config (clone and modify)
        let mut decision = self.topology.route(query);

        // For high recall, ensure minimum shard coverage
        if target_recall > 0.95 && decision.shards.len() < 4 {
            // Add more shards from nearby clusters
            decision.shards.extend(
                self.topology
                    .all_shards()
                    .into_iter()
                    .take(4 - decision.shards.len()),
            );
        }

        decision
    }

    /// Get estimated recall for routing decision.
    pub fn estimated_recall(&self, decision: &RoutingDecision) -> f32 {
        if self.topology.num_shards() == 0 {
            return 0.0;
        }

        // Simple model: recall ≈ coverage^0.5
        let coverage = decision.shards.len() as f32 / self.topology.num_shards() as f32;
        coverage.sqrt().min(1.0)
    }

    /// Get underlying topology.
    pub fn topology(&self) -> &ShardTopology {
        &self.topology
    }
}

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

    fn random_vectors(count: usize, dim: usize, seed: u64) -> Vec<Vec<f32>> {
        (0..count)
            .map(|i| {
                (0..dim)
                    .map(|d| {
                        let x = ((i as u64 * 13 + d as u64 * 7 + seed) % 1000) as f32 / 1000.0;
                        x * 2.0 - 1.0
                    })
                    .collect()
            })
            .collect()
    }

    #[test]
    fn test_centroid_distance() {
        let centroid = Centroid::new(0, vec![1.0, 0.0, 0.0]);
        let query = vec![0.0, 0.0, 0.0];

        assert!((centroid.distance_squared(&query) - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_topology_build() {
        let vectors = random_vectors(1000, 128, 42);
        let config = TopologyConfig {
            num_clusters: 4,
            shards_per_cluster: 4,
            probe_clusters: 2,
            ..Default::default()
        };

        let topology = ShardTopology::build_from_vectors(&vectors, config);

        assert_eq!(topology.num_clusters(), 4);
        assert_eq!(topology.num_shards(), 16);
    }

    #[test]
    fn test_query_routing() {
        let vectors = random_vectors(1000, 128, 42);
        let config = TopologyConfig {
            num_clusters: 4,
            shards_per_cluster: 4,
            probe_clusters: 2,
            ..Default::default()
        };

        let topology = ShardTopology::build_from_vectors(&vectors, config);
        let query = random_vectors(1, 128, 99)[0].clone();

        let decision = topology.route(&query);

        // Should probe 2 clusters × 4 shards = 8 shards
        assert_eq!(decision.clusters_probed, 2);
        assert_eq!(decision.shards.len(), 8);

        // Work reduction: 8/16 = 0.5
        assert!((decision.work_reduction(16) - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_shard_cluster_mapping() {
        let config = TopologyConfig {
            num_clusters: 4,
            shards_per_cluster: 4,
            ..Default::default()
        };

        let centroids: Vec<Centroid> = (0..4)
            .map(|i| {
                let mut c = Centroid::new(i, vec![i as f32; 128]);
                c.shards = vec![i * 4, i * 4 + 1, i * 4 + 2, i * 4 + 3];
                c
            })
            .collect();

        let topology = ShardTopology::new(centroids, config);

        assert_eq!(topology.shard_cluster(0), Some(0));
        assert_eq!(topology.shard_cluster(5), Some(1));
        assert_eq!(topology.shard_cluster(10), Some(2));
        assert_eq!(topology.shard_cluster(15), Some(3));
    }

    #[test]
    fn test_adaptive_routing() {
        let vectors = random_vectors(1000, 128, 42);
        let config = TopologyConfig {
            num_clusters: 8,
            shards_per_cluster: 4,
            probe_clusters: 2,
            ..Default::default()
        };

        let topology = ShardTopology::build_from_vectors(&vectors, config);
        let router = ShardRouter::new(topology);
        let query = random_vectors(1, 128, 99)[0].clone();

        // Low recall: fewer shards
        let low_recall = router.route_adaptive(&query, 0.80);

        // High recall: more shards
        let high_recall = router.route_adaptive(&query, 0.99);

        // High recall should probe at least as many shards
        assert!(high_recall.shards.len() >= low_recall.shards.len());
    }

    #[test]
    fn test_empty_topology() {
        let config = TopologyConfig::default();
        let topology = ShardTopology::empty(config);

        assert_eq!(topology.num_clusters(), 0);
        assert_eq!(topology.num_shards(), 0);

        let decision = topology.route(&[0.0, 0.0, 0.0]);
        assert!(decision.shards.is_empty());
    }

    #[test]
    fn test_stats_tracking() {
        let vectors = random_vectors(1000, 128, 42);
        let config = TopologyConfig {
            num_clusters: 4,
            shards_per_cluster: 4,
            probe_clusters: 2,
            ..Default::default()
        };

        let topology = ShardTopology::build_from_vectors(&vectors, config);

        // Route multiple queries
        for i in 0..10 {
            let query = random_vectors(1, 128, i)[0].clone();
            topology.route(&query);
        }

        let stats = topology.stats();
        assert_eq!(stats.queries_routed, 10);
        assert!(stats.avg_fanout > 0.0);
    }

    #[test]
    fn test_rebalance_detection() {
        let mut centroids: Vec<Centroid> = (0..4)
            .map(|i| {
                let mut c = Centroid::new(i, vec![i as f32; 128]);
                c.shards = vec![i * 4];
                c.count = if i == 0 { 1000 } else { 100 }; // Imbalanced
                c
            })
            .collect();

        let config = TopologyConfig {
            rebalance_threshold: 2.0,
            ..Default::default()
        };

        let topology = ShardTopology::new(centroids, config);
        assert!(topology.needs_rebalance());
    }

    #[test]
    fn test_estimated_recall() {
        let vectors = random_vectors(100, 128, 42);
        let config = TopologyConfig {
            num_clusters: 4,
            shards_per_cluster: 4,
            probe_clusters: 2,
            ..Default::default()
        };

        let topology = ShardTopology::build_from_vectors(&vectors, config);
        let router = ShardRouter::new(topology);
        let query = random_vectors(1, 128, 99)[0].clone();

        let decision = router.topology().route(&query);
        let recall = router.estimated_recall(&decision);

        // With 50% shard coverage, recall ≈ sqrt(0.5) ≈ 0.707
        assert!(recall > 0.5 && recall < 1.0);
    }
}