Skip to main content

reth_db_api/tables/
raw.rs

1use crate::{
2    table::{Compress, Decode, Decompress, DupSort, Encode, IntoVec, Key, Table, Value},
3    DatabaseError,
4};
5use reth_codecs::DecompressError;
6use serde::{Deserialize, Serialize};
7
8/// Tuple with `RawKey<T::Key>` and `RawValue<T::Value>`.
9pub type TableRawRow<T> = (RawKey<<T as Table>::Key>, RawValue<<T as Table>::Value>);
10
11/// Raw table that can be used to access any table and its data in raw mode.
12/// This is useful for delayed decoding/encoding of data.
13#[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/// Raw `DupSort` table that can be used to access any table and its data in raw mode.
27/// This is useful for delayed decoding/encoding of data.
28#[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/// Raw table key.
46#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
47pub struct RawKey<K: Key> {
48    /// Inner encoded key
49    key: Vec<u8>,
50    _phantom: std::marker::PhantomData<K>,
51}
52
53impl<K: Key> RawKey<K> {
54    /// Create new raw key.
55    pub fn new(key: K) -> Self {
56        Self { key: K::encode(key).into_vec(), _phantom: std::marker::PhantomData }
57    }
58
59    /// Creates a raw key from an existing `Vec`. Useful when we already have the encoded
60    /// key.
61    pub const fn from_vec(vec: Vec<u8>) -> Self {
62        Self { key: vec, _phantom: std::marker::PhantomData }
63    }
64
65    /// Returns the decoded value.
66    pub fn key(&self) -> Result<K, DatabaseError> {
67        K::decode(&self.key)
68    }
69
70    /// Returns the raw key as seen on the database.
71    pub const fn raw_key(&self) -> &Vec<u8> {
72        &self.key
73    }
74
75    /// Consumes [`Self`] and returns the inner raw key.
76    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
93// Encode
94impl<K: Key> Encode for RawKey<K> {
95    type Encoded = Vec<u8>;
96
97    fn encode(self) -> Self::Encoded {
98        self.key
99    }
100}
101
102// Decode
103impl<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/// Raw table value.
114#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Serialize, Ord, Hash)]
115pub struct RawValue<V: Value> {
116    /// Inner compressed value
117    value: Vec<u8>,
118    #[serde(skip)]
119    _phantom: std::marker::PhantomData<V>,
120}
121
122impl<V: Value> RawValue<V> {
123    /// Create new raw value.
124    pub fn new(value: V) -> Self {
125        Self { value: V::compress(value).into(), _phantom: std::marker::PhantomData }
126    }
127
128    /// Creates a raw value from an existing `Vec`. Useful when we already have the encoded
129    /// value.
130    pub const fn from_vec(vec: Vec<u8>) -> Self {
131        Self { value: vec, _phantom: std::marker::PhantomData }
132    }
133
134    /// Returns the decompressed value.
135    pub fn value(&self) -> Result<V, DatabaseError> {
136        Ok(V::decompress(&self.value)?)
137    }
138
139    /// Returns the raw value as seen on the database.
140    pub fn raw_value(&self) -> &[u8] {
141        &self.value
142    }
143
144    /// Consumes [`Self`] and returns the inner raw value.
145    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        // Already compressed
167        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}