Skip to content

Commit

Permalink
Merge pull request rust-rocksdb#150 from rrichardson/open-cfs-with-op…
Browse files Browse the repository at this point in the history
…tions

Open cfs with options - fixes merge conflict in rust-rocksdb#136
  • Loading branch information
spacejam authored Feb 10, 2018
2 parents 57f9597 + d065506 commit 5bb1b85
Show file tree
Hide file tree
Showing 6 changed files with 513 additions and 181 deletions.
2 changes: 1 addition & 1 deletion librocksdb-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ libc = "0.2"
const-cstr = "0.2"

[build-dependencies]
cc = { version = "1.0", features = ["parallel"] }
cc = { version = "^1.0", features = ["parallel"] }
make-cmd = "0.1"
bindgen = "0.29"
47 changes: 30 additions & 17 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//


use {DB, Error, Options, WriteOptions, ColumnFamily};
use {DB, Error, Options, WriteOptions, ColumnFamily, ColumnFamilyDescriptor};
use ffi;
use ffi_util::opt_bytes_to_ptr;

Expand Down Expand Up @@ -572,6 +572,16 @@ impl<'a> Drop for Snapshot<'a> {
}
}

impl ColumnFamilyDescriptor {
// Create a new column family descriptor with the specified name and options.
pub fn new<S>(name: S, options: Options) -> Self where S: Into<String> {
ColumnFamilyDescriptor {
name: name.into(),
options
}
}
}

impl DB {
/// Open a database with default options.
pub fn open_default<P: AsRef<Path>>(path: P) -> Result<DB, Error> {
Expand All @@ -585,14 +595,17 @@ impl DB {
DB::open_cf(opts, path, &[])
}

/// Open a database with specified options and column family.
///
/// A column family must be created first by calling `DB::create_cf`.
///
/// # Panics
/// Open a database with the given database options and column family names.
///
/// * Panics if the column family doesn't exist.
/// Column families opened using this function will be created with default `Options`.
pub fn open_cf<P: AsRef<Path>>(opts: &Options, path: P, cfs: &[&str]) -> Result<DB, Error> {
let cfs_v = cfs.to_vec().iter().map(|name| ColumnFamilyDescriptor::new(*name, Options::default())).collect();

DB::open_cf_descriptors(opts, path, cfs_v)
}

/// Open a database with the given database options and column family names/options.
pub fn open_cf_descriptors<P: AsRef<Path>>(opts: &Options, path: P, cfs: Vec<ColumnFamilyDescriptor>) -> Result<DB, Error> {
let path = path.as_ref();
let cpath = match CString::new(path.to_string_lossy().as_bytes()) {
Ok(c) => c,
Expand Down Expand Up @@ -621,28 +634,28 @@ impl DB {
db = ffi_try!(ffi::rocksdb_open(opts.inner, cpath.as_ptr() as *const _,));
}
} else {
let mut cfs_v = cfs.to_vec();
let mut cfs_v = cfs;
// Always open the default column family.
if !cfs_v.contains(&"default") {
cfs_v.push("default");
if !cfs_v.iter().any(|cf| cf.name == "default") {
cfs_v.push(ColumnFamilyDescriptor {
name: String::from("default"),
options: Options::default()
});
}

// We need to store our CStrings in an intermediate vector
// so that their pointers remain valid.
let c_cfs: Vec<CString> = cfs_v
.iter()
.map(|cf| CString::new(cf.as_bytes()).unwrap())
.map(|cf| CString::new(cf.name.as_bytes()).unwrap())
.collect();

let mut cfnames: Vec<_> = c_cfs.iter().map(|cf| cf.as_ptr()).collect();

// These handles will be populated by DB.
let mut cfhandles: Vec<_> = cfs_v.iter().map(|_| ptr::null_mut()).collect();

// TODO(tyler) allow options to be passed in.
let mut cfopts: Vec<_> = cfs_v
.iter()
.map(|_| unsafe { ffi::rocksdb_options_create() as *const _ })
let mut cfopts: Vec<_> = cfs_v.iter()
.map(|cf| cf.options.inner as *const _)
.collect();

unsafe {
Expand All @@ -666,7 +679,7 @@ impl DB {
}

for (n, h) in cfs_v.iter().zip(cfhandles) {
cf_map.insert(n.to_string(), ColumnFamily { inner: h });
cf_map.insert(n.name.clone(), ColumnFamily { inner: h });
}
}

Expand Down
26 changes: 23 additions & 3 deletions src/db_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,25 @@ impl Options {
}
}

/// If true, any column families that didn't exist when opening the database
/// will be created.
///
/// Default: `false`
///
/// # Example
///
/// ```
/// use rocksdb::Options;
///
/// let mut opts = Options::default();
/// opts.create_missing_column_families(true);
/// ```
pub fn create_missing_column_families(&mut self, create_missing_cfs: bool) {
unsafe {
ffi::rocksdb_options_set_create_missing_column_families(self.inner, create_missing_cfs as c_uchar);
}
}

/// Sets the compression algorithm that will be used for the bottommost level that
/// contain files. If level-compaction is used, this option will only affect
/// levels after base level.
Expand Down Expand Up @@ -202,10 +221,11 @@ impl Options {
}
}

pub fn set_merge_operator(&mut self, name: &str, merge_fn: MergeFn) {
pub fn set_merge_operator(&mut self, name: &str, full_merge_fn: MergeFn, partial_merge_fn: Option<MergeFn>) {
let cb = Box::new(MergeOperatorCallback {
name: CString::new(name.as_bytes()).unwrap(),
merge_fn: merge_fn,
full_merge_fn: full_merge_fn,
partial_merge_fn: partial_merge_fn.unwrap_or(full_merge_fn),
});

unsafe {
Expand All @@ -224,7 +244,7 @@ impl Options {
#[deprecated(since = "0.5.0",
note = "add_merge_operator has been renamed to set_merge_operator")]
pub fn add_merge_operator(&mut self, name: &str, merge_fn: MergeFn) {
self.set_merge_operator(name, merge_fn);
self.set_merge_operator(name, merge_fn, None);
}

/// Sets a compaction filter used to determine if entries should be kept, changed,
Expand Down
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@
//! db.delete(b"my key").unwrap();
//! ```
//!
//! Opening a database and a single column family with custom options:
//!
//! ```
//! use rocksdb::{DB, ColumnFamilyDescriptor, Options};
//! let mut cf_opts = Options::default();
//! cf_opts.set_max_write_buffer_number(16);
//! let cf = ColumnFamilyDescriptor::new("cf1", cf_opts);
//!
//! let mut db_opts = Options::default();
//! db_opts.create_missing_column_families(true);
//! db_opts.create_if_missing(true);
//!
//! let db = DB::open_cf_descriptors(&db_opts, "path/for/rocksdb/storage_with_cfs", vec![cf]).unwrap();
//! ```
//!
extern crate libc;
extern crate librocksdb_sys as ffi;
Expand Down Expand Up @@ -64,6 +79,14 @@ pub struct DB {
path: PathBuf,
}

/// A descriptor for a RocksDB column family.
///
/// A description of the column family, containing the name and `Options`.
pub struct ColumnFamilyDescriptor {
name: String,
options: Options,
}

/// A simple wrapper round a string, used for errors reported from
/// ffi calls.
#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -173,6 +196,7 @@ pub struct WriteOptions {
inner: *mut ffi::rocksdb_writeoptions_t,
}


/// An opaque type used to represent a column family. Returned from some functions, and used
/// in others
#[derive(Copy, Clone)]
Expand Down
Loading

0 comments on commit 5bb1b85

Please sign in to comment.