ruvector-postgres 2.0.5

High-performance PostgreSQL vector database extension v2 - pgvector drop-in replacement with 230+ SQL functions, SIMD acceleration, Flash Attention, GNN layers, hybrid search, multi-tenancy, self-healing, and self-learning capabilities
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
//! IVFFlat (Inverted File with Flat quantization) index implementation
//!
//! Provides approximate nearest neighbor search by partitioning vectors into clusters.

use std::cmp::Ordering;
use std::collections::BinaryHeap;

use dashmap::DashMap;
use parking_lot::RwLock;
use rayon::prelude::*;

use crate::distance::{distance, DistanceMetric};

/// IVFFlat configuration
#[derive(Debug, Clone)]
pub struct IvfFlatConfig {
    /// Number of clusters (lists)
    pub lists: usize,
    /// Number of lists to probe during search
    pub probes: usize,
    /// Distance metric
    pub metric: DistanceMetric,
    /// K-means iterations for training
    pub kmeans_iterations: usize,
    /// Random seed for reproducibility
    pub seed: u64,
}

impl Default for IvfFlatConfig {
    fn default() -> Self {
        Self {
            lists: 100,
            probes: 1,
            metric: DistanceMetric::Euclidean,
            kmeans_iterations: 10,
            seed: 42,
        }
    }
}

/// Vector ID type
pub type VectorId = u64;

/// Entry in a cluster
#[derive(Debug, Clone)]
struct ClusterEntry {
    id: VectorId,
    vector: Vec<f32>,
}

/// Search result with distance
#[derive(Debug, Clone, Copy)]
struct SearchResult {
    id: VectorId,
    distance: f32,
}

impl PartialEq for SearchResult {
    fn eq(&self, other: &Self) -> bool {
        self.distance == other.distance
    }
}

impl Eq for SearchResult {}

impl PartialOrd for SearchResult {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for SearchResult {
    fn cmp(&self, other: &Self) -> Ordering {
        // Reverse for max-heap
        other
            .distance
            .partial_cmp(&self.distance)
            .unwrap_or(Ordering::Equal)
    }
}

/// IVFFlat Index
pub struct IvfFlatIndex {
    /// Configuration
    config: IvfFlatConfig,
    /// Cluster centroids
    centroids: RwLock<Vec<Vec<f32>>>,
    /// Inverted lists (cluster_id -> vectors)
    lists: DashMap<usize, Vec<ClusterEntry>>,
    /// Vector ID to cluster mapping
    id_to_cluster: DashMap<VectorId, usize>,
    /// Next vector ID
    next_id: std::sync::atomic::AtomicU64,
    /// Total vector count
    vector_count: std::sync::atomic::AtomicUsize,
    /// Dimensions
    dimensions: usize,
    /// Whether the index has been trained
    trained: std::sync::atomic::AtomicBool,
}

impl IvfFlatIndex {
    /// Create a new IVFFlat index
    pub fn new(dimensions: usize, config: IvfFlatConfig) -> Self {
        Self {
            config,
            centroids: RwLock::new(Vec::new()),
            lists: DashMap::new(),
            id_to_cluster: DashMap::new(),
            next_id: std::sync::atomic::AtomicU64::new(0),
            vector_count: std::sync::atomic::AtomicUsize::new(0),
            dimensions,
            trained: std::sync::atomic::AtomicBool::new(false),
        }
    }

    /// Number of vectors in the index
    pub fn len(&self) -> usize {
        self.vector_count.load(std::sync::atomic::Ordering::Relaxed)
    }

    /// Check if index is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Check if index is trained
    pub fn is_trained(&self) -> bool {
        self.trained.load(std::sync::atomic::Ordering::Relaxed)
    }

    /// Calculate distance between vectors
    fn calc_distance(&self, a: &[f32], b: &[f32]) -> f32 {
        distance(a, b, self.config.metric)
    }

