typed_store/
traits.rs

1// Copyright (c) 2022, Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3use serde::{de::DeserializeOwned, Serialize};
4use std::{borrow::Borrow, collections::BTreeMap, error::Error};
5
6pub trait Map<'a, K, V>
7where
8    K: Serialize + DeserializeOwned + ?Sized,
9    V: Serialize + DeserializeOwned,
10{
11    type Error: Error;
12    type Iterator: Iterator<Item = (K, V)>;
13    type Keys: Iterator<Item = K>;
14    type Values: Iterator<Item = V>;
15
16    /// Returns true if the map contains a value for the specified key.
17    fn contains_key(&self, key: &K) -> Result<bool, Self::Error>;
18
19    /// Returns the value for the given key from the map, if it exists.
20    fn get(&self, key: &K) -> Result<Option<V>, Self::Error>;
21
22    /// Returns the raw value (bincode serialized bytes) for the given key from the map, if it exists.
23    fn get_raw_bytes(&self, key: &K) -> Result<Option<Vec<u8>>, Self::Error>;
24
25    /// Returns the value for the given key from the map, if it exists
26    /// or the given default value if it does not.
27    /// This method is not thread safe
28    fn get_or_insert_unsafe<F: FnOnce() -> V>(
29        &self,
30        key: &K,
31        default: F,
32    ) -> Result<V, Self::Error> {
33        self.get(key).and_then(|optv| match optv {
34            Some(v) => Ok(v),
35            None => {
36                self.insert(key, &default())?;
37                self.get(key).transpose().expect("default just inserted")
38            }
39        })
40    }
41
42    /// Inserts the given key-value pair into the map.
43    fn insert(&self, key: &K, value: &V) -> Result<(), Self::Error>;
44
45    /// Removes the entry for the given key from the map.
46    fn remove(&self, key: &K) -> Result<(), Self::Error>;
47
48    /// Removes every key-value pair from the map.
49    fn clear(&self) -> Result<(), Self::Error>;
50
51    /// Returns true if the map is empty, otherwise false.
52    fn is_empty(&self) -> bool;
53
54    /// Returns an iterator visiting each key-value pair in the map.
55    fn iter(&'a self) -> Self::Iterator;
56
57    /// Returns an iterator over each key in the map.
58    fn keys(&'a self) -> Self::Keys;
59
60    /// Returns an iterator over each value in the map.
61    fn values(&'a self) -> Self::Values;
62
63    /// Returns a vector of values corresponding to the keys provided, non-atomically.
64    fn multi_get<J>(&self, keys: impl IntoIterator<Item = J>) -> Result<Vec<Option<V>>, Self::Error>
65    where
66        J: Borrow<K>,
67    {
68        keys.into_iter().map(|key| self.get(key.borrow())).collect()
69    }
70
71    /// Inserts key-value pairs, non-atomically.
72    fn multi_insert<J, U>(
73        &self,
74        key_val_pairs: impl IntoIterator<Item = (J, U)>,
75    ) -> Result<(), Self::Error>
76    where
77        J: Borrow<K>,
78        U: Borrow<V>,
79    {
80        key_val_pairs
81            .into_iter()
82            .try_for_each(|(key, value)| self.insert(key.borrow(), value.borrow()))
83    }
84
85    /// Removes keys, non-atomically.
86    fn multi_remove<J>(&self, keys: impl IntoIterator<Item = J>) -> Result<(), Self::Error>
87    where
88        J: Borrow<K>,
89    {
90        keys.into_iter()
91            .try_for_each(|key| self.remove(key.borrow()))
92    }
93
94    /// Try to catch up with primary when running as secondary
95    fn try_catch_up_with_primary(&self) -> Result<(), Self::Error>;
96}
97
98pub trait TypedStoreDebug {
99    /// Dump a DB table with pagination
100    fn dump_table(
101        &self,
102        table_name: String,
103        page_size: u16,
104        page_number: usize,
105    ) -> eyre::Result<BTreeMap<String, String>>;
106
107    /// Get the name of the DB. This is simply the name of the struct
108    fn primary_db_name(&self) -> String;
109
110    /// Get a map of table names to key-value types
111    fn describe_all_tables(&self) -> BTreeMap<String, (String, String)>;
112
113    /// Count the entries in the table
114    fn count_table_keys(&self, table_name: String) -> eyre::Result<usize>;
115}