use crate::collection::GraphCollection;
use crate::{CollectionType, DistanceMetric, Result};
use super::Database;
impl Database {
#[allow(clippy::needless_pass_by_value)] pub fn create_graph_collection(
&self,
name: &str,
schema: crate::collection::GraphSchema,
) -> Result<()> {
self.ensure_collection_name_available(name)?;
let path = self.data_dir.join(name);
let coll =
GraphCollection::create(path, name, None, DistanceMetric::Cosine, schema.clone())?;
self.register_graph_collection(name, &coll, None, DistanceMetric::Cosine, &schema);
Ok(())
}
#[allow(clippy::needless_pass_by_value)] pub fn create_graph_collection_with_embeddings(
&self,
name: &str,
schema: crate::collection::GraphSchema,
dimension: usize,
metric: DistanceMetric,
) -> Result<()> {
self.ensure_collection_name_available(name)?;
self.enforce_vector_dimension_limit(dimension)?;
let path = self.data_dir.join(name);
let coll = GraphCollection::create(path, name, Some(dimension), metric, schema.clone())?;
self.register_graph_collection(name, &coll, Some(dimension), metric, &schema);
Ok(())
}
pub(super) fn create_graph_collection_from_type(
&self,
name: &str,
dimension: Option<usize>,
metric: DistanceMetric,
schema: &crate::collection::GraphSchema,
) -> Result<()> {
self.ensure_collection_name_available(name)?;
if let Some(d) = dimension {
self.enforce_vector_dimension_limit(d)?;
}
let path = self.data_dir.join(name);
let coll = GraphCollection::create(path, name, dimension, metric, schema.clone())?;
self.register_graph_collection(name, &coll, dimension, metric, schema);
Ok(())
}
fn register_graph_collection(
&self,
name: &str,
coll: &GraphCollection,
dimension: Option<usize>,
metric: DistanceMetric,
schema: &crate::collection::GraphSchema,
) {
self.graph_colls
.write()
.insert(name.to_string(), coll.clone());
if let Some(ref obs) = self.observer {
let kind = CollectionType::Graph {
dimension,
metric,
schema: schema.clone(),
};
obs.on_collection_created(name, &kind);
}
self.schema_version
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
#[must_use]
pub fn get_graph_collection(&self, name: &str) -> Option<GraphCollection> {
if let Some(c) = self.graph_colls.read().get(name).cloned() {
return Some(c);
}
self.open_graph_collection_from_disk(name)
}
fn open_graph_collection_from_disk(&self, name: &str) -> Option<GraphCollection> {
let cfg = self.read_collection_config(name)?;
cfg.graph_schema.as_ref()?;
let coll = GraphCollection::open(self.data_dir.join(name)).ok()?;
self.graph_colls
.write()
.insert(name.to_string(), coll.clone());
Some(coll)
}
}