velesdb_core/database/
graph_ops.rs1use crate::collection::GraphCollection;
4use crate::{CollectionType, DistanceMetric, Result};
5
6use super::Database;
7
8impl Database {
9 #[allow(clippy::needless_pass_by_value)] pub fn create_graph_collection(
16 &self,
17 name: &str,
18 schema: crate::collection::GraphSchema,
19 ) -> Result<()> {
20 self.ensure_collection_name_available(name)?;
21 let path = self.data_dir.join(name);
22 let coll =
23 GraphCollection::create(path, name, None, DistanceMetric::Cosine, schema.clone())?;
24 self.register_graph_collection(name, &coll, None, DistanceMetric::Cosine, &schema);
25 Ok(())
26 }
27
28 #[allow(clippy::needless_pass_by_value)] pub fn create_graph_collection_with_embeddings(
43 &self,
44 name: &str,
45 schema: crate::collection::GraphSchema,
46 dimension: usize,
47 metric: DistanceMetric,
48 ) -> Result<()> {
49 self.ensure_collection_name_available(name)?;
50 self.enforce_vector_dimension_limit(dimension)?;
51 let path = self.data_dir.join(name);
52 let params = self.resolve_hnsw_params(dimension, None, None);
55 let coll = GraphCollection::create_with_hnsw_params(
56 path,
57 name,
58 Some(dimension),
59 metric,
60 schema.clone(),
61 params,
62 )?;
63 self.register_graph_collection(name, &coll, Some(dimension), metric, &schema);
64 Ok(())
65 }
66
67 pub(super) fn create_graph_collection_from_type(
69 &self,
70 name: &str,
71 dimension: Option<usize>,
72 metric: DistanceMetric,
73 schema: &crate::collection::GraphSchema,
74 ) -> Result<()> {
75 self.ensure_collection_name_available(name)?;
76 if let Some(d) = dimension {
77 self.enforce_vector_dimension_limit(d)?;
78 }
79 let path = self.data_dir.join(name);
80 let params = dimension.and_then(|d| self.resolve_hnsw_params(d, None, None));
83 let coll = GraphCollection::create_with_hnsw_params(
84 path,
85 name,
86 dimension,
87 metric,
88 schema.clone(),
89 params,
90 )?;
91 self.register_graph_collection(name, &coll, dimension, metric, schema);
92 Ok(())
93 }
94
95 fn register_graph_collection(
98 &self,
99 name: &str,
100 coll: &GraphCollection,
101 dimension: Option<usize>,
102 metric: DistanceMetric,
103 schema: &crate::collection::GraphSchema,
104 ) {
105 self.push_runtime_limits(&coll.inner);
107
108 self.graph_colls
109 .write()
110 .insert(name.to_string(), coll.clone());
111
112 if let Some(ref obs) = self.observer {
113 let kind = CollectionType::Graph {
114 dimension,
115 metric,
116 schema: schema.clone(),
117 };
118 obs.on_collection_created(name, &kind);
119 }
120
121 self.schema_version
122 .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
123 }
124
125 #[must_use]
134 pub fn get_graph_collection(&self, name: &str) -> Option<GraphCollection> {
135 let cached = self.graph_colls.read().get(name).cloned();
138 if let Some(c) = cached {
139 return Some(c);
140 }
141 self.open_graph_collection_from_disk(name)
142 }
143
144 fn open_graph_collection_from_disk(&self, name: &str) -> Option<GraphCollection> {
146 let cfg = self.read_collection_config(name)?;
147 cfg.graph_schema.as_ref()?;
148 let coll = GraphCollection::open(self.data_dir.join(name)).ok()?;
149 self.push_runtime_limits(&coll.inner);
151 self.graph_colls
152 .write()
153 .insert(name.to_string(), coll.clone());
154 Some(coll)
155 }
156}