forked from nervosnetwork/ckb
/
lib.rs
39 lines (32 loc) · 1.01 KB
/
lib.rs
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
//! # The DB Library
//!
//! This Library contains the `KeyValueDB` traits
//! which provides key-value store interface
use failure::Fail;
use std::ops::Range;
use std::result;
pub mod config;
pub mod memorydb;
pub mod rocksdb;
pub use crate::config::DBConfig;
pub use crate::memorydb::MemoryKeyValueDB;
pub use crate::rocksdb::RocksDB;
pub type Col = u32;
pub type Error = ErrorKind;
pub type Result<T> = result::Result<T, Error>;
#[derive(Clone, Debug, PartialEq, Eq, Fail)]
pub enum ErrorKind {
#[fail(display = "DBError {}", _0)]
DBError(String),
}
pub trait KeyValueDB: Sync + Send {
type Batch: DbBatch;
fn read(&self, col: Col, key: &[u8]) -> Result<Option<Vec<u8>>>;
fn partial_read(&self, col: Col, key: &[u8], range: &Range<usize>) -> Result<Option<Vec<u8>>>;
fn batch(&self) -> Result<Self::Batch>;
}
pub trait DbBatch {
fn insert(&mut self, col: Col, key: &[u8], value: &[u8]) -> Result<()>;
fn delete(&mut self, col: Col, key: &[u8]) -> Result<()>;
fn commit(self) -> Result<()>;
}