-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- implemented db macro for KeyGenDb
- Loading branch information
Showing
5 changed files
with
105 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#[macro_export] | ||
macro_rules! createDb { | ||
($db_name: ident | ||
{ $($field_name: ident),*} | ||
) => { | ||
fn db_key(db_dst: &'static [u8], item_dst: &'static [u8], key: impl AsRef<[u8]>) -> Vec<u8> { | ||
let db_len = u8::try_from(db_dst.len()).unwrap(); | ||
let dst_len = u8::try_from(item_dst.len()).unwrap(); | ||
[[db_len].as_ref(), db_dst, [dst_len].as_ref(), item_dst, key.as_ref()].concat() | ||
} | ||
|
||
$( | ||
#[derive(Clone, Debug)] | ||
pub struct $field_name; | ||
impl $field_name { | ||
|
||
pub fn key(key: impl AsRef<[u8]>) -> Vec<u8> { | ||
db_key(stringify!($db_name).as_bytes(), stringify!($field_name).as_bytes(), key) | ||
} | ||
pub fn set(txn: &mut impl DbTxn, key: impl AsRef<[u8]>, data: &impl serde::Serialize) { | ||
let key = $field_name::key(key); | ||
txn.put(&key, bincode::serialize(data).unwrap()); | ||
} | ||
pub fn get<D: serde::de::DeserializeOwned>(getter: &impl Get, key: impl AsRef<[u8]>) -> Option<D> { | ||
getter.get($field_name::key(key)).map(|data| { | ||
bincode::deserialize(&mut data.as_ref()).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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod mem; | ||
pub use mem::*; | ||
mod db_macro; | ||
pub use db_macro::*; | ||
|
||
#[cfg(feature = "rocksdb")] | ||
mod rocks; | ||
|
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
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