Skip to main content

commonware_storage/qmdb/
operation.rs

1use crate::merkle::{Family, Location};
2use commonware_codec::CodecShared;
3use core::{fmt::Debug, hash::Hash, ops::Deref};
4
5/// Trait bound for key types used in QMDB operations. Satisfied by both fixed-size keys
6/// (`Array` types) and variable-length keys (`Vec<u8>`).
7pub trait Key:
8    CodecShared + Clone + 'static + Eq + Ord + Hash + AsRef<[u8]> + Deref<Target = [u8]> + Debug
9{
10}
11
12impl<T> Key for T where
13    T: CodecShared + Clone + 'static + Eq + Ord + Hash + AsRef<[u8]> + Deref<Target = [u8]> + Debug
14{
15}
16
17/// An operation that can be applied to a database.
18pub trait Operation<F: Family> {
19    /// The key type for this operation.
20    type Key: Key;
21
22    /// Returns the key if this operation involves a key, None otherwise.
23    fn key(&self) -> Option<&Self::Key>;
24
25    /// If this operation updates its key's value.
26    fn is_update(&self) -> bool;
27
28    /// If this operation deletes its key's value.
29    fn is_delete(&self) -> bool;
30
31    /// The inactivity floor location if this operation is a commit operation with a floor value,
32    /// None otherwise.
33    fn has_floor(&self) -> Option<Location<F>>;
34}
35
36/// A trait for operations used by database variants that support commit operations.
37pub trait Committable {
38    /// If this operation is a commit operation.
39    fn is_commit(&self) -> bool;
40}