From a467e6bfede2fa3094b1e6c18cab21451b96aa54 Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Fri, 9 Dec 2016 00:38:34 -0700 Subject: [PATCH] Add methods for compacting ranges. --- src/db.rs | 27 +++++++++++++++++++++++++++ src/ffi_util.rs | 8 ++++++++ 2 files changed, 35 insertions(+) diff --git a/src/db.rs b/src/db.rs index 7dcfb0c..b963440 100644 --- a/src/db.rs +++ b/src/db.rs @@ -16,6 +16,7 @@ use {DB, Error, Options, WriteOptions}; use ffi; +use ffi_util::opt_bytes_to_ptr; use libc::{self, c_char, c_int, c_uchar, c_void, size_t}; use std::collections::BTreeMap; @@ -674,6 +675,32 @@ impl DB { -> Result<(), Error> { self.delete_cf_opt(cf, key, &WriteOptions::default()) } + + pub fn compact_range(&self, + start: Option<&[u8]>, + end: Option<&[u8]>) { + unsafe { + ffi::rocksdb_compact_range(self.inner, + opt_bytes_to_ptr(start), + start.map_or(0, |s| s.len()) as size_t, + opt_bytes_to_ptr(end), + end.map_or(0, |e| e.len()) as size_t); + } + } + + pub fn compact_range_cf(&self, + cf: *mut ffi::rocksdb_column_family_handle_t, + start: Option<&[u8]>, + end: Option<&[u8]>) { + unsafe { + ffi::rocksdb_compact_range_cf(self.inner, + cf, + opt_bytes_to_ptr(start), + start.map_or(0, |s| s.len()) as size_t, + opt_bytes_to_ptr(end), + end.map_or(0, |e| e.len()) as size_t); + } + } } impl WriteBatch { diff --git a/src/ffi_util.rs b/src/ffi_util.rs index 9256845..e7a1790 100644 --- a/src/ffi_util.rs +++ b/src/ffi_util.rs @@ -15,6 +15,7 @@ use libc::{self, c_char, c_void}; use std::ffi::CStr; +use std::ptr; pub fn error_message(ptr: *const c_char) -> String { let cstr = unsafe { CStr::from_ptr(ptr as *const _) }; @@ -25,6 +26,13 @@ pub fn error_message(ptr: *const c_char) -> String { s } +pub fn opt_bytes_to_ptr(opt: Option<&[u8]>) -> *const c_char { + match opt { + Some(v) => v.as_ptr() as *const c_char, + None => ptr::null() + } +} + macro_rules! ffi_try { ( $($function:ident)::*( $( $arg:expr ),* ) ) => ({ let mut err: *mut ::libc::c_char = ::std::ptr::null_mut();