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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! Hybrid Collections Registry
//!
//! Tracks collections with hybrid search enabled and stores:
//! - BM25 corpus statistics
//! - Per-collection fusion settings
//! - Column mappings for vector and FTS

use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

use super::bm25::{BM25Config, CorpusStats};
use super::fusion::FusionConfig;
#[cfg(test)]
use super::fusion::FusionMethod;

/// Hybrid collection configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HybridCollectionConfig {
    /// Collection ID (from ruvector.collections table)
    pub collection_id: i32,
    /// Table name
    pub table_name: String,
    /// Schema name (default: public)
    pub schema_name: String,
    /// Vector column name
    pub vector_column: String,
    /// FTS tsvector column name
    pub fts_column: String,
    /// Original text column name (for BM25 stats)
    pub text_column: String,
    /// Primary key column name
    pub pk_column: String,

    /// BM25 configuration
    pub bm25_config: BM25Config,
    /// Fusion configuration
    pub fusion_config: FusionConfig,
    /// Corpus statistics
    pub corpus_stats: CorpusStats,

    /// Prefetch size for each branch
    pub prefetch_k: usize,
    /// Stats refresh interval in seconds
    pub stats_refresh_interval: i64,
    /// Enable parallel branch execution
    pub parallel_enabled: bool,

    /// Created timestamp (Unix epoch)
    pub created_at: i64,
    /// Last modified timestamp
    pub updated_at: i64,
}

impl HybridCollectionConfig {
    /// Create a new hybrid collection configuration
    pub fn new(
        collection_id: i32,
        table_name: String,
        vector_column: String,
        fts_column: String,
        text_column: String,
    ) -> Self {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        Self {
            collection_id,
            table_name,
            schema_name: "public".to_string(),
            vector_column,
            fts_column,
            text_column,
            pk_column: "id".to_string(),
            bm25_config: BM25Config::default(),
            fusion_config: FusionConfig::default(),
            corpus_stats: CorpusStats::default(),
            prefetch_k: 100,
            stats_refresh_interval: 3600, // 1 hour
            parallel_enabled: true,
            created_at: now,
            updated_at: now,
        }
    }

    /// Get fully qualified table name
    pub fn qualified_name(&self) -> String {
        format!("{}.{}", self.schema_name, self.table_name)
    }

    /// Check if stats need refresh
    pub fn needs_stats_refresh(&self) -> bool {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        now - self.corpus_stats.last_update > self.stats_refresh_interval
    }

    /// Update corpus statistics
    pub fn update_stats(&mut self, stats: CorpusStats) {
        self.corpus_stats = stats;
        self.updated_at = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;
    }
}

/// Registry entry for a hybrid collection
#[derive(Debug)]
struct RegistryEntry {
    /// Configuration
    config: HybridCollectionConfig,
    /// Cached IDF values (term -> idf)
    idf_cache: HashMap<String, f32>,
    /// Document frequency cache (term -> doc count)
    df_cache: HashMap<String, u64>,
}

impl RegistryEntry {
    fn new(config: HybridCollectionConfig) -> Self {
        Self {
            config,
            idf_cache: HashMap::new(),
            df_cache: HashMap::new(),
        }
    }
}

/// Hybrid Collections Registry
///
/// Global registry for hybrid-enabled collections.
/// In the PostgreSQL extension, this is backed by the ruvector.hybrid_collections table.
pub struct HybridRegistry {
    /// Collections by ID
    collections_by_id: RwLock<HashMap<i32, RegistryEntry>>,
    /// Collections by name (schema.table -> id)
    collections_by_name: RwLock<HashMap<String, i32>>,
}

impl HybridRegistry {
    /// Create a new registry
    pub fn new() -> Self {
        Self {
            collections_by_id: RwLock::new(HashMap::new()),
            collections_by_name: RwLock::new(HashMap::new()),
        }
    }

    /// Register a collection for hybrid search
    pub fn register(&self, config: HybridCollectionConfig) -> Result<(), RegistryError> {
        let qualified_name = config.qualified_name();
        let collection_id = config.collection_id;

        // Check for duplicates
        {
            let by_name = self.collections_by_name.read();
            if by_name.contains_key(&qualified_name) {
                return Err(RegistryError::AlreadyRegistered(qualified_name));
            }
        }

        // Insert into both maps
        let entry = RegistryEntry::new(config);

        self.collections_by_id.write().insert(collection_id, entry);
        self.collections_by_name
            .write()
            .insert(qualified_name, collection_id);

        Ok(())
    }

