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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use crate::errors::StorageError;
use crate::storage::types::{DbRecord, StorageType};
use async_trait::async_trait;
#[cfg(feature = "serde_serialization")]
use serde::{de::DeserializeOwned, Serialize};
use std::collections::HashMap;
use std::hash::Hash;
use std::marker::Send;
pub mod timed_cache;
pub mod transaction;
pub mod types;
pub mod memory;
#[cfg(any(test, feature = "public-tests"))]
pub mod tests;
#[cfg(feature = "serde_serialization")]
pub trait Storable: Clone + Serialize + DeserializeOwned + Sync {
type StorageKey: Clone + Serialize + Eq + Hash + Send + Sync + std::fmt::Debug;
fn data_type() -> StorageType;
fn get_id(&self) -> Self::StorageKey;
fn get_full_binary_id(&self) -> Vec<u8> {
Self::get_full_binary_key_id(&self.get_id())
}
fn get_full_binary_key_id(key: &Self::StorageKey) -> Vec<u8>;
fn key_from_full_binary(bin: &[u8]) -> Result<Self::StorageKey, String>;
}
#[cfg(not(feature = "serde_serialization"))]
pub trait Storable: Clone + Sync {
type StorageKey: Clone + Eq + Hash + Send + Sync + std::fmt::Debug;
fn data_type() -> StorageType;
fn get_id(&self) -> Self::StorageKey;
fn get_full_binary_id(&self) -> Vec<u8> {
Self::get_full_binary_key_id(&self.get_id())
}
fn get_full_binary_key_id(key: &Self::StorageKey) -> Vec<u8>;
fn key_from_full_binary(bin: &[u8]) -> Result<Self::StorageKey, String>;
}
#[async_trait]
pub trait Storage: Clone {
async fn log_metrics(&self, level: log::Level);
async fn begin_transaction(&self) -> bool;
async fn commit_transaction(&self) -> Result<(), StorageError>;
async fn rollback_transaction(&self) -> Result<(), StorageError>;
async fn is_transaction_active(&self) -> bool;
async fn set(&self, record: DbRecord) -> Result<(), StorageError>;
async fn batch_set(&self, records: Vec<DbRecord>) -> Result<(), StorageError>;
async fn get<St: Storable>(&self, id: &St::StorageKey) -> Result<DbRecord, StorageError>;
async fn get_direct<St: Storable>(&self, id: &St::StorageKey)
-> Result<DbRecord, StorageError>;
async fn flush_cache(&self);
async fn tombstone_value_states(
&self,
keys: &[types::ValueStateKey],
) -> Result<(), StorageError>;
async fn batch_get<St: Storable>(
&self,
ids: &[St::StorageKey],
) -> Result<Vec<DbRecord>, StorageError>;
async fn get_user_data(
&self,
username: &types::AkdLabel,
) -> Result<types::KeyData, StorageError>;
async fn get_user_state(
&self,
username: &types::AkdLabel,
flag: types::ValueStateRetrievalFlag,
) -> Result<types::ValueState, StorageError>;
async fn get_user_state_versions(
&self,
usernames: &[types::AkdLabel],
flag: types::ValueStateRetrievalFlag,
) -> Result<HashMap<types::AkdLabel, (u64, types::AkdValue)>, StorageError>;
}
#[async_trait]
pub trait StorageUtil: Storage {
async fn batch_get_type_direct<St: Storable>(&self) -> Result<Vec<DbRecord>, StorageError>;
async fn batch_get_all_direct(&self) -> Result<Vec<DbRecord>, StorageError>;
}