Skip to main content

ringdb/
config.rs

1use std::path::PathBuf;
2
3/// Which compute backend to use for ring queries.
4///
5/// Only CPU is supported for now. This enum exists so the config is ready
6/// to be extended with additional backends later.
7#[derive(Debug, Clone, Default, PartialEq, Eq)]
8pub enum BackendPreference {
9    /// CPU brute-force (always available).
10    #[default]
11    Cpu,
12}
13
14/// Configuration for a `RingDb` instance.
15#[derive(Debug, Clone)]
16pub struct RingDbConfig {
17    /// Number of dimensions per vector. Must be > 0.
18    pub dims: usize,
19    /// Backend selection strategy.
20    pub backend_preference: BackendPreference,
21    /// Optional directory for persisting the database.
22    ///
23    /// When set, [`RingDb::build()`](crate::engine::RingDb::build) writes the
24    /// full database (vectors, norms, payloads, offsets, and metadata) to this
25    /// directory. The sealed database can later be reloaded with
26    /// [`RingDb::load()`](crate::engine::RingDb::load).
27    ///
28    /// Leave `None` (the default) for a purely in-memory database.
29    pub persist_dir: Option<PathBuf>,
30}
31
32impl RingDbConfig {
33    /// Create a config with default settings (CPU backend, no persistence).
34    pub fn new(dims: usize) -> Self {
35        Self {
36            dims,
37            backend_preference: BackendPreference::Cpu,
38            persist_dir: None,
39        }
40    }
41
42    /// Set the directory to which the database will be persisted on
43    /// [`build()`](crate::engine::RingDb::build).
44    ///
45    /// The directory is created automatically if it does not exist.
46    ///
47    /// # Example
48    ///
49    /// ```no_run
50    /// use ringdb::RingDbConfig;
51    ///
52    /// let config = RingDbConfig::new(128).with_persist_dir("/var/data/mydb");
53    /// ```
54    pub fn with_persist_dir(mut self, dir: impl Into<PathBuf>) -> Self {
55        self.persist_dir = Some(dir.into());
56        self
57    }
58
59    /// Set the backend preference.
60    ///
61    /// Defaults to [`BackendPreference::Cpu`]. Use this when loading a
62    /// persisted database onto a specific backend via [`RingDb::load()`](crate::engine::RingDb::load).
63    ///
64    /// # Example
65    ///
66    /// ```no_run
67    /// use ringdb::RingDbConfig;
68    /// use ringdb::BackendPreference;
69    ///
70    /// let config = RingDbConfig::new(128).with_backend_preference(BackendPreference::Cpu);
71    /// ```
72    pub fn with_backend_preference(mut self, preference: BackendPreference) -> Self {
73        self.backend_preference = preference;
74        self
75    }
76}