velesdb_core/collection/vector_collection/crud.rs
1//! CRUD and index-mutation operations for `VectorCollection`.
2
3use crate::error::Result;
4use crate::point::Point;
5
6use super::VectorCollection;
7
8impl VectorCollection {
9 /// Bulk insert optimized for high-throughput import.
10 ///
11 /// # Errors
12 ///
13 /// Returns an error if any point has a mismatched dimension.
14 pub fn upsert_bulk(&self, points: &[Point]) -> Result<usize> {
15 self.inner.upsert_bulk(points)
16 }
17
18 /// Inserts or updates points in the collection.
19 ///
20 /// # Errors
21 ///
22 /// - Returns an error if any point's dimension does not match the collection.
23 /// - Returns an error if storage operations fail.
24 ///
25 /// # Examples
26 ///
27 /// ```rust,no_run
28 /// # use velesdb_core::{VectorCollection, DistanceMetric, Point, StorageMode};
29 /// # use serde_json::json;
30 /// # let coll = VectorCollection::create("./data/v".into(), "v", 128, DistanceMetric::Cosine, StorageMode::Full)?;
31 /// coll.upsert(vec![
32 /// Point::new(1, vec![0.1; 128], Some(json!({"title": "Hello"}))),
33 /// Point::new(2, vec![0.2; 128], None),
34 /// ])?;
35 /// # Ok::<(), velesdb_core::Error>(())
36 /// ```
37 pub fn upsert(&self, points: impl IntoIterator<Item = Point>) -> Result<()> {
38 self.inner.upsert(points)
39 }
40
41 /// Retrieves points by IDs, returning `None` for missing entries.
42 ///
43 /// # Examples
44 ///
45 /// ```rust,no_run
46 /// # use velesdb_core::{VectorCollection, DistanceMetric, StorageMode};
47 /// # let coll = VectorCollection::create("./data/v".into(), "v", 128, DistanceMetric::Cosine, StorageMode::Full)?;
48 /// let points = coll.get(&[1, 2, 3]);
49 /// for (id, maybe_point) in [1, 2, 3].iter().zip(&points) {
50 /// if let Some(p) = maybe_point {
51 /// println!("Found point {id} with payload {:?}", p.payload);
52 /// }
53 /// }
54 /// # Ok::<(), velesdb_core::Error>(())
55 /// ```
56 #[must_use]
57 pub fn get(&self, ids: &[u64]) -> Vec<Option<Point>> {
58 self.inner.get(ids)
59 }
60
61 /// Deletes points by IDs.
62 ///
63 /// Missing IDs are silently ignored.
64 ///
65 /// # Errors
66 ///
67 /// - Returns an error if storage operations fail.
68 ///
69 /// # Examples
70 ///
71 /// ```rust,no_run
72 /// # use velesdb_core::{VectorCollection, DistanceMetric, StorageMode};
73 /// # let coll = VectorCollection::create("./data/v".into(), "v", 128, DistanceMetric::Cosine, StorageMode::Full)?;
74 /// coll.delete(&[1, 2, 3])?;
75 /// # Ok::<(), velesdb_core::Error>(())
76 /// ```
77 pub fn delete(&self, ids: &[u64]) -> Result<()> {
78 self.inner.delete(ids)
79 }
80
81 /// Inserts or updates metadata-only points (no vectors).
82 ///
83 /// # Errors
84 ///
85 /// - Returns an error if storage operations fail.
86 pub fn upsert_metadata(
87 &self,
88 points: impl IntoIterator<Item = crate::point::Point>,
89 ) -> Result<()> {
90 self.inner.upsert_metadata(points)
91 }
92
93 /// Creates a secondary metadata index on a payload field.
94 ///
95 /// # Errors
96 ///
97 /// - Returns an error if the index already exists or storage fails.
98 pub fn create_index(&self, field: &str) -> Result<()> {
99 self.inner.create_index(field)
100 }
101
102 /// Creates a property index for O(1) equality lookups.
103 ///
104 /// # Errors
105 ///
106 /// - Returns an error if the index already exists or storage fails.
107 pub fn create_property_index(&self, label: &str, property: &str) -> Result<()> {
108 self.inner.create_property_index(label, property)
109 }
110
111 /// Creates a range index for O(log n) range queries.
112 ///
113 /// # Errors
114 ///
115 /// - Returns an error if the index already exists or storage fails.
116 pub fn create_range_index(&self, label: &str, property: &str) -> Result<()> {
117 self.inner.create_range_index(label, property)
118 }
119
120 /// Drops an index, returning `true` if an index was removed.
121 ///
122 /// # Errors
123 ///
124 /// - Returns an error if the drop operation fails.
125 pub fn drop_index(&self, label: &str, property: &str) -> Result<bool> {
126 self.inner.drop_index(label, property)
127 }
128}