From 0b11b93cfb785e530751f234bbf94f1a7b68c451 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 26 Mar 2024 06:56:20 -0700 Subject: [PATCH] Add method to set DBOptions from string. Signed-off-by: Jason Volk --- src/db_options.rs | 31 +++++++++++++++++++++++++++++++ tests/test_rocksdb_options.rs | 25 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/db_options.rs b/src/db_options.rs index 3c50698..b2bdd45 100644 --- a/src/db_options.rs +++ b/src/db_options.rs @@ -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::().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(); + } +}