Skip to main content

tsdb_reader/
time_range.rs

1//! Half-open time ranges.
2//!
3//! A [`TimeRange`] is `[min_time, max_time)`, in milliseconds since the Unix
4//! epoch. Block bounds use the same convention (`maxTime` is exclusive), while
5//! chunk metadata and tombstone intervals are closed; [`TimeRange::overlaps_chunk`]
6//! does the translation.
7//!
8//! The range is deliberately a plain value: a query layer computes the range a
9//! PromQL expression needs (including selector lookbacks such as `[5m]`) and
10//! hands it to the reader, which never interprets the query itself.
11
12/// Half-open time range `[min_time, max_time)`.
13///
14/// `max_time` is exclusive, matching Prometheus block bounds. A range with
15/// `max_time <= min_time` selects nothing.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub struct TimeRange {
18    /// Inclusive lower bound.
19    pub min_time: i64,
20    /// Exclusive upper bound.
21    pub max_time: i64,
22}
23
24impl TimeRange {
25    /// Builds a range. An empty range (`max_time <= min_time`) yields nothing.
26    pub const fn new(min_time: i64, max_time: i64) -> Self {
27        Self { min_time, max_time }
28    }
29
30    /// Everything.
31    pub const fn all() -> Self {
32        Self {
33            min_time: i64::MIN,
34            max_time: i64::MAX,
35        }
36    }
37
38    /// True when the range selects nothing.
39    pub const fn is_empty(&self) -> bool {
40        self.max_time <= self.min_time
41    }
42
43    /// True when `ts` is inside the range.
44    pub const fn contains(&self, ts: i64) -> bool {
45        ts >= self.min_time && ts < self.max_time
46    }
47
48    /// True when the closed interval `[min_time, max_time]` of a chunk
49    /// intersects the range.
50    pub const fn overlaps_chunk(&self, chunk_min: i64, chunk_max: i64) -> bool {
51        // An empty range must not select a chunk even when the bounds look like
52        // they straddle it.
53        !self.is_empty() && chunk_max >= self.min_time && chunk_min < self.max_time
54    }
55
56    /// Number of milliseconds covered, saturating at zero for empty ranges.
57    pub const fn duration_ms(&self) -> i64 {
58        let span = self.max_time.saturating_sub(self.min_time);
59        if span < 0 {
60            0
61        } else {
62            span
63        }
64    }
65}
66
67impl Default for TimeRange {
68    fn default() -> Self {
69        Self::all()
70    }
71}
72
73impl From<std::ops::Range<i64>> for TimeRange {
74    fn from(range: std::ops::Range<i64>) -> Self {
75        Self::new(range.start, range.end)
76    }
77}
78
79impl From<TimeRange> for std::ops::Range<i64> {
80    fn from(range: TimeRange) -> Self {
81        range.min_time..range.max_time
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn bounds_are_half_open() {
91        let r = TimeRange::new(100, 200);
92        assert!(r.contains(100));
93        assert!(r.contains(199));
94        assert!(!r.contains(200));
95        assert!(!r.contains(99));
96        assert_eq!(r.duration_ms(), 100);
97    }
98
99    #[test]
100    fn chunk_overlap_uses_closed_chunk_bounds() {
101        let r = TimeRange::new(100, 200);
102        assert!(r.overlaps_chunk(150, 150));
103        assert!(r.overlaps_chunk(50, 100));
104        assert!(r.overlaps_chunk(199, 300));
105        assert!(!r.overlaps_chunk(50, 99));
106        assert!(!r.overlaps_chunk(200, 300));
107    }
108
109    #[test]
110    fn empty_ranges_select_nothing() {
111        let r = TimeRange::new(5, 5);
112        assert!(r.is_empty());
113        assert!(!r.contains(5));
114        assert!(!r.overlaps_chunk(0, 10));
115        assert_eq!(r.duration_ms(), 0);
116        assert!(TimeRange::new(6, 5).is_empty());
117    }
118
119    #[test]
120    fn conversions_keep_the_bounds() {
121        let r = TimeRange::from(10i64..20i64);
122        assert_eq!(r, TimeRange::new(10, 20));
123        let std_range: std::ops::Range<i64> = r.into();
124        assert_eq!(std_range, 10..20);
125        assert!(TimeRange::default().contains(0));
126    }
127}