From d035454d9e96fbbf7bcfc630ee3cf735b6ed2983 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Tue, 7 Aug 2018 11:17:05 +0300 Subject: [PATCH] Allow retrieving WriteBatch serialized size --- src/db.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/db.rs b/src/db.rs index 78fd8c7..0bed62c 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1106,6 +1106,15 @@ impl WriteBatch { 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 { self.len() == 0 } @@ -1403,6 +1412,14 @@ fn writebatch_works() { assert!(p.is_ok()); 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(); assert!(DB::destroy(&opts, path).is_ok());