Skip to main content

velesdb_core/collection/
metadata_collection.rs

1//! `MetadataCollection`: payload-only storage without vectors.
2//!
3//! Ideal for reference tables, catalogs, and structured metadata.
4//! Supports CRUD and VelesQL queries on payload — NOT vector search.
5//!
6//! # Design
7//!
8//! `MetadataCollection` is a pure newtype over `Collection` — all operations
9//! delegate to the single `inner` instance, matching the `VectorCollection` pattern
10//! and eliminating any dual-storage desync risk (C-02).
11
12use std::collections::HashMap;
13use std::path::PathBuf;
14
15use crate::collection::types::Collection;
16use crate::error::{Error, Result};
17use crate::point::{Point, SearchResult};
18
19/// A metadata-only collection storing structured payloads without vector indexes.
20///
21/// # Examples
22///
23/// ```rust,no_run
24/// use velesdb_core::{MetadataCollection, Point};
25/// use serde_json::json;
26///
27/// let coll = MetadataCollection::create("./data/products".into(), "products")?;
28///
29/// coll.upsert(vec![
30///     Point::metadata_only(1, json!({"name": "Widget", "price": 9.99})),
31/// ])?;
32/// # Ok::<(), velesdb_core::Error>(())
33/// ```
34#[derive(Clone)]
35pub struct MetadataCollection {
36    /// Single source of truth — all operations delegate here (C-02 pure newtype).
37    pub(crate) inner: Collection,
38}
39
40impl MetadataCollection {
41    // -------------------------------------------------------------------------
42    // Lifecycle
43    // -------------------------------------------------------------------------
44
45    /// Creates a new `MetadataCollection`.
46    ///
47    /// # Errors
48    ///
49    /// Returns an error if the directory cannot be created or storage fails.
50    pub fn create(path: PathBuf, name: &str) -> Result<Self> {
51        Ok(Self {
52            inner: Collection::create_metadata_only(path, name)?,
53        })
54    }
55
56    /// Opens an existing `MetadataCollection` from disk.
57    ///
58    /// # Errors
59    ///
60    /// Returns an error if config or storage cannot be opened.
61    pub fn open(path: PathBuf) -> Result<Self> {
62        Ok(Self {
63            inner: Collection::open(path)?,
64        })
65    }
66
67    /// Flushes to disk.
68    ///
69    /// Issue #423: This fast-path flush skips `vectors.idx` serialization.
70    /// The WAL provides crash recovery for the vector index.
71    ///
72    /// # Errors
73    ///
74    /// Returns an error if the flush fails.
75    pub fn flush(&self) -> Result<()> {
76        self.inner.flush()
77    }
78
79    /// Full durability flush including `vectors.idx` serialization.
80    ///
81    /// Issue #423: Use on graceful shutdown to avoid a full WAL replay
82    /// on the next startup.
83    ///
84    /// # Errors
85    ///
86    /// Returns an error if the flush fails.
87    pub fn flush_full(&self) -> Result<()> {
88        self.inner.flush_full()
89    }
90
91    // -------------------------------------------------------------------------
92    // Metadata
93    // -------------------------------------------------------------------------
94
95    /// Returns the collection name.
96    #[must_use]
97    pub fn name(&self) -> String {
98        self.inner.config().name
99    }
100
101    /// Returns the number of items in the collection.
102    #[must_use]
103    pub fn len(&self) -> usize {
104        self.inner.len()
105    }
106
107    /// Returns `true` if the collection is empty.
108    #[must_use]
109    pub fn is_empty(&self) -> bool {
110        self.inner.is_empty()
111    }
112
113    /// Returns the collection configuration.
114    #[must_use]
115    pub fn config(&self) -> crate::collection::CollectionConfig {
116        self.inner.config()
117    }
118
119    /// Returns `true` — metadata collections are always metadata-only.
120    #[must_use]
121    pub fn is_metadata_only(&self) -> bool {
122        true
123    }
124
125    /// Inserts or updates metadata-only points (convenience alias for `upsert`).
126    ///
127    /// # Errors
128    ///
129    /// Returns an error if a point carries a non-empty vector.
130    pub fn upsert_metadata(&self, points: impl IntoIterator<Item = Point>) -> Result<()> {
131        self.upsert(points)
132    }
133
134    /// Returns all stored IDs.
135    #[must_use]
136    pub fn all_ids(&self) -> Vec<u64> {
137        self.inner.all_ids()
138    }
139
140    /// Returns the next batch of points for scroll iteration.
141    ///
142    /// Delegates to the inner collection's `scroll_batch` (parallel
143    /// implementation to [`VectorCollection::scroll_batch`](crate::VectorCollection::scroll_batch)).
144    ///
145    /// # Errors
146    ///
147    /// Returns an error if `batch_size` is 0.
148    pub fn scroll_batch(
149        &self,
150        cursor: Option<u64>,
151        batch_size: usize,
152        filter: Option<&crate::filter::Filter>,
153    ) -> Result<crate::collection::ScrollBatch> {
154        self.inner.scroll_batch(cursor, batch_size, filter)
155    }
156
157    // -------------------------------------------------------------------------
158    // CRUD
159    // -------------------------------------------------------------------------
160
161    /// Inserts or updates metadata points (must have no vector).
162    ///
163    /// # Errors
164    ///
165    /// Returns an error if a point carries a non-empty vector,
166    /// or if storage operations fail.
167    pub fn upsert(&self, points: impl IntoIterator<Item = Point>) -> Result<()> {
168        let points: Vec<Point> = points.into_iter().collect();
169        let name = self.inner.config().name;
170
171        for point in &points {
172            if !point.vector.is_empty() {
173                return Err(Error::VectorNotAllowed(name.clone()));
174            }
175        }
176
177        self.inner.upsert_metadata(points)
178    }
179
180    /// Retrieves items by IDs.
181    #[must_use]
182    pub fn get(&self, ids: &[u64]) -> Vec<Option<Point>> {
183        self.inner.get(ids)
184    }
185
186    /// Deletes items by IDs.
187    ///
188    /// # Errors
189    ///
190    /// Returns an error if storage operations fail.
191    pub fn delete(&self, ids: &[u64]) -> Result<()> {
192        self.inner.delete(ids)
193    }
194
195    // -------------------------------------------------------------------------
196    // Text search
197    // -------------------------------------------------------------------------
198
199    /// Performs BM25 full-text search over payloads.
200    ///
201    /// # Errors
202    ///
203    /// Returns an error if storage retrieval fails.
204    pub fn text_search(&self, query: &str, k: usize) -> Result<Vec<SearchResult>> {
205        self.inner.text_search(query, k)
206    }
207
208    /// Performs vector similarity search.
209    ///
210    /// Note: metadata-only collections have no vectors, so this will
211    /// return an empty result set.
212    ///
213    /// # Errors
214    ///
215    /// Returns an error if the search fails.
216    pub fn search(&self, query: &[f32], k: usize) -> Result<Vec<SearchResult>> {
217        self.inner.search(query, k)
218    }
219
220    // -------------------------------------------------------------------------
221    // VelesQL
222    // -------------------------------------------------------------------------
223
224    /// Executes a `VelesQL` query.
225    ///
226    /// # Errors
227    ///
228    /// Returns an error if the query is invalid or execution fails.
229    pub fn execute_query(
230        &self,
231        query: &crate::velesql::Query,
232        params: &HashMap<String, serde_json::Value>,
233    ) -> Result<Vec<SearchResult>> {
234        self.inner.execute_query(query, params)
235    }
236
237    /// Executes a raw VelesQL string.
238    ///
239    /// # Errors
240    ///
241    /// Returns an error if parsing or execution fails.
242    pub fn execute_query_str(
243        &self,
244        sql: &str,
245        params: &HashMap<String, serde_json::Value>,
246    ) -> Result<Vec<SearchResult>> {
247        self.inner.execute_query_str(sql, params)
248    }
249}