    /// Unregister a collection
    pub fn unregister(&self, collection_id: i32) -> Result<(), RegistryError> {
        let entry = self.collections_by_id.write().remove(&collection_id);

        if let Some(entry) = entry {
            let qualified_name = entry.config.qualified_name();
            self.collections_by_name.write().remove(&qualified_name);
            Ok(())
        } else {
            Err(RegistryError::NotFound(collection_id.to_string()))
        }
    }

    /// Get collection by ID
    pub fn get(&self, collection_id: i32) -> Option<HybridCollectionConfig> {
        self.collections_by_id
            .read()
            .get(&collection_id)
            .map(|e| e.config.clone())
    }

    /// Get collection by name
    pub fn get_by_name(&self, name: &str) -> Option<HybridCollectionConfig> {
        let collection_id = self.collections_by_name.read().get(name).copied()?;
        self.get(collection_id)
    }

    /// Update collection configuration
    pub fn update(&self, config: HybridCollectionConfig) -> Result<(), RegistryError> {
        let collection_id = config.collection_id;

        let mut by_id = self.collections_by_id.write();
        if let Some(entry) = by_id.get_mut(&collection_id) {
            entry.config = config;
            Ok(())
        } else {
            Err(RegistryError::NotFound(collection_id.to_string()))
        }
    }

    /// Update corpus statistics for a collection
    pub fn update_stats(
        &self,
        collection_id: i32,
        stats: CorpusStats,
    ) -> Result<(), RegistryError> {
        let mut by_id = self.collections_by_id.write();
        if let Some(entry) = by_id.get_mut(&collection_id) {
            entry.config.update_stats(stats);
            // Clear caches when stats change
            entry.idf_cache.clear();
            entry.df_cache.clear();
            Ok(())
        } else {
            Err(RegistryError::NotFound(collection_id.to_string()))
        }
    }

    /// Set document frequency for a term in a collection
    pub fn set_doc_freq(
        &self,
        collection_id: i32,
        term: &str,
        doc_freq: u64,
    ) -> Result<(), RegistryError> {
        let mut by_id = self.collections_by_id.write();
        if let Some(entry) = by_id.get_mut(&collection_id) {
            entry.df_cache.insert(term.to_string(), doc_freq);
            // Invalidate IDF cache for this term
            entry.idf_cache.remove(term);
            Ok(())
        } else {
            Err(RegistryError::NotFound(collection_id.to_string()))
        }
    }

    /// Get IDF for a term, computing if not cached
    pub fn get_idf(&self, collection_id: i32, term: &str) -> Option<f32> {
        let mut by_id = self.collections_by_id.write();
        let entry = by_id.get_mut(&collection_id)?;

        // Check cache
        if let Some(&idf) = entry.idf_cache.get(term) {
            return Some(idf);
        }

        // Compute IDF
        let df = entry.df_cache.get(term).copied().unwrap_or(0);
        let n = entry.config.corpus_stats.doc_count as f32;
        let df_f = df as f32;

        let idf = if df == 0 {
            (n + 0.5).ln()
        } else {
            ((n - df_f + 0.5) / (df_f + 0.5) + 1.0).ln()
        };

        // Cache and return
        entry.idf_cache.insert(term.to_string(), idf);
        Some(idf)
    }

    /// List all registered collections
    pub fn list(&self) -> Vec<HybridCollectionConfig> {
        self.collections_by_id
            .read()
            .values()
            .map(|e| e.config.clone())
            .collect()
    }

    /// Check if a collection is registered
    pub fn is_registered(&self, collection_id: i32) -> bool {
        self.collections_by_id.read().contains_key(&collection_id)
    }

    /// Get collections needing stats refresh
    pub fn collections_needing_refresh(&self) -> Vec<i32> {
        self.collections_by_id
            .read()
            .iter()
            .filter(|(_, e)| e.config.needs_stats_refresh())
            .map(|(id, _)| *id)
            .collect()
    }

    /// Clear all caches
    pub fn clear_caches(&self) {
        let mut by_id = self.collections_by_id.write();
        for entry in by_id.values_mut() {
            entry.idf_cache.clear();
            entry.df_cache.clear();
        }
    }
}

impl Default for HybridRegistry {
    fn default() -> Self {
        Self::new()
    }
}

/// Registry error types
#[derive(Debug, Clone)]
pub enum RegistryError {
    /// Collection already registered
    AlreadyRegistered(String),
    /// Collection not found
    NotFound(String),
    /// Invalid configuration
    InvalidConfig(String),
    /// Database error
    DatabaseError(String),
}

impl std::fmt::Display for RegistryError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RegistryError::AlreadyRegistered(name) => {
                write!(
                    f,
                    "Collection '{}' is already registered for hybrid search",
                    name
                )
            }
            RegistryError::NotFound(name) => {
                write!(f, "Hybrid collection '{}' not found", name)
            }
            RegistryError::InvalidConfig(msg) => {
                write!(f, "Invalid hybrid configuration: {}", msg)
            }
            RegistryError::DatabaseError(msg) => {
                write!(f, "Database error: {}", msg)
            }
        }
    }
}

impl std::error::Error for RegistryError {}

// Global registry instance
lazy_static::lazy_static! {
    /// Global hybrid collections registry
    pub static ref HYBRID_REGISTRY: Arc<HybridRegistry> = Arc::new(HybridRegistry::new());
}

/// Get the global hybrid registry
pub fn get_registry() -> Arc<HybridRegistry> {
    HYBRID_REGISTRY.clone()
}

/// Configuration update from JSONB
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HybridConfigUpdate {
    /// New fusion method
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_fusion: Option<String>,
    /// New alpha value
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_alpha: Option<f32>,
    /// New RRF k value
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rrf_k: Option<usize>,
    /// New prefetch k value
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub prefetch_k: Option<usize>,
    /// BM25 k1 parameter
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bm25_k1: Option<f32>,
    /// BM25 b parameter
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bm25_b: Option<f32>,
    /// Stats refresh interval (e.g., "1 hour", "30 minutes")
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub stats_refresh_interval: Option<String>,
    /// Enable parallel execution
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parallel_enabled: Option<bool>,
}

impl HybridConfigUpdate {
    /// Apply updates to a configuration
    pub fn apply(&self, config: &mut HybridCollectionConfig) -> Result<(), RegistryError> {
        if let Some(ref fusion) = self.default_fusion {
            config.fusion_config.method = fusion
                .parse()
                .map_err(|e: String| RegistryError::InvalidConfig(e))?;
        }

        if let Some(alpha) = self.default_alpha {
            if !(0.0..=1.0).contains(&alpha) {
                return Err(RegistryError::InvalidConfig(
                    "alpha must be between 0 and 1".into(),
                ));
            }
            config.fusion_config.alpha = alpha;
        }

        if let Some(rrf_k) = self.rrf_k {
            if rrf_k == 0 {
                return Err(RegistryError::InvalidConfig(
                    "rrf_k must be positive".into(),
                ));
            }
            config.fusion_config.rrf_k = rrf_k;
        }

        if let Some(prefetch_k) = self.prefetch_k {
            if prefetch_k == 0 {
                return Err(RegistryError::InvalidConfig(
                    "prefetch_k must be positive".into(),
                ));
            }
            config.prefetch_k = prefetch_k;
        }

        if let Some(k1) = self.bm25_k1 {
            config.bm25_config.k1 = k1.max(0.0);
        }

        if let Some(b) = self.bm25_b {
            config.bm25_config.b = b.clamp(0.0, 1.0);
        }

        if let Some(ref interval) = self.stats_refresh_interval {
            config.stats_refresh_interval = parse_interval(interval)?;
        }

        if let Some(parallel) = self.parallel_enabled {
            config.parallel_enabled = parallel;
        }

        config.updated_at = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        Ok(())
    }
}

