unum-cloud / usearch

Fast Open-Source Search & Clustering engine × for Vectors & 🔜 Strings × in C++, C, Python, JavaScript, Rust, Java, Objective-C, Swift, C#, GoLang, and Wolfram 🔍
https://unum-cloud.github.io/usearch/
Apache License 2.0
2.15k stars 130 forks source link

add serde Serialize/Deserialize for IndexOptions #284

Closed weedge closed 11 months ago

weedge commented 11 months ago

add serde Serialize/Deserialize for IndexOptions, in order to config index opts

ashvardanian commented 11 months ago

Hey, @weedge! We generally avoid dependencies in the higher-level bindings. It seems like it makes more sense to use it outside of the binding rather than inside. What's your opinion on that?

weedge commented 11 months ago

hi~! @ashvardanian use it outside of the binding, like this. ( it's ok, but have a little couple together;maybe there other good ways. )

#[derive(Serialize, Deserialize, Debug, PartialEq)]
enum MKind {
    IP,
    L2sq,
    Cos,
    Pearson,
    Haversine,
    Hamming,
    Tanimoto,
    Sorensen,
}

impl MKind {
    fn map_metric_kind(&self) -> MetricKind {
        match self {
            Self::IP => MetricKind::IP,
            Self::L2sq => MetricKind::L2sq,
            Self::Cos => MetricKind::Cos,
            Self::Pearson => MetricKind::Pearson,
            Self::Haversine => MetricKind::Haversine,
            Self::Hamming => MetricKind::Hamming,
            Self::Tanimoto => MetricKind::Tanimoto,
            Self::Sorensen => MetricKind::Sorensen,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
enum SKind {
    F64,
    F32,
    F16,
    I8,
    B1,
}

impl SKind {
    fn map_scalar_kind(&self) -> ScalarKind {
        match self {
            Self::F64 => ScalarKind::F64,
            Self::F32 => ScalarKind::F32,
            Self::F16 => ScalarKind::F16,
            Self::I8 => ScalarKind::I8,
            Self::B1 => ScalarKind::B1,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct IndexOpts {
    dimensions: usize,
    metric: MKind,
    quantization: SKind,
    connectivity: usize,
    expansion_add: usize,
    expansion_search: usize,
    multi: bool,
}

if enum MetricKind and ScalarKind change (add new one) , then update usearch crate package version, need to change this map_* fn and enum MKind and Scand.

ashvardanian commented 11 months ago

If this can be done externally, let's keep it that way.

ashvardanian commented 11 months ago

@weedge, thank you for your effort! Are you, by any chance, on our Discord? If you let me know beforehand, which features you are looking to implement, we can probably schedule it more effectively 🤗

weedge commented 11 months ago

@ashvardanian thanks, it's my pleasure, how to find it on Discord? my Discord ID: weedge

ashvardanian commented 11 months ago

There is an invite link in the README.md.