nut/db/
stats.rs

1use crate::tx::TxStats;
2use std::ops::{AddAssign, Sub};
3
4/// Stats represents statistics about the database.
5#[derive(Clone, Debug, Default)]
6pub struct Stats {
7    // Freelist stats
8    /// total number of free pages on the freelist
9    pub free_page_n: usize,
10    /// total number of pending pages on the freelist
11    pub pending_page_n: usize,
12    /// total bytes allocated in free pages
13    pub free_alloc: usize,
14    /// total bytes used by the freelist
15    pub freelist_in_use: usize,
16
17    // Transaction stats
18    /// total number of started read transactions
19    pub tx_n: usize,
20    /// number of currently open read transactions
21    pub open_tx_n: usize,
22
23    /// global, ongoing stats.
24    pub tx_stats: TxStats,
25}
26
27impl Sub for Stats {
28    type Output = Stats;
29
30    fn sub(self, other: Stats) -> Stats {
31        Stats {
32            free_page_n: self.free_page_n,
33            pending_page_n: self.pending_page_n,
34            free_alloc: self.free_alloc,
35            freelist_in_use: self.freelist_in_use,
36            tx_n: self.tx_n - other.tx_n,
37            open_tx_n: self.open_tx_n - other.open_tx_n,
38            tx_stats: self.tx_stats.sub(&other.tx_stats),
39        }
40    }
41}
42
43impl AddAssign for Stats {
44    fn add_assign(&mut self, other: Stats) {
45        self.tx_stats += other.tx_stats;
46    }
47}