From d82d5c6b77ab15dbeb504d8318ab055f7292ab23 Mon Sep 17 00:00:00 2001 From: Martin Ek Date: Wed, 2 May 2018 23:04:53 +0200 Subject: [PATCH 1/6] Add plain table format --- src/db_options.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/db_options.rs b/src/db_options.rs index 925eb5a..d4d2fc1 100644 --- a/src/db_options.rs +++ b/src/db_options.rs @@ -982,6 +982,26 @@ impl Options { } } + // See https://github.com/facebook/rocksdb/wiki/PlainTable-Format. + // + // Defaults: + // user_key_len: 0 (variable length) + // bloom_bits_per_key: 10 + // has_table_ratio: 0.75 + // index_sparseness: 16 + pub fn set_plain_table_factory(&mut self, user_key_len: u32, bloom_bits_per_key: i32, + hash_table_ratio: f64, index_sparseness: usize) { + unsafe { + ffi::rocksdb_options_set_plain_table_factory( + self.inner, + user_key_len, + bloom_bits_per_key, + hash_table_ratio, + index_sparseness, + ); + } + } + /// Measure IO stats in compactions and flushes, if `true`. /// /// Default: `false` From b01acb71a97f3ef809e15ce5c8082f92b7c28b0e Mon Sep 17 00:00:00 2001 From: Martin Ek Date: Sun, 27 Jan 2019 22:17:03 +0000 Subject: [PATCH 2/6] Use an options struct --- src/db_options.rs | 35 +++++++++++++++++++++-------------- src/lib.rs | 15 +++++++++++++++ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/db_options.rs b/src/db_options.rs index d4d2fc1..785c831 100644 --- a/src/db_options.rs +++ b/src/db_options.rs @@ -27,7 +27,7 @@ use merge_operator::{ use slice_transform::SliceTransform; use { BlockBasedIndexType, BlockBasedOptions, DBCompactionStyle, DBCompressionType, DBRecoveryMode, - MemtableFactory, Options, WriteOptions, + MemtableFactory, Options, PlainTableFactoryOptions, WriteOptions, }; pub fn new_cache(capacity: size_t) -> *mut ffi::rocksdb_cache_t { @@ -982,22 +982,29 @@ impl Options { } } - // See https://github.com/facebook/rocksdb/wiki/PlainTable-Format. - // - // Defaults: - // user_key_len: 0 (variable length) - // bloom_bits_per_key: 10 - // has_table_ratio: 0.75 - // index_sparseness: 16 - pub fn set_plain_table_factory(&mut self, user_key_len: u32, bloom_bits_per_key: i32, - hash_table_ratio: f64, index_sparseness: usize) { + /// See https://github.com/facebook/rocksdb/wiki/PlainTable-Format. + /// + /// ``` + /// use rocksdb::{Options, PlainTableFactoryOptions}; + /// + /// let mut opts = Options::default(); + /// let factory_opts = PlainTableFactoryOptions { + /// user_key_length: 0, + /// bloom_bits_per_key: 20, + /// hash_table_ratio: 0.75, + /// index_sparseness: 16, + /// }; + /// + /// opts.set_plain_table_factory(factory_opts); + /// ``` + pub fn set_plain_table_factory(&mut self, options: PlainTableFactoryOptions) { unsafe { ffi::rocksdb_options_set_plain_table_factory( self.inner, - user_key_len, - bloom_bits_per_key, - hash_table_ratio, - index_sparseness, + options.user_key_length, + options.bloom_bits_per_key, + options.hash_table_ratio, + options.index_sparseness, ); } } diff --git a/src/lib.rs b/src/lib.rs index cb46114..8dbd1bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -167,6 +167,21 @@ pub enum MemtableFactory { }, } +/// Used with DBOptions::set_plain_table_factory. +/// See https://github.com/facebook/rocksdb/wiki/PlainTable-Format. +/// +/// Defaults: +/// user_key_length: 0 (variable length) +/// bloom_bits_per_key: 10 +/// hash_table_ratio: 0.75 +/// index_sparseness: 16 +pub struct PlainTableFactoryOptions { + pub user_key_length: u32, + pub bloom_bits_per_key: i32, + pub hash_table_ratio: f64, + pub index_sparseness: usize, +} + /// Database-wide options around performance and behavior. /// /// Please read [the official tuning guide](https://github.com/facebook/rocksdb/wiki/RocksDB-Tuning-Guide), and most importantly, measure performance under realistic workloads with realistic hardware. From d00e03f72a5f9fe34abefba102e6dd61fb76a69a Mon Sep 17 00:00:00 2001 From: Jordan Terrell Date: Mon, 4 Feb 2019 06:28:12 -0600 Subject: [PATCH 3/6] Using generic types to specify column families when opening database... --- src/db.rs | 32 +++++++++++++++++++++----------- tests/test_column_family.rs | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/db.rs b/src/db.rs index 274e483..bed2769 100644 --- a/src/db.rs +++ b/src/db.rs @@ -22,6 +22,7 @@ use std::collections::BTreeMap; use std::ffi::{CStr, CString}; use std::fmt; use std::fs; +use std::iter; use std::marker::PhantomData; use std::ops::Deref; use std::path::Path; @@ -660,28 +661,37 @@ impl DB { /// Open the database with the specified options. pub fn open>(opts: &Options, path: P) -> Result { - DB::open_cf(opts, path, &[]) + DB::open_cf(opts, path, iter::empty::<&str>()) } /// Open a database with the given database options and column family names. /// /// Column families opened using this function will be created with default `Options`. - pub fn open_cf>(opts: &Options, path: P, cfs: &[&str]) -> Result { - let cfs_v = cfs - .to_vec() - .iter() - .map(|name| ColumnFamilyDescriptor::new(*name, Options::default())) - .collect(); + pub fn open_cf(opts: &Options, path: P, cfs: I) -> Result + where P: AsRef, + I: IntoIterator, + N: AsRef { + + let cfs = cfs + .into_iter() + .map(|name| ColumnFamilyDescriptor::new(name.as_ref(), Options::default())); - DB::open_cf_descriptors(opts, path, cfs_v) + DB::open_cf_descriptors(opts, path, cfs) } /// Open a database with the given database options and column family names/options. - pub fn open_cf_descriptors>( + pub fn open_cf_descriptors( opts: &Options, path: P, - cfs: Vec, - ) -> Result { + cfs: I, + ) -> Result + where P: AsRef, + I: IntoIterator { + + let cfs: Vec<_> = cfs + .into_iter() + .collect(); + let path = path.as_ref(); let cpath = match CString::new(path.to_string_lossy().as_bytes()) { Ok(c) => c, diff --git a/tests/test_column_family.rs b/tests/test_column_family.rs index bdd4eec..b7bbef6 100644 --- a/tests/test_column_family.rs +++ b/tests/test_column_family.rs @@ -19,7 +19,7 @@ use rocksdb::{ColumnFamilyDescriptor, MergeOperands, Options, DB}; use util::DBPath; #[test] -pub fn test_column_family() { +fn test_column_family() { let n = DBPath::new("_rust_rocksdb_cftest"); // should be able to create column families @@ -88,6 +88,35 @@ pub fn test_column_family() { } } +#[test] +fn test_can_open_db_with_results_of_list_cf() { + // Test scenario derived from GitHub issue #175 and 177 + + let n = DBPath::new("_rust_rocksdb_cftest_with_list_cf"); + + { + let mut opts = Options::default(); + opts.create_if_missing(true); + let db = DB::open(&opts, &n).unwrap(); + let opts = Options::default(); + + match db.create_cf("cf1", &opts) { + Ok(_db) => println!("cf1 created successfully"), + Err(e) => { + panic!("could not create column family: {}", e); + } + } + } + + { + let options = Options::default(); + let cfs = DB::list_cf(&options, &n).unwrap(); + let db = DB::open_cf(&options, &n, &cfs).unwrap(); + + assert!(db.cf_handle("cf1").is_some()); + } +} + #[test] fn test_create_missing_column_family() { let n = DBPath::new("_rust_rocksdb_missing_cftest"); @@ -172,7 +201,7 @@ fn test_provided_merge( } #[test] -pub fn test_column_family_with_options() { +fn test_column_family_with_options() { let n = DBPath::new("_rust_rocksdb_cf_with_optionstest"); { let mut cfopts = Options::default(); From ceb5744c51d83d256d94bb317ef91fa13ffde9ce Mon Sep 17 00:00:00 2001 From: Jordan Terrell Date: Mon, 4 Feb 2019 09:42:17 -0600 Subject: [PATCH 4/6] Implementing review feedback... --- src/db.rs | 2 +- tests/test_column_family.rs | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/db.rs b/src/db.rs index bed2769..4f155ff 100644 --- a/src/db.rs +++ b/src/db.rs @@ -679,7 +679,7 @@ impl DB { DB::open_cf_descriptors(opts, path, cfs) } - /// Open a database with the given database options and column family names/options. + /// Open a database with the given database options and column family descriptors. pub fn open_cf_descriptors( opts: &Options, path: P, diff --git a/tests/test_column_family.rs b/tests/test_column_family.rs index b7bbef6..c9c44f2 100644 --- a/tests/test_column_family.rs +++ b/tests/test_column_family.rs @@ -100,12 +100,7 @@ fn test_can_open_db_with_results_of_list_cf() { let db = DB::open(&opts, &n).unwrap(); let opts = Options::default(); - match db.create_cf("cf1", &opts) { - Ok(_db) => println!("cf1 created successfully"), - Err(e) => { - panic!("could not create column family: {}", e); - } - } + assert!(db.create_cf("cf1", &opts).is_ok()); } { From 4ff418ab43227f53b09f94908f3efb4021c7de09 Mon Sep 17 00:00:00 2001 From: Martin Ek Date: Mon, 4 Feb 2019 22:19:11 +0000 Subject: [PATCH 5/6] Pass options as immutable borrow --- src/db_options.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/db_options.rs b/src/db_options.rs index 785c831..0454d41 100644 --- a/src/db_options.rs +++ b/src/db_options.rs @@ -995,9 +995,9 @@ impl Options { /// index_sparseness: 16, /// }; /// - /// opts.set_plain_table_factory(factory_opts); + /// opts.set_plain_table_factory(&factory_opts); /// ``` - pub fn set_plain_table_factory(&mut self, options: PlainTableFactoryOptions) { + pub fn set_plain_table_factory(&mut self, options: &PlainTableFactoryOptions) { unsafe { ffi::rocksdb_options_set_plain_table_factory( self.inner, From 48c9e03e076d508a2b99d3a32bf22142773367fd Mon Sep 17 00:00:00 2001 From: Jordan Terrell Date: Mon, 4 Feb 2019 17:02:06 -0600 Subject: [PATCH 6/6] Using None instead of iter::empty for open_cf function... --- src/db.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/db.rs b/src/db.rs index 4f155ff..f100158 100644 --- a/src/db.rs +++ b/src/db.rs @@ -22,7 +22,6 @@ use std::collections::BTreeMap; use std::ffi::{CStr, CString}; use std::fmt; use std::fs; -use std::iter; use std::marker::PhantomData; use std::ops::Deref; use std::path::Path; @@ -661,7 +660,7 @@ impl DB { /// Open the database with the specified options. pub fn open>(opts: &Options, path: P) -> Result { - DB::open_cf(opts, path, iter::empty::<&str>()) + DB::open_cf(opts, path, None::<&str>) } /// Open a database with the given database options and column family names.