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 #[must_use]
55 pub fn with_persist_dir(mut self, dir: impl Into<PathBuf>) -> Self {
56 self.persist_dir = Some(dir.into());
57 self
58 }
59
60 /// Set the backend preference.
61 ///
62 /// Defaults to [`BackendPreference::Cpu`]. Use this when loading a
63 /// persisted database onto a specific backend via [`RingDb::load()`](crate::engine::RingDb::load).
64 ///
65 /// # Example
66 ///
67 /// ```no_run
68 /// use ringdb::RingDbConfig;
69 /// use ringdb::BackendPreference;
70 ///
71 /// let config = RingDbConfig::new(128).with_backend_preference(BackendPreference::Cpu);
72 /// ```
73 #[must_use]
74 pub fn with_backend_preference(mut self, preference: BackendPreference) -> Self {
75 self.backend_preference = preference;
76 self
77 }
78}