dbstruct/traits/data_store.rs
1#![allow(clippy::type_complexity)]
2//! The traits used by the wrapper to operate on the database.
3use core::fmt;
4use std::ops::RangeBounds;
5
6use serde::de::DeserializeOwned;
7use serde::Serialize;
8
9/// Base trait needed by every wrapper. It is usually more convenient to implement
10/// [`ByteStore`][super::byte_store::ByteStore] instead.
11pub trait DataStore {
12 type DbError: fmt::Debug;
13 fn get<K, V>(&self, key: &K) -> Result<Option<V>, crate::Error<Self::DbError>>
14 where
15 K: Serialize,
16 V: DeserializeOwned;
17 fn remove<K, V>(&self, key: &K) -> Result<Option<V>, crate::Error<Self::DbError>>
18 where
19 K: Serialize,
20 V: DeserializeOwned;
21 fn insert<'a, K, V, OwnedV>(
22 &self,
23 key: &'a K,
24 val: &'a V,
25 ) -> Result<Option<OwnedV>, crate::Error<Self::DbError>>
26 where
27 K: Serialize,
28 V: Serialize + ?Sized,
29 OwnedV: std::borrow::Borrow<V> + DeserializeOwned;
30}
31
32/// This trait enables wrapper to provide `update` and `conditional` update.
33/// It is usually more convenient to implement
34/// [`byte_store::Atomic`][super::byte_store::Atomic] instead.
35pub trait Atomic: DataStore {
36 fn atomic_update<K, V>(
37 &self,
38 key: &K,
39 op: impl FnMut(V) -> V + Clone,
40 ) -> Result<(), crate::Error<Self::DbError>>
41 where
42 K: Serialize,
43 V: Serialize + DeserializeOwned;
44 /// On error the update is aborted
45 fn conditional_update<K, V>(
46 &self,
47 key: &K,
48 new: &V,
49 expected: &V,
50 ) -> Result<(), crate::Error<Self::DbError>>
51 where
52 K: Serialize + ?Sized,
53 V: Serialize + ?Sized;
54}
55
56/// This trait needed for the Vec wrapper. It is usually more convenient to implement
57/// [`byte_store::Ordered`][super::byte_store::Ordered] instead.
58///
59/// You can deserialize to a different key than you serialize too.
60/// This is useful when using `get_lt` an `InKey` that borrows data. As
61/// you need to deserialize to a type owning all its data.
62pub trait Ordered: DataStore {
63 /// Retrieve the next key and value from the Tree after the provided key.
64 ///
65 /// ### Note
66 /// The order follows the `Ord` implementation for `Vec<u8>`:
67 /// [] < [0] < [255] < [255, 0] < [255, 255] ...
68 /// To retain the ordering of numerical types use big endian representation
69 fn get_lt<InKey, OutKey, Value>(
70 &self,
71 key: &InKey,
72 ) -> Result<Option<(OutKey, Value)>, crate::Error<Self::DbError>>
73 where
74 InKey: Serialize,
75 OutKey: Serialize + DeserializeOwned,
76 Value: Serialize + DeserializeOwned;
77
78 /// Retrieve the previous key and value from the Tree before the provided key.
79 ///
80 /// ### Note
81 /// The order follows the `Ord` implementation for `Vec<u8>`:
82 /// [] < [0] < [255] < [255, 0] < [255, 255] ...
83 /// To retain the ordering of numerical types use big endian representation
84 fn get_gt<InKey, OutKey, Value>(
85 &self,
86 key: &InKey,
87 ) -> Result<Option<(OutKey, Value)>, crate::Error<Self::DbError>>
88 where
89 InKey: Serialize,
90 OutKey: Serialize + DeserializeOwned,
91 Value: Serialize + DeserializeOwned;
92}
93
94/// This trait expand the functionality of the Map wrapper. It is usually more
95/// convenient to implement [`byte_store::Ranged`][super::byte_store::Ranged]
96/// instead.
97///
98/// You can deserialize to a different key than you serialize too. This is
99/// useful when using range an `InKey` that borrows data. As you need to
100/// deserialize to a type owning all its data.
101pub trait Ranged: DataStore {
102 fn range<InKey, OutKey, Value>(
103 &self,
104 range: impl RangeBounds<InKey>,
105 ) -> Result<
106 impl Iterator<Item = Result<(OutKey, Value), crate::Error<Self::DbError>>>,
107 crate::Error<Self::DbError>,
108 >
109 where
110 InKey: Serialize,
111 OutKey: Serialize + DeserializeOwned,
112 Value: Serialize + DeserializeOwned;
113}