Skip to content

Return Cow<[u8]> instead of &[u8] in storage abstraction layer #652

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

Merged
merged 3 commits into from
Jan 26, 2023

Conversation

TheQuantumPhysicist
Copy link
Contributor

Generally speaking, it's not possible to get a slice of bytes from all storage backends with arbitrary lifetime. This is because this assumes that all the data is readily available for reading directly from the database. This is more of a wish.

In this PR we return Cow<[u8]> instead of &[u8] from the storage interface's get() function.

Using an owned value would be the last resort if a storage layer does not provide solutions for getting values by reference.

@TheQuantumPhysicist TheQuantumPhysicist changed the title Return Cow<[u8]> instead of &[u8] in database Return Cow<[u8]> instead of &[u8] in storage abstraction layer Jan 25, 2023
@TheQuantumPhysicist TheQuantumPhysicist marked this pull request as draft January 26, 2023 09:32
@TheQuantumPhysicist TheQuantumPhysicist marked this pull request as ready for review January 26, 2023 10:33
Copy link
Contributor

@iljakuklic iljakuklic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@@ -34,7 +36,7 @@ pub trait PrefixIter<'i> {
/// Read-only database operations
pub trait ReadOps: for<'i> PrefixIter<'i> {
/// Get value associated with given key.
fn get(&self, idx: DbIndex, key: &[u8]) -> crate::Result<Option<&[u8]>>;
fn get(&self, idx: DbIndex, key: &[u8]) -> crate::Result<Option<Cow<[u8]>>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the Option<Cow<[u8]>> part deserves an alias. Cannot think of a suitable name though. Maybe DbData or DbValue? Not sure. Would also resolve the Clippy warnings about type complexity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants