reth_db_api/tables/
raw.rs
1use crate::{
2 table::{Compress, Decode, Decompress, DupSort, Encode, Key, Table, Value},
3 DatabaseError,
4};
5use serde::{Deserialize, Serialize};
6
7pub type TableRawRow<T> = (RawKey<<T as Table>::Key>, RawValue<<T as Table>::Value>);
9
10#[derive(Default, Copy, Clone, Debug)]
13pub struct RawTable<T: Table> {
14 phantom: std::marker::PhantomData<T>,
15}
16
17impl<T: Table> Table for RawTable<T> {
18 const NAME: &'static str = T::NAME;
19 const DUPSORT: bool = false;
20
21 type Key = RawKey<T::Key>;
22 type Value = RawValue<T::Value>;
23}
24
25#[derive(Default, Copy, Clone, Debug)]
28pub struct RawDupSort<T: DupSort> {
29 phantom: std::marker::PhantomData<T>,
30}
31
32impl<T: DupSort> Table for RawDupSort<T> {
33 const NAME: &'static str = T::NAME;
34 const DUPSORT: bool = true;
35
36 type Key = RawKey<T::Key>;
37 type Value = RawValue<T::Value>;
38}
39
40impl<T: DupSort> DupSort for RawDupSort<T> {
41 type SubKey = RawKey<T::SubKey>;
42}
43
44#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
46pub struct RawKey<K: Key> {
47 key: Vec<u8>,
49 _phantom: std::marker::PhantomData<K>,
50}
51
52impl<K: Key> RawKey<K> {
53 pub fn new(key: K) -> Self {
55 Self { key: K::encode(key).into(), _phantom: std::marker::PhantomData }
56 }
57
58 pub const fn from_vec(vec: Vec<u8>) -> Self {
61 Self { key: vec, _phantom: std::marker::PhantomData }
62 }
63
64 pub fn key(&self) -> Result<K, DatabaseError> {
66 K::decode(&self.key)
67 }
68
69 pub const fn raw_key(&self) -> &Vec<u8> {
71 &self.key
72 }
73
74 pub fn into_key(self) -> Vec<u8> {
76 self.key
77 }
78}
79
80impl<K: Key> From<K> for RawKey<K> {
81 fn from(key: K) -> Self {
82 Self::new(key)
83 }
84}
85
86impl AsRef<[u8]> for RawKey<Vec<u8>> {
87 fn as_ref(&self) -> &[u8] {
88 &self.key
89 }
90}
91
92impl<K: Key> Encode for RawKey<K> {
94 type Encoded = Vec<u8>;
95
96 fn encode(self) -> Self::Encoded {
97 self.key
98 }
99}
100
101impl<K: Key> Decode for RawKey<K> {
103 fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
104 Ok(Self { key: value.to_vec(), _phantom: std::marker::PhantomData })
105 }
106
107 fn decode_owned(value: Vec<u8>) -> Result<Self, DatabaseError> {
108 Ok(Self { key: value, _phantom: std::marker::PhantomData })
109 }
110}
111
112#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Serialize, Ord, Hash)]
114pub struct RawValue<V: Value> {
115 value: Vec<u8>,
117 #[serde(skip)]
118 _phantom: std::marker::PhantomData<V>,
119}
120
121impl<V: Value> RawValue<V> {
122 pub fn new(value: V) -> Self {
124 Self { value: V::compress(value).into(), _phantom: std::marker::PhantomData }
125 }
126
127 pub const fn from_vec(vec: Vec<u8>) -> Self {
130 Self { value: vec, _phantom: std::marker::PhantomData }
131 }
132
133 pub fn value(&self) -> Result<V, DatabaseError> {
135 V::decompress(&self.value)
136 }
137
138 pub fn raw_value(&self) -> &[u8] {
140 &self.value
141 }
142
143 pub fn into_value(self) -> Vec<u8> {
145 self.value
146 }
147}
148
149impl<V: Value> From<V> for RawValue<V> {
150 fn from(value: V) -> Self {
151 Self::new(value)
152 }
153}
154
155impl AsRef<[u8]> for RawValue<Vec<u8>> {
156 fn as_ref(&self) -> &[u8] {
157 &self.value
158 }
159}
160
161impl<V: Value> Compress for RawValue<V> {
162 type Compressed = Vec<u8>;
163
164 fn uncompressable_ref(&self) -> Option<&[u8]> {
165 Some(&self.value)
167 }
168
169 fn compress(self) -> Self::Compressed {
170 self.value
171 }
172
173 fn compress_to_buf<B: bytes::BufMut + AsMut<[u8]>>(&self, buf: &mut B) {
174 buf.put_slice(self.value.as_slice())
175 }
176}
177
178impl<V: Value> Decompress for RawValue<V> {
179 fn decompress(value: &[u8]) -> Result<Self, DatabaseError> {
180 Ok(Self { value: value.to_vec(), _phantom: std::marker::PhantomData })
181 }
182
183 fn decompress_owned(value: Vec<u8>) -> Result<Self, DatabaseError> {
184 Ok(Self { value, _phantom: std::marker::PhantomData })
185 }
186}