Add 'key_may_exist' functions family

master
Stanislav Tkach 3 years ago
parent 7dd6258b07
commit dfeda6ebc6
  1. 56
      src/db.rs
  2. 33
      tests/test_db.rs

@ -844,6 +844,62 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
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<K: AsRef<[u8]>>(&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<K: AsRef<[u8]>>(&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<K: AsRef<[u8]>>(&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<K: AsRef<[u8]>>(
&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,

@ -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();

Loading…
Cancel
Save