Merge pull request #201 from romanz/writebatch-size

Allow retrieving WriteBatch serialized size
master
Oleksandr Anyshchenko 6 years ago committed by GitHub
commit 4471217a60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      src/db.rs

@ -1107,6 +1107,15 @@ impl WriteBatch {
unsafe { ffi::rocksdb_writebatch_count(self.inner) as usize } unsafe { ffi::rocksdb_writebatch_count(self.inner) as usize }
} }
/// Return WriteBatch serialized size (in bytes).
pub fn size_in_bytes(&self) -> usize {
unsafe {
let mut batch_size: size_t = 0;
ffi::rocksdb_writebatch_data(self.inner, &mut batch_size);
batch_size as usize
}
}
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.len() == 0 self.len() == 0
} }
@ -1404,6 +1413,14 @@ fn writebatch_works() {
assert!(p.is_ok()); assert!(p.is_ok());
assert!(db.get(b"k1").unwrap().is_none()); assert!(db.get(b"k1").unwrap().is_none());
} }
{
// test size_in_bytes
let mut batch = WriteBatch::default();
let before = batch.size_in_bytes();
let _ = batch.put(b"k1", b"v1234567890");
let after = batch.size_in_bytes();
assert!(before + 10 <= after);
}
} }
let opts = Options::default(); let opts = Options::default();
assert!(DB::destroy(&opts, path).is_ok()); assert!(DB::destroy(&opts, path).is_ok());

Loading…
Cancel
Save