oxirs-vec 0.3.1

Vector index abstractions for semantic similarity and AI-augmented querying
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
use crate::store_integration_types::*;
use crate::{
    embeddings::EmbeddingStrategy, rdf_integration::RdfVectorConfig,
    sparql_integration::SparqlVectorService, VectorStoreTrait,
};
use anyhow::Result;
use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::{
    atomic::{AtomicU64, Ordering},
    Arc,
};
use std::time::{Duration, SystemTime};

pub fn new_integrated_store(
    config: StoreIntegrationConfig,
    embedding_strategy: EmbeddingStrategy,
) -> Result<IntegratedVectorStore> {
    let vector_store = Arc::new(RwLock::new(
        crate::VectorStore::with_embedding_strategy(embedding_strategy.clone())?.with_config(
            crate::VectorStoreConfig {
                auto_embed: true,
                cache_embeddings: config.cache_config.enable_vector_cache,
                similarity_threshold: 0.7,
                max_results: 1000,
            },
        ),
    ));

    let rdf_config = RdfVectorConfig::default();
    let vector_store_wrapper = VectorStoreWrapper {
        store: vector_store.clone(),
    };
    let vector_store_trait: Arc<std::sync::RwLock<dyn VectorStoreTrait>> =
        Arc::new(std::sync::RwLock::new(vector_store_wrapper));
    let rdf_integration = Arc::new(RwLock::new(
        crate::rdf_integration::RdfVectorIntegration::new(rdf_config, vector_store_trait),
    ));

    let sparql_config = crate::sparql_integration::VectorServiceConfig::default();
    let sparql_service = Arc::new(RwLock::new(SparqlVectorService::new(
        sparql_config,
        embedding_strategy,
    )?));

    let transaction_manager = Arc::new(TransactionManager::new(config.clone()));
    let streaming_engine = Arc::new(StreamingEngine::new(config.streaming_config.clone()));
    let cache_manager = Arc::new(CacheManager::new(config.cache_config.clone()));
    let replication_manager = Arc::new(ReplicationManager::new(config.replication_config.clone()));
    let consistency_manager = Arc::new(ConsistencyManager::new(config.consistency_level));
    let change_log = Arc::new(ChangeLog::new(10000));
    let metrics = Arc::new(StoreMetrics::default());

    Ok(IntegratedVectorStore {
        config,
        vector_store,
        rdf_integration,
        sparql_service,
        transaction_manager,
        streaming_engine,
        cache_manager,
        replication_manager,
        consistency_manager,
        change_log,
        metrics,
    })
}

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

impl WriteAheadLog {
    pub fn new() -> Self {
        Self {
            log_entries: Arc::new(RwLock::new(std::collections::VecDeque::new())),
            log_file: None,
            checkpoint_interval: Duration::from_secs(60),
            last_checkpoint: Arc::new(RwLock::new(SystemTime::now())),
        }
    }

    pub fn append(
        &self,
        transaction_id: TransactionId,
        operation: SerializableOperation,
    ) -> Result<()> {
        let entry = LogEntry {
            lsn: self.generate_lsn(),
            transaction_id,
            operation,
            timestamp: SystemTime::now(),
            checksum: 0,
        };

        let mut log = self.log_entries.write();
        log.push_back(entry);

        if self.should_checkpoint() {
            self.checkpoint()?;
        }

        Ok(())
    }

    fn generate_lsn(&self) -> u64 {
        static LSN_COUNTER: AtomicU64 = AtomicU64::new(0);
        LSN_COUNTER.fetch_add(1, Ordering::Relaxed)
    }

    fn should_checkpoint(&self) -> bool {
        let last_checkpoint = *self.last_checkpoint.read();
        last_checkpoint
            .elapsed()
            .expect("SystemTime should not go backwards")
            > self.checkpoint_interval
    }

    fn checkpoint(&self) -> Result<()> {
        let mut last_checkpoint = self.last_checkpoint.write();
        *last_checkpoint = SystemTime::now();
        Ok(())
    }
}

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

