claw-vector 0.1.1

The semantic memory engine for ClawDB — HNSW vector indexing and storage
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// search/filters.rs — metadata filter DSL extension methods and helpers.
use crate::{
    error::{VectorError, VectorResult},
    types::MetadataFilter,
};

/// Extension trait adding helper constructors to [`MetadataFilter`].
pub trait MetadataFilterExt {
    /// Build an equality filter: `key == value`.
    fn eq(key: impl Into<String>, value: serde_json::Value) -> MetadataFilter;

    /// Build a greater-than filter: `key > value`.
    fn gt(key: impl Into<String>, value: f64) -> MetadataFilter;

    /// Build a less-than filter: `key < value`.
    fn lt(key: impl Into<String>, value: f64) -> MetadataFilter;

    /// Build a case-insensitive string contains filter.
    fn contains(key: impl Into<String>, value: impl Into<String>) -> MetadataFilter;

    /// Build a membership filter.
    fn one_of(key: impl Into<String>, values: Vec<serde_json::Value>) -> MetadataFilter;

    /// Build an existence filter.
    fn exists(key: impl Into<String>) -> MetadataFilter;

    /// Build an AND filter from a list of sub-filters.
    fn and(filters: Vec<MetadataFilter>) -> MetadataFilter;

    /// Build an OR filter from a list of sub-filters.
    fn or(filters: Vec<MetadataFilter>) -> MetadataFilter;

    /// Build a NOT filter wrapping another filter.
    fn not(filter: MetadataFilter) -> MetadataFilter;
}

impl MetadataFilterExt for MetadataFilter {
    fn eq(key: impl Into<String>, value: serde_json::Value) -> MetadataFilter {
        MetadataFilter::Eq {
            key: key.into(),
            value,
        }
    }

    fn gt(key: impl Into<String>, value: f64) -> MetadataFilter {
        MetadataFilter::Gt {
            key: key.into(),
            value,
        }
    }

    fn lt(key: impl Into<String>, value: f64) -> MetadataFilter {
        MetadataFilter::Lt {
            key: key.into(),
            value,
        }
    }

    fn contains(key: impl Into<String>, value: impl Into<String>) -> MetadataFilter {
        MetadataFilter::Contains {
            key: key.into(),
            value: value.into(),
        }
    }

    fn one_of(key: impl Into<String>, values: Vec<serde_json::Value>) -> MetadataFilter {
        MetadataFilter::In {
            key: key.into(),
            values,
        }
    }

    fn exists(key: impl Into<String>) -> MetadataFilter {
        MetadataFilter::Exists { key: key.into() }
    }

    fn and(filters: Vec<MetadataFilter>) -> MetadataFilter {
        MetadataFilter::And(filters)
    }

    fn or(filters: Vec<MetadataFilter>) -> MetadataFilter {
        MetadataFilter::Or(filters)
    }

    fn not(filter: MetadataFilter) -> MetadataFilter {
        MetadataFilter::Not(Box::new(filter))
    }
}

/// Traverse nested JSON using dot notation with optional array indices.
pub fn parse_json_path<'a>(
    metadata: &'a serde_json::Value,
    path: &str,
) -> Option<&'a serde_json::Value> {
    if path.trim().is_empty() {
        return None;
    }

    let mut current = metadata;
    for segment in path.split('.') {
        if segment.is_empty() {
            return None;
        }

        current = match current {
            serde_json::Value::Object(map) => map.get(segment)?,
            serde_json::Value::Array(items) => {
                let index = segment.parse::<usize>().ok()?;
                items.get(index)?
            }
            _ => return None,
        };
    }

    Some(current)
}

/// Apply a metadata filter to a JSON metadata document.
pub fn apply_filter(filter: &MetadataFilter, metadata: &serde_json::Value) -> bool {
    match filter {
        MetadataFilter::Eq { key, value } => parse_json_path(metadata, key) == Some(value),
        MetadataFilter::Gt { key, value } => parse_json_path(metadata, key)
            .and_then(serde_json::Value::as_f64)
            .is_some_and(|candidate| candidate > *value),
        MetadataFilter::Lt { key, value } => parse_json_path(metadata, key)
            .and_then(serde_json::Value::as_f64)
            .is_some_and(|candidate| candidate < *value),
        MetadataFilter::Contains { key, value } => parse_json_path(metadata, key)
            .and_then(serde_json::Value::as_str)
            .is_some_and(|candidate| {
                candidate
                    .to_ascii_lowercase()
                    .contains(&value.to_ascii_lowercase())
            }),
        MetadataFilter::In { key, values } => parse_json_path(metadata, key)
            .is_some_and(|candidate| values.iter().any(|value| value == candidate)),
        MetadataFilter::Exists { key } => parse_json_path(metadata, key).is_some(),
        MetadataFilter::And(filters) => filters.iter().all(|nested| apply_filter(nested, metadata)),
        MetadataFilter::Or(filters) => filters.iter().any(|nested| apply_filter(nested, metadata)),
        MetadataFilter::Not(filter) => !apply_filter(filter, metadata),
    }
}

