From f967d3c50fb3207c62f8267e0b19a77331bddd5f Mon Sep 17 00:00:00 2001 From: Marat Safin Date: Mon, 28 Aug 2017 12:09:02 +0300 Subject: [PATCH 1/2] list column family --- src/db.rs | 27 +++++++++++++++++++++++++++ tests/test_column_family.rs | 11 +++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/db.rs b/src/db.rs index 3067928..9e53be7 100644 --- a/src/db.rs +++ b/src/db.rs @@ -659,6 +659,33 @@ impl DB { }) } + pub fn list_cf>(opts: &Options, path: P) -> Result, Error> { + let path = path.as_ref(); + let cpath = match CString::new(path.to_string_lossy().as_bytes()) { + Ok(c) => c, + Err(_) => { + return Err(Error::new("Failed to convert path to CString \ + when opening DB." + .to_owned())) + } + }; + + let mut length = 0; + let vec; + + unsafe { + let ptr = ffi_try!(ffi::rocksdb_list_column_families(opts.inner, + cpath.as_ptr() as *const _, + &mut length)); + + vec = Vec::from_raw_parts(ptr, length, length).iter().map(|&ptr| { + CString::from_raw(ptr).into_string().unwrap() + }).collect(); + } + Ok(vec) + } + + pub fn destroy>(opts: &Options, path: P) -> Result<(), Error> { let cpath = CString::new(path.as_ref().to_string_lossy().as_bytes()).unwrap(); unsafe { diff --git a/tests/test_column_family.rs b/tests/test_column_family.rs index 8644aaa..02d9220 100644 --- a/tests/test_column_family.rs +++ b/tests/test_column_family.rs @@ -62,6 +62,17 @@ pub fn test_column_family() { Err(e) => panic!("failed to open db with column family: {}", e), } } + + // should be able to list a cf + { + let opts = Options::default(); + let vec = DB::list_cf(&opts, path); + match vec { + Ok(vec) => println!("list cf: {:?}", vec), + Err(e) => panic!("failed to drop column family: {}", e), + } + } + // TODO should be able to use writebatch ops with a cf { } From 56e5829b89b96f7ff35143d07120426628ddb8cd Mon Sep 17 00:00:00 2001 From: Marat Safin Date: Fri, 8 Sep 2017 18:56:06 +0300 Subject: [PATCH 2/2] use assert_eq! in test and return vec from unsafe --- src/db.rs | 5 ++--- tests/test_column_family.rs | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/db.rs b/src/db.rs index 9e53be7..d73ff63 100644 --- a/src/db.rs +++ b/src/db.rs @@ -671,18 +671,17 @@ impl DB { }; let mut length = 0; - let vec; unsafe { let ptr = ffi_try!(ffi::rocksdb_list_column_families(opts.inner, cpath.as_ptr() as *const _, &mut length)); - vec = Vec::from_raw_parts(ptr, length, length).iter().map(|&ptr| { + let vec = Vec::from_raw_parts(ptr, length, length).iter().map(|&ptr| { CString::from_raw(ptr).into_string().unwrap() }).collect(); + Ok(vec) } - Ok(vec) } diff --git a/tests/test_column_family.rs b/tests/test_column_family.rs index 02d9220..9795b78 100644 --- a/tests/test_column_family.rs +++ b/tests/test_column_family.rs @@ -68,7 +68,7 @@ pub fn test_column_family() { let opts = Options::default(); let vec = DB::list_cf(&opts, path); match vec { - Ok(vec) => println!("list cf: {:?}", vec), + Ok(vec) => assert_eq!(vec, vec!["default", "cf1"]), Err(e) => panic!("failed to drop column family: {}", e), } }