use std::collections::HashMap;
use crate::collection::graph_collection::GraphCollection;
use crate::collection::metadata_collection::MetadataCollection;
use crate::collection::types::CollectionConfig;
use crate::collection::vector_collection::VectorCollection;
use crate::error::Result;
use crate::point::SearchResult;
#[derive(Clone)]
#[non_exhaustive]
pub enum AnyCollection {
Vector(VectorCollection),
Graph(GraphCollection),
Metadata(MetadataCollection),
}
impl AnyCollection {
#[must_use]
pub fn config(&self) -> CollectionConfig {
match self {
Self::Vector(c) => c.config(),
Self::Graph(c) => c.inner.config(),
Self::Metadata(c) => c.inner.config(),
}
}
pub fn flush(&self) -> Result<()> {
match self {
Self::Vector(c) => c.flush(),
Self::Graph(c) => c.flush(),
Self::Metadata(c) => c.flush(),
}
}
#[must_use]
pub fn point_count(&self) -> usize {
self.config().point_count
}
#[must_use]
pub fn is_empty(&self) -> bool {
match self {
Self::Vector(c) => c.inner.is_empty(),
Self::Graph(c) => c.is_empty(),
Self::Metadata(c) => c.is_empty(),
}
}
#[must_use]
pub fn is_metadata_only(&self) -> bool {
matches!(self, Self::Metadata(_))
}
#[must_use]
pub fn name(&self) -> String {
self.config().name
}
pub fn execute_query_str(
&self,
sql: &str,
params: &HashMap<String, serde_json::Value>,
) -> Result<Vec<SearchResult>> {
match self {
Self::Vector(c) => c.execute_query_str(sql, params),
Self::Graph(c) => c.execute_query_str(sql, params),
Self::Metadata(c) => c.execute_query_str(sql, params),
}
}
pub fn execute_aggregate(
&self,
query: &crate::velesql::Query,
params: &HashMap<String, serde_json::Value>,
) -> Result<serde_json::Value> {
match self {
Self::Vector(c) => c.execute_aggregate(query, params),
Self::Graph(c) => c.inner.execute_aggregate(query, params),
Self::Metadata(c) => c.inner.execute_aggregate(query, params),
}
}
#[must_use]
pub fn diagnostics(&self) -> crate::collection::CollectionDiagnostics {
match self {
Self::Vector(c) => c.diagnostics(),
Self::Graph(c) => c.diagnostics(),
Self::Metadata(c) => c.diagnostics(),
}
}
#[must_use]
pub fn is_vector(&self) -> bool {
matches!(self, Self::Vector(_))
}
#[must_use]
pub fn is_graph(&self) -> bool {
matches!(self, Self::Graph(_))
}
#[must_use]
pub fn is_metadata(&self) -> bool {
matches!(self, Self::Metadata(_))
}
#[must_use]
pub fn as_vector(&self) -> Option<&VectorCollection> {
match self {
Self::Vector(c) => Some(c),
_ => None,
}
}
#[must_use]
pub fn as_vector_mut(&mut self) -> Option<&mut VectorCollection> {
match self {
Self::Vector(c) => Some(c),
_ => None,
}
}
#[must_use]
pub fn as_graph(&self) -> Option<&GraphCollection> {
match self {
Self::Graph(c) => Some(c),
_ => None,
}
}
#[must_use]
pub fn as_graph_mut(&mut self) -> Option<&mut GraphCollection> {
match self {
Self::Graph(c) => Some(c),
_ => None,
}
}
#[must_use]
pub fn as_metadata(&self) -> Option<&MetadataCollection> {
match self {
Self::Metadata(c) => Some(c),
_ => None,
}
}
#[must_use]
pub fn as_metadata_mut(&mut self) -> Option<&mut MetadataCollection> {
match self {
Self::Metadata(c) => Some(c),
_ => None,
}
}
#[allow(clippy::result_large_err)]
pub fn into_vector(self) -> core::result::Result<VectorCollection, Self> {
match self {
Self::Vector(c) => Ok(c),
other => Err(other),
}
}
#[allow(clippy::result_large_err)]
pub fn into_graph(self) -> core::result::Result<GraphCollection, Self> {
match self {
Self::Graph(c) => Ok(c),
other => Err(other),
}
}
#[allow(clippy::result_large_err)]
pub fn into_metadata(self) -> core::result::Result<MetadataCollection, Self> {
match self {
Self::Metadata(c) => Ok(c),
other => Err(other),
}
}
#[must_use]
pub unsafe fn into_vector_unchecked(self) -> VectorCollection {
match self {
Self::Vector(c) => c,
Self::Graph(c) => VectorCollection { inner: c.inner },
Self::Metadata(c) => VectorCollection { inner: c.inner },
}
}
}