-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathdb_object.rs
More file actions
54 lines (42 loc) · 1.87 KB
/
db_object.rs
File metadata and controls
54 lines (42 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::errors::DeserializationError;
use crate::storage_trait::{create_db_key, DbKey, DbKeyPrefix, DbValue};
pub struct EmptyKeyContext;
pub trait HasDynamicPrefix {
/// Extra data needed to construct a leaf for node db key prefix. For example, in index layout,
/// we need to know the trie type of inner nodes.
type KeyContext;
/// Returns the storage key prefix of the DB object.
fn get_prefix(&self, key_context: &Self::KeyContext) -> DbKeyPrefix;
}
pub trait HasStaticPrefix {
/// Extra data needed to construct a leaf for node db key prefix. For example, in index layout,
/// we need to know the trie type of inner nodes.
type KeyContext;
/// Returns the storage key prefix of the DB object.
fn get_static_prefix(key_context: &Self::KeyContext) -> DbKeyPrefix;
}
impl<T: HasStaticPrefix> HasDynamicPrefix for T {
/// Inherit the KeyContext from the HasStaticPrefix trait.
type KeyContext = T::KeyContext;
fn get_prefix(&self, key_context: &Self::KeyContext) -> DbKeyPrefix {
T::get_static_prefix(key_context)
}
}
pub struct EmptyDeserializationContext;
pub trait DBObject: Sized + HasDynamicPrefix {
/// Extra data needed to deserialize the object. For example, facts layout nodes need the node
/// hash and an indication of whether or not it's a leaf node (index layout nodes only need the
/// latter).
type DeserializeContext;
/// Serializes the given value.
fn serialize(&self) -> DbValue;
/// Deserializes the given value using the provided context.
fn deserialize(
value: &DbValue,
deserialize_context: &Self::DeserializeContext,
) -> Result<Self, DeserializationError>;
/// Returns a [DbKey] from a prefix and a suffix.
fn get_db_key(&self, key_context: &Self::KeyContext, suffix: &[u8]) -> DbKey {
create_db_key(self.get_prefix(key_context), suffix)
}
}