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
use crate::onto::individual::Individual;
use crate::storage::lmdb_storage::LMDBStorage;
use crate::storage::remote_storage_client::*;
use crate::storage::tt_storage::TTStorage;
pub enum StorageError {
None,
NotReady,
}
#[derive(PartialEq, Debug, Clone)]
pub enum StorageMode {
ReadOnly,
ReadWrite,
}
#[derive(PartialEq, Debug, Clone)]
pub enum StorageId {
Individuals,
Tickets,
Az,
}
pub(crate) enum EStorage {
Lmdb(LMDBStorage),
Tt(TTStorage),
Remote(StorageROClient),
None,
}
pub trait Storage {
fn get_individual_from_db(&mut self, storage: StorageId, id: &str, iraw: &mut Individual) -> bool;
fn get_v(&mut self, storage: StorageId, key: &str) -> Option<String>;
fn get_raw(&mut self, storage: StorageId, key: &str) -> Vec<u8>;
fn put_kv(&mut self, storage: StorageId, key: &str, val: &str) -> bool;
fn put_kv_raw(&mut self, storage: StorageId, key: &str, val: Vec<u8>) -> bool;
fn remove(&mut self, storage: StorageId, key: &str) -> bool;
fn count(&mut self, storage: StorageId) -> usize;
}
pub struct VStorage {
storage: EStorage,
}
impl VStorage {
pub fn none() -> VStorage {
VStorage {
storage: EStorage::None,
}
}
pub fn new_remote(addr: &str) -> VStorage {
VStorage {
storage: EStorage::Remote(StorageROClient::new(addr)),
}
}
pub fn new_tt(tt_id: String, login: &str, pass: &str) -> VStorage {
VStorage {
storage: EStorage::Tt(TTStorage::new(tt_id, login, pass)),
}
}
pub fn new_lmdb(db_path: &str, mode: StorageMode, max_read_counter_reopen: Option<u64>) -> VStorage {
VStorage {
storage: EStorage::Lmdb(LMDBStorage::new(db_path, mode, max_read_counter_reopen)),
}
}
pub fn get_individual(&mut self, id: &str, iraw: &mut Individual) -> bool {
match &mut self.storage {
EStorage::Tt(s) => s.get_individual_from_db(StorageId::Individuals, id, iraw),
EStorage::Lmdb(s) => s.get_individual_from_db(StorageId::Individuals, id, iraw),
EStorage::Remote(s) => s.get_individual_from_db(StorageId::Individuals, id, iraw),
_ => false,
}
}
pub fn get_individual_from_db(&mut self, storage: StorageId, id: &str, iraw: &mut Individual) -> bool {
match &mut self.storage {
EStorage::Tt(s) => s.get_individual_from_db(storage, id, iraw),
EStorage::Lmdb(s) => s.get_individual_from_db(storage, id, iraw),
EStorage::Remote(s) => s.get_individual_from_db(storage, id, iraw),
_ => false,
}
}
pub fn get_value(&mut self, storage: StorageId, id: &str) -> Option<String> {
match &mut self.storage {
EStorage::Tt(s) => s.get_v(storage, id),
EStorage::Lmdb(s) => s.get_v(storage, id),
EStorage::Remote(_s) => None,
_ => None,
}
}
pub fn get_raw_value(&mut self, storage: StorageId, id: &str) -> Vec<u8> {
match &mut self.storage {
EStorage::Tt(s) => s.get_raw(storage, id),
EStorage::Lmdb(s) => s.get_raw(storage, id),
EStorage::Remote(_s) => Default::default(),
_ => Default::default(),
}
}
pub fn put_kv(&mut self, storage: StorageId, key: &str, val: &str) -> bool {
match &mut self.storage {
EStorage::Tt(s) => s.put_kv(storage, key, val),
EStorage::Lmdb(s) => s.put_kv(storage, key, val),
EStorage::Remote(_s) => false,
_ => false,
}
}
pub fn put_kv_raw(&mut self, storage: StorageId, key: &str, val: Vec<u8>) -> bool {
match &mut self.storage {
EStorage::Tt(s) => s.put_kv_raw(storage, key, val),
EStorage::Lmdb(s) => s.put_kv_raw(storage, key, val),
EStorage::Remote(_s) => false,
_ => false,
}
}
pub fn remove(&mut self, storage: StorageId, key: &str) -> bool {
match &mut self.storage {
EStorage::Tt(s) => s.remove(storage, key),
EStorage::Lmdb(s) => s.remove(storage, key),
EStorage::Remote(_s) => false,
_ => false,
}
}
pub fn count(&mut self, storage: StorageId) -> usize {
match &mut self.storage {
EStorage::Tt(s) => s.count(storage),
EStorage::Lmdb(s) => s.count(storage),
EStorage::Remote(s) => s.count(storage),
_ => 0,
}
}
}