1use dashmap::DashMap;
4use ruvector_core::VectorDB;
5use std::sync::Arc;
6
7#[derive(Clone)]
9pub struct AppState {
10 pub collections: Arc<DashMap<String, Arc<VectorDB>>>,
12}
13
14impl AppState {
15 pub fn new() -> Self {
17 Self {
18 collections: Arc::new(DashMap::new()),
19 }
20 }
21
22 pub fn get_collection(&self, name: &str) -> Option<Arc<VectorDB>> {
24 self.collections.get(name).map(|c| c.clone())
25 }
26
27 pub fn insert_collection(&self, name: String, db: Arc<VectorDB>) {
29 self.collections.insert(name, db);
30 }
31
32 pub fn remove_collection(&self, name: &str) -> Option<Arc<VectorDB>> {
34 self.collections.remove(name).map(|(_, c)| c)
35 }
36
37 pub fn contains_collection(&self, name: &str) -> bool {
39 self.collections.contains_key(name)
40 }
41
42 pub fn collection_names(&self) -> Vec<String> {
44 self.collections
45 .iter()
46 .map(|entry| entry.key().clone())
47 .collect()
48 }
49
50 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}