From 70eec41aa323695c3f5a48ea3974d3fad62cb68a Mon Sep 17 00:00:00 2001 From: fdeantoni Date: Mon, 22 Mar 2021 15:55:38 +0800 Subject: [PATCH 1/3] Add DB::open_cf_with_ttl method. --- CHANGELOG.md | 1 + src/db.rs | 31 ++++++++++++++++++++++++++++++- tests/test_db.rs | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b5c7a615..8f9dd96b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Bump `librocksdb-sys` up to 6.13.3 (aleksuss) * Add `multi_get`, `multi_get_opt`, `multi_get_cf` and `multi_get_cf_opt` `DB` methods (stanislav-tkach) * Bump `librocksdb-sys` up to 6.17.3 (ordian) +* Add `DB::open_cf_with_ttl` method (fdeantoni) ## 0.15.0 (2020-08-25) diff --git a/src/db.rs b/src/db.rs index e49fe366e..23583dfe5 100644 --- a/src/db.rs +++ b/src/db.rs @@ -110,6 +110,25 @@ impl DB { }) } + /// Opens the database with a Time to Live compaction filter and column family names. + pub fn open_cf_with_ttl( + opts: &Options, + path: P, + cfs: I, + ttl: Duration, + ) -> Result + where + P: AsRef, + I: IntoIterator, + N: AsRef, + { + let cfs = cfs + .into_iter() + .map(|name| ColumnFamilyDescriptor::new(name.as_ref(), Options::default())); + + DB::open_cf_descriptors_internal(opts, path, cfs, &AccessType::WithTTL { ttl }) + } + /// Opens a database with the given database options and column family names. /// /// Column families opened using this function will be created with default `Options`. @@ -348,7 +367,17 @@ impl DB { cfhandles.as_mut_ptr(), )) } - _ => return Err(Error::new("Unsupported access type".to_owned())), + AccessType::WithTTL { ttl } => { + ffi_try!(ffi::rocksdb_open_column_families_with_ttl( + opts.inner, + cpath.as_ptr(), + cfs_v.len() as c_int, + cfnames.as_ptr(), + cfopts.as_ptr(), + cfhandles.as_mut_ptr(), + &(ttl.as_secs() as c_int) as *const _, + )) + } } }; Ok(db) diff --git a/tests/test_db.rs b/tests/test_db.rs index d9ec85b24..ea1942639 100644 --- a/tests/test_db.rs +++ b/tests/test_db.rs @@ -528,6 +528,25 @@ fn test_open_with_ttl() { assert!(db.get(b"key1").unwrap().is_none()); } +#[test] +fn test_open_cf_with_ttl() { + let path = DBPath::new("_rust_rocksdb_test_open_cf_with_ttl"); + + let mut opts = Options::default(); + opts.create_if_missing(true); + opts.create_missing_column_families(true); + let db = DB::open_cf_with_ttl(&opts, &path, &["test_cf"], Duration::from_secs(1)).unwrap(); + let cf = db.cf_handle("test_cf").unwrap(); + db.put_cf(cf, b"key1", b"value1").unwrap(); + + thread::sleep(Duration::from_secs(2)); + // Trigger a manual compaction, this will check the TTL filter + // in the database and drop all expired entries. + db.compact_range_cf(cf, None::<&[u8]>, None::<&[u8]>); + + assert!(db.get_cf(cf, b"key1").unwrap().is_none()); +} + #[test] fn compact_range_test() { let path = DBPath::new("_rust_rocksdb_compact_range_test"); From 83ff5bc2000f0bee77cfbeb931f52bdd400c0902 Mon Sep 17 00:00:00 2001 From: fdeantoni Date: Tue, 23 Mar 2021 08:54:21 +0800 Subject: [PATCH 2/3] Add DB::open_cf_descriptors_with_ttl method Added comment clarifying default cf options will be used for `open_cf_with_ttl` and added method `open_cf_descriptors_with_ttl` to allow opening up of database with TTL and ColumnFamilyDescriptors. --- src/db.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/db.rs b/src/db.rs index 23583dfe5..5b69c572a 100644 --- a/src/db.rs +++ b/src/db.rs @@ -111,6 +111,8 @@ impl DB { } /// Opens the database with a Time to Live compaction filter and column family names. + /// + /// Column families opened using this function will be created with default `Options`. pub fn open_cf_with_ttl( opts: &Options, path: P, @@ -126,6 +128,21 @@ impl DB { .into_iter() .map(|name| ColumnFamilyDescriptor::new(name.as_ref(), Options::default())); + DB::open_cf_descriptors_with_ttl(opts, path, cfs, ttl) + } + + /// Opens a database with the given database with a Time to Live compaction filter and + /// column family descriptors. + pub fn open_cf_descriptors_with_ttl( + opts: &Options, + path: P, + cfs: I, + ttl: Duration, + ) -> Result + where + P: AsRef, + I: IntoIterator, + { DB::open_cf_descriptors_internal(opts, path, cfs, &AccessType::WithTTL { ttl }) } From 8928ca8f2ea49ecc59a734d9f70c74c5681db160 Mon Sep 17 00:00:00 2001 From: Oleksandr Anyshchenko Date: Sun, 18 Apr 2021 11:09:57 +0300 Subject: [PATCH 3/3] Tiny refactoring --- src/db.rs | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/src/db.rs b/src/db.rs index 06f9e6221..ae7f5956e 100644 --- a/src/db.rs +++ b/src/db.rs @@ -231,18 +231,7 @@ impl DBWithThreadMode { path: P, ttl: Duration, ) -> Result { - let c_path = to_cpath(&path)?; - let db = Self::open_raw(opts, &c_path, &AccessType::WithTTL { ttl })?; - if db.is_null() { - return Err(Error::new("Could not initialize database.".to_owned())); - } - - Ok(Self { - inner: db, - cfs: T::new(BTreeMap::new()), - path: path.as_ref().to_path_buf(), - _outlive: vec![opts.outlive.clone()], - }) + Self::open_cf_descriptors_with_ttl(opts, path, std::iter::empty(), ttl) } /// Opens the database with a Time to Live compaction filter and column family names. @@ -253,7 +242,7 @@ impl DBWithThreadMode { path: P, cfs: I, ttl: Duration, - ) -> Result + ) -> Result where P: AsRef, I: IntoIterator, @@ -263,7 +252,7 @@ impl DBWithThreadMode { .into_iter() .map(|name| ColumnFamilyDescriptor::new(name.as_ref(), Options::default())); - DB::open_cf_descriptors_with_ttl(opts, path, cfs, ttl) + Self::open_cf_descriptors_with_ttl(opts, path, cfs, ttl) } /// Opens a database with the given database with a Time to Live compaction filter and @@ -273,12 +262,12 @@ impl DBWithThreadMode { path: P, cfs: I, ttl: Duration, - ) -> Result + ) -> Result where P: AsRef, I: IntoIterator, { - DB::open_cf_descriptors_internal(opts, path, cfs, &AccessType::WithTTL { ttl }) + Self::open_cf_descriptors_internal(opts, path, cfs, &AccessType::WithTTL { ttl }) } /// Opens a database with the given database options and column family names.