1use crate::TypedStoreError;
4use serde::{Serialize, de::DeserializeOwned};
5use std::ops::RangeBounds;
6use std::{borrow::Borrow, error::Error};
7
8pub type DbIterator<'a, T> = Box<dyn Iterator<Item = Result<T, TypedStoreError>> + 'a>;
9
10pub trait Map<'a, K, V>
11where
12 K: Serialize + DeserializeOwned,
13 V: Serialize + DeserializeOwned,
14{
15 type Error: Error;
16
17 fn contains_key(&self, key: &K) -> Result<bool, Self::Error>;
19
20 fn multi_contains_keys<J>(
22 &self,
23 keys: impl IntoIterator<Item = J>,
24 ) -> Result<Vec<bool>, Self::Error>
25 where
26 J: Borrow<K>,
27 {
28 keys.into_iter()
29 .map(|key| self.contains_key(key.borrow()))
30 .collect()
31 }
32
33 fn get(&self, key: &K) -> Result<Option<V>, Self::Error>;
35
36 fn insert(&self, key: &K, value: &V) -> Result<(), Self::Error>;
38
39 fn remove(&self, key: &K) -> Result<(), Self::Error>;
41
42 fn schedule_delete_all(&self) -> Result<(), TypedStoreError>;
44
45 fn is_empty(&self) -> bool;
47
48 fn safe_iter(&'a self) -> DbIterator<'a, (K, V)>;
50
51 fn safe_iter_with_bounds(
53 &'a self,
54 lower_bound: Option<K>,
55 upper_bound: Option<K>,
56 ) -> DbIterator<'a, (K, V)>;
57
58 fn safe_range_iter(&'a self, range: impl RangeBounds<K>) -> DbIterator<'a, (K, V)>;
60
61 fn multi_get<J>(&self, keys: impl IntoIterator<Item = J>) -> Result<Vec<Option<V>>, Self::Error>
63 where
64 J: Borrow<K>,
65 {
66 keys.into_iter().map(|key| self.get(key.borrow())).collect()
67 }
68
69 fn multi_insert<J, U>(
71 &self,
72 key_val_pairs: impl IntoIterator<Item = (J, U)>,
73 ) -> Result<(), Self::Error>
74 where
75 J: Borrow<K>,
76 U: Borrow<V>,
77 {
78 key_val_pairs
79 .into_iter()
80 .try_for_each(|(key, value)| self.insert(key.borrow(), value.borrow()))
81 }
82
83 fn multi_remove<J>(&self, keys: impl IntoIterator<Item = J>) -> Result<(), Self::Error>
85 where
86 J: Borrow<K>,
87 {
88 keys.into_iter()
89 .try_for_each(|key| self.remove(key.borrow()))
90 }
91
92 fn try_catch_up_with_primary(&self) -> Result<(), Self::Error>;
94}
95
96pub struct TableSummary {
97 pub num_keys: u64,
98 pub key_bytes_total: usize,
99 pub value_bytes_total: usize,
100 pub key_hist: hdrhistogram::Histogram<u64>,
101 pub value_hist: hdrhistogram::Histogram<u64>,
102}