    /// Train the index on a sample of vectors
    pub fn train(&self, training_vectors: &[Vec<f32>]) {
        if training_vectors.is_empty() {
            return;
        }

        let n_clusters = self.config.lists.min(training_vectors.len());

        // Initialize centroids using k-means++
        let mut centroids = self.kmeans_plus_plus_init(training_vectors, n_clusters);

        // K-means iterations
        for _ in 0..self.config.kmeans_iterations {
            // Assign vectors to clusters
            let mut cluster_sums: Vec<Vec<f32>> = (0..n_clusters)
                .map(|_| vec![0.0; self.dimensions])
                .collect();
            let mut cluster_counts: Vec<usize> = vec![0; n_clusters];

            for vector in training_vectors {
                let cluster = self.find_nearest_centroid(vector, &centroids);
                for (i, &v) in vector.iter().enumerate() {
                    cluster_sums[cluster][i] += v;
                }
                cluster_counts[cluster] += 1;
            }

            // Update centroids
            for (i, centroid) in centroids.iter_mut().enumerate() {
                if cluster_counts[i] > 0 {
                    for j in 0..self.dimensions {
                        centroid[j] = cluster_sums[i][j] / cluster_counts[i] as f32;
                    }
                }
            }
        }

        *self.centroids.write() = centroids;

        // Initialize empty lists
        for i in 0..n_clusters {
            self.lists.insert(i, Vec::new());
        }

        self.trained
            .store(true, std::sync::atomic::Ordering::Relaxed);
    }

    /// K-means++ initialization
    fn kmeans_plus_plus_init(&self, vectors: &[Vec<f32>], k: usize) -> Vec<Vec<f32>> {
        use rand::prelude::*;
        use rand_chacha::ChaCha8Rng;

        let mut rng = ChaCha8Rng::seed_from_u64(self.config.seed);
        let mut centroids = Vec::with_capacity(k);

        // Choose first centroid randomly
        let first_idx = rng.gen_range(0..vectors.len());
        centroids.push(vectors[first_idx].clone());

        // Choose remaining centroids
        for _ in 1..k {
            let mut distances: Vec<f32> = vectors
                .iter()
                .map(|v| {
                    centroids
                        .iter()
                        .map(|c| self.calc_distance(v, c))
                        .fold(f32::MAX, f32::min)
                })
                .collect();

            // Square distances for probability weighting
            for d in &mut distances {
                *d = *d * *d;
            }

            let total: f32 = distances.iter().sum();
            if total == 0.0 {
                break;
            }

            // Roulette wheel selection
            let target = rng.gen_range(0.0..total);
            let mut cumsum = 0.0;
            let mut selected = 0;
            for (i, d) in distances.iter().enumerate() {
                cumsum += d;
                if cumsum >= target {
                    selected = i;
                    break;
                }
            }

            centroids.push(vectors[selected].clone());
        }

        centroids
    }

    /// Find nearest centroid to a vector
    fn find_nearest_centroid(&self, vector: &[f32], centroids: &[Vec<f32>]) -> usize {
        let mut best_cluster = 0;
        let mut best_dist = f32::MAX;

        for (i, centroid) in centroids.iter().enumerate() {
            let dist = self.calc_distance(vector, centroid);
            if dist < best_dist {
                best_dist = dist;
                best_cluster = i;
            }
        }

        best_cluster
    }

    /// Insert a vector into the index
    pub fn insert(&self, vector: Vec<f32>) -> VectorId {
        assert_eq!(vector.len(), self.dimensions, "Vector dimension mismatch");
        assert!(self.is_trained(), "Index must be trained before insertion");

        let id = self
            .next_id
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);

        let centroids = self.centroids.read();
        let cluster = self.find_nearest_centroid(&vector, &centroids);
        drop(centroids);

        let entry = ClusterEntry { id, vector };

        if let Some(mut list) = self.lists.get_mut(&cluster) {
            list.push(entry);
        }

        self.id_to_cluster.insert(id, cluster);
        self.vector_count
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);

