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