reth_db_api/tables/
raw.rs1use crate::{
2 table::{Compress, Decode, Decompress, DupSort, Encode, IntoVec, Key, Table, Value},
3 DatabaseError,
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
22 type Key = RawKey<T::Key>;
23 type Value = RawValue<T::Value>;
24}
25
26#[derive(Default, Copy, Clone, Debug)]
29pub struct RawDupSort<T: DupSort> {
30 phantom: std::marker::PhantomData<T>,
31}
32
33impl<T: DupSort> Table for RawDupSort<T> {
34 const NAME: &'static str = T::NAME;
35 const DUPSORT: bool = true;
36
37 type Key = RawKey<T::Key>;
38 type Value = RawValue<T::Value>;
39}
40
41impl<T: DupSort> DupSort for RawDupSort<T> {
42 type SubKey = RawKey<T::SubKey>;
43}
44
45#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
47pub struct RawKey<K: Key> {
48 key: Vec<u8>,
50 _phantom: std::marker::PhantomData<K>,
51}
52
53impl<K: Key> RawKey<K> {
54 pub fn new(key: K) -> Self {
56 Self { key: K::encode(key).into_vec(), _phantom: std::marker::PhantomData }
57 }
58
59 pub const fn from_vec(vec: Vec<u8>) -> Self {
62 Self { key: vec, _phantom: std::marker::PhantomData }
63 }
64
65 pub fn key(&self) -> Result<K, DatabaseError> {
67 K::decode(&self.key)
68 }
69
70 pub const fn raw_key(&self) -> &Vec<u8> {
72 &self.key
73 }
74
75 pub fn into_key(self) -> Vec<u8> {
77 self.key
78 }
79}
80
81impl<K: Key> From<K> for RawKey<K> {
82 fn from(key: K) -> Self {
83 Self::new(key)
84 }
85}
86
87impl AsRef<[u8]> for RawKey<Vec<u8>> {
88 fn as_ref(&self) -> &[u8] {
89 &self.key
90 }
91}
92
93impl<K: Key> Encode for RawKey<K> {
95 type Encoded = Vec<u8>;
96
97 fn encode(self) -> Self::Encoded {
98 self.key
99 }
100}
101
102impl<K: Key> Decode for RawKey<K> {
104 fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
105 Ok(Self { key: value.to_vec(), _phantom: std::marker::PhantomData })
106 }
107
108 fn decode_owned(value: Vec<u8>) -> Result<Self, DatabaseError> {
109 Ok(Self { key: value, _phantom: std::marker::PhantomData })
110 }
111}
112
113#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Serialize, Ord, Hash)]
115pub struct RawValue<V: Value> {
116 value: Vec<u8>,
118 #[serde(skip)]
119 _phantom: std::marker::PhantomData<V>,
120}
121
122impl<V: Value> RawValue<V> {
123 pub fn new(value: V) -> Self {
125 Self { value: V::compress(value).into(), _phantom: std::marker::PhantomData }
126 }
127
128 pub const fn from_vec(vec: Vec<u8>) -> Self {
131 Self { value: vec, _phantom: std::marker::PhantomData }
132 }
133
134 pub fn value(&self) -> Result<V, DatabaseError> {
136 Ok(V::decompress(&self.value)?)
137 }
138
139 pub fn raw_value(&self) -> &[u8] {
141 &self.value
142 }
143
144 pub fn into_value(self) -> Vec<u8> {
146 self.value
147 }
148}
149
150impl<V: Value> From<V> for RawValue<V> {
151 fn from(value: V) -> Self {
152 Self::new(value)
153 }
154}
155
156impl AsRef<[u8]> for RawValue<Vec<u8>> {
157 fn as_ref(&self) -> &[u8] {
158 &self.value
159 }
160}
161
162impl<V: Value> Compress for RawValue<V> {
163 type Compressed = Vec<u8>;
164
165 fn uncompressable_ref(&self) -> Option<&[u8]> {
166 Some(&self.value)
168 }
169
170 fn compress(self) -> Self::Compressed {
171 self.value
172 }
173
174 fn compress_to_buf<B: bytes::BufMut + AsMut<[u8]>>(&self, buf: &mut B) {
175 buf.put_slice(self.value.as_slice())
176 }
177}
178
179impl<V: Value> Decompress for RawValue<V> {
180 fn decompress(value: &[u8]) -> Result<Self, DecompressError> {
181 Ok(Self { value: value.to_vec(), _phantom: std::marker::PhantomData })
182 }
183
184 fn decompress_owned(value: Vec<u8>) -> Result<Self, DecompressError> {
185 Ok(Self { value, _phantom: std::marker::PhantomData })
186 }
187}