ruvector_server/
state.rs

1//! Shared application state
2
3use dashmap::DashMap;
4use ruvector_core::VectorDB;
5use std::sync::Arc;
6
7/// Shared application state
8#[derive(Clone)]
9pub struct AppState {
10    /// Map of collection name to VectorDB
11    pub collections: Arc<DashMap<String, Arc<VectorDB>>>,
12}
13
14impl AppState {
15    /// Create a new application state
16    pub fn new() -> Self {
17        Self {
18            collections: Arc::new(DashMap::new()),
19        }
20    }
21
22    /// Get a collection by name
23    pub fn get_collection(&self, name: &str) -> Option<Arc<VectorDB>> {
24        self.collections.get(name).map(|c| c.clone())
25    }
26
27    /// Insert a collection
28    pub fn insert_collection(&self, name: String, db: Arc<VectorDB>) {
29        self.collections.insert(name, db);
30    }
31
32    /// Remove a collection
33    pub fn remove_collection(&self, name: &str) -> Option<Arc<VectorDB>> {
34        self.collections.remove(name).map(|(_, c)| c)
35    }
36
37    /// Check if a collection exists
38    pub fn contains_collection(&self, name: &str) -> bool {
39        self.collections.contains_key(name)
40    }
41
42    /// Get all collection names
43    pub fn collection_names(&self) -> Vec<String> {
44        self.collections
45            .iter()
46            .map(|entry| entry.key().clone())
47            .collect()
48    }
49
50    /// Get the number of collections
51    pub fn collection_count(&self) -> usize {
52        self.collections.len()
53    }
54}
55
56impl Default for AppState {
57    fn default() -> Self {
58        Self::new()
59    }
60}