-
Notifications
You must be signed in to change notification settings - Fork 718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for opening a column family with options #136
Add support for opening a column family with options #136
Conversation
Adds a new `DB::open_cf_descriptors` method that allows passing in Options for each column family. A new `ColumnFamilyDescriptor` type was added to contain the congfiguration for each column family. This fixes an issue where a column family was created with non-default options, and then fails to re-open due to a config mismatch.
9838eff
to
9afa195
Compare
This change is similar to #130. I might even like this one better. Though I would make it a breaking change and change |
Apologies @vmx, I completely missed your open pull request or I'd have pulled that instead! I'd be happy to update this to be a breaking change if you'd rather go with this PR over your own :). |
It would be great to get feedback from others, especially @spacejam on which approach he prefers and if he agrees with me that this should be a breaking change or not. |
I like coupling column family identifiers to their options, reducing the chance of messing up the order by users. I see it being relatively inexpensive in maintenance costs to keep |
|
||
assert!(DB::destroy(&Options::default(), path).is_ok()); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a test exercising the new open_cf_descriptors
method?
@@ -63,6 +63,11 @@ pub struct DB { | |||
path: PathBuf, | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a short comment describing this public struct?
src/db.rs
Outdated
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to use ColumnFamilyDescriptor::new(name, Options::defaut())
instead?
@spacejam My point about making this one the So I'm in favour for keeping the API as small as possible, especially since we are before 1.0. |
I've made the requested changes and added some crate level documentation on With regards to folding pub fn open_cf<P, C>(opts: &Options, path: P, cfs: &[C]) -> Result<DB, Error>
where P: AsRef<Path>, C: Into<ColumnFamilyDescriptor> {}
impl From<&str> for ColumnFamilyDescriptor {
fn from(name: &str) -> Self {
ColumnFamilyDescriptor::new(name, Options::default())
}
} Though, I think in most cases when you want to open a |
@garyttierney I like the idea of using an automatic conversion to support the new use case while preserving backwards compatibility, and reduce API bloat. The implementation may require a couple more lifetimes, but it's feasible. @vmx what do you think about this approach? |
That sounds like a good solution, I wasn't even aware that things like that are possible with Rust. |
Awesome! That feels like a good way forward to me at this point. Great idea, @garyttierney ! Would you mind implementing When playing around with the idea I made this quick PoC of migrating a |
@spacejam yep, happy to go ahead and implement this :-) I'll get some changes pushed later on tonight for review. |
@garyttierney Can I help somehow with this pull request? If yes, tell me which part is missing and I'll be working on it. |
@vmx sorry for the delay. I got a bit stuck keeping backwards compatibility for the Currently The solution there might be to accept pub fn open_cf<P, C>(opts: &Options, path: P, cfs: &[C]) -> Result<DB, Error>
where P: AsRef<Path>, C: Into<ColumnFamilyDescriptor>
{
for cf in cfs.into_iter() { // cf = &ColumnFamilyDescriptor, need ColumnFamilyDescriptor
}
} I'm relatively new to Rust, so if you have any ideas there I'd appreciate the help. Thanks! |
I'm still trying to get this working and I think I'll get this working. Though I really wonder if it's worth it. I still don't see the point of the original API, I think if you use Column Families, the usual case is to have custom options. I would make it a breaking change with a note in the README saying:
Or opening a bug with the exact error message and then posting this line so you can find it if you hit the problem. |
Here's the code I came up with. The problem is that it needs to clone the elements of the colum family slice even if it is of type diff --git a/src/db.rs b/src/db.rs
index 6660863..f9c3995 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -569,6 +569,12 @@ impl ColumnFamilyDescriptor {
}
}
+impl<'a> From<&'a str> for ColumnFamilyDescriptor {
+ fn from(name: &'a str) -> ColumnFamilyDescriptor {
+ ColumnFamilyDescriptor::new(name, Options::default())
+ }
+}
+
impl DB {
/// Open a database with default options.
pub fn open_default<P: AsRef<Path>>(path: P) -> Result<DB, Error> {
@@ -579,14 +585,16 @@ impl DB {
/// Open the database with the specified options.
pub fn open<P: AsRef<Path>>(opts: &Options, path: P) -> Result<DB, Error> {
- DB::open_cf(opts, path, &[])
+ let empty: &[&str] = &[];
+ DB::open_cf(opts, path, empty)
}
/// 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<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();
+ pub fn open_cf<P, C>(opts: &Options, path: P, cfs: &[C]) -> Result<DB, Error>
+ where P: AsRef<Path>, C: Clone + Into<ColumnFamilyDescriptor> {
+ let cfs_v = cfs.into_iter().map(|cf| cf.clone().into()).collect();
DB::open_cf_descriptors(opts, path, cfs_v)
}
diff --git a/src/lib.rs b/src/lib.rs
index 788e97a..07c947b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -81,6 +81,7 @@ pub struct DB {
/// A descriptor for a RocksDB column family.
///
/// A description of the column family, containing the name and `Options`.
+#[derive(Clone)]
pub struct ColumnFamilyDescriptor {
name: String,
options: Options,
@@ -165,6 +166,7 @@ pub struct BlockBasedOptions {
/// DB::open(&opts, path).unwrap()
/// }
/// ```
+#[derive(Clone)]
pub struct Options {
inner: *mut ffi::rocksdb_options_t,
} |
Although the above code compiles, it doesn't work (it segfaults), probably due to the cloning of the I think all boils down to that |
I've create a version of |
I've spent another few hours. This time I come to the conclusion that converting a reference to another one — in our case a |
This pull request relates to the discussions at #145. |
Open cfs with options - fixes merge conflict in #136
crate version |
Update rocksdb with the following changes: ``` cb7efe6d7 2020-01-08 zbk602423539@gmail.. Add oldest snapshot sequence property (#6228) (rust-rocksdb#141) c7d775168 2020-01-07 zbk602423539@gmail.. statistics: make ticker and histogram extendible (rust-rocksdb#136) ebec1bd8a 2020-01-07 bupt2013211450@gma.. add multibatch write into memtable (rust-rocksdb#131) 4dcfb8789 2020-01-07 zbk602423539@gmail.. fix invalid register for .seh_savexmm (rust-rocksdb#140) ``` Signed-off-by: Yi Wu <yiwu@pingcap.com>
Update rocksdb with the following changes: ``` cb7efe6d7 2020-01-08 zbk602423539@gmail.. Add oldest snapshot sequence property (#6228) (rust-rocksdb#141) c7d775168 2020-01-07 zbk602423539@gmail.. statistics: make ticker and histogram extendible (rust-rocksdb#136) ebec1bd8a 2020-01-07 bupt2013211450@gma.. add multibatch write into memtable (rust-rocksdb#131) 4dcfb8789 2020-01-07 zbk602423539@gmail.. fix invalid register for .seh_savexmm (rust-rocksdb#140) ``` Signed-off-by: Yi Wu <yiwu@pingcap.com>
I've hit an issue when I create a column family with a custom compare function and then try to
re-open the same database. Since I can't pass in options to
open_cf
, my existing column familesare opened with the default options and I get an error.
This adds a new
DB::open_cf_descriptors
method that allows passing in Options foreach column family, and a new
ColumnFamilyDescriptor
type was added to containthe congfiguration for each column family.