#![forbid(unsafe_code)]
mod config;
mod data;
mod filter;
mod glob;
mod lock;
mod log;
mod model;
mod search;
mod store;
pub use anyhow::Result;
pub use config::{Config, Fsync, OpenMode};
pub use model::{Filter, Hit, Predicate, Record, SearchOpts, Value};
use std::collections::BTreeMap;
use std::path::Path;
pub enum Scope<'a> {
Collection(&'a str),
Collections(&'a [&'a str]),
All,
}
impl<'a> From<&'a str> for Scope<'a> {
fn from(s: &'a str) -> Self {
Scope::Collection(s)
}
}
impl<'a> From<&'a [&'a str]> for Scope<'a> {
fn from(s: &'a [&'a str]) -> Self {
Scope::Collections(s)
}
}
pub struct Nidus {
store: store::Store,
}
impl Nidus {
pub fn open(config: Config) -> Result<Self> {
Ok(Self {
store: store::Store::open(config)?,
})
}
pub fn open_dir(dir: impl AsRef<Path>, dimension: usize) -> Result<Self> {
Self::open(Config::new(dir.as_ref().to_path_buf(), dimension))
}
pub fn open_in_memory(dimension: usize) -> Result<Self> {
Ok(Self {
store: store::Store::in_memory(dimension)?,
})
}
pub fn dimension(&self) -> usize {
self.store.dimension()
}
pub fn config(&self) -> &Config {
self.store.config()
}
pub fn create_collection(&mut self, name: &str) -> Result<()> {
self.store.create_collection(name)
}
pub fn drop_collection(&mut self, name: &str) -> Result<()> {
self.store.drop_collection(name)
}
pub fn has_collection(&self, name: &str) -> bool {
self.store.has_collection(name)
}
pub fn collections(&self) -> Vec<String> {
self.store.collections()
}
pub fn get_meta(&self, collection: &str) -> BTreeMap<String, String> {
self.store.get_meta(collection)
}
pub fn set_meta(&mut self, collection: &str, meta: BTreeMap<String, String>) -> Result<()> {
self.store.set_meta(collection, meta)
}
pub fn upsert(&mut self, collection: &str, records: &[Record]) -> Result<usize> {
self.store.upsert(collection, records)
}
pub fn delete(&mut self, collection: &str, ids: &[&str]) -> Result<usize> {
self.store.delete(collection, ids)
}
pub fn delete_where(&mut self, collection: &str, filter: &Filter) -> Result<usize> {
self.store.delete_where(collection, filter)
}
pub fn get_all(&self, collection: &str) -> Vec<Record> {
self.store.get_all(collection)
}
pub fn search<'a>(
&self,
scope: impl Into<Scope<'a>>,
query: &[f32],
opts: &SearchOpts,
) -> Result<Vec<Hit>> {
let names: Vec<String> = match scope.into() {
Scope::Collection(c) => vec![c.to_string()],
Scope::Collections(cs) => cs.iter().map(|s| s.to_string()).collect(),
Scope::All => self.store.collections(),
};
let refs: Vec<&str> = names.iter().map(String::as_str).collect();
self.store.search(&refs, query, opts)
}
pub fn flush(&mut self) -> Result<()> {
self.store.flush()
}
pub fn compact(&mut self) -> Result<()> {
self.store.compact()
}
}