        id
    }

    /// Search for k nearest neighbors
    pub fn search(&self, query: &[f32], k: usize, probes: Option<usize>) -> Vec<(VectorId, f32)> {
        assert_eq!(query.len(), self.dimensions, "Query dimension mismatch");

        if !self.is_trained() {
            return Vec::new();
        }

        let n_probes = probes.unwrap_or(self.config.probes);
        let centroids = self.centroids.read();

        // Find nearest centroids
        let mut centroid_dists: Vec<(usize, f32)> = centroids
            .iter()
            .enumerate()
            .map(|(i, c)| (i, self.calc_distance(query, c)))
            .collect();

        centroid_dists.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal));

        drop(centroids);

        // Search in top probes clusters
        let mut heap = BinaryHeap::new();

        for (cluster_id, _) in centroid_dists.iter().take(n_probes) {
            if let Some(list) = self.lists.get(cluster_id) {
                for entry in list.iter() {
                    let dist = self.calc_distance(query, &entry.vector);
                    heap.push(SearchResult {
                        id: entry.id,
                        distance: dist,
                    });

                    if heap.len() > k {
                        heap.pop();
                    }
                }
            }
        }

        // Convert to sorted results
        let mut results: Vec<_> = heap.into_iter().map(|r| (r.id, r.distance)).collect();
        results.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal));
        results
    }

    /// Parallel search
    pub fn search_parallel(
        &self,
        query: &[f32],
        k: usize,
        probes: Option<usize>,
    ) -> Vec<(VectorId, f32)> {
        assert_eq!(query.len(), self.dimensions, "Query dimension mismatch");

        if !self.is_trained() {
            return Vec::new();
        }

        let n_probes = probes.unwrap_or(self.config.probes);
        let centroids = self.centroids.read();

        // Find nearest centroids
        let mut centroid_dists: Vec<(usize, f32)> = centroids
            .iter()
            .enumerate()
            .map(|(i, c)| (i, self.calc_distance(query, c)))
            .collect();

        centroid_dists.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal));

        drop(centroids);

        // Get cluster IDs to probe
        let probe_clusters: Vec<usize> = centroid_dists
            .iter()
            .take(n_probes)
            .map(|(id, _)| *id)
            .collect();

        // Parallel search across clusters
        let results: Vec<(VectorId, f32)> = probe_clusters
            .par_iter()
            .flat_map(|cluster_id| {
                let mut local_results = Vec::new();
                if let Some(list) = self.lists.get(cluster_id) {
                    for entry in list.iter() {
                        let dist = self.calc_distance(query, &entry.vector);
                        local_results.push((entry.id, dist));
                    }
                }
                local_results
            })
            .collect();

        // Merge and get top k
        let mut heap = BinaryHeap::new();
        for (id, dist) in results {
            heap.push(SearchResult { id, distance: dist });
            if heap.len() > k {
                heap.pop();
            }
        }

        let mut final_results: Vec<_> = heap.into_iter().map(|r| (r.id, r.distance)).collect();
        final_results.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal));
        final_results
    }

    /// Get vector by ID
    pub fn get_vector(&self, id: VectorId) -> Option<Vec<f32>> {
        if let Some(cluster) = self.id_to_cluster.get(&id) {
            if let Some(list) = self.lists.get(&*cluster) {
                for entry in list.iter() {
                    if entry.id == id {
                        return Some(entry.vector.clone());
                    }
                }
            }
        }
        None
    }

    /// Get approximate memory usage in bytes
    pub fn memory_usage(&self) -> usize {
        let vector_bytes = self.len() * self.dimensions * 4;
        let centroid_bytes = self.config.lists * self.dimensions * 4;
        vector_bytes + centroid_bytes
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    fn generate_random_vectors(n: usize, dims: usize, seed: u64) -> Vec<Vec<f32>> {
        use rand::prelude::*;
        use rand_chacha::ChaCha8Rng;

        let mut rng = ChaCha8Rng::seed_from_u64(seed);
        (0..n)
            .map(|_| (0..dims).map(|_| rng.gen_range(-1.0..1.0)).collect())
            .collect()
    }

    #[test]
    fn test_train_and_search() {
        let config = IvfFlatConfig {
            lists: 10,
            probes: 3,
            metric: DistanceMetric::Euclidean,
            kmeans_iterations: 5,
            seed: 42,
        };

        let index = IvfFlatIndex::new(16, config);

        // Generate training data
        let training = generate_random_vectors(100, 16, 42);
        index.train(&training);

        assert!(index.is_trained());

        // Insert vectors
        for v in training.iter() {
            index.insert(v.clone());
        }

        assert_eq!(index.len(), 100);

        // Search
        let query = generate_random_vectors(1, 16, 123)[0].clone();
        let results = index.search(&query, 10, None);

        assert_eq!(results.len(), 10);
    }

    #[test]
    fn test_empty_index() {
        let index = IvfFlatIndex::new(8, IvfFlatConfig::default());
        assert!(index.is_empty());
        assert!(!index.is_trained());

        let results = index.search(&[0.0; 8], 10, None);
        assert!(results.is_empty());
    }

    #[test]
    fn test_parallel_search() {
        let config = IvfFlatConfig {
            lists: 20,
            probes: 5,
            metric: DistanceMetric::Euclidean,
            kmeans_iterations: 5,
            seed: 42,
        };

        let index = IvfFlatIndex::new(32, config);

        let training = generate_random_vectors(500, 32, 42);
        index.train(&training);

        for v in training.iter() {
            index.insert(v.clone());
        }

        let query = generate_random_vectors(1, 32, 999)[0].clone();

        let serial = index.search(&query, 10, None);
        let parallel = index.search_parallel(&query, 10, None);

        // Results should be the same
        assert_eq!(serial.len(), parallel.len());
    }
}