use std::path::Path;
use std::sync::{Arc, RwLock};
use crate::collection::Collection;
use crate::doc::{Doc, DocList, DocMap, WriteResults};
use crate::error::Result;
use crate::query::{GroupByVectorQuery, GroupResults, VectorQuery};
use crate::schema::CollectionSchema;
use crate::{IndexParams, MultiQuery};
pub struct SharedCollection {
inner: Arc<RwLock<Collection>>,
}
impl SharedCollection {
pub fn new(collection: Collection) -> Self {
Self {
inner: Arc::new(RwLock::new(collection)),
}
}
pub fn query(&self, query: VectorQuery) -> Result<DocList> {
let guard = self.inner.read().expect("collection lock poisoned");
guard.query(query)
}
pub fn group_by_query(&self, query: GroupByVectorQuery) -> Result<GroupResults> {
let guard = self.inner.read().expect("collection lock poisoned");
guard.group_by_query(query)
}
pub fn multi_query(&self, query: &MultiQuery) -> Result<DocList> {
let guard = self.inner.read().expect("collection lock poisoned");
guard.multi_query(query)
}
pub fn fetch(&self, pks: &[&str]) -> Result<DocMap> {
let guard = self.inner.read().expect("collection lock poisoned");
guard.fetch(pks)
}
pub fn path(&self) -> Result<String> {
let guard = self.inner.read().expect("collection lock poisoned");
guard.path()
}
pub fn insert(&self, docs: &[Doc]) -> Result<WriteResults> {
let guard = self.inner.write().expect("collection lock poisoned");
guard.insert(docs)
}
pub fn upsert(&self, docs: &[Doc]) -> Result<WriteResults> {
let guard = self.inner.write().expect("collection lock poisoned");
guard.upsert(docs)
}
pub fn update(&self, docs: &[Doc]) -> Result<WriteResults> {
let guard = self.inner.write().expect("collection lock poisoned");
guard.update(docs)
}
pub fn delete(&self, pks: &[&str]) -> Result<WriteResults> {
let guard = self.inner.write().expect("collection lock poisoned");
guard.delete(pks)
}
pub fn delete_by_filter(&self, filter: &str) -> Result<()> {
let guard = self.inner.write().expect("collection lock poisoned");
guard.delete_by_filter(filter)
}
pub fn create_index(&self, column_name: &str, params: IndexParams) -> Result<()> {
let guard = self.inner.write().expect("collection lock poisoned");
guard.create_index(column_name, params)
}
pub fn drop_index(&self, column_name: &str) -> Result<()> {
let guard = self.inner.write().expect("collection lock poisoned");
guard.drop_index(column_name)
}
pub fn optimize(&self) -> Result<()> {
let guard = self.inner.write().expect("collection lock poisoned");
guard.optimize()
}
pub fn flush(&self) -> Result<()> {
let guard = self.inner.write().expect("collection lock poisoned");
guard.flush()
}
pub fn destroy(self) -> Result<()> {
match Arc::try_unwrap(self.inner) {
Ok(lock) => {
let collection = lock.into_inner().expect("collection lock poisoned");
collection.destroy()
}
Err(_) => Err(crate::error::Error::InvalidArgument(
"cannot destroy SharedCollection: other clones exist".into(),
)),
}
}
}
impl Clone for SharedCollection {
fn clone(&self) -> Self {
Self {
inner: Arc::clone(&self.inner),
}
}
}
pub fn create_and_open_shared<P: AsRef<Path>>(
path: P,
schema: CollectionSchema,
) -> Result<SharedCollection> {
let collection = Collection::create_and_open(path, schema)?;
Ok(SharedCollection::new(collection))
}
pub fn open_shared<P: AsRef<Path>>(path: P) -> Result<SharedCollection> {
let collection = Collection::open(path)?;
Ok(SharedCollection::new(collection))
}