Skip to content

Commit f883ad4

Browse files
committed
feat: implement hnsw index
Signed-off-by: Arshdeep54 <[email protected]>
1 parent 0e78312 commit f883ad4

File tree

13 files changed

+1002
-32
lines changed

13 files changed

+1002
-32
lines changed

Cargo.lock

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dotenv = "0.15.0"
2626
dotenvy = "0.15"
2727
prost = "0.14.1"
2828
prost-types = "0.14.1"
29+
rand = "0.9.2"
2930
ratatui = "0.26"
3031
reqwest = { version = "0.12", features = ["json", "blocking", "multipart"] }
3132
rocksdb = "0.21.0"

crates/api/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use defs::{DbError, IndexedVector, Similarity};
1+
use defs::{DbError, Dimension, IndexedVector, Similarity};
22

33
use defs::{DenseVector, Payload, Point, PointId};
44
use std::path::PathBuf;
@@ -136,7 +136,7 @@ pub struct DbConfig {
136136
pub storage_type: StorageType,
137137
pub index_type: IndexType,
138138
pub data_path: PathBuf,
139-
pub dimension: usize,
139+
pub dimension: Dimension,
140140
}
141141

142142
pub fn init_api(config: DbConfig) -> Result<VectorDb, DbError> {

crates/defs/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use std::io;
2+
3+
use crate::Dimension;
24
#[derive(Debug, PartialEq, Eq)]
35
pub enum DbError {
46
ParseError,
@@ -8,6 +10,7 @@ pub enum DbError {
810
IndexError(String),
911
LockError,
1012
DimensionMismatch,
13+
InvalidDimension { expected: Dimension, got: Dimension },
1114
}
1215

1316
#[derive(Debug)]

crates/defs/src/types.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub type Element = f32;
99
// pub type ElementHalf = f16; - Unstable https://github.com/rust-lang/rust/issues/116909
1010
pub type ElementByte = u8;
1111

12+
pub type Dimension = usize;
13+
1214
// Dense Vector and Vector are considered same
1315
// Sparse vector implementation not supported yet. Refer lib/sparse/src/common/sparse_vector.rs
1416
pub type DenseVector = Vec<Element>;
@@ -89,3 +91,29 @@ impl<'q> Eq for DistanceOrderedVector<'q> {}
8991
// Discovery(DiscoveryQuery<VectorInternal>),
9092
// Context(ContextQuery<VectorInternal>),
9193
// }
94+
95+
#[derive(Clone, Copy, Debug, PartialEq)]
96+
pub struct OrdF32(f32);
97+
98+
impl OrdF32 {
99+
pub fn new(x: f32) -> Self {
100+
Self(x)
101+
}
102+
pub fn into_inner(self) -> f32 {
103+
self.0
104+
}
105+
}
106+
107+
impl Eq for OrdF32 {}
108+
109+
impl Ord for OrdF32 {
110+
fn cmp(&self, other: &Self) -> Ordering {
111+
self.0.total_cmp(&other.0)
112+
}
113+
}
114+
115+
impl PartialOrd for OrdF32 {
116+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
117+
Some(self.cmp(other))
118+
}
119+
}

crates/index/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ license.workspace = true
88

99
[dependencies]
1010
defs.workspace = true
11+
rand.workspace = true
1112
uuid.workspace = true

crates/index/src/hnsw.rs

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)