-
Notifications
You must be signed in to change notification settings - Fork 158
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
Support db with ttl open #227
Changes from 2 commits
696a60f
127aa1a
ca382f4
8bec72e
7098da9
749b1fb
20f0860
78d8dcf
c3918a5
59099ea
d827ba7
beb6990
451f64d
be60a5f
987684e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -370,13 +370,27 @@ impl DB { | |
DB::open_cf(opts, path, cfds) | ||
} | ||
|
||
pub fn open_with_ttl(opts: DBOptions, path: &str, ttls: &[i32]) -> Result<DB, String> { | ||
let cfds: Vec<&str> = vec![]; | ||
if ttls.len() > 0 { | ||
DB::open_cf_with_ttl(opts, path, cfds, ttls) | ||
} else { | ||
DB::open_cf(opts, path, cfds) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If someone calls this function without giving There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This condition is redundent here, I will remove it. Thus put all |
||
} | ||
} | ||
|
||
pub fn open_cf<'a, T>(opts: DBOptions, path: &str, cfds: Vec<T>) -> Result<DB, String> | ||
where | ||
T: Into<ColumnFamilyDescriptor<'a>>, | ||
{ | ||
DB::open_cf_internal(opts, path, cfds, None) | ||
DB::open_cf_internal(opts, path, cfds, &[], None) | ||
} | ||
|
||
pub fn open_cf_with_ttl<'a, T>(opts: DBOptions, path: &str, cfds: Vec<T>, ttls: &[i32]) ->Result<DB, String> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I have committed it again. |
||
where | ||
T: Into<ColumnFamilyDescriptor<'a>>, { DB::open_cf_internal(opts, path, cfds, ttls,None) } | ||
|
||
|
||
pub fn open_for_read_only( | ||
opts: DBOptions, | ||
path: &str, | ||
|
@@ -395,13 +409,14 @@ impl DB { | |
where | ||
T: Into<ColumnFamilyDescriptor<'a>>, | ||
{ | ||
DB::open_cf_internal(opts, path, cfds, Some(error_if_log_file_exist)) | ||
DB::open_cf_internal(opts, path, cfds, &[], Some(error_if_log_file_exist)) | ||
} | ||
|
||
fn open_cf_internal<'a, T>( | ||
opts: DBOptions, | ||
path: &str, | ||
cfds: Vec<T>, | ||
ttls: &[i32], | ||
// if none, open for read write mode. | ||
// otherwise, open for read only. | ||
error_if_log_file_exist: Option<bool>, | ||
|
@@ -441,16 +456,23 @@ impl DB { | |
} else { | ||
false | ||
}; | ||
let with_ttl = if ttls.len() > 0 && ttls.len() == cf_names.len() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it's a misuse when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I will fix it. |
||
true | ||
} else { | ||
false | ||
}; | ||
let db = { | ||
let db_options = opts.inner; | ||
let db_path = cpath.as_ptr(); | ||
let db_cfs_count = cf_names.len() as c_int; | ||
let db_cf_ptrs = cf_names.as_ptr(); | ||
let db_cf_opts = cf_options.as_ptr(); | ||
let db_cf_handles = cf_handles.as_ptr(); | ||
if let Some(flag) = error_if_log_file_exist { | ||
unsafe { | ||
ffi_try!(crocksdb_open_for_read_only_column_families( | ||
|
||
if !with_ttl { | ||
if let Some(flag) = error_if_log_file_exist { | ||
unsafe { | ||
ffi_try!(crocksdb_open_for_read_only_column_families( | ||
db_options, | ||
db_path, | ||
db_cfs_count, | ||
|
@@ -459,19 +481,35 @@ impl DB { | |
db_cf_handles, | ||
flag | ||
)) | ||
} | ||
} else { | ||
unsafe { | ||
ffi_try!(crocksdb_open_column_families( | ||
db_options, | ||
db_path, | ||
db_cfs_count, | ||
db_cf_ptrs, | ||
db_cf_opts, | ||
db_cf_handles | ||
)) | ||
} | ||
} | ||
} else { | ||
let ttl_array = ttls.as_ptr() as *const c_int; | ||
unsafe { | ||
ffi_try!(crocksdb_open_column_families( | ||
ffi_try!(crocksdb_open_column_families_with_ttl( | ||
db_options, | ||
db_path, | ||
db_cfs_count, | ||
db_cf_ptrs, | ||
db_cf_opts, | ||
db_cf_handles | ||
db_cf_handles, | ||
ttl_array, | ||
readonly | ||
)) | ||
} | ||
} | ||
|
||
}; | ||
if cf_handles.iter().any(|h| h.is_null()) { | ||
return Err(ERR_NULL_CF_HANDLE.to_owned()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use rocksdb::{ColumnFamilyOptions, DBOptions, Writable, DB}; | ||
use tempdir::TempDir; | ||
|
||
#[test] | ||
pub fn test_ttl() { | ||
let path = TempDir::new("_rust_rocksdb_ttl_test").expect(""); | ||
let path_str = path.path().to_str().unwrap(); | ||
|
||
// should be able to open db with ttl | ||
{ | ||
let mut opts = DBOptions::new(); | ||
let cf_opts = ColumnFamilyOptions::new(); | ||
let ttl = 10; | ||
opts.create_if_missing(true); | ||
// cf_opts.set_num_levels(2); | ||
// assert_eq!(2, cf_opts.get_num_levels()); | ||
let mut db = match DB::open_cf_with_ttl( | ||
opts, | ||
path.path().to_str().unwrap(), | ||
vec![("default", cf_opts)], | ||
&[ttl] | ||
) { | ||
Ok(db) => { | ||
println!("successfully opened db with ttl"); | ||
db | ||
} | ||
Err(e) => panic!("failed to open db with ttl: {}", e), | ||
}; | ||
|
||
match db.create_cf("cf1") { | ||
Ok(_) => println!("cf1 created successfully"), | ||
Err(e) => { | ||
panic!("could not create column family: {}", e); | ||
} | ||
} | ||
assert_eq!(db.cf_names(), vec!["cf1", "default"]); | ||
drop(db); | ||
} | ||
|
||
|
||
// should be able to write, read over a cf with ttl | ||
{ | ||
let cf_opts = ColumnFamilyOptions::new(); | ||
let ttl = 0; | ||
let db = match DB::open_cf_with_ttl(DBOptions::new(), path_str, vec![("cf1", cf_opts)], &[ttl]) { | ||
Ok(db) => { | ||
println!("successfully opened cf with ttl"); | ||
db | ||
} | ||
Err(e) => panic!("failed to open cf with ttl: {}", e), | ||
}; | ||
let cf1 = db.cf_handle("cf1").unwrap(); | ||
assert!(db.put_cf(cf1, b"k1", b"v1").is_ok()); | ||
assert!(db.get_cf(cf1, b"k1").unwrap().unwrap().to_utf8().unwrap() == "v1"); | ||
let p = db.put_cf(cf1, b"k1", b"a"); | ||
assert!(p.is_ok()); | ||
} | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add test for when the length of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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.
Please put
ttl_array
andread_only
before column_family_handles, cause it's a input parameter.FYI: https://google.github.io/styleguide/cppguide.html#Output_Parameters
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.
This parameter order is according to the declaration of Open function in rocksdb/include/rocksdb/utilities/db_ttl.h. If it is better to conform to google cpp style, I will change it.