typed_store/
traits.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3use 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    /// Returns true if the map contains a value for the specified key.
18    fn contains_key(&self, key: &K) -> Result<bool, Self::Error>;
19
20    /// Returns true if the map contains a value for the specified key.
21    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    /// Returns the value for the given key from the map, if it exists.
34    fn get(&self, key: &K) -> Result<Option<V>, Self::Error>;
35
36    /// Inserts the given key-value pair into the map.
37    fn insert(&self, key: &K, value: &V) -> Result<(), Self::Error>;
38
39    /// Removes the entry for the given key from the map.
40    fn remove(&self, key: &K) -> Result<(), Self::Error>;
41
42    /// Uses delete range on the entire key range
43    fn schedule_delete_all(&self) -> Result<(), TypedStoreError>;
44
45    /// Returns true if the map is empty, otherwise false.
46    fn is_empty(&self) -> bool;
47
48    /// Same as `iter` but performs status check.
49    fn safe_iter(&'a self) -> DbIterator<'a, (K, V)>;
50
51    // Same as `iter_with_bounds` but performs status check.
52    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    // Same as `range_iter` but performs status check.
59    fn safe_range_iter(&'a self, range: impl RangeBounds<K>) -> DbIterator<'a, (K, V)>;
60
61    /// Returns a vector of values corresponding to the keys provided, non-atomically.
62    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    /// Inserts key-value pairs, non-atomically.
70    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    /// Removes keys, non-atomically.
84    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    /// Try to catch up with primary when running as secondary
93    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}