Skip to content

Commit

Permalink
add more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
anchpop committed Dec 26, 2024
1 parent b028ddc commit 3462b38
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
6 changes: 2 additions & 4 deletions src/filesystem/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())))
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ extern "C" {
fn warn(s: &str);
}

/// A browser-optimized vector database.
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub struct Db {
Expand All @@ -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();
Expand All @@ -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<Vec<JsValue>>) {
let embedding = embedding.iter().map(|x| *x as f32).collect::<Vec<_>>();

Expand All @@ -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],
Expand All @@ -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();

Expand Down
40 changes: 23 additions & 17 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>::new())
.await;

let result = victor
.search_embedding(embedding, vec![], 1)
.search_embedding(embedding, Vec::<String>::new(), 1)
.await
.first()
.unwrap()
Expand All @@ -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::<String>::new())
.await;
victor
.add_embedding("goodbye", embedding_2.clone(), vec![])
.add_embedding("goodbye", embedding_2.clone(), Vec::<String>::new())
.await;

{
let result = victor
.search_embedding(embedding_1, vec![], 1)
.search_embedding(embedding_1, Vec::<String>::new(), 1)
.await
.first()
.unwrap()
Expand All @@ -48,7 +48,7 @@ async fn store_two_and_retrieve() {
}
{
let result = victor
.search_embedding(embedding_2, vec![], 1)
.search_embedding(embedding_2, Vec::<String>::new(), 1)
.await
.first()
.unwrap()
Expand All @@ -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()])
Expand All @@ -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::<String>::new(), 1)
.await
.first()
.unwrap()
Expand All @@ -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::<String>::new(), 1)
.await
.first()
.unwrap()
Expand Down Expand Up @@ -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::<String>::new())
.await;
victor
.add_embedding("hello", embedding_2, Vec::<String>::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::<String>::new())
.await;

let result = victor
.search("hawaiian pizza", vec![], 1)
.search("hawaiian pizza", Vec::<String>::new(), 1)
.await
.first()
.unwrap()
Expand Down

0 comments on commit 3462b38

Please sign in to comment.