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