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
pub mod btree;
pub mod error;
pub mod lmdb;
use crate::database::error::DatabaseError;
pub type DatabaseCursor<'a> = Box<dyn DatabaseReaderCursor<Item = (Vec<u8>, Vec<u8>)> + 'a>;
pub trait Database: Sync + Send {
fn get_reader<'a>(&'a self) -> Result<Box<dyn DatabaseReader + 'a>, DatabaseError>;
fn get_writer<'a>(&'a self) -> Result<Box<dyn DatabaseWriter + 'a>, DatabaseError>;
fn clone_box(&self) -> Box<Database>;
}
impl Clone for Box<Database> {
fn clone(&self) -> Box<Database> {
self.clone_box()
}
}
pub trait DatabaseReader {
fn get(&self, key: &[u8]) -> Option<Vec<u8>>;
fn index_get(&self, index: &str, key: &[u8]) -> Result<Option<Vec<u8>>, DatabaseError>;
fn cursor(&self) -> Result<DatabaseCursor, DatabaseError>;
fn index_cursor(&self, index: &str) -> Result<DatabaseCursor, DatabaseError>;
fn count(&self) -> Result<usize, DatabaseError>;
fn index_count(&self, index: &str) -> Result<usize, DatabaseError>;
}
pub trait DatabaseWriter: DatabaseReader {
fn put(&mut self, key: &[u8], value: &[u8]) -> Result<(), DatabaseError>;
fn overwrite(&mut self, key: &[u8], value: &[u8]) -> Result<(), DatabaseError>;
fn delete(&mut self, key: &[u8]) -> Result<(), DatabaseError>;
fn index_put(&mut self, index: &str, key: &[u8], value: &[u8]) -> Result<(), DatabaseError>;
fn index_delete(&mut self, index: &str, key: &[u8]) -> Result<(), DatabaseError>;
fn commit(self: Box<Self>) -> Result<(), DatabaseError>;
fn as_reader(&self) -> &dyn DatabaseReader;
}
pub trait DatabaseReaderCursor: Iterator {
fn first(&mut self) -> Option<(Vec<u8>, Vec<u8>)>;
fn last(&mut self) -> Option<(Vec<u8>, Vec<u8>)>;
}