From dfeda6ebc6fc82718e46e39cf7e37ada790dd30a Mon Sep 17 00:00:00 2001 From: Stanislav Tkach Date: Sat, 1 May 2021 00:02:23 +0300 Subject: [PATCH 1/3] Add 'key_may_exist' functions family --- src/db.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/test_db.rs | 33 ++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/src/db.rs b/src/db.rs index ae7f595..a785de3 100644 --- a/src/db.rs +++ b/src/db.rs @@ -844,6 +844,62 @@ impl DBWithThreadMode { Ok(convert_values(values, values_sizes)) } + /// Returns `false` if the given key definitely doesn't exist in the database, otherwise returns + /// `false`. This function uses default `ReadOptions`. + pub fn key_may_exist>(&self, key: K) -> bool { + self.key_may_exist_opt(key, &ReadOptions::default()) + } + + /// Returns `false` if the given key definitely doesn't exist in the database, otherwise returns + /// `false`. + pub fn key_may_exist_opt>(&self, key: K, readopts: &ReadOptions) -> bool { + let key = key.as_ref(); + unsafe { + 0 != ffi::rocksdb_key_may_exist( + self.inner, + readopts.inner, + key.as_ptr() as *const c_char, + key.len() as size_t, + ptr::null_mut(), /*value*/ + ptr::null_mut(), /*val_len*/ + ptr::null(), /*timestamp*/ + 0, /*timestamp_len*/ + ptr::null_mut(), /*value_found*/ + ) + } + } + + /// Returns `false` if the given key definitely doesn't exist in the specified column family, + /// otherwise returns `false`. This function uses default `ReadOptions`. + pub fn key_may_exist_cf>(&self, cf: impl AsColumnFamilyRef, key: K) -> bool { + self.key_may_exist_cf_opt(cf, key, &ReadOptions::default()) + } + + /// Returns `false` if the given key definitely doesn't exist in the specified column family, + /// otherwise returns `false`. + pub fn key_may_exist_cf_opt>( + &self, + cf: impl AsColumnFamilyRef, + key: K, + readopts: &ReadOptions, + ) -> bool { + let key = key.as_ref(); + 0 != unsafe { + ffi::rocksdb_key_may_exist_cf( + self.inner, + readopts.inner, + cf.inner(), + key.as_ptr() as *const c_char, + key.len() as size_t, + ptr::null_mut(), /*value*/ + ptr::null_mut(), /*val_len*/ + ptr::null(), /*timestamp*/ + 0, /*timestamp_len*/ + ptr::null_mut(), /*value_found*/ + ) + } + } + fn create_inner_cf_handle( &self, name: &str, diff --git a/tests/test_db.rs b/tests/test_db.rs index 1447ed0..8aa682f 100644 --- a/tests/test_db.rs +++ b/tests/test_db.rs @@ -966,6 +966,39 @@ fn multi_get_cf() { } } +#[test] +fn key_may_exist() { + let path = DBPath::new("_rust_rocksdb_multi_get"); + + { + let db = DB::open_default(&path).unwrap(); + assert_eq!(false, db.key_may_exist("nonexistent")); + assert_eq!( + false, + db.key_may_exist_opt("nonexistent", &ReadOptions::default()) + ); + } +} + +#[test] +fn key_may_exist_cf() { + let path = DBPath::new("_rust_rocksdb_multi_get_cf"); + + { + let mut opts = Options::default(); + opts.create_if_missing(true); + opts.create_missing_column_families(true); + let db = DB::open_cf(&opts, &path, &["cf"]).unwrap(); + let cf = db.cf_handle("cf").unwrap(); + + assert_eq!(false, db.key_may_exist_cf(cf, "nonexistent")); + assert_eq!( + false, + db.key_may_exist_cf_opt(cf, "nonexistent", &ReadOptions::default()) + ); + } +} + #[test] fn test_snapshot_outlive_db() { let t = trybuild::TestCases::new(); From 841c23b26549b6a313b66cf9eacb130f8d0d2138 Mon Sep 17 00:00:00 2001 From: Stanislav Tkach Date: Wed, 12 May 2021 11:37:59 +0300 Subject: [PATCH 2/3] Add the DB::key_may_exist_cf_opt method --- CHANGELOG.md | 7 +++++-- src/db_options.rs | 13 +++++++++++++ tests/test_db.rs | 4 ++-- tests/test_rocksdb_options.rs | 11 +++++++++++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03b1ddc..260d6f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## [Unreleased] +* Add `DB::key_may_exist_cf_opt` method (stanislav-tkach) +* Add `Options::set_zstd_max_train_bytes` method (stanislav-tkach) + ## 0.16.0 (2021-04-18) * Add `DB::cancel_all_background_work` method (stanislav-tkach) @@ -15,13 +18,13 @@ * Bump `librocksdb-sys` up to 6.17.3 (ordian) * Remove the need for `&mut self` in `create_cf` and `drop_cf` (v2) (ryoqun) * Keep Cache and Env alive with Rc (acrrd) -* Add DB::open_cf_with_ttl method (fdeantoni) +* Add `DB::open_cf_with_ttl` method (fdeantoni) ## 0.15.0 (2020-08-25) * Fix building rocksdb library on windows host (aleksuss) * Add github actions CI for windows build (aleksuss) -* Update doc for Options::set_compression_type (wqfish) +* Update doc for `Options::set_compression_type` (wqfish) * Add clippy linter in CI (aleksuss) * Use DBPath for backup_restore test (wqfish) * Allow to build RocksDB with a different stdlib (calavera) diff --git a/src/db_options.rs b/src/db_options.rs index 50e2d28..53d28bf 100644 --- a/src/db_options.rs +++ b/src/db_options.rs @@ -977,6 +977,19 @@ impl Options { } } + /// Sets maximum size of training data passed to zstd's dictionary trainer. Using zstd's + /// dictionary trainer can achieve even better compression ratio improvements than using + /// `max_dict_bytes` alone. + /// + /// The training data will be used to generate a dictionary of max_dict_bytes. + /// + /// Default: 0. + pub fn set_zstd_max_train_bytes(&mut self, value: c_int) { + unsafe { + ffi::rocksdb_options_set_compression_options_zstd_max_train_bytes(self.inner, value); + } + } + /// If non-zero, we perform bigger reads when doing compaction. If you're /// running RocksDB on spinning disks, you should set this to at least 2MB. /// That way RocksDB's compaction is doing sequential instead of random reads. diff --git a/tests/test_db.rs b/tests/test_db.rs index 8aa682f..d808cdc 100644 --- a/tests/test_db.rs +++ b/tests/test_db.rs @@ -968,7 +968,7 @@ fn multi_get_cf() { #[test] fn key_may_exist() { - let path = DBPath::new("_rust_rocksdb_multi_get"); + let path = DBPath::new("_rust_key_may_exist"); { let db = DB::open_default(&path).unwrap(); @@ -982,7 +982,7 @@ fn key_may_exist() { #[test] fn key_may_exist_cf() { - let path = DBPath::new("_rust_rocksdb_multi_get_cf"); + let path = DBPath::new("_rust_key_may_exist_cf"); { let mut opts = Options::default(); diff --git a/tests/test_rocksdb_options.rs b/tests/test_rocksdb_options.rs index 8089b3d..62a08bb 100644 --- a/tests/test_rocksdb_options.rs +++ b/tests/test_rocksdb_options.rs @@ -134,3 +134,14 @@ fn test_set_data_block_index_type() { assert!(settings.contains("data_block_hash_table_util_ratio: 0.350000")); } } + +#[test] +fn set_compression_options_zstd_max_train_bytes() { + let path = DBPath::new("_rust_set_compression_options_zstd_max_train_bytes"); + { + let mut opts = Options::default(); + opts.create_if_missing(true); + opts.set_zstd_max_train_bytes(100); + let _db = DB::open(&opts, &path).unwrap(); + } +} From c43ac614e3830a3909d3ee44fd2365fa6368804b Mon Sep 17 00:00:00 2001 From: Stanislav Tkach Date: Wed, 12 May 2021 12:13:32 +0300 Subject: [PATCH 3/3] Fix test (set compression options) --- tests/test_rocksdb_options.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_rocksdb_options.rs b/tests/test_rocksdb_options.rs index 62a08bb..cb1b3d7 100644 --- a/tests/test_rocksdb_options.rs +++ b/tests/test_rocksdb_options.rs @@ -141,6 +141,7 @@ fn set_compression_options_zstd_max_train_bytes() { { let mut opts = Options::default(); opts.create_if_missing(true); + opts.set_compression_options(4, 5, 6, 7); opts.set_zstd_max_train_bytes(100); let _db = DB::open(&opts, &path).unwrap(); }