Skip to content

Commit

Permalink
Revert "added env struct"
Browse files Browse the repository at this point in the history
  • Loading branch information
Gua00va authored Jun 27, 2023
1 parent 1305402 commit ef0915d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 99 deletions.
2 changes: 0 additions & 2 deletions slasher/src/database/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ pub enum Database<'env> {
Mdbx(mdbx_impl::Database<'env>),
#[cfg(feature = "lmdb")]
Lmdb(lmdb_impl::Database<'env>),
#[cfg(feature = "redb")]
Redb(redb_impl::Database<'env>),
Disabled(PhantomData<&'env ()>),
}

Expand Down
2 changes: 1 addition & 1 deletion slasher/src/database/lmdb_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Environment {
}

impl<'env> RwTransaction<'env> {
pub fn x<K: AsRef<[u8]> + ?Sized>(
pub fn get<K: AsRef<[u8]> + ?Sized>(
&'env self,
db: &Database<'env>,
key: &K,
Expand Down
102 changes: 6 additions & 96 deletions slasher/src/database/redb_impl.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
#![cfg(feature = "redb")]

use redb::{TableDefinition, ReadableTable};

use crate::config::Config;
use crate::database::{interface::OpenDatabases, *};
use crate::Error;
use std::marker::PhantomData;
use std::path::PathBuf;

const TABLE: TableDefinition<u8, u8> = TableDefinition::new("data");
use crate::config::{Config};

pub struct Builder {
builder: redb::Builder,
Expand All @@ -17,104 +10,21 @@ pub struct Builder {
#[derive(Debug)]
pub struct Database<'db> {
db: redb::Database,
_phantom: PhantomData<&'db ()>,
_phantom: PhantomData<&'db ()>
}

pub struct RwTransaction<'db> {
pub struct WriteTransaction<'db> {
txn: redb::WriteTransaction<'db>,
}

pub struct Table<'db, 'txn, K: redb::RedbKey + 'static, V: redb::RedbValue + 'static> {
table: redb::Table<'db, 'txn, K, V>,
table: redb::Table<'db, 'txn, K, V>
}

impl Builder {
pub fn new(config: &Config) -> Builder {
let builder = redb::Builder::new();

Builder { builder }
}
}

pub struct Environment {
builder: redb::Builder,
dbi_open_mutex: Mutex<i32>,
max_num_dbs: u32,
database_path: PathBuf,
}

impl Environment {
pub fn new(config: &Config) -> Result<Environment, Error> {
let builder = redb::Builder::new();
let mutex = Mutex::new(0);
Ok(Environment {
builder,
dbi_open_mutex: mutex,
max_num_dbs: MAX_NUM_DBS as u32,
database_path: config.database_path.clone(),
})
}

pub fn create_database(&self, name: &str) -> Result<redb::Database, Error> {
match self.builder.create(self.database_path.join(name)) {
Ok(database) => Ok(database),
Err(_) => {
panic!("Failed to create db")
}
}
}

pub fn create_databases(&self) -> Result<OpenDatabases, Error> {
let mutex = self.dbi_open_mutex.lock();
let indexed_attestation_db = self.create_database(INDEXED_ATTESTATION_DB).unwrap();
let indexed_attestation_id_db = self.create_database(INDEXED_ATTESTATION_ID_DB).unwrap();
let attesters_db = self.create_database(ATTESTERS_DB).unwrap();
let attesters_max_targets_db = self.create_database(ATTESTERS_MAX_TARGETS_DB).unwrap();
let min_targets_db = self.create_database(MIN_TARGETS_DB).unwrap();
let max_targets_db = self.create_database(MAX_TARGETS_DB).unwrap();
let current_epochs_db = self.create_database(CURRENT_EPOCHS_DB).unwrap();
let proposers_db = self.create_database(PROPOSERS_DB).unwrap();
let metadata_db = self.create_database(METADATA_DB).unwrap();

drop(mutex);

let wrap = |db| {
crate::Database::Redb(Database {
db,
_phantom: PhantomData,
})
};

Ok(OpenDatabases {
indexed_attestation_db: wrap(indexed_attestation_db),
indexed_attestation_id_db: wrap(indexed_attestation_id_db),
attesters_db: wrap(attesters_db),
attesters_max_targets_db: wrap(attesters_max_targets_db),
min_targets_db: wrap(min_targets_db),
max_targets_db: wrap(max_targets_db),
current_epochs_db: wrap(current_epochs_db),
proposers_db: wrap(proposers_db),
metadata_db: wrap(metadata_db),
})
}

pub fn begin_rw_txn(&self) -> Result<RwTransaction, Error> {
todo!()
Builder{ builder }
}

pub fn filenames(&self, config: &Config) -> Vec<PathBuf> {
todo!()
}
}

impl<'db> RwTransaction<'db> {
pub fn get<K: std::borrow::Borrow<u8>>(
&'db self,
db: &Database<'db>,
key: &K,
) {
let tx = db.db.begin_read().unwrap();
let table = tx.open_table(TABLE).unwrap();
table.get(*key.clone());
}
}
}

0 comments on commit ef0915d

Please sign in to comment.