use leveldb::database::Database;
use leveldb::database::Slice;
use leveldb::database::kv::KV;
use leveldb::options::{Options, WriteOptions};
use std::path::Path;
use tempdir::TempDir;

pub fn open_database(path: &Path, create_if_missing: bool) -> Database {
    let mut opts = Options::new();
    opts.create_if_missing = create_if_missing;
    match Database::open(path, opts) {
        Ok(db) => db,
        Err(e) => {
            panic!("failed to open database: {:?}", e)
        }
    }
}

pub fn tmpdir(name: &str) -> TempDir {
    TempDir::new(name).unwrap()
}

pub fn db_put_simple(database: &Database, key: &[u8], val: &[u8]) {
    let write_opts = WriteOptions::new();
    let key_slice = Slice::from(key);
    let val_slice = Slice::from(val);
    match database.put(write_opts, key_slice, val_slice) {
        Ok(_) => (),
        Err(e) => {
            panic!("failed to write to database: {:?}", e)
        }
    }
}
