Skip to content

Commit b6ebe50

Browse files
authored
Add delete_range to OptimisticTransactionDB (rust-rocksdb#879)
1 parent 961abc8 commit b6ebe50

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/transactions/optimistic_transaction_db.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515

1616
use std::{collections::BTreeMap, ffi::CString, fs, iter, marker::PhantomData, path::Path, ptr};
1717

18-
use libc::{c_char, c_int};
18+
use libc::{c_char, c_int, size_t};
1919

2020
use crate::{
21-
db::DBCommon, db::DBInner, ffi, ffi_util::to_cpath, write_batch::WriteBatchWithTransaction,
22-
ColumnFamilyDescriptor, Error, OptimisticTransactionOptions, Options, ThreadMode, Transaction,
23-
WriteOptions, DEFAULT_COLUMN_FAMILY_NAME,
21+
db::{DBCommon, DBInner},
22+
ffi,
23+
ffi_util::to_cpath,
24+
write_batch::WriteBatchWithTransaction,
25+
AsColumnFamilyRef, ColumnFamilyDescriptor, Error, OptimisticTransactionOptions, Options,
26+
ThreadMode, Transaction, WriteOptions, DEFAULT_COLUMN_FAMILY_NAME,
2427
};
2528

2629
/// A type alias to RocksDB Optimistic Transaction DB.
@@ -42,7 +45,7 @@ use crate::{
4245
/// {
4346
/// let db: OptimisticTransactionDB = OptimisticTransactionDB::open_default(path).unwrap();
4447
/// db.put(b"my key", b"my value").unwrap();
45-
///
48+
///
4649
/// // create transaction
4750
/// let txn = db.transaction();
4851
/// txn.put(b"key2", b"value2");
@@ -290,4 +293,39 @@ impl<T: ThreadMode> OptimisticTransactionDB<T> {
290293
wo.disable_wal(true);
291294
self.write_opt(batch, &wo)
292295
}
296+
297+
/// Removes the database entries in the range `["from", "to")` using given write options.
298+
pub fn delete_range_cf_opt<K: AsRef<[u8]>>(
299+
&self,
300+
cf: &impl AsColumnFamilyRef,
301+
from: K,
302+
to: K,
303+
writeopts: &WriteOptions,
304+
) -> Result<(), Error> {
305+
let from = from.as_ref();
306+
let to = to.as_ref();
307+
308+
unsafe {
309+
ffi_try!(ffi::rocksdb_delete_range_cf(
310+
self.inner.inner(),
311+
writeopts.inner,
312+
cf.inner(),
313+
from.as_ptr() as *const c_char,
314+
from.len() as size_t,
315+
to.as_ptr() as *const c_char,
316+
to.len() as size_t,
317+
));
318+
Ok(())
319+
}
320+
}
321+
322+
/// Removes the database entries in the range `["from", "to")` using default write options.
323+
pub fn delete_range_cf<K: AsRef<[u8]>>(
324+
&self,
325+
cf: &impl AsColumnFamilyRef,
326+
from: K,
327+
to: K,
328+
) -> Result<(), Error> {
329+
self.delete_range_cf_opt(cf, from, to, &WriteOptions::default())
330+
}
293331
}

tests/test_optimistic_transaction_db.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,31 @@ fn transaction_snapshot() {
581581
assert_eq!(snapshot.get(b"k3").unwrap().unwrap(), b"v3");
582582
}
583583
}
584+
585+
#[test]
586+
fn delete_range_test() {
587+
let path = DBPath::new("_rust_rocksdb_optimistic_transaction_db_delete_range_test");
588+
{
589+
let mut opts = Options::default();
590+
opts.create_if_missing(true);
591+
opts.create_missing_column_families(true);
592+
593+
let cfs = vec!["cf1"];
594+
let db: OptimisticTransactionDB =
595+
OptimisticTransactionDB::open_cf(&opts, &path, cfs).unwrap();
596+
597+
let cf1 = db.cf_handle("cf1").unwrap();
598+
db.put_cf(&cf1, b"k1", b"v1").unwrap();
599+
db.put_cf(&cf1, b"k2", b"v2").unwrap();
600+
db.put_cf(&cf1, b"k3", b"v3").unwrap();
601+
db.put_cf(&cf1, b"k4", b"v4").unwrap();
602+
db.put_cf(&cf1, b"k5", b"v5").unwrap();
603+
604+
db.delete_range_cf(&cf1, b"k2", b"k4").unwrap();
605+
assert_eq!(db.get_cf(&cf1, b"k1").unwrap().unwrap(), b"v1");
606+
assert_eq!(db.get_cf(&cf1, b"k4").unwrap().unwrap(), b"v4");
607+
assert_eq!(db.get_cf(&cf1, b"k5").unwrap().unwrap(), b"v5");
608+
assert!(db.get_cf(&cf1, b"k2").unwrap().is_none());
609+
assert!(db.get_cf(&cf1, b"k3").unwrap().is_none());
610+
}
611+
}

0 commit comments

Comments
 (0)