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
use std;
use std::error::Error;
use std::sync::Arc;
pub use storage::fs_storage::FsStorage;
pub use storage::ram_storage::RamStorage;
pub use storage::byte_code::{ByteDecodable, ByteEncodable};
pub use storage::compression::{vbyte_encode, VByteDecoder};
mod fs_storage;
mod ram_storage;
mod byte_code;
mod compression;
pub type Result<T> = std::result::Result<T, StorageError>;
#[derive(Debug)]
pub enum StorageError {
KeyNotFound,
ReadError(Option<std::io::Error>),
WriteError(Option<std::io::Error>),
}
impl std::fmt::Display for StorageError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl Error for StorageError {
fn description(&self) -> &str {
match *self {
StorageError::KeyNotFound => "Key was not found in storage!",
StorageError::ReadError(_) => "An error occured while trying to read from storage!",
StorageError::WriteError(_) => "An error occured while trying to write to storage!",
}
}
fn cause(&self) -> Option<&Error> {
match *self {
StorageError::ReadError(Some(ref cause)) |
StorageError::WriteError(Some(ref cause)) => Some(cause),
_ => None,
}
}
}
pub trait Storage<T>
where Self: Sync + Send
{
fn get(&self, id: u64) -> Result<Arc<T>>;
fn store(&mut self, id: u64, data: T) -> Result<()>;
}