impl LockManager {
    pub fn new() -> Self {
        Self {
            locks: Arc::new(RwLock::new(HashMap::new())),
            deadlock_detector: Arc::new(DeadlockDetector::new()),
        }
    }

    pub fn acquire_lock(
        &self,
        transaction_id: TransactionId,
        resource: &str,
        lock_type: LockType,
    ) -> Result<()> {
        let mut locks = self.locks.write();
        let lock_info = locks
            .entry(resource.to_string())
            .or_insert_with(|| LockInfo {
                lock_type: LockType::Shared,
                holders: std::collections::HashSet::new(),
                waiters: std::collections::VecDeque::new(),
                granted_time: SystemTime::now(),
            });

        if self.can_grant_lock(lock_info, lock_type) {
            lock_info.holders.insert(transaction_id);
            lock_info.lock_type = lock_type;
            lock_info.granted_time = SystemTime::now();
            Ok(())
        } else {
            lock_info.waiters.push_back((transaction_id, lock_type));
            self.deadlock_detector.check_deadlock(transaction_id)?;
            Err(anyhow::anyhow!("Lock not available, transaction waiting"))
        }
    }

    pub fn release_transaction_locks(&self, transaction_id: TransactionId) {
        let mut locks = self.locks.write();
        let mut to_remove = Vec::new();

        for (resource, lock_info) in locks.iter_mut() {
            lock_info.holders.remove(&transaction_id);
            lock_info.waiters.retain(|(tid, _)| *tid != transaction_id);

            if lock_info.holders.is_empty() {
                to_remove.push(resource.clone());
            }
        }

        for resource in to_remove {
            locks.remove(&resource);
        }
    }

    fn can_grant_lock(&self, lock_info: &LockInfo, requested_type: LockType) -> bool {
        if lock_info.holders.is_empty() {
            return true;
        }

        matches!(
            (lock_info.lock_type, requested_type),
            (LockType::Shared, LockType::Shared)
        )
    }
}

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

impl DeadlockDetector {
    pub fn new() -> Self {
        Self {
            wait_for_graph: Arc::new(RwLock::new(HashMap::new())),
            detection_interval: Duration::from_secs(1),
        }
    }

    pub fn check_deadlock(&self, _transaction_id: TransactionId) -> Result<()> {
        Ok(())
    }
}

impl StreamingEngine {
    pub fn new(config: StreamingConfig) -> Self {
        Self {
            config,
            stream_buffer: Arc::new(RwLock::new(std::collections::VecDeque::new())),
            processor_thread: None,
            backpressure_controller: Arc::new(BackpressureController::new()),
            stream_metrics: Arc::new(StreamingMetrics::default()),
        }
    }

    pub fn submit_operation(&self, operation: StreamingOperation) -> Result<()> {
        if self.backpressure_controller.should_apply_backpressure() {
            return Err(anyhow::anyhow!("Backpressure applied, operation rejected"));
        }

        let mut buffer = self.stream_buffer.write();
        buffer.push_back(operation);

        self.stream_metrics
            .operations_pending
            .fetch_add(1, Ordering::Relaxed);
        Ok(())
    }

    pub fn get_metrics(&self) -> &StreamingMetrics {
        &self.stream_metrics
    }
}

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

impl BackpressureController {
    pub fn new() -> Self {
        Self {
            current_load: Arc::new(RwLock::new(0.0)),
            max_load_threshold: 0.8,
            adaptive_batching: true,
            load_shedding: true,
        }
    }

    pub fn should_apply_backpressure(&self) -> bool {
        let load = *self.current_load.read();
        load > self.max_load_threshold
    }
}

impl CacheManager {
    pub fn new(config: StoreCacheConfig) -> Self {
        Self {
            vector_cache: Arc::new(RwLock::new(HashMap::new())),
            query_cache: Arc::new(RwLock::new(HashMap::new())),
            config,
            cache_stats: Arc::new(CacheStats::default()),
            eviction_policy: EvictionPolicy::LRU,
        }
    }

