leveldb/database/
management.rs

1use super::options::{Options, c_options};
2use super::error::Error;
3use std::ffi::CString;
4use std::path::Path;
5use std::ptr;
6use libc::c_char;
7
8use leveldb_sys::{leveldb_destroy_db, leveldb_repair_db};
9
10/// destroy a database. You shouldn't hold a handle on the database anywhere at that time.
11pub fn destroy(name: &Path, options: &Options) -> Result<(), Error> {
12    let mut error = ptr::null_mut();
13    unsafe {
14        let c_string = CString::new(name.to_str().unwrap()).unwrap();
15        let c_options = c_options(options, None);
16        leveldb_destroy_db(c_options,
17                           c_string.as_bytes_with_nul().as_ptr() as *const c_char,
18                           &mut error);
19
20        if error == ptr::null_mut() {
21            Ok(())
22        } else {
23            Err(Error::new_from_char(error))
24        }
25    }
26}
27
28/// repair the database. The database should be closed at this moment.
29pub fn repair(name: &Path, options: &Options) -> Result<(), Error> {
30    let mut error = ptr::null_mut();
31    unsafe {
32        let c_string = CString::new(name.to_str().unwrap()).unwrap();
33        let c_options = c_options(options, None);
34        leveldb_repair_db(c_options,
35                          c_string.as_bytes_with_nul().as_ptr() as *const c_char,
36                          &mut error);
37
38        if error == ptr::null_mut() {
39            Ok(())
40        } else {
41            Err(Error::new_from_char(error))
42        }
43    }
44}
45