chore: reduce size of common types (#5304)

This commit is contained in:
DaniPopes
2023-11-06 13:45:20 +01:00
committed by GitHub
parent 2d315c2f3a
commit f8ceda9ea8
55 changed files with 580 additions and 532 deletions

View File

@ -45,7 +45,7 @@ pub trait Decompress: Send + Sync + Sized + Debug {
/// Trait that will transform the data to be saved in the DB.
pub trait Encode: Send + Sync + Sized + Debug {
/// Encoded type.
type Encoded: AsRef<[u8]> + Send + Sync;
type Encoded: AsRef<[u8]> + Into<Vec<u8>> + Send + Sync;
/// Encodes data going into the database.
fn encode(self) -> Self::Encoded;

View File

@ -1,6 +1,6 @@
//! Cursor wrapper for libmdbx-sys.
use reth_interfaces::db::DatabaseWriteOperation;
use reth_interfaces::db::{DatabaseWriteError, DatabaseWriteOperation};
use std::{borrow::Cow, collections::Bound, marker::PhantomData, ops::RangeBounds};
use crate::{
@ -262,11 +262,14 @@ impl<T: Table> DbCursorRW<T> for Cursor<'_, RW, T> {
|this| {
this.inner
.put(key.as_ref(), value.unwrap_or(&this.buf), WriteFlags::UPSERT)
.map_err(|e| DatabaseError::Write {
code: e.into(),
operation: DatabaseWriteOperation::CursorUpsert,
table_name: T::NAME,
key: Box::from(key.as_ref()),
.map_err(|e| {
DatabaseWriteError {
code: e.into(),
operation: DatabaseWriteOperation::CursorUpsert,
table_name: T::NAME,
key: key.into(),
}
.into()
})
},
)
@ -281,11 +284,14 @@ impl<T: Table> DbCursorRW<T> for Cursor<'_, RW, T> {
|this| {
this.inner
.put(key.as_ref(), value.unwrap_or(&this.buf), WriteFlags::NO_OVERWRITE)
.map_err(|e| DatabaseError::Write {
code: e.into(),
operation: DatabaseWriteOperation::CursorInsert,
table_name: T::NAME,
key: Box::from(key.as_ref()),
.map_err(|e| {
DatabaseWriteError {
code: e.into(),
operation: DatabaseWriteOperation::CursorInsert,
table_name: T::NAME,
key: key.into(),
}
.into()
})
},
)
@ -302,11 +308,14 @@ impl<T: Table> DbCursorRW<T> for Cursor<'_, RW, T> {
|this| {
this.inner
.put(key.as_ref(), value.unwrap_or(&this.buf), WriteFlags::APPEND)
.map_err(|e| DatabaseError::Write {
code: e.into(),
operation: DatabaseWriteOperation::CursorAppend,
table_name: T::NAME,
key: Box::from(key.as_ref()),
.map_err(|e| {
DatabaseWriteError {
code: e.into(),
operation: DatabaseWriteOperation::CursorAppend,
table_name: T::NAME,
key: key.into(),
}
.into()
})
},
)
@ -335,11 +344,14 @@ impl<T: DupSort> DbDupCursorRW<T> for Cursor<'_, RW, T> {
|this| {
this.inner
.put(key.as_ref(), value.unwrap_or(&this.buf), WriteFlags::APPEND_DUP)
.map_err(|e| DatabaseError::Write {
code: e.into(),
operation: DatabaseWriteOperation::CursorAppendDup,
table_name: T::NAME,
key: Box::from(key.as_ref()),
.map_err(|e| {
DatabaseWriteError {
code: e.into(),
operation: DatabaseWriteOperation::CursorAppendDup,
table_name: T::NAME,
key: key.into(),
}
.into()
})
},
)

View File

@ -177,9 +177,9 @@ mod tests {
tables::{AccountHistory, CanonicalHeaders, Headers, PlainAccountState, PlainStorageState},
test_utils::*,
transaction::{DbTx, DbTxMut},
AccountChangeSet, DatabaseError,
AccountChangeSet,
};
use reth_interfaces::db::DatabaseWriteOperation;
use reth_interfaces::db::{DatabaseWriteError, DatabaseWriteOperation};
use reth_libmdbx::{NoWriteMap, WriteMap};
use reth_primitives::{Account, Address, Header, IntegerList, StorageEntry, B256, U256};
use std::{path::Path, str::FromStr, sync::Arc};
@ -541,12 +541,13 @@ mod tests {
// INSERT (failure)
assert_eq!(
cursor.insert(key_to_insert, B256::ZERO),
Err(DatabaseError::Write {
Err(DatabaseWriteError {
code: -30799,
operation: DatabaseWriteOperation::CursorInsert,
table_name: CanonicalHeaders::NAME,
key: Box::from(key_to_insert.encode().as_ref())
})
key: key_to_insert.encode().into(),
}
.into())
);
assert_eq!(cursor.current(), Ok(Some((key_to_insert, B256::ZERO))));
@ -684,12 +685,13 @@ mod tests {
let mut cursor = tx.cursor_write::<CanonicalHeaders>().unwrap();
assert_eq!(
cursor.append(key_to_append, B256::ZERO),
Err(DatabaseError::Write {
Err(DatabaseWriteError {
code: -30418,
operation: DatabaseWriteOperation::CursorAppend,
table_name: CanonicalHeaders::NAME,
key: Box::from(key_to_append.encode().as_ref())
})
key: key_to_append.encode().into(),
}
.into())
);
assert_eq!(cursor.current(), Ok(Some((5, B256::ZERO)))); // the end of table
tx.commit().expect(ERROR_COMMIT);
@ -765,24 +767,26 @@ mod tests {
transition_id,
AccountBeforeTx { address: Address::with_last_byte(subkey_to_append), info: None }
),
Err(DatabaseError::Write {
Err(DatabaseWriteError {
code: -30418,
operation: DatabaseWriteOperation::CursorAppendDup,
table_name: AccountChangeSet::NAME,
key: Box::from(transition_id.encode().as_ref())
})
key: transition_id.encode().into(),
}
.into())
);
assert_eq!(
cursor.append(
transition_id - 1,
AccountBeforeTx { address: Address::with_last_byte(subkey_to_append), info: None }
),
Err(DatabaseError::Write {
Err(DatabaseWriteError {
code: -30418,
operation: DatabaseWriteOperation::CursorAppend,
table_name: AccountChangeSet::NAME,
key: Box::from((transition_id - 1).encode().as_ref())
})
key: (transition_id - 1).encode().into(),
}
.into())
);
assert_eq!(
cursor.append(

View File

@ -11,7 +11,7 @@ use crate::{
DatabaseError,
};
use parking_lot::RwLock;
use reth_interfaces::db::DatabaseWriteOperation;
use reth_interfaces::db::{DatabaseWriteError, DatabaseWriteOperation};
use reth_libmdbx::{ffi::DBI, EnvironmentKind, Transaction, TransactionKind, WriteFlags, RW};
use std::{marker::PhantomData, str::FromStr, sync::Arc, time::Instant};
@ -238,12 +238,13 @@ impl<E: EnvironmentKind> DbTxMut for Tx<'_, RW, E> {
Some(value.as_ref().len()),
|tx| {
tx.put(self.get_dbi::<T>()?, key.as_ref(), value, WriteFlags::UPSERT).map_err(|e| {
DatabaseError::Write {
DatabaseWriteError {
code: e.into(),
operation: DatabaseWriteOperation::Put,
table_name: T::NAME,
key: Box::from(key.as_ref()),
key: key.into(),
}
.into()
})
},
)