typed_store/
traits.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::{borrow::Borrow, error::Error, ops::RangeBounds};
6
7use serde::{Serialize, de::DeserializeOwned};
8
9use crate::TypedStoreError;
10
11pub type DbIterator<'a, T> = Box<dyn Iterator<Item = Result<T, TypedStoreError>> + 'a>;
12
13pub trait Map<'a, K, V>
14where
15    K: Serialize + DeserializeOwned,
16    V: Serialize + DeserializeOwned,
17{
18    type Error: Error;
19
20    /// Returns true if the map contains a value for the specified key.
21    fn contains_key(&self, key: &K) -> Result<bool, Self::Error>;
22
23    /// Returns true if the map contains a value for the specified key.
24    fn multi_contains_keys<J>(
25        &self,
26        keys: impl IntoIterator<Item = J>,
27    ) -> Result<Vec<bool>, Self::Error>
28    where
29        J: Borrow<K>,
30    {
31        keys.into_iter()
32            .map(|key| self.contains_key(key.borrow()))
33            .collect()
34    }
35
36    /// Returns the value for the given key from the map, if it exists.
37    fn get(&self, key: &K) -> Result<Option<V>, Self::Error>;
38
39    /// Inserts the given key-value pair into the map.
40    fn insert(&self, key: &K, value: &V) -> Result<(), Self::Error>;
41
42    /// Removes the entry for the given key from the map.
43    fn remove(&self, key: &K) -> Result<(), Self::Error>;
44
45    /// Uses delete range on the entire key range
46    fn schedule_delete_all(&self) -> Result<(), TypedStoreError>;
47
48    /// Returns true if the map is empty, otherwise false.
49    fn is_empty(&self) -> bool;
50
51    /// Same as `iter` but performs status check.
52    fn safe_iter(&'a self) -> DbIterator<'a, (K, V)>;
53
54    // Same as `iter_with_bounds` but performs status check.
55    fn safe_iter_with_bounds(
56        &'a self,
57        lower_bound: Option<K>,
58        upper_bound: Option<K>,
59    ) -> DbIterator<'a, (K, V)>;
60
61    // Same as `range_iter` but performs status check.
62    fn safe_range_iter(&'a self, range: impl RangeBounds<K>) -> DbIterator<'a, (K, V)>;
63
64    /// Returns a vector of values corresponding to the keys provided,
65    /// non-atomically.
66    fn multi_get<J>(&self, keys: impl IntoIterator<Item = J>) -> Result<Vec<Option<V>>, Self::Error>
67    where
68        J: Borrow<K>,
69    {
70        keys.into_iter().map(|key| self.get(key.borrow())).collect()
71    }
72
73    /// Inserts key-value pairs, non-atomically.
74    fn multi_insert<J, U>(
75        &self,
76        key_val_pairs: impl IntoIterator<Item = (J, U)>,
77    ) -> Result<(), Self::Error>
78    where
79        J: Borrow<K>,
80        U: Borrow<V>,
81    {
82        key_val_pairs
83            .into_iter()
84            .try_for_each(|(key, value)| self.insert(key.borrow(), value.borrow()))
85    }
86
87    /// Removes keys, non-atomically.
88    fn multi_remove<J>(&self, keys: impl IntoIterator<Item = J>) -> Result<(), Self::Error>
89    where
90        J: Borrow<K>,
91    {
92        keys.into_iter()
93            .try_for_each(|key| self.remove(key.borrow()))
94    }
95
96    /// Try to catch up with primary when running as secondary
97    fn try_catch_up_with_primary(&self) -> Result<(), Self::Error>;
98}
99
100pub struct TableSummary {
101    pub num_keys: u64,
102    pub key_bytes_total: usize,
103    pub value_bytes_total: usize,
104    pub key_hist: hdrhistogram::Histogram<u64>,
105    pub value_hist: hdrhistogram::Histogram<u64>,
106}