Skip to content

Commit

Permalink
Add method to set DBOptions from string.
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Volk <[email protected]>
  • Loading branch information
jevolk committed Mar 28, 2024
1 parent 2516edd commit 56f0bce
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/db_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,35 @@ impl Options {
column_descriptors
}

/// Updates DBOptions with values parsed from a string.
///
/// https://github.com/facebook/rocksdb/wiki/Option-String-and-Option-Map#option-string
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();
}
}

0 comments on commit 56f0bce

Please sign in to comment.