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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
pub mod block_db;
pub mod block_file;
pub mod dbkeys;
pub mod state_db;
use crate::account::db_state::DBState;
use crate::traits::{Decode, Encode};
use rocksdb::{
    BlockBasedIndexType, BlockBasedOptions, Error as RocksdbError, MergeOperands,
    Options as RocksDBOptions, SliceTransform, WriteBatch, DB as RocksDB,
};
use std::error::Error;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::io;
use std::path::PathBuf;

type DBResult<T> = Result<T, Box<DBError>>;
type HashValue = Vec<u8>;

pub fn merge_function(
    _new_key: &[u8],
    existing_value: Option<&[u8]>,
    operands: &mut MergeOperands,
) -> Option<Vec<u8>> {
    if let Some(value) = existing_value {
        let mut db_state: DBState = DBState::decode(value).unwrap();
        for _op in operands {
            db_state.ref_count += 1;
        }
        return Some(db_state.encode().unwrap());
    } else {
        let refs = operands.size_hint().0;
        if let Some(value) = operands.last() {
            let mut db_state: DBState = DBState::decode(value).unwrap();
            db_state.ref_count += refs as u32 - 1;
            return Some(db_state.encode().unwrap());
        } else {
            None
        }
    }
}

pub trait IDB {
    type OptionType;
    fn get_default_option() -> Self::OptionType;
    fn open(db_path: PathBuf, options: Option<Self::OptionType>) -> DBResult<Self>
    where
        Self: Sized;
    fn destroy(db_path: PathBuf) -> DBResult<()>
    where
        Self: Sized;
    fn _get(&self, key: &[u8]) -> DBResult<Vec<u8>>;
    fn set(&mut self, key: &[u8], value: &Vec<u8>) -> DBResult<()>;
    fn delete(&mut self, key: &[u8]) -> DBResult<()>;
    fn write_batch(&mut self, key_pairs: Vec<(Vec<u8>, Vec<u8>)>) -> DBResult<()>;
}

impl IDB for RocksDB {
    type OptionType = RocksDBOptions;

    fn get_default_option() -> Self::OptionType {
        let mut opts = Self::OptionType::default();
        let mut block_opts = BlockBasedOptions::default();
        block_opts.set_index_type(BlockBasedIndexType::HashSearch);
        opts.set_block_based_table_factory(&block_opts);
        let prefix_extractor = SliceTransform::create_fixed_prefix(32);
        opts.set_prefix_extractor(prefix_extractor);
        opts.create_if_missing(true);
        opts
    }

    fn open(db_path: PathBuf, options: Option<Self::OptionType>) -> DBResult<Self> {
        if let Some(opt) = options {
            match RocksDB::open(&opt, db_path) {
                Ok(database) => return Ok(database),
                Err(e) => return Err(Box::new(DBError::new(DBErrorType::RocksDBError(e)))),
            }
        } else {
            let opt: Self::OptionType = Self::get_default_option();
            match RocksDB::open(&opt, db_path) {
                Ok(database) => return Ok(database),
                Err(e) => return Err(Box::new(DBError::new(DBErrorType::RocksDBError(e)))),
            }
        }
    }

    fn destroy(db_path: PathBuf) -> DBResult<()> {
        match RocksDB::destroy(&(RocksDB::get_default_option()), db_path) {
            Ok(_) => return Ok(()),
            Err(e) => return Err(Box::new(DBError::new(DBErrorType::RocksDBError(e)))),
        }
    }

    fn _get(&self, key: &[u8]) -> DBResult<Vec<u8>> {
        match self.get(key) {
            Ok(Some(val)) => Ok(val.to_vec()),
            Ok(None) => Err(Box::new(DBError::new(DBErrorType::NotFoundError))),
            Err(err) => Err(Box::new(DBError::new(DBErrorType::RocksDBError(err)))),
        }
    }

    fn set(&mut self, key: &[u8], value: &Vec<u8>) -> DBResult<()> {
        match self.put(key, value) {
            Ok(()) => Ok(()),
            Err(err) => Err(Box::new(DBError::new(DBErrorType::RocksDBError(err)))),
        }
    }

    fn delete(&mut self, _key: &[u8]) -> DBResult<()> {
        Ok(())
    }

    fn write_batch(&mut self, key_pairs: Vec<(Vec<u8>, Vec<u8>)>) -> DBResult<()> {
        let mut batch = WriteBatch::default();
        for (k, v) in key_pairs {
            match batch.merge(&k, &v) {
                Ok(_) => {}
                Err(e) => {
                    return Err(Box::new(DBError::new(DBErrorType::RocksDBError(e))));
                }
            }
        }
        match self.write(batch) {
            Ok(_) => Ok(()),
            Err(e) => Err(Box::new(DBError::new(DBErrorType::RocksDBError(e)))),
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum DBErrorType {
    RocksDBError(RocksdbError),
    NotFoundError,
    UnexpectedError(String),
}

#[derive(Debug)]
pub struct DBError {
    error_type: DBErrorType,
}

impl DBError {
    pub fn new(error_type: DBErrorType) -> DBError {
        DBError { error_type }
    }
}

impl Display for DBError {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        match self.error_type {
            DBErrorType::RocksDBError(ref err) => err.fmt(f),
            DBErrorType::NotFoundError => write!(f, "Not Found"),
            DBErrorType::UnexpectedError(ref err) => write!(f, "Unexpected Error Occurs {}", err),
        }
    }
}
impl Error for DBError {
    fn description(&self) -> &str {
        match self.error_type {
            DBErrorType::RocksDBError(ref err) => err.description(),
            DBErrorType::NotFoundError => From::from("Not found error"),
            DBErrorType::UnexpectedError(ref err) => &err,
        }
    }
}

impl From<RocksdbError> for DBError {
    fn from(err: RocksdbError) -> Self {
        DBError::new(DBErrorType::RocksDBError(err))
    }
}

impl From<String> for DBError {
    fn from(err_msg: String) -> Self {
        DBError::new(DBErrorType::UnexpectedError(err_msg))
    }
}

impl From<Box<Error>> for DBError {
    fn from(err: Box<Error>) -> Self {
        DBError::new(DBErrorType::UnexpectedError(format!(
            "UNEXPECTED DB ERROR : {:?} ",
            err
        )))
    }
}

impl From<io::Error> for DBError {
    fn from(err: io::Error) -> Self {
        DBError::new(DBErrorType::UnexpectedError(format!(
            "UNEXPECTED DB ERROR : {:?} ",
            err
        )))
    }
}

pub mod mock {
    use super::*;
    use crate::account::db_state::DBState;
    use crate::traits::{Decode, Encode};
    use std::collections::HashMap;
    pub struct RocksDBMock {
        db: HashMap<Vec<u8>, Vec<u8>>,
    }

    impl RocksDBMock {
        pub fn new(db: HashMap<Vec<u8>, Vec<u8>>) -> RocksDBMock {
            RocksDBMock { db }
        }
    }

    impl IDB for RocksDBMock {
        type OptionType = ();

        fn get_default_option() -> () {
            ()
        }
        fn open(_db_path: PathBuf, _options: Option<Self::OptionType>) -> DBResult<Self> {
            Ok(RocksDBMock::new(HashMap::with_capacity(50000)))
        }

        fn destroy(_db_path: PathBuf) -> DBResult<()> {
            Ok(())
        }

        fn _get(&self, key: &[u8]) -> DBResult<Vec<u8>> {
            match self.db.get(key) {
                Some(val) => Ok(val.clone()),
                None => Err(Box::new(DBError::new(DBErrorType::NotFoundError))),
            }
        }

        fn set(&mut self, key: &[u8], value: &Vec<u8>) -> DBResult<()> {
            self.db.insert(key.to_vec(), value.clone());
            Ok(())
        }

        fn delete(&mut self, key: &[u8]) -> DBResult<()> {
            self.db.remove(key);
            Ok(())
        }
        fn write_batch(&mut self, key_pairs: Vec<(Vec<u8>, Vec<u8>)>) -> DBResult<()> {
            for (k, v) in key_pairs {
                self.db
                    .entry(k.to_vec())
                    .and_modify(|value| {
                        let mut db_state = DBState::decode(&value).unwrap();
                        db_state.ref_count += 1;
                        *value = db_state.encode().unwrap()
                    })
                    .or_insert(v.to_vec());
            }
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::account::account::Account;
    use crate::account::db_state::DBState;
    use crate::util::hash::hash;
    use std::path::PathBuf;
    #[test]
    fn it_performs_merge_operations_on_rocks_instance() {
        let mut options = RocksDB::get_default_option();
        options.set_merge_operator("Update Ref Count", merge_function, None);
        let path = PathBuf::from("./test/rocks");
        let db = RocksDB::open(&options, &path).unwrap();
        let account = Account::new(123, 123);
        let db_state = DBState::new(Some(account), None, 1);
        let encoded = db_state.encode().unwrap();
        let _ = db.delete(&hash(&encoded, 32));
        let res = db.merge(&hash(&encoded, 32), &encoded);
        match res {
            Ok(_) => {}
            Err(e) => {
                println!("Error: {:?}", e);
                unimplemented!();
            }
        }
        let retrieved = db.get(&hash(&encoded, 32)).unwrap();
        match retrieved {
            Some(v) => {
                let state = DBState::decode(&v).unwrap();
                assert_eq!(state.ref_count, 1u32);
            }
            None => {
                println!("Got nothing");
                unimplemented!();
            }
        }
        let new_res = db.merge(&hash(&encoded, 32), &encoded);
        match new_res {
            Ok(_) => {}
            Err(e) => {
                println!("Error: {:?}", e);
                unimplemented!();
            }
        }
        let new_retrieved = db.get(&hash(&encoded, 32)).unwrap();
        match new_retrieved {
            Some(v) => {
                let state = DBState::decode(&v).unwrap();
                assert_eq!(state.ref_count, 2u32);
            }
            None => {
                println!("Got nothing");
                unimplemented!();
            }
        }
    }
}