Skip to content
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 method to set DBOptions from string. #47

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/db_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,37 @@ impl Options {
column_descriptors
}

/// Updates DBOptions with values parsed from a string.
///
/// See official [wiki](
/// https://github.com/facebook/rocksdb/wiki/Option-String-and-Option-Map#option-string)
/// for more information.
pub fn set_options_from_string(&mut self, string: impl CStrLike) -> Result<&mut Self, Error> {
let c_string = string.into_c_string().unwrap();
let mut err_p: *mut i8 = std::ptr::null::<i8>().cast_mut();
unsafe {
ffi::rocksdb_get_options_from_string(
self.inner,
c_string.as_ptr(),
self.inner,
&mut err_p,
);
}

if err_p.is_null() {
Ok(self)
} else {
let err: String;
unsafe {
err = CStr::from_ptr(err_p).to_str().unwrap().to_owned();
ffi::rocksdb_free(err_p as *mut c_void);
}
Err(Error::new(format!(
"Could not set options from string: {err}",
)))
}
}

/// By default, RocksDB uses only one background thread for flush and
/// compaction. Calling this function will set it up such that total of
/// `total_threads` is used. Good value for `total_threads` is the number of
Expand Down
25 changes: 25 additions & 0 deletions tests/test_rocksdb_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,28 @@ fn test_set_ttl() {
let _db = DB::open(&opts, &path).unwrap();
}
}

#[test]
fn test_set_options_from_string() {
let path = DBPath::new("_set_options_from_string");
{
let mut opts = Options::default();
opts.create_if_missing(true);
opts.set_options_from_string("compaction_pri=kOldestSmallestSeqFirst")
.expect("option compaction_pri is set");
let _db = DB::open(&opts, &path).unwrap();
}
}

#[test]
#[should_panic(expected = "Could not set options from string: ")]
fn test_set_options_from_string_failure() {
let path = DBPath::new("_set_options_from_string");
{
let mut opts = Options::default();
opts.create_if_missing(true);
opts.set_options_from_string("compaction_prikOldestSmallestSeqFirst")
.unwrap();
let _db = DB::open(&opts, &path).unwrap();
}
}
Loading