Commit b9f0eee4 authored by zhangjinpeng1987's avatar zhangjinpeng1987

use None as largest/smallest key

parent 8173993f
...@@ -26,6 +26,7 @@ use std::ops::Deref; ...@@ -26,6 +26,7 @@ use std::ops::Deref;
use std::path::Path; use std::path::Path;
use std::slice; use std::slice;
use std::str::from_utf8; use std::str::from_utf8;
use std::ptr;
const DEFAULT_COLUMN_FAMILY: &'static str = "default"; const DEFAULT_COLUMN_FAMILY: &'static str = "default";
...@@ -848,47 +849,31 @@ impl DB { ...@@ -848,47 +849,31 @@ impl DB {
sizes sizes
} }
pub fn compact_range(&self, start_key: &[u8], end_key: &[u8]) { pub fn compact_range(&self, start_key: Option<&[u8]>, end_key: Option<&[u8]>) {
unsafe { unsafe {
let start = if start_key.is_empty() { let (start, s_len) = start_key.map_or((ptr::null(), 0), |k| (k.as_ptr(), k.len()));
0 as *const u8 let (end, e_len) = end_key.map_or((ptr::null(), 0), |k| (k.as_ptr(), k.len()));
} else {
start_key.as_ptr()
};
let end = if end_key.is_empty() {
0 as *const u8
} else {
end_key.as_ptr()
};
rocksdb_ffi::rocksdb_compact_range(self.inner, rocksdb_ffi::rocksdb_compact_range(self.inner,
start, start,
start_key.len() as size_t, s_len,
end, end,
end_key.len()); e_len);
} }
} }
pub fn compact_range_cf(&self, pub fn compact_range_cf(&self,
cf: &CFHandle, cf: &CFHandle,
start_key: &[u8], start_key: Option<&[u8]>,
end_key: &[u8]) { end_key: Option<&[u8]>) {
unsafe { unsafe {
let start = if start_key.is_empty() { let (start, s_len) = start_key.map_or((ptr::null(), 0), |k| (k.as_ptr(), k.len()));
0 as *const u8 let (end, e_len) = end_key.map_or((ptr::null(), 0), |k| (k.as_ptr(), k.len()));
} else {
start_key.as_ptr()
};
let end = if end_key.is_empty() {
0 as *const u8
} else {
end_key.as_ptr()
};
rocksdb_ffi::rocksdb_compact_range_cf(self.inner, rocksdb_ffi::rocksdb_compact_range_cf(self.inner,
cf.inner, cf.inner,
start, start,
start_key.len() as size_t, s_len,
end, end,
end_key.len()); e_len);
} }
} }
......
...@@ -28,7 +28,7 @@ fn test_compact_range() { ...@@ -28,7 +28,7 @@ fn test_compact_range() {
for &(ref k, _) in &samples { for &(ref k, _) in &samples {
db.delete(k).unwrap() db.delete(k).unwrap()
} }
db.compact_range(b"", b""); db.compact_range(None, None);
let new_size = db.get_approximate_sizes(&[Range::new(b"k0", b"k6")])[0]; let new_size = db.get_approximate_sizes(&[Range::new(b"k0", b"k6")])[0];
assert!(old_size > new_size); assert!(old_size > new_size);
} }
...@@ -47,7 +47,7 @@ fn test_compaction_filter() { ...@@ -47,7 +47,7 @@ fn test_compaction_filter() {
let _snap = db.snapshot(); let _snap = db.snapshot();
// Because ignore_snapshots is false, so force compact will not effect // Because ignore_snapshots is false, so force compact will not effect
// the keys written before. // the keys written before.
db.compact_range(b"key1", b"key3"); db.compact_range(Some(b"key1"), Some(b"key3"));
for &(ref k, ref v) in &samples { for &(ref k, ref v) in &samples {
assert_eq!(v.as_slice(), &*db.get(k).unwrap().unwrap()); assert_eq!(v.as_slice(), &*db.get(k).unwrap().unwrap());
} }
...@@ -66,7 +66,7 @@ fn test_compaction_filter() { ...@@ -66,7 +66,7 @@ fn test_compaction_filter() {
let db = DB::open(&opts, path.path().to_str().unwrap()).unwrap(); let db = DB::open(&opts, path.path().to_str().unwrap()).unwrap();
let _snap = db.snapshot(); let _snap = db.snapshot();
// Because ignore_snapshots is true, so all the keys will be compacted. // Because ignore_snapshots is true, so all the keys will be compacted.
db.compact_range(b"key1", b"key3"); db.compact_range(Some(b"key1"), Some(b"key3"));
for &(ref k, _) in &samples { for &(ref k, _) in &samples {
assert!(db.get(k).unwrap().is_none()); assert!(db.get(k).unwrap().is_none());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment