diff --git a/store/src/lib.rs b/store/src/lib.rs index 5814bf6c..b2408ca5 100644 --- a/store/src/lib.rs +++ b/store/src/lib.rs @@ -32,6 +32,39 @@ pub mod pmmr; pub mod prune_list; pub mod types; +const SEP: u8 = b':'; + +use byteorder::{BigEndian, WriteBytesExt}; + +/// Build a db key from a prefix and a byte vector identifier. +pub fn to_key>(prefix: u8, k: K) -> Vec { + let k = k.as_ref(); + let mut res = Vec::with_capacity(k.len() + 2); + res.push(prefix); + res.push(SEP); + res.extend_from_slice(k); + res +} + +/// Build a db key from a prefix and a byte vector identifier and numeric identifier +pub fn to_key_u64>(prefix: u8, k: K, val: u64) -> Vec { + let k = k.as_ref(); + let mut res = Vec::with_capacity(k.len() + 10); + res.push(prefix); + res.push(SEP); + res.extend_from_slice(k); + res.write_u64::(val).unwrap(); + res +} +/// Build a db key from a prefix and a numeric identifier. +pub fn u64_to_key(prefix: u8, val: u64) -> Vec { + let mut res = Vec::with_capacity(10); + res.push(prefix); + res.push(SEP); + res.write_u64::(val).unwrap(); + res +} + pub use crate::lmdb::*; use std::ffi::OsStr;