From 3462b3825f72d161ba0ac4ec6cb71050afb8ea74 Mon Sep 17 00:00:00 2001 From: Andre Popovitch Date: Thu, 26 Dec 2024 03:02:43 -0600 Subject: [PATCH] add more docs --- examples/in_memory.rs | 2 +- src/filesystem/memory.rs | 6 ++---- src/lib.rs | 5 +++++ src/tests.rs | 40 +++++++++++++++++++++++----------------- 4 files changed, 31 insertions(+), 22 deletions(-) diff --git a/examples/in_memory.rs b/examples/in_memory.rs index 3b25ec9..3424dcb 100644 --- a/examples/in_memory.rs +++ b/examples/in_memory.rs @@ -2,7 +2,7 @@ async fn main() { use victor_db::memory::{Db, DirectoryHandle}; - let mut victor = Db::new(DirectoryHandle::new()); + let mut victor = Db::new(DirectoryHandle::default()); victor.clear_db().await.unwrap(); diff --git a/src/filesystem/memory.rs b/src/filesystem/memory.rs index e0f046d..0976436 100644 --- a/src/filesystem/memory.rs +++ b/src/filesystem/memory.rs @@ -65,10 +65,8 @@ impl filesystem::DirectoryHandle for DirectoryHandle { Ok(()) } } - -impl DirectoryHandle { - /// Create a new in-memory virtual directory. - pub fn new() -> Self { +impl Default for DirectoryHandle { + fn default() -> Self { Self(Rc::new(RefCell::new(HashMap::new()))) } } diff --git a/src/lib.rs b/src/lib.rs index 56dfe22..9305f91 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,6 +65,7 @@ extern "C" { fn warn(s: &str); } +/// A browser-optimized vector database. #[cfg(target_arch = "wasm32")] #[wasm_bindgen] pub struct Db { @@ -74,6 +75,7 @@ pub struct Db { #[cfg(target_arch = "wasm32")] #[wasm_bindgen] impl Db { + /// Connect to victor. #[wasm_bindgen(constructor)] pub async fn new() -> Self { utils::set_panic_hook(); @@ -91,6 +93,7 @@ impl Db { Self { victor } } + /// Add a document to the database. pub async fn insert(&mut self, content: &str, embedding: &[f64], tags: Option>) { let embedding = embedding.iter().map(|x| *x as f32).collect::>(); @@ -105,6 +108,7 @@ impl Db { self.victor.add_embedding(content, embedding, tags).await; } + /// Search the database for the nearest neighbors to a given embedding. pub async fn search( &mut self, embedding: &[f64], @@ -129,6 +133,7 @@ impl Db { serde_wasm_bindgen::to_value(&nearest_neighbors).unwrap() } + /// Clear the database, permanently removing all data. pub async fn clear(&mut self) { utils::set_panic_hook(); diff --git a/src/tests.rs b/src/tests.rs index ea71dc5..c105ac2 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -4,14 +4,14 @@ use crate::memory::{Db, DirectoryHandle}; async fn store_and_retrieve() { let embedding = vec![1.0, 2.0, 3.0]; - let mut victor = Db::new(DirectoryHandle::new()); + let mut victor = Db::new(DirectoryHandle::default()); victor - .add_embedding("hello", embedding.clone(), vec![]) + .add_embedding("hello", embedding.clone(), Vec::::new()) .await; let result = victor - .search_embedding(embedding, vec![], 1) + .search_embedding(embedding, Vec::::new(), 1) .await .first() .unwrap() @@ -26,18 +26,18 @@ async fn store_two_and_retrieve() { let embedding_1 = vec![1.0, 2.0, 3.0]; let embedding_2 = vec![-1.0, -2.0, -3.0]; - let mut victor = Db::new(DirectoryHandle::new()); + let mut victor = Db::new(DirectoryHandle::default()); victor - .add_embedding("hello", embedding_1.clone(), vec![]) + .add_embedding("hello", embedding_1.clone(), Vec::::new()) .await; victor - .add_embedding("goodbye", embedding_2.clone(), vec![]) + .add_embedding("goodbye", embedding_2.clone(), Vec::::new()) .await; { let result = victor - .search_embedding(embedding_1, vec![], 1) + .search_embedding(embedding_1, Vec::::new(), 1) .await .first() .unwrap() @@ -48,7 +48,7 @@ async fn store_two_and_retrieve() { } { let result = victor - .search_embedding(embedding_2, vec![], 1) + .search_embedding(embedding_2, Vec::::new(), 1) .await .first() .unwrap() @@ -64,7 +64,7 @@ async fn store_two_and_retrieve_with_tags() { let embedding_1 = vec![1.0, 2.0, 3.0]; let embedding_2 = vec![-1.0, -2.0, -3.0]; - let mut victor = Db::new(DirectoryHandle::new()); + let mut victor = Db::new(DirectoryHandle::default()); victor .add_embedding("hello", embedding_1.clone(), vec!["greetings".to_string()]) @@ -75,7 +75,7 @@ async fn store_two_and_retrieve_with_tags() { { let result = victor - .search_embedding(embedding_1.clone(), vec![], 1) + .search_embedding(embedding_1.clone(), Vec::::new(), 1) .await .first() .unwrap() @@ -86,7 +86,7 @@ async fn store_two_and_retrieve_with_tags() { } { let result = victor - .search_embedding(embedding_2.clone(), vec![], 1) + .search_embedding(embedding_2.clone(), Vec::::new(), 1) .await .first() .unwrap() @@ -132,20 +132,26 @@ async fn incompatible_size_panic() { let embedding_1 = vec![1.0, 2.0, 3.0]; let embedding_2 = vec![1.0, 2.0, 3.0, 4.0]; - let mut victor = Db::new(DirectoryHandle::new()); + let mut victor = Db::new(DirectoryHandle::default()); - victor.add_embedding("hello", embedding_1, vec![]).await; - victor.add_embedding("hello", embedding_2, vec![]).await; + victor + .add_embedding("hello", embedding_1, Vec::::new()) + .await; + victor + .add_embedding("hello", embedding_2, Vec::::new()) + .await; } #[tokio::test] async fn add_many() { - let mut victor = Db::new(DirectoryHandle::new()); + let mut victor = Db::new(DirectoryHandle::default()); - victor.add_many(vec!["pineapple", "rocks"], vec![]).await; + victor + .add_many(vec!["pineapple", "rocks"], Vec::::new()) + .await; let result = victor - .search("hawaiian pizza", vec![], 1) + .search("hawaiian pizza", Vec::::new(), 1) .await .first() .unwrap()