#![allow(dead_code)]
use nitrite::collection::{Document, NitriteCollection};
use nitrite::nitrite::Nitrite;
use nitrite_fjall_adapter::FjallModule;
use nitrite_vector::{vector_to_value, VectorIndexConfig, VectorModule};
pub fn open_db(path: &str, config: VectorIndexConfig) -> Nitrite {
let storage_module = FjallModule::with_config()
.db_path(path)
.low_memory_preset()
.build();
Nitrite::builder()
.load_module(storage_module)
.load_module(VectorModule::new(config))
.open_or_create(None, None)
.expect("failed to open vector-enabled Nitrite database")
}
pub fn temp_db(config: VectorIndexConfig) -> (tempfile::TempDir, Nitrite) {
let dir = tempfile::tempdir().expect("temp dir");
let path = dir.path().to_str().expect("utf8 path").to_string();
let db = open_db(&path, config);
(dir, db)
}
pub fn open_plain_db(path: &str) -> Nitrite {
let storage_module = FjallModule::with_config()
.db_path(path)
.low_memory_preset()
.build();
Nitrite::builder()
.load_module(storage_module)
.open_or_create(None, None)
.expect("failed to open Nitrite database")
}
pub fn temp_plain_db() -> (tempfile::TempDir, Nitrite) {
let dir = tempfile::tempdir().expect("temp dir");
let path = dir.path().to_str().expect("utf8 path").to_string();
let db = open_plain_db(&path);
(dir, db)
}
pub fn doc_with_vector(name: &str, vector: &[f32]) -> Document {
let mut d = Document::new();
d.put("name", name.to_string()).unwrap();
d.put("embedding", vector_to_value(vector)).unwrap();
d
}
pub fn names(collection: &NitriteCollection, filter: nitrite::filter::Filter) -> Vec<String> {
let cursor = collection.find(filter).expect("find");
cursor
.map(|r| {
let doc = r.expect("doc");
match doc.get("name").expect("name") {
nitrite::common::Value::String(s) => s,
_ => String::new(),
}
})
.collect()
}