Commit 07d44ea8 authored by disksing's avatar disksing

update open_cf(), add cf_names().

parent 97993162
......@@ -157,7 +157,7 @@ impl<'a> DBIterator<'a> {
unsafe { rocksdb_ffi::rocksdb_iter_valid(self.inner) }
}
fn new_cf(db: &'a DB,
pub fn new_cf(db: &'a DB,
cf_handle: DBCFHandle,
readopts: &ReadOptions)
-> DBIterator<'a> {
......@@ -166,7 +166,6 @@ impl<'a> DBIterator<'a> {
rocksdb_ffi::rocksdb_create_iterator_cf(db.inner,
readopts.inner,
cf_handle);
DBIterator {
db: db,
inner: iterator,
......@@ -289,12 +288,13 @@ impl DB {
}
pub fn open(opts: &Options, path: &str) -> Result<DB, String> {
DB::open_cf(opts, path, &[])
DB::open_cf(opts, path, &[], &[])
}
pub fn open_cf(opts: &Options,
path: &str,
cfs: &[&str])
cfs: &[&str],
cf_opts: &[&Options])
-> Result<DB, String> {
let cpath = match CString::new(path.as_bytes()) {
Ok(c) => c,
......@@ -304,33 +304,23 @@ impl DB {
.to_owned())
}
};
let cpath_ptr = cpath.as_ptr();
let ospath = Path::new(path);
if let Err(e) = fs::create_dir_all(&ospath) {
if let Err(e) = fs::create_dir_all(&Path::new(path)) {
return Err(format!("Failed to create rocksdb directory: \
src/rocksdb.rs: \
{:?}",
e));
}
let mut err: *const i8 = 0 as *const i8;
let err_ptr: *mut *const i8 = &mut err;
let db: rocksdb_ffi::DBInstance;
let mut cf_map = BTreeMap::new();
if cfs.len() == 0 {
unsafe {
db = rocksdb_ffi::rocksdb_open(opts.inner,
cpath_ptr as *const _,
err_ptr);
if cfs.len() != cf_opts.len() {
return Err(format!("cfs.len() and cf_opts.len() not match."));
}
} else {
let mut cfs_v = cfs.to_vec();
let mut cf_opts_v = cf_opts.to_vec();
// Always open the default column family
if !cfs_v.contains(&DEFAULT_COLUMN_FAMILY) {
cfs_v.push(DEFAULT_COLUMN_FAMILY);
cf_opts_v.push(opts);
}
// We need to store our CStrings in an intermediate vector
......@@ -348,20 +338,23 @@ impl DB {
.map(|_| rocksdb_ffi::DBCFHandle(0 as *mut c_void))
.collect();
// TODO(tyler) allow options to be passed in.
let cfopts: Vec<rocksdb_ffi::DBOptions> = cfs_v.iter()
.map(|_| unsafe { rocksdb_ffi::rocksdb_options_create() })
.collect();
let cfopts: Vec<rocksdb_ffi::DBOptions> =
cf_opts_v.iter().map(|x| x.inner).collect();
// Prepare to ship to C.
let cfopts_ptr: *const rocksdb_ffi::DBOptions = cfopts.as_ptr();
let handles: *const rocksdb_ffi::DBCFHandle = cfhandles.as_ptr();
let nfam = cfs_v.len();
let db: rocksdb_ffi::DBInstance;
let mut err: *const i8 = 0 as *const i8;
let err_ptr: *mut *const i8 = &mut err;
unsafe {
db = rocksdb_ffi::rocksdb_open_column_families(opts.inner, cpath_ptr as *const _,
nfam as c_int,
db = rocksdb_ffi::rocksdb_open_column_families(opts.inner,
cpath.as_ptr() as *const _,
cfs_v.len() as c_int,
cfnames.as_ptr() as *const _,
cfopts_ptr, handles, err_ptr);
cfopts.as_ptr(),
cfhandles.as_ptr(),
err_ptr);
}
if !err.is_null() {
return Err(error_message(err));
}
for handle in &cfhandles {
......@@ -371,14 +364,11 @@ impl DB {
}
}
let mut cf_map = BTreeMap::new();
for (n, h) in cfs_v.iter().zip(cfhandles) {
cf_map.insert((*n).to_owned(), h);
}
}
if !err.is_null() {
return Err(error_message(err));
}
if db.0.is_null() {
return Err("Could not initialize database.".to_owned());
}
......@@ -593,6 +583,11 @@ impl DB {
self.cfs.get(name)
}
/// get all column family names, without 'default'.
pub fn cf_names(&self) -> Vec<&str> {
self.cfs.iter().map(|(k, _)| k.as_str()).collect()
}
pub fn iter(&self) -> DBIterator {
let opts = ReadOptions::new();
self.iter_opt(&opts)
......
......@@ -33,6 +33,7 @@ pub fn test_column_family() {
panic!("could not create column family: {}", e);
}
}
assert_eq!(db.cf_names(), vec!["cf1", "default"]);
}
// should fail to open db without specifying same column families
......@@ -56,7 +57,7 @@ pub fn test_column_family() {
{
let mut opts = Options::new();
opts.add_merge_operator("test operator", test_provided_merge);
match DB::open_cf(&opts, path_str, &["cf1"]) {
match DB::open_cf(&opts, path_str, &["cf1"], &[&opts]) {
Ok(_) => println!("successfully opened db with column family"),
Err(e) => panic!("failed to open db with column family: {}", e),
}
......@@ -65,7 +66,7 @@ pub fn test_column_family() {
{
let mut opts = Options::new();
opts.add_merge_operator("test operator", test_provided_merge);
let db = match DB::open_cf(&opts, path_str, &["cf1"]) {
let db = match DB::open_cf(&opts, path_str, &["cf1"], &[&opts]) {
Ok(db) => {
println!("successfully opened db with column family");
db
......@@ -113,7 +114,11 @@ pub fn test_column_family() {
}
// should b able to drop a cf
{
let mut db = DB::open_cf(&Options::new(), path_str, &["cf1"]).unwrap();
let mut db = DB::open_cf(&Options::new(),
path_str,
&["cf1"],
&[&Options::new()])
.unwrap();
match db.drop_cf("cf1") {
Ok(_) => println!("cf1 successfully dropped."),
Err(e) => panic!("failed to drop column family: {}", e),
......
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