/// Validate a metadata filter before search execution.
pub fn validate_filter(filter: &MetadataFilter) -> VectorResult<()> {
    validate_filter_inner(filter, 1)
}

fn validate_filter_inner(filter: &MetadataFilter, depth: usize) -> VectorResult<()> {
    if depth > 10 {
        return Err(VectorError::FilterError(
            "metadata filter nesting exceeds maximum depth of 10".into(),
        ));
    }

    match filter {
        MetadataFilter::Eq { key, .. }
        | MetadataFilter::Gt { key, .. }
        | MetadataFilter::Lt { key, .. }
        | MetadataFilter::Contains { key, .. }
        | MetadataFilter::In { key, .. }
        | MetadataFilter::Exists { key } => {
            if key.trim().is_empty() {
                return Err(VectorError::FilterError(
                    "metadata filter key must not be empty".into(),
                ));
            }
            Ok(())
        }
        MetadataFilter::And(filters) => {
            if filters.is_empty() {
                return Err(VectorError::FilterError(
                    "metadata AND filter must contain at least one clause".into(),
                ));
            }
            for nested in filters {
                validate_filter_inner(nested, depth + 1)?;
            }
            Ok(())
        }
        MetadataFilter::Or(filters) => {
            if filters.is_empty() {
                return Err(VectorError::FilterError(
                    "metadata OR filter must contain at least one clause".into(),
                ));
            }
            for nested in filters {
                validate_filter_inner(nested, depth + 1)?;
            }
            Ok(())
        }
        MetadataFilter::Not(filter) => validate_filter_inner(filter, depth + 1),
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::{apply_filter, parse_json_path, validate_filter};
    use crate::types::MetadataFilter;

    fn sample_metadata() -> serde_json::Value {
        json!({
            "user": {
                "role": "Admin",
                "age": 34,
                "tags": ["alpha", "beta"],
                "profile": {
                    "score": 91.5,
                    "active": true
                }
            },
            "items": [
                { "name": "First", "price": 9.5 },
                { "name": "Second", "price": 12.0 }
            ],
            "title": "Vector Search Primer",
            "priority": 7,
            "status": "published"
        })
    }

    #[test]
    fn parse_json_path_reads_top_level_value() {
        let metadata = sample_metadata();
        assert_eq!(
            parse_json_path(&metadata, "status"),
            Some(&json!("published"))
        );
    }

    #[test]
    fn parse_json_path_reads_nested_value() {
        let metadata = sample_metadata();
        assert_eq!(
            parse_json_path(&metadata, "user.profile.score"),
            Some(&json!(91.5))
        );
    }

    #[test]
    fn parse_json_path_reads_array_index() {
        let metadata = sample_metadata();
        assert_eq!(
            parse_json_path(&metadata, "items.1.name"),
            Some(&json!("Second"))
        );
    }

    #[test]
    fn parse_json_path_supports_nested_array_scalar() {
        let metadata = sample_metadata();
        assert_eq!(
            parse_json_path(&metadata, "user.tags.0"),
            Some(&json!("alpha"))
        );
    }

    #[test]
    fn parse_json_path_returns_none_for_missing_path() {
        let metadata = sample_metadata();
        assert_eq!(parse_json_path(&metadata, "user.profile.rank"), None);
    }

    #[test]
    fn parse_json_path_returns_none_for_invalid_array_index() {
        let metadata = sample_metadata();
        assert_eq!(parse_json_path(&metadata, "items.two.name"), None);
    }

    #[test]
    fn apply_filter_eq_matches_nested_value() {
        let metadata = sample_metadata();
        let filter = MetadataFilter::Eq {
            key: "user.role".into(),
            value: json!("Admin"),
        };
        assert!(apply_filter(&filter, &metadata));
    }

    #[test]
    fn apply_filter_gt_matches_numeric_threshold() {
        let metadata = sample_metadata();
        let filter = MetadataFilter::Gt {
            key: "user.age".into(),
            value: 30.0,
        };
        assert!(apply_filter(&filter, &metadata));
    }

    #[test]
    fn apply_filter_lt_matches_numeric_threshold() {
        let metadata = sample_metadata();
        let filter = MetadataFilter::Lt {
            key: "items.0.price".into(),
            value: 10.0,
        };
        assert!(apply_filter(&filter, &metadata));
    }

    #[test]
    fn apply_filter_contains_is_case_insensitive() {
        let metadata = sample_metadata();
        let filter = MetadataFilter::Contains {
            key: "title".into(),
            value: "search".into(),
        };
        assert!(apply_filter(&filter, &metadata));
    }

    #[test]
    fn apply_filter_in_matches_candidate_set() {
        let metadata = sample_metadata();
        let filter = MetadataFilter::In {
            key: "status".into(),
            values: vec![json!("draft"), json!("published")],
        };
        assert!(apply_filter(&filter, &metadata));
    }

    #[test]
    fn apply_filter_exists_detects_nested_key() {
        let metadata = sample_metadata();
        let filter = MetadataFilter::Exists {
            key: "user.profile.active".into(),
        };
        assert!(apply_filter(&filter, &metadata));
    }

    #[test]
    fn apply_filter_and_requires_all_clauses() {
        let metadata = sample_metadata();
        let filter = MetadataFilter::And(vec![
            MetadataFilter::Eq {
                key: "status".into(),
                value: json!("published"),
            },
            MetadataFilter::Gt {
                key: "priority".into(),
                value: 5.0,
            },
        ]);
        assert!(apply_filter(&filter, &metadata));
    }

    #[test]
    fn apply_filter_or_matches_any_clause() {
        let metadata = sample_metadata();
        let filter = MetadataFilter::Or(vec![
            MetadataFilter::Eq {
                key: "status".into(),
                value: json!("draft"),
            },
            MetadataFilter::Contains {
                key: "title".into(),
                value: "primer".into(),
            },
        ]);
        assert!(apply_filter(&filter, &metadata));
    }

    #[test]
    fn apply_filter_not_negates_match() {
        let metadata = sample_metadata();
        let filter = MetadataFilter::Not(Box::new(MetadataFilter::Eq {
            key: "status".into(),
            value: json!("draft"),
        }));
        assert!(apply_filter(&filter, &metadata));
    }

    #[test]
    fn apply_filter_handles_complex_boolean_expression() {
        let metadata = sample_metadata();
        let filter = MetadataFilter::And(vec![
            MetadataFilter::Or(vec![
                MetadataFilter::Eq {
                    key: "user.role".into(),
                    value: json!("Admin"),
                },
                MetadataFilter::Eq {
                    key: "user.role".into(),
                    value: json!("Editor"),
                },
            ]),
            MetadataFilter::Not(Box::new(MetadataFilter::Lt {
                key: "user.profile.score".into(),
                value: 90.0,
            })),
        ]);
        assert!(apply_filter(&filter, &metadata));
    }

    #[test]
    fn validate_filter_accepts_valid_nested_filter() {
        let filter = MetadataFilter::And(vec![
            MetadataFilter::Exists {
                key: "user.role".into(),
            },
            MetadataFilter::Not(Box::new(MetadataFilter::Contains {
                key: "title".into(),
                value: "draft".into(),
            })),
        ]);
        assert!(validate_filter(&filter).is_ok());
    }

    #[test]
    fn validate_filter_rejects_empty_and() {
        let err = validate_filter(&MetadataFilter::And(vec![])).unwrap_err();
        assert!(err.to_string().contains("AND filter"));
    }

    #[test]
    fn validate_filter_rejects_empty_or() {
        let err = validate_filter(&MetadataFilter::Or(vec![])).unwrap_err();
        assert!(err.to_string().contains("OR filter"));
    }

    #[test]
    fn validate_filter_rejects_blank_keys() {
        let err = validate_filter(&MetadataFilter::Exists { key: " ".into() }).unwrap_err();
        assert!(err.to_string().contains("must not be empty"));
    }

    #[test]
    fn validate_filter_rejects_excessive_depth() {
        let mut filter = MetadataFilter::Exists {
            key: "status".into(),
        };
        for _ in 0..10 {
            filter = MetadataFilter::Not(Box::new(filter));
        }
        let err = validate_filter(&filter).unwrap_err();
        assert!(err.to_string().contains("maximum depth"));
    }
}