lighthouse-pulse/beacon_node/store/src/leveldb_store.rs

105 lines
2.8 KiB
Rust
Raw Normal View History

2019-05-21 06:29:34 +00:00
use super::*;
use db_key::Key;
use leveldb::database::kv::KV;
use leveldb::database::Database;
use leveldb::error::Error as LevelDBError;
use leveldb::options::{Options, ReadOptions, WriteOptions};
use std::path::Path;
2019-06-08 13:46:04 +00:00
use std::sync::Arc;
2019-05-21 06:29:34 +00:00
2019-05-21 08:49:24 +00:00
/// A wrapped leveldb database.
2019-06-08 13:46:04 +00:00
#[derive(Clone)]
pub struct LevelDB {
2019-06-08 13:46:04 +00:00
// Note: this `Arc` is only included because of an artificial constraint by gRPC. Hopefully we
// can remove this one day.
db: Arc<Database<BytesKey>>,
2019-05-21 06:29:34 +00:00
}
impl LevelDB {
2019-05-21 08:49:24 +00:00
/// Open a database at `path`, creating a new database if one does not already exist.
2019-05-21 06:29:34 +00:00
pub fn open(path: &Path) -> Result<Self, Error> {
let mut options = Options::new();
options.create_if_missing = true;
2019-06-08 13:46:04 +00:00
let db = Arc::new(Database::open(path, options)?);
2019-05-21 06:29:34 +00:00
2019-05-21 06:36:06 +00:00
Ok(Self { db })
2019-05-21 06:29:34 +00:00
}
fn read_options(&self) -> ReadOptions<BytesKey> {
2019-05-21 06:29:34 +00:00
ReadOptions::new()
}
fn write_options(&self) -> WriteOptions {
WriteOptions::new()
}
fn get_key_for_col(col: &str, key: &[u8]) -> BytesKey {
let mut col = col.as_bytes().to_vec();
col.append(&mut key.to_vec());
BytesKey { key: col }
}
}
2019-05-21 08:49:24 +00:00
/// Used for keying leveldb.
2019-05-21 06:29:34 +00:00
pub struct BytesKey {
key: Vec<u8>,
}
impl Key for BytesKey {
fn from_u8(key: &[u8]) -> Self {
Self { key: key.to_vec() }
}
fn as_slice<T, F: Fn(&[u8]) -> T>(&self, f: F) -> T {
f(self.key.as_slice())
}
}
impl Store for LevelDB {
2019-05-21 08:49:24 +00:00
/// Retrieve some bytes in `column` with `key`.
fn get_bytes(&self, col: &str, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
2019-05-21 06:29:34 +00:00
let column_key = Self::get_key_for_col(col, key);
self.db
.get(self.read_options(), column_key)
.map_err(Into::into)
}
2019-05-21 08:49:24 +00:00
/// Store some `value` in `column`, indexed with `key`.
2019-05-21 06:29:34 +00:00
fn put_bytes(&self, col: &str, key: &[u8], val: &[u8]) -> Result<(), Error> {
let column_key = Self::get_key_for_col(col, key);
self.db
.put(self.write_options(), column_key, val)
.map_err(Into::into)
}
2019-05-21 08:49:24 +00:00
/// Return `true` if `key` exists in `column`.
2019-05-21 06:29:34 +00:00
fn key_exists(&self, col: &str, key: &[u8]) -> Result<bool, Error> {
let column_key = Self::get_key_for_col(col, key);
self.db
.get(self.read_options(), column_key)
.map_err(Into::into)
.and_then(|val| Ok(val.is_some()))
}
2019-05-21 08:49:24 +00:00
/// Removes `key` from `column`.
2019-05-21 06:29:34 +00:00
fn key_delete(&self, col: &str, key: &[u8]) -> Result<(), Error> {
let column_key = Self::get_key_for_col(col, key);
self.db
.delete(self.write_options(), column_key)
.map_err(Into::into)
}
}
impl From<LevelDBError> for Error {
fn from(e: LevelDBError) -> Error {
Error::DBError {
message: format!("{:?}", e),
}
}
}