use std::path::Path;
use rusty_leveldb::{LdbIterator, Options, DB};
use spinners::Spinner;
pub fn create(path: impl AsRef<Path>, spinner: &mut Option<Spinner>) {
tracing::debug!("Creating db \"{}\"", path.as_ref().to_string_lossy());
let open_options = Options {
create_if_missing: true,
..Default::default()
};
let mut db = DB::open(path.as_ref(), open_options).unwrap();
db.put(b"key", b"value").unwrap();
db.flush().unwrap();
let mut iter = match db.new_iter() {
Ok(iter) => iter,
Err(e) => {
tracing::error!("Failed to create leveldb iterator: {}", e);
return;
}
};
if let Some(sp) = spinner {
sp.stop();
println!();
}
*spinner = None;
if iter.next().is_none() {
tracing::warn!("Error: Database is empty");
}
iter.reset();
while let Some((k, v)) = iter.next() {
let key = String::from_utf8_lossy(k.as_slice());
let value = String::from_utf8_lossy(v.as_slice());
println!("Key: {}, Value: {}", key, value);
}
}