velesdb_core/storage/
traits.rs

1//! Storage traits for vectors and payloads.
2//!
3//! This module defines the core storage abstractions used by `VelesDB`.
4
5use std::io;
6
7/// Trait defining storage operations for vectors.
8pub trait VectorStorage: Send + Sync {
9    /// Stores a vector with the given ID.
10    ///
11    /// # Errors
12    ///
13    /// Returns an error if the write operation fails.
14    fn store(&mut self, id: u64, vector: &[f32]) -> io::Result<()>;
15
16    /// Stores multiple vectors in a single batch operation.
17    ///
18    /// This is optimized for bulk imports:
19    /// - Single WAL write for the entire batch
20    /// - Contiguous memory writes
21    /// - Single fsync at the end
22    ///
23    /// # Errors
24    ///
25    /// Returns an error if the write operation fails.
26    fn store_batch(&mut self, vectors: &[(u64, &[f32])]) -> io::Result<usize>;
27
28    /// Retrieves a vector by ID.
29    ///
30    /// # Errors
31    ///
32    /// Returns an error if the read operation fails.
33    fn retrieve(&self, id: u64) -> io::Result<Option<Vec<f32>>>;
34
35    /// Deletes a vector by ID.
36    ///
37    /// # Errors
38    ///
39    /// Returns an error if the delete operation fails.
40    fn delete(&mut self, id: u64) -> io::Result<()>;
41
42    /// Flushes pending writes to disk.
43    ///
44    /// # Errors
45    ///
46    /// Returns an error if the flush operation fails.
47    fn flush(&mut self) -> io::Result<()>;
48
49    /// Returns the number of vectors stored.
50    fn len(&self) -> usize;
51
52    /// Returns true if the storage is empty.
53    fn is_empty(&self) -> bool {
54        self.len() == 0
55    }
56
57    /// Returns all stored IDs.
58    fn ids(&self) -> Vec<u64>;
59}
60
61/// Trait defining storage operations for metadata payloads.
62pub trait PayloadStorage: Send + Sync {
63    /// Stores a payload with the given ID.
64    ///
65    /// # Errors
66    ///
67    /// Returns an error if the write operation fails.
68    fn store(&mut self, id: u64, payload: &serde_json::Value) -> io::Result<()>;
69
70    /// Retrieves a payload by ID.
71    ///
72    /// # Errors
73    ///
74    /// Returns an error if the read operation fails.
75    fn retrieve(&self, id: u64) -> io::Result<Option<serde_json::Value>>;
76
77    /// Deletes a payload by ID.
78    ///
79    /// # Errors
80    ///
81    /// Returns an error if the delete operation fails.
82    fn delete(&mut self, id: u64) -> io::Result<()>;
83
84    /// Flushes pending writes to disk.
85    ///
86    /// # Errors
87    ///
88    /// Returns an error if the flush operation fails.
89    fn flush(&mut self) -> io::Result<()>;
90
91    /// Returns all stored IDs.
92    fn ids(&self) -> Vec<u64>;
93}