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
use crate::lmdb_storage::LMDBStorage;
use crate::remote_storage_client::*;
use crate::tt_storage::TTStorage;
use v_onto::individual::*;

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 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 get_v(&mut self, storage: StorageId, key: &str) -> Option<String>;
    fn get_raw(&mut self, storage: StorageId, key: &str) -> Vec<u8>;
    fn remove(&mut self, storage: StorageId, key: &str) -> bool;
}

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) -> VStorage {
        VStorage {
            storage: EStorage::LMDB(LMDBStorage::new(db_path, mode)),
        }
    }

    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,
        }
    }
}