    pub fn get_vector(&self, uri: &str) -> Option<CachedVector> {
        let cache = self.vector_cache.read();
        if let Some(cached) = cache.get(uri) {
            self.cache_stats
                .vector_cache_hits
                .fetch_add(1, Ordering::Relaxed);
            Some(cached.clone())
        } else {
            self.cache_stats
                .vector_cache_misses
                .fetch_add(1, Ordering::Relaxed);
            None
        }
    }

    pub fn cache_vector(&self, uri: String, vector: crate::Vector) {
        let cached_vector = CachedVector {
            vector,
            last_accessed: SystemTime::now(),
            access_count: 1,
            compression_ratio: 1.0,
            cache_level: CacheLevel::Memory,
        };

        let mut cache = self.vector_cache.write();
        cache.insert(uri, cached_vector);
    }

    pub fn get_cached_query(&self, query_hash: &u64) -> Option<CachedQueryResult> {
        let cache = self.query_cache.read();
        if let Some(cached) = cache.get(&query_hash.to_string()) {
            self.cache_stats
                .query_cache_hits
                .fetch_add(1, Ordering::Relaxed);
            Some(cached.clone())
        } else {
            self.cache_stats
                .query_cache_misses
                .fetch_add(1, Ordering::Relaxed);
            None
        }
    }

    pub fn cache_query_result(&self, query_hash: u64, results: Vec<(String, f32)>) {
        let cached_result = CachedQueryResult {
            results,
            query_hash,
            last_accessed: SystemTime::now(),
            ttl: self.config.cache_ttl,
            hit_count: 0,
        };

        let mut cache = self.query_cache.write();
        cache.insert(query_hash.to_string(), cached_result);
    }

    pub fn get_stats(&self) -> &CacheStats {
        &self.cache_stats
    }
}

impl ReplicationManager {
    pub fn new(config: ReplicationConfig) -> Self {
        Self {
            config,
            replicas: Arc::new(RwLock::new(Vec::new())),
            replication_log: Arc::new(RwLock::new(std::collections::VecDeque::new())),
            consensus_algorithm: ConsensusAlgorithm::SimpleMajority,
            health_checker: Arc::new(HealthChecker::new()),
        }
    }
}

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

impl HealthChecker {
    pub fn new() -> Self {
        Self {
            check_interval: Duration::from_secs(30),
            timeout: Duration::from_secs(5),
            failure_threshold: 3,
        }
    }
}

impl ConsistencyManager {
    pub fn new(consistency_level: ConsistencyLevel) -> Self {
        Self {
            consistency_level,
            vector_clocks: Arc::new(RwLock::new(HashMap::new())),
            conflict_resolver: Arc::new(ConflictResolver::new()),
            causal_order_tracker: Arc::new(CausalOrderTracker::new()),
        }
    }
}

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

impl ConflictResolver {
    pub fn new() -> Self {
        Self {
            strategy: ConflictResolution::LastWriteWins,
            custom_resolvers: HashMap::new(),
        }
    }
}

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

impl CausalOrderTracker {
    pub fn new() -> Self {
        Self {
            happens_before: Arc::new(RwLock::new(HashMap::new())),
        }
    }
}

impl ChangeLog {
    pub fn new(max_entries: usize) -> Self {
        Self {
            entries: Arc::new(RwLock::new(std::collections::VecDeque::new())),
            max_entries,
            subscribers: Arc::new(RwLock::new(Vec::new())),
        }
    }

    pub fn add_entry(&self, entry: ChangeLogEntry) {
        let mut entries = self.entries.write();
        entries.push_back(entry.clone());

        if entries.len() > self.max_entries {
            entries.pop_front();
        }

        let subscribers = self.subscribers.read();
        for subscriber in subscribers.iter() {
            let _ = subscriber.on_change(&entry);
        }
    }

    pub fn add_subscriber(&self, subscriber: Arc<dyn ChangeSubscriber>) {
        let mut subscribers = self.subscribers.write();
        subscribers.push(subscriber);
    }
}