Skip to main content

tycho_storage/kv/
context.rs

1use std::sync::{Arc, Mutex};
2
3use bytesize::ByteSize;
4
5// TODO: Add potato mode.
6#[derive(Clone)]
7pub struct TableContext {
8    caches: weedb::Caches,
9    usage: Arc<Mutex<BufferUsage>>,
10}
11
12impl TableContext {
13    pub const DEFAULT_LRU_CAPACITY: ByteSize = ByteSize::gib(1);
14
15    pub fn caches(&self) -> &weedb::Caches {
16        &self.caches
17    }
18
19    pub fn buffer_usage(&self) -> BufferUsage {
20        *self.usage.lock().unwrap()
21    }
22
23    pub fn track_buffer_usage(&self, min: ByteSize, max: ByteSize) {
24        assert!(min <= max);
25
26        let mut usage = self.usage.lock().unwrap();
27        usage.min_bytes = ByteSize(usage.min_bytes.0.saturating_add(min.0));
28        usage.max_bytes = ByteSize(usage.max_bytes.0.saturating_add(max.0));
29    }
30}
31
32impl AsRef<weedb::Caches> for TableContext {
33    #[inline]
34    fn as_ref(&self) -> &weedb::Caches {
35        &self.caches
36    }
37}
38
39impl Default for TableContext {
40    fn default() -> Self {
41        Self {
42            caches: weedb::Caches::with_capacity(Self::DEFAULT_LRU_CAPACITY.0 as usize),
43            usage: Default::default(),
44        }
45    }
46}
47
48#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
49pub struct BufferUsage {
50    pub min_bytes: ByteSize,
51    pub max_bytes: ByteSize,
52}