Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DB::open_cf_with_ttl method. #505

Merged
merged 5 commits into from Apr 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)

Expand Down
31 changes: 30 additions & 1 deletion src/db.rs
Expand Up @@ -110,6 +110,25 @@ impl DB {
})
}

/// Opens the database with a Time to Live compaction filter and column family names.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple nitpicks.

  1. As I can see, you provide default options when create ColumnFamilyDescriptor. I guess it should be added to the function description for notifying user about this.
  2. Sometimes a necessity occurs to create a ColumnFamilyDescriptor with custom options. What do you think about adding extra function with custom options for ColumnFamilyDescriptors ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing this PR @aleksuss! I have pushed a new commit that contains the fixes you suggested. Let me know if this addresses what you were suggesting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean to create a method like this:

pub fn open_cf_with_ttl_opt<P, I, N>(
         opts: &Options,
         path: P,
         cfs: I,
         ttl: Duration,
         cfs_opts: Options,
     ) -> Result<DB, Error>
     where
         P: AsRef<Path>,
         I: IntoIterator<Item = N>,
         N: AsRef<str>,
     {
         let cfs = cfs
             .into_iter()
             .map(|name| ColumnFamilyDescriptor::new(name.as_ref(), cfs_opts.clone()));

         DB::open_cf_descriptors_internal(opts, path, cfs, &AccessType::WithTTL { ttl })
}

pub fn open_cf_with_ttl<P, I, N>(
opts: &Options,
path: P,
cfs: I,
ttl: Duration,
) -> Result<DB, Error>
where
P: AsRef<Path>,
I: IntoIterator<Item = N>,
N: AsRef<str>,
{
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`.
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_db.rs
Expand Up @@ -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");
Expand Down