Skip to main content

qualia_core_db/q42/volume/
query_mode.rs

1//! Choose resident vs range query from file size. Small graphs stay in RAM;
2//! everything above the cap must use the range/BIDX path.
3
4/// 4 MiB decoded-file threshold. Above this, callers must not `read_all_quins`.
5pub const RESIDENT_QUERY_MAX_BYTES: u64 = 4 * 1024 * 1024;
6
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub enum Q42QueryMode {
9    Resident,
10    Range,
11}
12
13impl Q42QueryMode {
14    /// `for-file` size in bytes.
15    pub fn for_file_bytes(file_bytes: u64) -> Self {
16        if file_bytes <= RESIDENT_QUERY_MAX_BYTES {
17            Self::Resident
18        } else {
19            Self::Range
20        }
21    }
22
23    /// `true` when the mode permits reading all quins.
24    pub fn allows_read_all_quins(self) -> bool {
25        matches!(self, Self::Resident)
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn small_files_may_reside_large_files_must_range() {
35        assert_eq!(Q42QueryMode::for_file_bytes(1024), Q42QueryMode::Resident);
36        assert_eq!(
37            Q42QueryMode::for_file_bytes(RESIDENT_QUERY_MAX_BYTES + 1),
38            Q42QueryMode::Range
39        );
40        assert!(!Q42QueryMode::Range.allows_read_all_quins());
41    }
42}