Repository navigation
INTERNAL: Create and register a usearch index on vop create #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+229
โ161
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| use std::collections::HashMap; | ||
| use std::sync::{LazyLock, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard}; | ||
|
|
||
| use usearch::{Index, IndexOptions}; | ||
|
|
||
| use crate::error::Error; | ||
| use crate::protocol::Create; | ||
|
|
||
| static INDEXES: LazyLock<RwLock<HashMap<String, Index>>> = LazyLock::new(RwLock::default); | ||
|
|
||
| /// Builds the index `spec` describes and registers it under its name. | ||
| pub fn create(spec: &Create<'_>) -> Result<(), Error> { | ||
| // A native index reserves per-thread buffers up front; skip building one for a taken name. | ||
| if read().contains_key(spec.index) { | ||
| return Err(Error::Exists); | ||
| } | ||
| let index = Index::new(&options(spec)).map_err(|_| Error::Internal)?; | ||
| let mut indexes = write(); | ||
| if indexes.contains_key(spec.index) { | ||
| return Err(Error::Exists); | ||
| } | ||
| indexes.insert(spec.index.to_owned(), index); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn read() -> RwLockReadGuard<'static, HashMap<String, Index>> { | ||
| INDEXES.read().unwrap_or_else(PoisonError::into_inner) | ||
| } | ||
|
|
||
| fn write() -> RwLockWriteGuard<'static, HashMap<String, Index>> { | ||
| INDEXES.write().unwrap_or_else(PoisonError::into_inner) | ||
| } | ||
|
|
||
| fn options(spec: &Create<'_>) -> IndexOptions { | ||
| IndexOptions { | ||
| dimensions: spec.dim.into(), | ||
| metric: spec.metric, | ||
| quantization: spec.quant, | ||
| connectivity: spec.m, | ||
| expansion_add: spec.efc, | ||
| expansion_search: spec.efs, | ||
| multi: false, | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::os::raw::c_int; | ||
|
|
||
| use super::*; | ||
| use crate::protocol::{self, Request}; | ||
|
|
||
| fn create_line(line: &str) -> Result<(), Error> { | ||
| let (_buf, tokens) = protocol::tokenize(line); | ||
| // SAFETY: `tokens` point into `_buf`, which outlives the call. | ||
| let tokens = unsafe { protocol::Tokens::new(tokens.as_ptr(), tokens.len() as c_int) }; | ||
| let Ok(Request::Create(spec)) = protocol::parse(&tokens) else { | ||
| panic!("not a create line: {line}"); | ||
| }; | ||
| create(&spec) | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_name_is_created_once() { | ||
| assert_eq!(create_line("vop create once 4 0"), Ok(())); | ||
| assert_eq!(create_line("vop create once 4 0"), Err(Error::Exists)); | ||
| assert!(read().contains_key("once")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn every_metric_and_quant_builds() { | ||
| for (n, line) in [ | ||
| "metric cos quant f32", | ||
| "metric l2 quant f16", | ||
| "metric ip quant i8", | ||
| "metric hamming quant b1", | ||
| "metric tanimoto quant b1", | ||
| ] | ||
| .iter() | ||
| .enumerate() | ||
| { | ||
| assert_eq!( | ||
| create_line(&format!("vop create kind{n} 8 0 {line}")), | ||
| Ok(()) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| mod aio; | ||
| mod ascii; | ||
| mod error; | ||
| mod index; | ||
| mod memcached; | ||
| mod protocol; | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.