rskit-vectorstore 0.2.0-alpha.1

Vector store abstraction with in-memory default and opt-in adapter backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Vector store trait definition.

use std::collections::HashMap;
use std::fmt;

use async_trait::async_trait;
use rskit_errors::{AppError, AppResult, ErrorCode};
use serde::de::Visitor;
use serde::{Deserialize, Serialize};

use crate::VectorStoreLimits;

/// Typed scalar payload value stored alongside vector points.
#[derive(Debug, Clone, Serialize, PartialEq)]
#[non_exhaustive]
#[serde(untagged)]
pub enum PayloadValue {
    /// UTF-8 string value.
    String(String),
    /// Signed integer value.
    Integer(i64),
    /// Floating-point value.
    ///
    /// Values are validated as finite by store/adaptor request validation before
    /// they are accepted for storage or filtering.
    Float(f64),
    /// Boolean value.
    Bool(bool),
}

impl PayloadValue {
    /// Return the value as a string when it is string-typed.
    #[must_use]
    pub fn as_str(&self) -> Option<&str> {
        match self {
            Self::String(value) => Some(value),
            Self::Integer(_) | Self::Float(_) | Self::Bool(_) => None,
        }
    }

    pub(crate) fn encoded_len(&self) -> usize {
        match self {
            Self::String(value) => value.len(),
            Self::Integer(_) => std::mem::size_of::<i64>(),
            Self::Float(_) => std::mem::size_of::<f64>(),
            Self::Bool(_) => std::mem::size_of::<bool>(),
        }
    }
}

impl<'de> Deserialize<'de> for PayloadValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_any(PayloadValueVisitor)
    }
}

struct PayloadValueVisitor;

impl Visitor<'_> for PayloadValueVisitor {
    type Value = PayloadValue;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a string, signed integer, finite float, or boolean payload value")
    }

    fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(PayloadValue::Bool(value))
    }

    fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(PayloadValue::Integer(value))
    }

    fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        i64::try_from(value)
            .map(PayloadValue::Integer)
            .map_err(|_| {
                <E as serde::de::Error>::custom("unsigned payload integers must fit in i64")
            })
    }

    fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(PayloadValue::Float(value))
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(PayloadValue::String(value.to_owned()))
    }

    fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(PayloadValue::String(value))
    }
}

impl From<String> for PayloadValue {
    fn from(value: String) -> Self {
        Self::String(value)
    }
}

impl From<&str> for PayloadValue {
    fn from(value: &str) -> Self {
        Self::String(value.to_owned())
    }
}

impl From<i64> for PayloadValue {
    fn from(value: i64) -> Self {
        Self::Integer(value)
    }
}

impl From<i32> for PayloadValue {
    fn from(value: i32) -> Self {
        Self::Integer(i64::from(value))
    }
}

impl From<u32> for PayloadValue {
    fn from(value: u32) -> Self {
        Self::Integer(i64::from(value))
    }
}

impl From<f64> for PayloadValue {
    fn from(value: f64) -> Self {
        Self::Float(value)
    }
}

impl From<f32> for PayloadValue {
    fn from(value: f32) -> Self {
        Self::Float(f64::from(value))
    }
}

impl From<bool> for PayloadValue {
    fn from(value: bool) -> Self {
        Self::Bool(value)
    }
}

/// Payload stored alongside each vector point.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PointPayload {
    /// Metadata fields carried with the point.
    pub fields: HashMap<String, PayloadValue>,
}

impl PointPayload {
    /// Create an empty payload.
    #[must_use]
    pub fn new() -> Self {
        Self {
            fields: HashMap::new(),
        }
    }

    /// Add a payload field.
    #[must_use]
    pub fn with_field(mut self, key: impl Into<String>, value: impl Into<PayloadValue>) -> Self {
        self.fields.insert(key.into(), value.into());
        self
    }

    /// Validate field count and approximate scalar payload bytes against limits.
    pub fn validate_limits(&self, limits: &VectorStoreLimits) -> AppResult<()> {
        if self.fields.len() > limits.max_payload_fields {
            return Err(AppError::new(
                ErrorCode::InvalidInput,
                format!(
                    "vector payload has {} fields, exceeding max_payload_fields {}",
                    self.fields.len(),
                    limits.max_payload_fields
                ),
            ));
        }
        let mut total_bytes = 0usize;
        for (key, payload_value) in &self.fields {
            if let PayloadValue::Float(float_value) = payload_value
                && !float_value.is_finite()
            {
                return Err(AppError::new(
                    ErrorCode::InvalidInput,
                    "vector payload float values must be finite",
                ));
            }
            total_bytes = total_bytes
                .checked_add(key.len())
                .and_then(|bytes| bytes.checked_add(payload_value.encoded_len()))
                .ok_or_else(|| {
                    AppError::new(
                        ErrorCode::InvalidInput,
                        "vector payload byte size overflowed validation bounds",
                    )
                })?;
        }
        if total_bytes > limits.max_payload_bytes {
            return Err(AppError::new(
                ErrorCode::InvalidInput,
                format!(
                    "vector payload is {total_bytes} bytes, exceeding max_payload_bytes {}",
                    limits.max_payload_bytes
                ),
            ));
        }
        Ok(())
    }
}

impl Default for PointPayload {
    fn default() -> Self {
        Self::new()
    }
}

/// A single search result from the vector store.
#[derive(Debug, Clone)]
pub struct SearchResult {
    /// Point identifier.
    pub id: String,
    /// Backend-specific similarity score.
    pub score: f32,
    /// Payload attached to the point.
    pub payload: PointPayload,
}

/// Canonical vector distance/similarity metrics.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
#[serde(rename_all = "lowercase")]
pub enum SimilarityMetric {
    /// Cosine similarity.
    #[default]
    Cosine,
    /// Dot product.
    Dot,
    /// Euclidean L2 distance.
    L2,
}

/// Exact-match metadata filter condition.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FilterCondition {
    /// Payload field path.
    pub field: String,
    /// Exact value to match.
    pub equals: PayloadValue,
}

/// Optional filters for search queries.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SearchFilter {
    /// Filter by exact field match (e.g., platform = "youtube").
    #[serde(default)]
    pub must: Vec<FilterCondition>,
}

impl SearchFilter {
    /// Create an empty filter.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Add an exact-match condition to the `must` list.
    #[must_use]
    pub fn must_match(mut self, field: impl Into<String>, value: impl Into<PayloadValue>) -> Self {
        self.must.push(FilterCondition {
            field: field.into(),
            equals: value.into(),
        });
        self
    }

    /// Validate filter condition count and approximate scalar bytes against limits.
    pub fn validate_limits(&self, limits: &VectorStoreLimits) -> AppResult<()> {
        if self.must.len() > limits.max_filter_conditions {
            return Err(AppError::new(
                ErrorCode::InvalidInput,
                format!(
                    "vector search filter has {} conditions, exceeding max_filter_conditions {}",
                    self.must.len(),
                    limits.max_filter_conditions
                ),
            ));
        }
        let mut total_bytes = 0usize;
        for condition in &self.must {
            if let PayloadValue::Float(float_value) = &condition.equals
                && !float_value.is_finite()
            {
                return Err(AppError::new(
                    ErrorCode::InvalidInput,
                    "vector filter float values must be finite",
                ));
            }
            total_bytes = total_bytes
                .checked_add(condition.field.len())
                .and_then(|bytes| bytes.checked_add(condition.equals.encoded_len()))
                .ok_or_else(|| {
                    AppError::new(
                        ErrorCode::InvalidInput,
                        "vector filter byte size overflowed validation bounds",
                    )
                })?;
        }
        if total_bytes > limits.max_payload_bytes {
            return Err(AppError::new(
                ErrorCode::InvalidInput,
                format!(
                    "vector search filter is {total_bytes} bytes, exceeding max_payload_bytes {}",
                    limits.max_payload_bytes
                ),
            ));
        }
        Ok(())
    }
}

/// Trait for vector similarity search stores.
#[async_trait]
pub trait VectorStore: Send + Sync {
    /// Ensure a collection exists, creating it if necessary.
    async fn ensure_collection(&self, collection: &str, dimensions: usize) -> AppResult<()>;

    /// Insert or update a vector point.
    async fn upsert(
        &self,
        collection: &str,
        id: &str,
        vector: Vec<f32>,
        payload: PointPayload,
    ) -> AppResult<()>;

    /// Search for similar vectors.
    async fn search(
        &self,
        collection: &str,
        vector: Vec<f32>,
        limit: usize,
        filter: Option<SearchFilter>,
    ) -> AppResult<Vec<SearchResult>>;

    /// Delete a point by ID.
    async fn delete(&self, collection: &str, id: &str) -> AppResult<()>;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn payload_value_serializes_as_json_scalar() {
        assert_eq!(
            serde_json::to_value(PayloadValue::String("doc".to_owned())).unwrap(),
            serde_json::json!("doc")
        );
        assert_eq!(
            serde_json::to_value(PayloadValue::Integer(42)).unwrap(),
            serde_json::json!(42)
        );
        assert_eq!(
            serde_json::to_value(PayloadValue::Float(1.5)).unwrap(),
            serde_json::json!(1.5)
        );
        assert_eq!(
            serde_json::to_value(PayloadValue::Bool(true)).unwrap(),
            serde_json::json!(true)
        );
    }

    #[test]
    fn payload_value_deserializes_from_json_scalar() {
        assert_eq!(
            serde_json::from_value::<PayloadValue>(serde_json::json!("doc")).unwrap(),
            PayloadValue::String("doc".to_owned())
        );
        assert_eq!(
            serde_json::from_value::<PayloadValue>(serde_json::json!(42)).unwrap(),
            PayloadValue::Integer(42)
        );
        assert_eq!(
            serde_json::from_value::<PayloadValue>(serde_json::json!(1.5)).unwrap(),
            PayloadValue::Float(1.5)
        );
        assert_eq!(
            serde_json::from_value::<PayloadValue>(serde_json::json!(true)).unwrap(),
            PayloadValue::Bool(true)
        );
    }

    #[test]
    fn payload_value_rejects_unsigned_integer_over_i64_max() {
        let value = serde_json::Value::Number(serde_json::Number::from(u64::MAX));

        assert!(serde_json::from_value::<PayloadValue>(value).is_err());
    }
}