Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,8 @@ unimplemented = "warn"
use_self = "warn"
or_fun_call = "warn"

[dependencies]
usearch = "2.26"

[build-dependencies]
bindgen = "0.73.2"
12 changes: 11 additions & 1 deletion src/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::ptr;
use crate::aio;
use crate::error::Error;
use crate::guard;
use crate::index;
use crate::memcached::ffi::{AUTHZ_NONE, EXTENSION_ASCII_PROTOCOL_DESCRIPTOR, token_t};
use crate::memcached::server::{Connection, ResponseHandler};
use crate::protocol::{self, Request, Tokens};
Expand Down Expand Up @@ -98,6 +99,10 @@ unsafe extern "C" fn execute(
unsafe { aio::begin(conn, reply) };
return true;
}
Ok(Request::Create(spec)) => match index::create(&spec) {
Ok(()) => b"CREATED\r\n",
Err(e) => e.reply(),
},
Ok(_) => Error::NotSupported.reply(),
Err(e) => e.reply(),
};
Expand Down Expand Up @@ -204,10 +209,15 @@ mod tests {

#[test]
fn well_formed_lines_are_not_supported_yet() {
assert_eq!(reply_to("vop create idx 2 0"), "NOT_SUPPORTED\r\n");
assert_eq!(reply_to("vop drop idx"), "NOT_SUPPORTED\r\n");
}

#[test]
fn create_registers_the_index_once() {
assert_eq!(reply_to("vop create ascii 2 0"), "CREATED\r\n");
assert_eq!(reply_to("vop create ascii 2 0"), "EXISTS\r\n");
}

#[test]
fn malformed_lines_get_the_reason() {
assert_eq!(
Expand Down
88 changes: 88 additions & 0 deletions src/index.rs
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)?;
Comment thread
ing-eoking marked this conversation as resolved.
Resolved
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(())
);
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod aio;
mod ascii;
mod error;
mod index;
mod memcached;
mod protocol;

Expand Down
Loading
Loading