use dashmap::DashMap;
use ruvector_core::VectorDB;
use std::sync::Arc;
#[derive(Clone)]
pub struct AppState {
pub collections: Arc<DashMap<String, Arc<VectorDB>>>,
}
impl AppState {
pub fn new() -> Self {
Self {
collections: Arc::new(DashMap::new()),
}
}
pub fn get_collection(&self, name: &str) -> Option<Arc<VectorDB>> {
self.collections.get(name).map(|c| c.clone())
}
pub fn insert_collection(&self, name: String, db: Arc<VectorDB>) {
self.collections.insert(name, db);
}
pub fn remove_collection(&self, name: &str) -> Option<Arc<VectorDB>> {
self.collections.remove(name).map(|(_, c)| c)
}
pub fn contains_collection(&self, name: &str) -> bool {
self.collections.contains_key(name)
}
pub fn collection_names(&self) -> Vec<String> {
self.collections
.iter()
.map(|entry| entry.key().clone())
.collect()
}
pub fn collection_count(&self) -> usize {
self.collections.len()
}
}
impl Default for AppState {
fn default() -> Self {
Self::new()
}
}