/// Parse interval string to seconds
fn parse_interval(s: &str) -> Result<i64, RegistryError> {
    let s = s.trim().to_lowercase();

    // Try common formats
    if let Some(hours) = s.strip_suffix(" hour").or_else(|| s.strip_suffix(" hours")) {
        return hours
            .trim()
            .parse::<i64>()
            .map(|h| h * 3600)
            .map_err(|_| RegistryError::InvalidConfig(format!("Invalid interval: {}", s)));
    }

    if let Some(mins) = s
        .strip_suffix(" minute")
        .or_else(|| s.strip_suffix(" minutes"))
    {
        return mins
            .trim()
            .parse::<i64>()
            .map(|m| m * 60)
            .map_err(|_| RegistryError::InvalidConfig(format!("Invalid interval: {}", s)));
    }

    if let Some(secs) = s
        .strip_suffix(" second")
        .or_else(|| s.strip_suffix(" seconds"))
    {
        return secs
            .trim()
            .parse::<i64>()
            .map_err(|_| RegistryError::InvalidConfig(format!("Invalid interval: {}", s)));
    }

    // Try as plain seconds
    s.parse::<i64>()
        .map_err(|_| RegistryError::InvalidConfig(format!("Invalid interval: {}", s)))
}

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

    #[test]
    fn test_registry_register_get() {
        let registry = HybridRegistry::new();

        let config = HybridCollectionConfig::new(
            1,
            "documents".to_string(),
            "embedding".to_string(),
            "fts".to_string(),
            "content".to_string(),
        );

        registry.register(config.clone()).unwrap();

        let retrieved = registry.get(1).unwrap();
        assert_eq!(retrieved.table_name, "documents");
        assert_eq!(retrieved.vector_column, "embedding");
    }

    #[test]
    fn test_registry_duplicate() {
        let registry = HybridRegistry::new();

        let config = HybridCollectionConfig::new(
            1,
            "documents".to_string(),
            "embedding".to_string(),
            "fts".to_string(),
            "content".to_string(),
        );

        registry.register(config.clone()).unwrap();
        let result = registry.register(config);

        assert!(matches!(result, Err(RegistryError::AlreadyRegistered(_))));
    }

    #[test]
    fn test_registry_get_by_name() {
        let registry = HybridRegistry::new();

        let config = HybridCollectionConfig::new(
            42,
            "my_table".to_string(),
            "vec".to_string(),
            "tsv".to_string(),
            "text".to_string(),
        );

        registry.register(config).unwrap();

        let retrieved = registry.get_by_name("public.my_table").unwrap();
        assert_eq!(retrieved.collection_id, 42);
    }

    #[test]
    fn test_registry_update_stats() {
        let registry = HybridRegistry::new();

        let config = HybridCollectionConfig::new(
            1,
            "test".to_string(),
            "vec".to_string(),
            "fts".to_string(),
            "text".to_string(),
        );

        registry.register(config).unwrap();

        let new_stats = CorpusStats {
            avg_doc_length: 150.0,
            doc_count: 5000,
            total_terms: 500000,
            last_update: 12345,
        };

        registry.update_stats(1, new_stats).unwrap();

        let updated = registry.get(1).unwrap();
        assert!((updated.corpus_stats.avg_doc_length - 150.0).abs() < 0.01);
        assert_eq!(updated.corpus_stats.doc_count, 5000);
    }

    #[test]
    fn test_config_update() {
        let mut config = HybridCollectionConfig::new(
            1,
            "test".to_string(),
            "vec".to_string(),
            "fts".to_string(),
            "text".to_string(),
        );

        let update = HybridConfigUpdate {
            default_fusion: Some("linear".to_string()),
            default_alpha: Some(0.7),
            rrf_k: Some(40),
            prefetch_k: Some(200),
            bm25_k1: Some(1.5),
            bm25_b: Some(0.8),
            stats_refresh_interval: Some("2 hours".to_string()),
            parallel_enabled: Some(false),
        };

        update.apply(&mut config).unwrap();

        assert_eq!(config.fusion_config.method, FusionMethod::Linear);
        assert!((config.fusion_config.alpha - 0.7).abs() < 0.01);
        assert_eq!(config.fusion_config.rrf_k, 40);
        assert_eq!(config.prefetch_k, 200);
        assert!((config.bm25_config.k1 - 1.5).abs() < 0.01);
        assert!((config.bm25_config.b - 0.8).abs() < 0.01);
        assert_eq!(config.stats_refresh_interval, 7200);
        assert!(!config.parallel_enabled);
    }

    #[test]
    fn test_parse_interval() {
        assert_eq!(parse_interval("1 hour").unwrap(), 3600);
        assert_eq!(parse_interval("2 hours").unwrap(), 7200);
        assert_eq!(parse_interval("30 minutes").unwrap(), 1800);
        assert_eq!(parse_interval("60 seconds").unwrap(), 60);
        assert_eq!(parse_interval("120").unwrap(), 120);
    }

    #[test]
    fn test_needs_refresh() {
        let mut config = HybridCollectionConfig::new(
            1,
            "test".to_string(),
            "vec".to_string(),
            "fts".to_string(),
            "text".to_string(),
        );

        // Fresh stats should not need refresh
        config.corpus_stats.last_update = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;
        config.stats_refresh_interval = 3600;

        assert!(!config.needs_stats_refresh());

        // Old stats should need refresh
        config.corpus_stats.last_update -= 7200;
        assert!(config.needs_stats_refresh());
    }
}