crate::ix!();
pub const DBWRAPPER_PREALLOC_KEY_SIZE: usize = 64;
pub const DBWRAPPER_PREALLOC_VALUE_SIZE: usize = 1024;
pub mod dbwrapper {
use super::*;
pub fn handle_error(status: &leveldb::Status) -> Result<(),String> {
if status.is_ok() {
return Ok(());
}
let errmsg: String = "Fatal LevelDB error: ".to_owned() + status.to_string().as_str();
log_printf!("%s\n", errmsg);
log_printf!("You can use -debug=leveldb to get more complete diagnostic messages\n");
return Err(dbwrapper_error(&errmsg).to_owned());
}
pub fn get_obfuscate_key<'a>(w: &'a DBWrapper) -> &'a Vec<u8> {
&w.obfuscate_key
}
}
pub struct DBWrapper {
penv: Rc<RefCell<dyn leveldb::Env>>,
options: leveldb::Options,
readoptions: leveldb::ReadOptions,
iteroptions: leveldb::ReadOptions,
writeoptions: leveldb::WriteOptions,
syncoptions: leveldb::WriteOptions,
pdb: Rc<RefCell<dyn leveldb::DB>>,
name: String,
obfuscate_key: Vec<u8>,
}
lazy_static!{
pub static ref OBFUSCATE_KEY_KEY: String = String::from("\000obfuscate_key");
pub static ref OBFUSCATE_KEY_NUM_BYTES: usize = 8;
}
impl Drop for DBWrapper {
fn drop(&mut self) {
todo!();
}
}
impl DBWrapper {
pub fn read<K, V>(&self,
key: &K,
value: &mut V) -> bool {
let mut ss_key: DataStream = DataStream::new(SER_DISK.try_into().unwrap(), CLIENT_VERSION);
ss_key.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ss_key.stream(&key);
let mut sl_key: leveldb::Slice = leveldb::Slice::from_ptr_len(ss_key.data() as *mut u8, ss_key.size());
let mut str_value = String::default();
let status: leveldb::Status = (*self.pdb).borrow_mut().get(&self.readoptions, &sl_key, &mut str_value as *mut String);
if !status.is_ok() {
if status.is_not_found() {
return false;
}
log_printf!("LevelDB read failure: %s\n", status.to_string());
dbwrapper::handle_error(&status);
}
todo!();
}
pub fn write<K, V>(&mut self,
key: &K,
value: &V,
sync: Option<bool>) -> bool {
let sync: bool = sync.unwrap_or(false);
todo!();
}
pub fn exists<K>(&self, key: &K) -> bool {
todo!();
}
pub fn erase<K>(&mut self,
key: &K,
sync: Option<bool>) -> bool {
let sync: bool = sync.unwrap_or(false);
todo!();
}
pub fn new_iterator(&mut self) -> *mut DBIterator {
todo!();
}
pub fn estimate_size<K>(&self,
key_begin: &K,
key_end: &K) -> usize {
todo!();
}
pub fn compact_range<K>(&self,
key_begin: &K,
key_end: &K) {
todo!();
}
pub fn new(
path: &Path,
n_cache_size: usize,
memory: Option<bool>,
wipe: Option<bool>,
obfuscate: Option<bool>) -> Self {
let memory: bool = memory.unwrap_or(false);
let wipe: bool = wipe.unwrap_or(false);
let obfuscate: bool = obfuscate.unwrap_or(false);
todo!();
}
pub fn write_batch(&mut self,
batch: &mut DBBatch,
sync: Option<bool>) -> bool {
let sync: bool = sync.unwrap_or(false);
let log_memory: bool = log_accept_category(LogFlags::LEVELDB);
let mut mem_before: f64 = 0.0;
todo!();
}
pub fn dynamic_memory_usage(&self) -> Option<usize> {
let mut memory = String::default();
let mut parsed: Option<usize> = None;
let log_fail = || {
log_print!(BCLog::LEVELDB, "Failed to get approximate-memory-usage property\n");
};
if !(*self.pdb).borrow_mut().get_property("leveldb.approximate-memory-usage", &mut memory as *mut String)
{
log_fail();
return None;
} else {
let maybe_val = memory.parse::<usize>().ok();
if maybe_val.is_none() {
log_fail();
return None;
}
parsed = maybe_val;
}
parsed
}
pub fn create_obfuscate_key(&self) -> Vec<u8> {
let mut ret: Vec::<u8> = Vec::<u8>::with_capacity(*OBFUSCATE_KEY_NUM_BYTES);
get_rand_bytes(ret.as_mut_slice(), *OBFUSCATE_KEY_NUM_BYTES as i32);
return ret;
}
pub fn is_empty(&mut self) -> bool {
let mut it: Box::<DBIterator> = unsafe { Box::<DBIterator>::from_raw(self.new_iterator()) };
(*it).seek_to_first();
!((*it).valid())
}
}