use crate::DatabaseError; use reth_db_api::table::{Compress, Decode, Decompress, DupSort, Encode, Key, Table, Value}; use serde::{Deserialize, Serialize}; /// Tuple with `RawKey` and `RawValue`. pub type TableRawRow = (RawKey<::Key>, RawValue<::Value>); /// Raw table that can be used to access any table and its data in raw mode. /// This is useful for delayed decoding/encoding of data. #[derive(Default, Copy, Clone, Debug)] pub struct RawTable { phantom: std::marker::PhantomData, } impl Table for RawTable { const NAME: &'static str = T::NAME; type Key = RawKey; type Value = RawValue; } /// Raw `DupSort` table that can be used to access any table and its data in raw mode. /// This is useful for delayed decoding/encoding of data. #[derive(Default, Copy, Clone, Debug)] pub struct RawDupSort { phantom: std::marker::PhantomData, } impl Table for RawDupSort { const NAME: &'static str = T::NAME; type Key = RawKey; type Value = RawValue; } impl DupSort for RawDupSort { type SubKey = RawKey; } /// Raw table key. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] pub struct RawKey { /// Inner encoded key key: Vec, _phantom: std::marker::PhantomData, } impl RawKey { /// Create new raw key. pub fn new(key: K) -> Self { Self { key: K::encode(key).into(), _phantom: std::marker::PhantomData } } /// Creates a raw key from an existing `Vec`. Useful when we already have the encoded /// key. pub const fn from_vec(vec: Vec) -> Self { Self { key: vec, _phantom: std::marker::PhantomData } } /// Returns the decoded value. pub fn key(&self) -> Result { K::decode(&self.key) } /// Returns the raw key as seen on the database. pub const fn raw_key(&self) -> &Vec { &self.key } /// Consumes [`Self`] and returns the inner raw key. pub fn into_key(self) -> Vec { self.key } } impl From for RawKey { fn from(key: K) -> Self { Self::new(key) } } impl AsRef<[u8]> for RawKey> { fn as_ref(&self) -> &[u8] { &self.key } } // Encode impl Encode for RawKey { type Encoded = Vec; fn encode(self) -> Self::Encoded { self.key } } // Decode impl Decode for RawKey { fn decode>(key: B) -> Result { Ok(Self { key: key.as_ref().to_vec(), _phantom: std::marker::PhantomData }) } } /// Raw table value. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Serialize, Ord, Hash)] pub struct RawValue { /// Inner compressed value value: Vec, #[serde(skip)] _phantom: std::marker::PhantomData, } impl RawValue { /// Create new raw value. pub fn new(value: V) -> Self { Self { value: V::compress(value).into(), _phantom: std::marker::PhantomData } } /// Creates a raw value from an existing `Vec`. Useful when we already have the encoded /// value. pub const fn from_vec(vec: Vec) -> Self { Self { value: vec, _phantom: std::marker::PhantomData } } /// Returns the decompressed value. pub fn value(&self) -> Result { V::decompress(&self.value) } /// Returns the raw value as seen on the database. pub fn raw_value(&self) -> &[u8] { &self.value } /// Consumes [`Self`] and returns the inner raw value. pub fn into_value(self) -> Vec { self.value } } impl From for RawValue { fn from(value: V) -> Self { Self::new(value) } } impl AsRef<[u8]> for RawValue> { fn as_ref(&self) -> &[u8] { &self.value } } impl Compress for RawValue { type Compressed = Vec; fn uncompressable_ref(&self) -> Option<&[u8]> { // Already compressed Some(&self.value) } fn compress(self) -> Self::Compressed { self.value } fn compress_to_buf>(self, buf: &mut B) { buf.put_slice(self.value.as_slice()) } } impl Decompress for RawValue { fn decompress>(value: B) -> Result { Ok(Self { value: value.as_ref().to_vec(), _phantom: std::marker::PhantomData }) } fn decompress_owned(value: Vec) -> Result { Ok(Self { value, _phantom: std::marker::PhantomData }) } }