diff --git a/src/db_options.rs b/src/db_options.rs index 3c50698..59c08be 100644 --- a/src/db_options.rs +++ b/src/db_options.rs @@ -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::().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 diff --git a/tests/test_rocksdb_options.rs b/tests/test_rocksdb_options.rs index 85e4e8f..19abb5c 100644 --- a/tests/test_rocksdb_options.rs +++ b/tests/test_rocksdb_options.rs @@ -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(); + } +}