Commit 37e70775 authored by zhangjinpeng1987's avatar zhangjinpeng1987 Committed by Jay

add list_column_families api (#37)

parent a73ebe88
...@@ -432,6 +432,13 @@ extern "C" { ...@@ -432,6 +432,13 @@ extern "C" {
column_family_handle: DBCFHandle, column_family_handle: DBCFHandle,
err: *mut *const i8); err: *mut *const i8);
pub fn rocksdb_column_family_handle_destroy(column_family_handle: DBCFHandle); pub fn rocksdb_column_family_handle_destroy(column_family_handle: DBCFHandle);
pub fn rocksdb_list_column_families(db: *const DBOptions,
path: *const i8,
lencf: *mut size_t,
err: *mut *const i8
) -> *const *const i8;
pub fn rocksdb_list_column_families_destroy(list: *mut *mut i8,
len: size_t);
// Flush options // Flush options
pub fn rocksdb_flushoptions_create() -> DBFlushOptions; pub fn rocksdb_flushoptions_create() -> DBFlushOptions;
......
...@@ -407,6 +407,40 @@ impl DB { ...@@ -407,6 +407,40 @@ impl DB {
Ok(()) Ok(())
} }
pub fn list_column_families(opts: &Options, path: &str) -> Result<Vec<String>, String> {
let cpath = match CString::new(path.as_bytes()) {
Ok(c) => c,
Err(_) => {
return Err("Failed to convert path to CString when list column families".to_owned())
}
};
let mut cfs: Vec<String> = vec![];
unsafe {
let mut lencf: size_t = 0;
let mut err: *const i8 = 0 as *const i8;
let list = rocksdb_ffi::rocksdb_list_column_families(opts.inner,
cpath.as_ptr() as *const _,
&mut lencf,
&mut err);
if !err.is_null() {
return Err(error_message(err));
}
let list_cfs = slice::from_raw_parts(list, lencf);
for cf_name in list_cfs {
let len = libc::strlen(*cf_name);
let cf = match String::from_utf8(slice::from_raw_parts(*cf_name as *const u8, len).to_vec()) {
Ok(s) => s,
Err(_) => return Err("Invalid utf8 bytes".to_owned()),
};
cfs.push(cf);
}
rocksdb_ffi::rocksdb_list_column_families_destroy(list as *mut *mut _, lencf);
}
Ok(cfs)
}
pub fn path(&self) -> &str { pub fn path(&self) -> &str {
&self.path &self.path
} }
...@@ -1218,6 +1252,34 @@ mod test { ...@@ -1218,6 +1252,34 @@ mod test {
let st2 = db.get_property_int(prop_name).unwrap(); let st2 = db.get_property_int(prop_name).unwrap();
assert!(st2 > st1); assert!(st2 > st1);
} }
#[test]
fn list_column_families_test() {
let path = TempDir::new("_rust_rocksdb_list_column_families_test").expect("");
let mut cfs = ["default", "cf1", "cf2", "cf3"];
{
let mut cfs_opts = vec![];
for _ in 0..cfs.len() {
cfs_opts.push(Options::new());
}
let cfs_ref_opts: Vec<&Options> = cfs_opts.iter().collect();
let mut opts = Options::new();
opts.create_if_missing(true);
let mut db = DB::open(&opts, path.path().to_str().unwrap()).unwrap();
for (&cf, &cf_opts) in cfs.iter().zip(&cfs_ref_opts) {
if cf == "default" {
continue;
}
db.create_cf(cf, cf_opts).unwrap();
}
}
let opts_list_cfs = Options::new();
let mut cfs_vec = DB::list_column_families(&opts_list_cfs, path.path().to_str().unwrap()).unwrap();
cfs_vec.sort();
cfs.sort();
assert_eq!(cfs_vec, cfs);
}
} }
#[test] #[test]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment