Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(db_lookup_times): Modify benchmarks to use TableBlueprint to avoid manual serde #2169

Open
rymnc opened this issue Sep 5, 2024 · 1 comment
Assignees
Labels
tech-debt The issue is to improve the current code and make it more clear/generic/reusable/pretty/avoidable.

Comments

@rymnc
Copy link
Member

rymnc commented Sep 5, 2024

In #2142 we introduced benchmarks that were not too clean, and part of the rework has been addressed in -

The last part of the rework involves implementing TableWithBlueprint ~

pub trait TableWithBlueprint: Mappable + Sized {
/// The type of the blueprint used by the table.
type Blueprint;
/// The column type used by the table.
type Column: StorageColumn;
/// The column occupied by the table.
fn column() -> Self::Column;
}

for the custom columns we have defined in the benchmarks ~

pub enum BenchDbColumn {
/// See [`Transactions`](crate::tables::Transactions)
Transactions = 6,
/// See [`FuelBlocks`](crate::tables::FuelBlocks)
FuelBlocks = 7,
FullFuelBlocks = 10902,
Metadata = 10903,
}

so that we can use KeyValueInspect and KeyValueMutate storage traits ~

pub trait KeyValueInspect {
/// The type of the column.
type Column: StorageColumn;
/// Checks if the value exists in the storage.
fn exists(&self, key: &[u8], column: Self::Column) -> StorageResult<bool> {
Ok(self.size_of_value(key, column)?.is_some())
}
/// Returns the size of the value in the storage.
fn size_of_value(
&self,
key: &[u8],
column: Self::Column,
) -> StorageResult<Option<usize>> {
Ok(self.get(key, column)?.map(|value| value.len()))
}
/// Returns the value from the storage.
fn get(&self, key: &[u8], column: Self::Column) -> StorageResult<Option<Value>>;
/// Reads the value from the storage into the `buf` and returns the number of read bytes.
fn read(
&self,
key: &[u8],
column: Self::Column,
buf: &mut [u8],
) -> StorageResult<Option<usize>> {
self.get(key, column)?
.map(|value| {
let read = value.len();
if read != buf.len() {
return Err(StorageError::Other(anyhow::anyhow!(
"Buffer size is not equal to the value size"
)));
}
buf.copy_from_slice(value.as_ref());
Ok(read)
})
.transpose()
}
}
&
pub trait KeyValueMutate: KeyValueInspect {
/// Inserts the `Value` into the storage.
fn put(
&mut self,
key: &[u8],
column: Self::Column,
value: Value,
) -> StorageResult<()> {
self.write(key, column, value.as_ref()).map(|_| ())
}
/// Put the `Value` into the storage and return the old value.
fn replace(
&mut self,
key: &[u8],
column: Self::Column,
value: Value,
) -> StorageResult<Option<Value>> {
let old_value = self.get(key, column)?;
self.put(key, column, value)?;
Ok(old_value)
}
/// Writes the `buf` into the storage and returns the number of written bytes.
fn write(
&mut self,
key: &[u8],
column: Self::Column,
buf: &[u8],
) -> StorageResult<usize>;
/// Removes the value from the storage and returns it.
fn take(&mut self, key: &[u8], column: Self::Column) -> StorageResult<Option<Value>> {
let old_value = self.get(key, column)?;
self.delete(key, column)?;
Ok(old_value)
}
/// Removes the value from the storage.
fn delete(&mut self, key: &[u8], column: Self::Column) -> StorageResult<()>;
}

to get/put data from the tables in batches without doing manual serde.

It is suggested to use Plain<Primitive<4>, Postcard> for the Blueprint associated type in the impls of TableWithBlueprint

@rymnc rymnc added the tech-debt The issue is to improve the current code and make it more clear/generic/reusable/pretty/avoidable. label Sep 5, 2024
@rymnc rymnc self-assigned this Sep 5, 2024
@xgreenx
Copy link
Collaborator

xgreenx commented Sep 6, 2024

And it would be nice to insert transactions and blocks by using InMemoryTransaction to speed up seeding=)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tech-debt The issue is to improve the current code and make it more clear/generic/reusable/pretty/avoidable.
Projects
None yet
Development

No branches or pull requests

2 participants