velesdb_core/database/
collection_ops.rs1#[allow(deprecated)]
9use crate::{Collection, CollectionType, DistanceMetric, Error, Result, StorageMode};
10
11use super::Database;
12
13#[allow(deprecated)]
16impl Database {
17 pub(super) fn ensure_collection_name_available(&self, name: &str) -> Result<()> {
22 let exists_in_registry = self.collections.read().contains_key(name)
23 || self.vector_colls.read().contains_key(name)
24 || self.graph_colls.read().contains_key(name)
25 || self.metadata_colls.read().contains_key(name);
26 if exists_in_registry {
27 return Err(Error::CollectionExists(name.to_string()));
28 }
29
30 let collection_path = self.data_dir.join(name);
31 if collection_path.exists() {
32 return Err(Error::CollectionExists(name.to_string()));
33 }
34
35 Ok(())
36 }
37
38 pub fn create_collection(
60 &self,
61 name: &str,
62 dimension: usize,
63 metric: DistanceMetric,
64 ) -> Result<()> {
65 self.create_collection_with_options(name, dimension, metric, StorageMode::default())
66 }
67
68 pub fn create_collection_with_options(
74 &self,
75 name: &str,
76 dimension: usize,
77 metric: DistanceMetric,
78 storage_mode: StorageMode,
79 ) -> Result<()> {
80 self.create_vector_collection_with_options(name, dimension, metric, storage_mode)
81 }
82
83 #[deprecated(
89 since = "2.0.0",
90 note = "Use get_vector_collection(), get_graph_collection(), or get_metadata_collection()"
91 )]
92 pub fn get_collection(&self, name: &str) -> Option<Collection> {
93 self.collections.read().get(name).cloned()
94 }
95
96 #[must_use]
98 pub fn collection_write_generation(&self, name: &str) -> Option<u64> {
99 self.collections
100 .read()
101 .get(name)
102 .map(crate::Collection::write_generation)
103 }
104
105 pub fn list_collections(&self) -> Vec<String> {
109 let collections = self.collections.read();
111 let vector_colls = self.vector_colls.read();
112 let graph_colls = self.graph_colls.read();
113 let metadata_colls = self.metadata_colls.read();
114
115 let mut names: std::collections::HashSet<String> = collections.keys().cloned().collect();
116 for k in vector_colls.keys() {
117 names.insert(k.clone());
118 }
119 for k in graph_colls.keys() {
120 names.insert(k.clone());
121 }
122 for k in metadata_colls.keys() {
123 names.insert(k.clone());
124 }
125 let mut result: Vec<String> = names.into_iter().collect();
126 result.sort();
127 result
128 }
129
130 pub fn delete_collection(&self, name: &str) -> Result<()> {
136 let exists = self.collections.read().contains_key(name)
137 || self.vector_colls.read().contains_key(name)
138 || self.graph_colls.read().contains_key(name)
139 || self.metadata_colls.read().contains_key(name);
140
141 if !exists {
142 return Err(Error::CollectionNotFound(name.to_string()));
143 }
144
145 let collection_path = self.data_dir.join(name);
146 if collection_path.exists() {
147 std::fs::remove_dir_all(&collection_path)?;
148 }
149
150 self.collections.write().remove(name);
151 self.vector_colls.write().remove(name);
152 self.graph_colls.write().remove(name);
153 self.metadata_colls.write().remove(name);
154 self.collection_stats.write().remove(name);
155
156 if let Some(ref obs) = self.observer {
157 obs.on_collection_deleted(name);
158 }
159
160 self.schema_version
161 .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
162
163 Ok(())
164 }
165
166 pub fn create_collection_typed(
172 &self,
173 name: &str,
174 collection_type: &CollectionType,
175 ) -> Result<()> {
176 match collection_type {
177 CollectionType::Vector {
178 dimension,
179 metric,
180 storage_mode,
181 } => {
182 self.create_vector_collection_with_options(name, *dimension, *metric, *storage_mode)
183 }
184 CollectionType::MetadataOnly => self.create_metadata_collection(name),
185 CollectionType::Graph {
186 dimension,
187 metric,
188 schema,
189 } => self.create_graph_collection_from_type(name, *dimension, *metric, schema),
190 }
191 }
192
193 pub(super) fn read_collection_config(
197 &self,
198 name: &str,
199 ) -> Option<crate::collection::CollectionConfig> {
200 let path = self.data_dir.join(name);
201 let config_path = path.join("config.json");
202 if !config_path.exists() {
203 return None;
204 }
205 let data = std::fs::read_to_string(&config_path).ok()?;
206 serde_json::from_str(&data).ok()
207 }
208
209 pub fn update_guardrails(&self, limits: &crate::guardrails::QueryLimits) {
211 let collections = self.collections.read();
212 for collection in collections.values() {
213 collection.guard_rails.update_limits(limits);
214 }
215 }
216
217 pub fn collection_diagnostics(
223 &self,
224 name: &str,
225 ) -> Result<crate::collection::CollectionDiagnostics> {
226 if let Some(c) = self.get_vector_collection(name) {
227 return Ok(c.diagnostics());
228 }
229 if let Some(c) = self.get_graph_collection(name) {
230 return Ok(c.diagnostics());
231 }
232 if let Some(c) = self.get_metadata_collection(name) {
233 return Ok(c.diagnostics());
234 }
235 Err(Error::CollectionNotFound(name.to_string()))
236 }
237}