Skip to main content

velesdb_core/collection/vector_collection/
lifecycle.rs

1//! Constructor and persistence methods for `VectorCollection`.
2
3use std::path::PathBuf;
4
5use crate::collection::types::Collection;
6use crate::distance::DistanceMetric;
7use crate::error::Result;
8use crate::quantization::StorageMode;
9
10use super::VectorCollection;
11
12impl VectorCollection {
13    /// Creates a new `VectorCollection` at the given path.
14    ///
15    /// # Errors
16    ///
17    /// Returns an error if the directory cannot be created or storage fails.
18    pub fn create(
19        path: PathBuf,
20        _name: &str,
21        dimension: usize,
22        metric: DistanceMetric,
23        storage_mode: StorageMode,
24    ) -> Result<Self> {
25        Ok(Self {
26            inner: Collection::create_with_options(path, dimension, metric, storage_mode)?,
27        })
28    }
29
30    /// Creates a new `VectorCollection` with custom HNSW parameters.
31    ///
32    /// When `m` or `ef_construction` are `Some`, those values override the
33    /// auto-tuned defaults. When both are `None`, this is equivalent to
34    /// [`VectorCollection::create`].
35    ///
36    /// # Errors
37    ///
38    /// Returns an error if the directory cannot be created or storage fails.
39    pub fn create_with_hnsw(
40        path: PathBuf,
41        _name: &str,
42        dimension: usize,
43        metric: DistanceMetric,
44        storage_mode: StorageMode,
45        m: Option<usize>,
46        ef_construction: Option<usize>,
47    ) -> Result<Self> {
48        let mut params = crate::index::hnsw::HnswParams::auto(dimension);
49        if let Some(m) = m {
50            params.max_connections = m;
51        }
52        if let Some(ef) = ef_construction {
53            params.ef_construction = ef;
54        }
55        params.storage_mode = storage_mode;
56        Ok(Self {
57            inner: Collection::create_with_hnsw_params(
58                path,
59                dimension,
60                metric,
61                storage_mode,
62                params,
63            )?,
64        })
65    }
66
67    /// Opens an existing `VectorCollection` from disk.
68    ///
69    /// # Errors
70    ///
71    /// Returns an error if the config file cannot be read or storage cannot be opened.
72    pub fn open(path: PathBuf) -> Result<Self> {
73        Ok(Self {
74            inner: Collection::open(path)?,
75        })
76    }
77
78    /// Flushes all engines to disk and saves the config.
79    ///
80    /// Issue #423: This fast-path flush skips `vectors.idx` serialization.
81    /// The WAL provides crash recovery for the vector index.
82    ///
83    /// # Errors
84    ///
85    /// Returns an error if any flush operation fails.
86    pub fn flush(&self) -> Result<()> {
87        self.inner.flush()
88    }
89
90    /// Full durability flush including `vectors.idx` serialization.
91    ///
92    /// Issue #423: Use on graceful shutdown to avoid a full WAL replay
93    /// on the next startup.
94    ///
95    /// # Errors
96    ///
97    /// Returns an error if any flush operation fails.
98    pub fn flush_full(&self) -> Result<()> {
99        self.inner.flush_full()
100    }
101}