-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f4b651
commit 2a311b5
Showing
11 changed files
with
57 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
//! | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1 @@ | ||
use reth_libmdbx::*; | ||
use db::Database as EigenDB; | ||
use crate::db; | ||
|
||
pub struct Bd(Database); | ||
|
||
impl EigenDB for Bd { | ||
fn get(&self, key: &[u8]) -> Option<Vec<u8>> { | ||
self.0.get(key).unwrap() | ||
} | ||
|
||
fn put(&mut self, key: Vec<u8>, value: Vec<u8>) { | ||
self.0.put(key, value).unwrap() | ||
} | ||
|
||
fn del(&mut self, key: Vec<u8>) { | ||
self.0.del(key).unwrap() | ||
} | ||
|
||
fn commit(&mut self, transaction: db::Transaction) { | ||
for change in transaction.0 { | ||
match change { | ||
db::ChangeOpt::Put(key, value) => self.put(key, value), | ||
db::ChangeOpt::Del(key) => self.del(key), | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
fn test_open() { | ||
let dir = "/tmp/eigen-reth2"; | ||
let env = Environment::builder() | ||
.open(std::path::Path::new(dir)) | ||
.unwrap(); | ||
let txn = env.begin_rw_txn().unwrap(); | ||
let db = txn.open_db(None).unwrap(); | ||
txn.put(db.dbi(), b"key1", b"val1", WriteFlags::empty()) | ||
.unwrap(); | ||
txn.put(db.dbi(), b"key2", b"val2", WriteFlags::empty()) | ||
.unwrap(); | ||
txn.put(db.dbi(), b"key3", b"val3", WriteFlags::empty()) | ||
.unwrap(); | ||
txn.commit().unwrap(); | ||
|
||
let txn = env.begin_rw_txn().unwrap(); | ||
let db = txn.open_db(None).unwrap(); | ||
assert_eq!(txn.get(db.dbi(), b"key1").unwrap(), Some(*b"val1")); | ||
assert_eq!(txn.get(db.dbi(), b"key2").unwrap(), Some(*b"val2")); | ||
assert_eq!(txn.get(db.dbi(), b"key3").unwrap(), Some(*b"val3")); | ||
assert_eq!(txn.get::<()>(db.dbi(), b"key").unwrap(), None); | ||
|
||
txn.del(db.dbi(), b"key1", None).unwrap(); | ||
assert_eq!(txn.get::<()>(db.dbi(), b"key1").unwrap(), None); | ||
txn.commit().unwrap(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use crate::db::Database as EigenDB; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Default)] | ||
pub struct Db(HashMap<Vec<u8>, Vec<u8>>); | ||
|
||
impl EigenDB for Db { | ||
fn get(&self, key: &[u8]) -> Option<Vec<u8>> { | ||
self.0.get(key).cloned() | ||
} | ||
|
||
fn put(&mut self, key: Vec<u8>, value: Vec<u8>) { | ||
self.0.insert(key, value); | ||
} | ||
|
||
fn del(&mut self, key: Vec<u8>) -> Option<Vec<u8>> { | ||
self.0.remove(&key) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,32 @@ | ||
mod memory_kvdb; | ||
mod libmdbx; | ||
// TODO: Fix me | ||
#![allow(dead_code)] | ||
|
||
use std::path::PathBuf; | ||
mod libmdbx; | ||
mod mem; | ||
|
||
use db::Database as EigenDB; | ||
use crate::db; | ||
use db::Database as EigenDB; | ||
|
||
pub(crate) enum DBConfig { | ||
/// memory kv-database | ||
Memory, | ||
/// libmdbx database | ||
MDBX { | ||
Mdbx { | ||
/// Path to the mdbx database | ||
path: PathBuf, | ||
// path: String, | ||
// path: PathBuf, | ||
path: String, | ||
}, | ||
} | ||
|
||
// TODO: | ||
|
||
|
||
pub(crate) fn open_db(config: &DBConfig) -> Result<EigenDB, ()> { | ||
|
||
pub(crate) fn open_db(config: &DBConfig) -> Result<Box<dyn EigenDB>, ()> { | ||
match config { | ||
DBConfig::Memory => open_memory_db(), | ||
DBConfig::Mdbx { .. } => { | ||
unimplemented!("open_mdbx_db") | ||
} | ||
} | ||
} | ||
|
||
|
||
fn open_mdbx_db(path: &PathBuf) -> Result<EigenDB, ()> { | ||
let db = libmdbx::Database::open(path); | ||
Ok(EigenDB::MDBX(db)) | ||
fn open_memory_db() -> Result<Box<dyn EigenDB>, ()> { | ||
Ok(Box::new(mem::Db::default())) | ||
} | ||
|
||
// | ||
// fn open_memory_db() -> Result<EigenDB, ()> { | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,13 @@ | ||
// TODO: Fix me | ||
#![allow(dead_code)] | ||
|
||
mod data_availability_db; | ||
mod db_utils; | ||
|
||
/// TODO: we need a trait to abstract the database operations in order to support multiple databases? | ||
pub enum ChangeOpt { | ||
Put(Vec<u8>, Vec<u8>), | ||
Del(Vec<u8>), | ||
} | ||
|
||
pub struct Transaction(pub Vec<ChangeOpt>); | ||
|
||
impl Transaction { | ||
pub fn new() -> Self { | ||
Transaction(Vec::new()) | ||
} | ||
|
||
pub fn put(&mut self, key: Vec<u8>, value: Vec<u8>) { | ||
self.0.push(ChangeOpt::Put(key, value)); | ||
} | ||
|
||
pub fn del(&mut self, key: Vec<u8>) { | ||
self.0.push(ChangeOpt::Del(key)); | ||
} | ||
} | ||
/// TODO: we need a trait to abstract the database operations in order to support multiple databases | ||
pub trait Database { | ||
fn get(&self, key: &[u8]) -> Option<Vec<u8>>; | ||
fn put(&mut self, key: Vec<u8>, value: Vec<u8>); | ||
fn del(&mut self, key: Vec<u8>); | ||
fn commit(&mut self, transaction: Transaction); | ||
fn del(&mut self, key: Vec<u8>) -> Option<Vec<u8>>; | ||
} | ||
/// key prefixes for different databases | ||
pub(crate) mod columns{ | ||
pub const NUM_COLUMNS: u8 = 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
//! | ||
//! | ||
//! | ||
|