use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub type VectorId = String;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct VectorMetadata {
pub data: HashMap<String, MetadataValue>,
}
impl VectorMetadata {
pub fn new() -> Self {
Self {
data: HashMap::new(),
}
}
pub fn from_pairs<I, K, V>(pairs: I) -> Self
where
I: IntoIterator<Item = (K, V)>,
K: Into<String>,
V: Into<MetadataValue>,
{
Self {
data: pairs.into_iter().map(|(k, v)| (k.into(), v.into())).collect(),
}
}
pub fn insert<K: Into<String>, V: Into<MetadataValue>>(&mut self, key: K, value: V) {
self.data.insert(key.into(), value.into());
}
pub fn get(&self, key: &str) -> Option<&MetadataValue> {
self.data.get(key)
}
pub fn get_string(&self, key: &str) -> Option<&str> {
match self.data.get(key)? {
MetadataValue::String(s) => Some(s),
_ => None,
}
}
pub fn get_int(&self, key: &str) -> Option<i64> {
match self.data.get(key)? {
MetadataValue::Int(i) => Some(*i),
_ => None,
}
}
pub fn get_float(&self, key: &str) -> Option<f64> {
match self.data.get(key)? {
MetadataValue::Float(f) => Some(*f),
_ => None,
}
}
pub fn get_bool(&self, key: &str) -> Option<bool> {
match self.data.get(key)? {
MetadataValue::Bool(b) => Some(*b),
_ => None,
}
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
pub fn len(&self) -> usize {
self.data.len()
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum MetadataValue {
String(String),
Int(i64),
Float(f64),
Bool(bool),
List(Vec<MetadataValue>),
}
impl From<String> for MetadataValue {
fn from(s: String) -> Self {
MetadataValue::String(s)
}
}
impl From<&str> for MetadataValue {
fn from(s: &str) -> Self {
MetadataValue::String(s.to_string())
}
}
impl From<i64> for MetadataValue {
fn from(i: i64) -> Self {
MetadataValue::Int(i)
}
}
impl From<i32> for MetadataValue {
fn from(i: i32) -> Self {
MetadataValue::Int(i as i64)
}
}
impl From<f64> for MetadataValue {
fn from(f: f64) -> Self {
MetadataValue::Float(f)
}
}
impl From<f32> for MetadataValue {
fn from(f: f32) -> Self {
MetadataValue::Float(f as f64)
}
}
impl From<bool> for MetadataValue {
fn from(b: bool) -> Self {
MetadataValue::Bool(b)
}
}
impl<T: Into<MetadataValue>> From<Vec<T>> for MetadataValue {
fn from(v: Vec<T>) -> Self {
MetadataValue::List(v.into_iter().map(Into::into).collect())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
pub id: VectorId,
pub score: f32,
pub metadata: Option<VectorMetadata>,
}
impl SearchResult {
pub fn new(id: VectorId, score: f32, metadata: Option<VectorMetadata>) -> Self {
Self { id, score, metadata }
}
}
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct StoredVector {
pub id: VectorId,
pub internal_id: usize,
pub vector: Vec<f32>,
pub metadata: Option<VectorMetadata>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_metadata_basic() {
let mut meta = VectorMetadata::new();
meta.insert("title", "Test Document");
meta.insert("score", 0.95f64);
meta.insert("count", 42i64);
meta.insert("active", true);
assert_eq!(meta.get_string("title"), Some("Test Document"));
assert_eq!(meta.get_float("score"), Some(0.95));
assert_eq!(meta.get_int("count"), Some(42));
assert_eq!(meta.get_bool("active"), Some(true));
}
#[test]
fn test_metadata_from_pairs() {
let meta = VectorMetadata::from_pairs([
("key1", MetadataValue::String("value1".to_string())),
("key2", MetadataValue::Int(123)),
]);
assert_eq!(meta.len(), 2);
assert_eq!(meta.get_string("key1"), Some("value1"));
assert_eq!(meta.get_int("key2"), Some(123));
}
#[test]
fn test_search_result() {
let result = SearchResult::new(
"doc1".to_string(),
0.95,
Some(VectorMetadata::from_pairs([("title", MetadataValue::String("Test".to_string()))])),
);
assert_eq!(result.id, "doc1");
assert_eq!(result.score, 0.95);
assert!(result.metadata.is_some());
}
}