reth_db/tables/
raw.rs

1use crate::DatabaseError;
2use reth_db_api::table::{Compress, Decode, Decompress, DupSort, Encode, Key, Table, Value};
3use serde::{Deserialize, Serialize};
4
5/// Tuple with `RawKey<T::Key>` and `RawValue<T::Value>`.
6pub type TableRawRow<T> = (RawKey<<T as Table>::Key>, RawValue<<T as Table>::Value>);
7
8/// Raw table that can be used to access any table and its data in raw mode.
9/// This is useful for delayed decoding/encoding of data.
10#[derive(Default, Copy, Clone, Debug)]
11pub struct RawTable<T: Table> {
12    phantom: std::marker::PhantomData<T>,
13}
14
15impl<T: Table> Table for RawTable<T> {
16    const NAME: &'static str = T::NAME;
17    const DUPSORT: bool = false;
18
19    type Key = RawKey<T::Key>;
20    type Value = RawValue<T::Value>;
21}
22
23/// Raw `DupSort` table that can be used to access any table and its data in raw mode.
24/// This is useful for delayed decoding/encoding of data.
25#[derive(Default, Copy, Clone, Debug)]
26pub struct RawDupSort<T: DupSort> {
27    phantom: std::marker::PhantomData<T>,
28}
29
30impl<T: DupSort> Table for RawDupSort<T> {
31    const NAME: &'static str = T::NAME;
32    const DUPSORT: bool = true;
33
34    type Key = RawKey<T::Key>;
35    type Value = RawValue<T::Value>;
36}
37
38impl<T: DupSort> DupSort for RawDupSort<T> {
39    type SubKey = RawKey<T::SubKey>;
40}
41
42/// Raw table key.
43#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
44pub struct RawKey<K: Key> {
45    /// Inner encoded key
46    key: Vec<u8>,
47    _phantom: std::marker::PhantomData<K>,
48}
49
50impl<K: Key> RawKey<K> {
51    /// Create new raw key.
52    pub fn new(key: K) -> Self {
53        Self { key: K::encode(key).into(), _phantom: std::marker::PhantomData }
54    }
55
56    /// Creates a raw key from an existing `Vec`. Useful when we already have the encoded
57    /// key.
58    pub const fn from_vec(vec: Vec<u8>) -> Self {
59        Self { key: vec, _phantom: std::marker::PhantomData }
60    }
61
62    /// Returns the decoded value.
63    pub fn key(&self) -> Result<K, DatabaseError> {
64        K::decode(&self.key)
65    }
66
67    /// Returns the raw key as seen on the database.
68    pub const fn raw_key(&self) -> &Vec<u8> {
69        &self.key
70    }
71
72    /// Consumes [`Self`] and returns the inner raw key.
73    pub fn into_key(self) -> Vec<u8> {
74        self.key
75    }
76}
77
78impl<K: Key> From<K> for RawKey<K> {
79    fn from(key: K) -> Self {
80        Self::new(key)
81    }
82}
83
84impl AsRef<[u8]> for RawKey<Vec<u8>> {
85    fn as_ref(&self) -> &[u8] {
86        &self.key
87    }
88}
89
90// Encode
91impl<K: Key> Encode for RawKey<K> {
92    type Encoded = Vec<u8>;
93
94    fn encode(self) -> Self::Encoded {
95        self.key
96    }
97}
98
99// Decode
100impl<K: Key> Decode for RawKey<K> {
101    fn decode(value: &[u8]) -> Result<Self, DatabaseError> {
102        Ok(Self { key: value.to_vec(), _phantom: std::marker::PhantomData })
103    }
104
105    fn decode_owned(value: Vec<u8>) -> Result<Self, DatabaseError> {
106        Ok(Self { key: value, _phantom: std::marker::PhantomData })
107    }
108}
109
110/// Raw table value.
111#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Serialize, Ord, Hash)]
112pub struct RawValue<V: Value> {
113    /// Inner compressed value
114    value: Vec<u8>,
115    #[serde(skip)]
116    _phantom: std::marker::PhantomData<V>,
117}
118
119impl<V: Value> RawValue<V> {
120    /// Create new raw value.
121    pub fn new(value: V) -> Self {
122        Self { value: V::compress(value).into(), _phantom: std::marker::PhantomData }
123    }
124
125    /// Creates a raw value from an existing `Vec`. Useful when we already have the encoded
126    /// value.
127    pub const fn from_vec(vec: Vec<u8>) -> Self {
128        Self { value: vec, _phantom: std::marker::PhantomData }
129    }
130
131    /// Returns the decompressed value.
132    pub fn value(&self) -> Result<V, DatabaseError> {
133        V::decompress(&self.value)
134    }
135
136    /// Returns the raw value as seen on the database.
137    pub fn raw_value(&self) -> &[u8] {
138        &self.value
139    }
140
141    /// Consumes [`Self`] and returns the inner raw value.
142    pub fn into_value(self) -> Vec<u8> {
143        self.value
144    }
145}
146
147impl<V: Value> From<V> for RawValue<V> {
148    fn from(value: V) -> Self {
149        Self::new(value)
150    }
151}
152
153impl AsRef<[u8]> for RawValue<Vec<u8>> {
154    fn as_ref(&self) -> &[u8] {
155        &self.value
156    }
157}
158
159impl<V: Value> Compress for RawValue<V> {
160    type Compressed = Vec<u8>;
161
162    fn uncompressable_ref(&self) -> Option<&[u8]> {
163        // Already compressed
164        Some(&self.value)
165    }
166
167    fn compress(self) -> Self::Compressed {
168        self.value
169    }
170
171    fn compress_to_buf<B: bytes::BufMut + AsMut<[u8]>>(self, buf: &mut B) {
172        buf.put_slice(self.value.as_slice())
173    }
174}
175
176impl<V: Value> Decompress for RawValue<V> {
177    fn decompress(value: &[u8]) -> Result<Self, DatabaseError> {
178        Ok(Self { value: value.to_vec(), _phantom: std::marker::PhantomData })
179    }
180
181    fn decompress_owned(value: Vec<u8>) -> Result<Self, DatabaseError> {
182        Ok(Self { value, _phantom: std::marker::PhantomData })
183    }
184}