tsdb-reader 0.3.0

Read-only, zero-copy Prometheus TSDB block reader: index v1-v3, all chunk encodings, integer histograms, postings and label matchers, tombstones
Documentation
//! Half-open time ranges.
//!
//! A [`TimeRange`] is `[min_time, max_time)`, in milliseconds since the Unix
//! epoch. Block bounds use the same convention (`maxTime` is exclusive), while
//! chunk metadata and tombstone intervals are closed; [`TimeRange::overlaps_chunk`]
//! does the translation.
//!
//! The range is deliberately a plain value: a query layer computes the range a
//! PromQL expression needs (including selector lookbacks such as `[5m]`) and
//! hands it to the reader, which never interprets the query itself.

/// Half-open time range `[min_time, max_time)`.
///
/// `max_time` is exclusive, matching Prometheus block bounds. A range with
/// `max_time <= min_time` selects nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TimeRange {
    /// Inclusive lower bound.
    pub min_time: i64,
    /// Exclusive upper bound.
    pub max_time: i64,
}

impl TimeRange {
    /// Builds a range. An empty range (`max_time <= min_time`) yields nothing.
    pub const fn new(min_time: i64, max_time: i64) -> Self {
        Self { min_time, max_time }
    }

    /// Everything.
    pub const fn all() -> Self {
        Self {
            min_time: i64::MIN,
            max_time: i64::MAX,
        }
    }

    /// True when the range selects nothing.
    pub const fn is_empty(&self) -> bool {
        self.max_time <= self.min_time
    }

    /// True when `ts` is inside the range.
    pub const fn contains(&self, ts: i64) -> bool {
        ts >= self.min_time && ts < self.max_time
    }

    /// True when the closed interval `[min_time, max_time]` of a chunk
    /// intersects the range.
    pub const fn overlaps_chunk(&self, chunk_min: i64, chunk_max: i64) -> bool {
        // An empty range must not select a chunk even when the bounds look like
        // they straddle it.
        !self.is_empty() && chunk_max >= self.min_time && chunk_min < self.max_time
    }

    /// Number of milliseconds covered, saturating at zero for empty ranges.
    pub const fn duration_ms(&self) -> i64 {
        let span = self.max_time.saturating_sub(self.min_time);
        if span < 0 {
            0
        } else {
            span
        }
    }
}

impl Default for TimeRange {
    fn default() -> Self {
        Self::all()
    }
}

impl From<std::ops::Range<i64>> for TimeRange {
    fn from(range: std::ops::Range<i64>) -> Self {
        Self::new(range.start, range.end)
    }
}

impl From<TimeRange> for std::ops::Range<i64> {
    fn from(range: TimeRange) -> Self {
        range.min_time..range.max_time
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn bounds_are_half_open() {
        let r = TimeRange::new(100, 200);
        assert!(r.contains(100));
        assert!(r.contains(199));
        assert!(!r.contains(200));
        assert!(!r.contains(99));
        assert_eq!(r.duration_ms(), 100);
    }

    #[test]
    fn chunk_overlap_uses_closed_chunk_bounds() {
        let r = TimeRange::new(100, 200);
        assert!(r.overlaps_chunk(150, 150));
        assert!(r.overlaps_chunk(50, 100));
        assert!(r.overlaps_chunk(199, 300));
        assert!(!r.overlaps_chunk(50, 99));
        assert!(!r.overlaps_chunk(200, 300));
    }

    #[test]
    fn empty_ranges_select_nothing() {
        let r = TimeRange::new(5, 5);
        assert!(r.is_empty());
        assert!(!r.contains(5));
        assert!(!r.overlaps_chunk(0, 10));
        assert_eq!(r.duration_ms(), 0);
        assert!(TimeRange::new(6, 5).is_empty());
    }

    #[test]
    fn conversions_keep_the_bounds() {
        let r = TimeRange::from(10i64..20i64);
        assert_eq!(r, TimeRange::new(10, 20));
        let std_range: std::ops::Range<i64> = r.into();
        assert_eq!(std_range, 10..20);
        assert!(TimeRange::default().contains(0));
    }
}