Skip to main content

cbtop/incremental_snapshot/
query.rs

1//! Query filtering and configuration for snapshot storage.
2
3use std::time::Duration;
4
5use super::snapshot::SnapshotIndex;
6
7/// Query filter for snapshots
8#[derive(Debug, Clone, Default)]
9pub struct SnapshotQuery {
10    /// Start timestamp (inclusive)
11    pub start_ns: Option<u64>,
12    /// End timestamp (inclusive)
13    pub end_ns: Option<u64>,
14    /// Workload fingerprint filter
15    pub fingerprint: Option<String>,
16    /// Metric name filter
17    pub metric_name: Option<String>,
18    /// Maximum results
19    pub limit: Option<usize>,
20}
21
22impl SnapshotQuery {
23    /// Create new query
24    pub fn new() -> Self {
25        Self::default()
26    }
27
28    /// Filter by time range
29    pub fn time_range(mut self, start_ns: u64, end_ns: u64) -> Self {
30        self.start_ns = Some(start_ns);
31        self.end_ns = Some(end_ns);
32        self
33    }
34
35    /// Filter by fingerprint
36    pub fn fingerprint(mut self, fp: impl Into<String>) -> Self {
37        self.fingerprint = Some(fp.into());
38        self
39    }
40
41    /// Filter by metric name
42    pub fn metric(mut self, name: impl Into<String>) -> Self {
43        self.metric_name = Some(name.into());
44        self
45    }
46
47    /// Limit results
48    pub fn limit(mut self, n: usize) -> Self {
49        self.limit = Some(n);
50        self
51    }
52
53    /// Check if index matches query
54    pub fn matches(&self, idx: &SnapshotIndex) -> bool {
55        if let Some(start) = self.start_ns {
56            if idx.timestamp_ns < start {
57                return false;
58            }
59        }
60        if let Some(end) = self.end_ns {
61            if idx.timestamp_ns > end {
62                return false;
63            }
64        }
65        if let Some(ref fp) = self.fingerprint {
66            if &idx.fingerprint != fp {
67                return false;
68            }
69        }
70        true
71    }
72}
73
74/// Configuration for snapshot storage
75#[derive(Debug, Clone)]
76pub struct SnapshotConfig {
77    /// Maximum memory for query operations
78    pub max_query_memory_bytes: usize,
79    /// Keyframe interval (full snapshot every N deltas)
80    pub keyframe_interval: usize,
81    /// Enable checksum verification
82    pub verify_checksums: bool,
83    /// Raw tier max age
84    pub raw_max_age: Duration,
85    /// Compressed tier max age
86    pub compressed_max_age: Duration,
87}
88
89impl Default for SnapshotConfig {
90    fn default() -> Self {
91        Self {
92            max_query_memory_bytes: 50 * 1024 * 1024, // 50MB
93            keyframe_interval: 10,
94            verify_checksums: true,
95            raw_max_age: Duration::from_secs(24 * 60 * 60),
96            compressed_max_age: Duration::from_secs(30 * 24 * 60 * 60),
97        }
98    }
99}