Skip to main content

coven_database/
live_query_runs.rs

1//! How often a live query has run, counted for tests and benchmarks (ยง4).
2
3use crate::LiveQueryCause;
4
5/// How often a live query had run when asked, for tests and benchmarks
6/// that measure how many of its runs writes caused, and how many found
7/// nothing new.
8#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
9pub struct LiveQueryRuns {
10    runs: u64,
11    writes: u64,
12    equal: u64,
13}
14
15impl LiveQueryRuns {
16    /// The runs that have finished.
17    pub fn runs(&self) -> u64 {
18        self.runs
19    }
20
21    /// Of those, the runs a write caused, with no new request.
22    pub fn writes(&self) -> u64 {
23        self.writes
24    }
25
26    /// Of those, the runs whose result equaled the one before, which the
27    /// query didn't return.
28    pub fn equal(&self) -> u64 {
29        self.equal
30    }
31
32    /// Count a finished run, caused by `cause`, whose result was `equal`
33    /// to the one before.
34    pub(crate) fn count(&mut self, cause: LiveQueryCause, equal: bool) {
35        self.runs += 1;
36        if cause == LiveQueryCause::Write {
37            self.writes += 1;
38            if equal {
39                self.equal += 1;
40            }
41        }
42    }
43}
44
45#[cfg(test)]
46#[path = "live_query_runs_tests.rs"]
47mod tests;