Skip to content
Open
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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ GRPC_ROOT_PASSWORD=your-secure-password
STORAGE_TYPE=rocksdb
INDEX_TYPE=flat
DIMENSION=512
SIMILARITY=cosine

DATA_PATH=./data

Expand Down
59 changes: 59 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dotenv = "0.15.0"
dotenvy = "0.15"
prost = "0.14.1"
prost-types = "0.14.1"
rand = "0.9.2"
ratatui = "0.26"
reqwest = { version = "0.12", features = ["json", "blocking", "multipart"] }
rocksdb = "0.21.0"
Expand Down
11 changes: 9 additions & 2 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use defs::{DbError, IndexedVector, Similarity};
use defs::{DbError, Dimension, IndexedVector, Similarity};

use defs::{DenseVector, Payload, Point, PointId};
use index::hnsw::HnswIndex;
use std::path::PathBuf;
// use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, RwLock};
Expand Down Expand Up @@ -136,7 +137,8 @@ pub struct DbConfig {
pub storage_type: StorageType,
pub index_type: IndexType,
pub data_path: PathBuf,
pub dimension: usize,
pub dimension: Dimension,
pub similarity: Similarity,
}

pub fn init_api(config: DbConfig) -> Result<VectorDb, DbError> {
Expand All @@ -149,6 +151,10 @@ pub fn init_api(config: DbConfig) -> Result<VectorDb, DbError> {
// Initialize the vector index
let index: Arc<RwLock<dyn VectorIndex>> = match config.index_type {
IndexType::Flat => Arc::new(RwLock::new(FlatIndex::new())),
IndexType::HNSW => Arc::new(RwLock::new(HnswIndex::new(
config.similarity,
config.dimension,
))),
_ => Arc::new(RwLock::new(FlatIndex::new())),
};

Expand Down Expand Up @@ -178,6 +184,7 @@ mod tests {
index_type: IndexType::Flat,
data_path: temp_dir.path().to_path_buf(),
dimension: 3,
similarity: Similarity::Cosine,
};
init_api(config).unwrap()
}
Expand Down
3 changes: 3 additions & 0 deletions crates/defs/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::io;

use crate::Dimension;
#[derive(Debug, PartialEq, Eq)]
pub enum DbError {
ParseError,
Expand All @@ -8,6 +10,7 @@ pub enum DbError {
IndexError(String),
LockError,
DimensionMismatch,
InvalidDimension { expected: Dimension, got: Dimension },
}

#[derive(Debug)]
Expand Down
30 changes: 29 additions & 1 deletion crates/defs/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub type Element = f32;
// pub type ElementHalf = f16; - Unstable https://github.com/rust-lang/rust/issues/116909
pub type ElementByte = u8;

pub type Dimension = usize;

// Dense Vector and Vector are considered same
// Sparse vector implementation not supported yet. Refer lib/sparse/src/common/sparse_vector.rs
pub type DenseVector = Vec<Element>;
Expand Down Expand Up @@ -43,7 +45,7 @@ pub struct IndexedVector {
pub vector: DenseVector,
}

#[derive(Deserialize, Copy, Clone)]
#[derive(Debug, Deserialize, Copy, Clone)]
pub enum Similarity {
Euclidean,
Manhattan,
Expand Down Expand Up @@ -89,3 +91,29 @@ impl<'q> Eq for DistanceOrderedVector<'q> {}
// Discovery(DiscoveryQuery<VectorInternal>),
// Context(ContextQuery<VectorInternal>),
// }

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct OrdF32(f32);

impl OrdF32 {
pub fn new(x: f32) -> Self {
Self(x)
}
pub fn into_inner(self) -> f32 {
self.0
}
}

impl Eq for OrdF32 {}

impl Ord for OrdF32 {
fn cmp(&self, other: &Self) -> Ordering {
self.0.total_cmp(&other.0)
}
}

impl PartialOrd for OrdF32 {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
2 changes: 2 additions & 0 deletions crates/grpc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::service::vectordb::{DenseVector, InsertVectorRequest, Payload, PointI
use crate::service::{VectorDBService, run_server};
use crate::utils::ServerEndpoint;
use api::DbConfig;
use defs::Similarity;
use index::IndexType;
use std::net::SocketAddr;
use std::sync::Arc;
Expand Down Expand Up @@ -31,6 +32,7 @@ async fn start_test_server() -> Result<SocketAddr, Box<dyn std::error::Error>> {
index_type: IndexType::Flat,
data_path: temp_dir.path().to_path_buf(),
dimension: 3,
similarity: Similarity::Cosine,
};

let vector_db_api = api::init_api(db_config)?;
Expand Down
1 change: 1 addition & 0 deletions crates/index/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ license.workspace = true

[dependencies]
defs.workspace = true
rand.workspace = true
uuid.workspace = true
29 changes: 0 additions & 29 deletions crates/index/src/hnsw.rs

This file was deleted.

Loading
Loading