velesdb_core/
point.rs

1//! Point data structure representing a vector with metadata.
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value as JsonValue;
5
6/// A point in the vector database.
7///
8/// A point consists of:
9/// - A unique identifier
10/// - A vector (embedding)
11/// - Optional payload (metadata)
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct Point {
14    /// Unique identifier for the point.
15    pub id: u64,
16
17    /// The vector embedding.
18    pub vector: Vec<f32>,
19
20    /// Optional JSON payload containing metadata.
21    #[serde(default)]
22    pub payload: Option<JsonValue>,
23}
24
25impl Point {
26    /// Creates a new point with the given ID, vector, and optional payload.
27    ///
28    /// # Arguments
29    ///
30    /// * `id` - Unique identifier
31    /// * `vector` - Vector embedding
32    /// * `payload` - Optional metadata
33    #[must_use]
34    pub fn new(id: u64, vector: Vec<f32>, payload: Option<JsonValue>) -> Self {
35        Self {
36            id,
37            vector,
38            payload,
39        }
40    }
41
42    /// Creates a new point without payload.
43    #[must_use]
44    pub fn without_payload(id: u64, vector: Vec<f32>) -> Self {
45        Self::new(id, vector, None)
46    }
47
48    /// Returns the dimension of the vector.
49    #[must_use]
50    pub fn dimension(&self) -> usize {
51        self.vector.len()
52    }
53}
54
55/// A search result containing a point and its similarity score.
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct SearchResult {
58    /// The matching point.
59    pub point: Point,
60
61    /// Similarity score (interpretation depends on the distance metric).
62    pub score: f32,
63}
64
65impl SearchResult {
66    /// Creates a new search result.
67    #[must_use]
68    pub const fn new(point: Point, score: f32) -> Self {
69        Self { point, score }
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76    use serde_json::json;
77
78    #[test]
79    fn test_point_creation() {
80        let point = Point::new(1, vec![0.1, 0.2, 0.3], Some(json!({"title": "Test"})));
81
82        assert_eq!(point.id, 1);
83        assert_eq!(point.dimension(), 3);
84        assert!(point.payload.is_some());
85    }
86
87    #[test]
88    fn test_point_without_payload() {
89        let point = Point::without_payload(1, vec![0.1, 0.2, 0.3]);
90
91        assert_eq!(point.id, 1);
92        assert!(point.payload.is_none());
93    }
94
95    #[test]
96    fn test_point_serialization() {
97        let point = Point::new(1, vec![0.1, 0.2], Some(json!({"key": "value"})));
98        let json = serde_json::to_string(&point).unwrap();
99        let deserialized: Point = serde_json::from_str(&json).unwrap();
100
101        assert_eq!(point.id, deserialized.id);
102        assert_eq!(point.vector, deserialized.vector);
103    }
104}