From ee7483204c385cab850405f6e8f90317b1af5f3e Mon Sep 17 00:00:00 2001 From: siddontang Date: Wed, 22 Jun 2016 13:01:51 +0800 Subject: [PATCH 1/3] support writebatch count --- src/rocksdb.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/rocksdb.rs b/src/rocksdb.rs index 62fd506..8d8da3f 100644 --- a/src/rocksdb.rs +++ b/src/rocksdb.rs @@ -759,6 +759,12 @@ impl Writable for DB { } } +impl WriteBatch { + pub fn count(&self) -> usize { + unsafe { rocksdb_ffi::rocksdb_writebatch_count(self.inner) as usize } + } +} + impl Default for WriteBatch { fn default() -> WriteBatch { WriteBatch { @@ -972,7 +978,9 @@ fn writebatch_works() { // test put let batch = WriteBatch::default(); assert!(db.get(b"k1").unwrap().is_none()); + assert_eq!(batch.count(), 0); let _ = batch.put(b"k1", b"v1111"); + assert_eq!(batch.count(), 1); assert!(db.get(b"k1").unwrap().is_none()); let p = db.write(batch); assert!(p.is_ok()); @@ -983,6 +991,7 @@ fn writebatch_works() { // test delete let batch = WriteBatch::default(); let _ = batch.delete(b"k1"); + assert_eq!(batch.count(), 1); let p = db.write(batch); assert!(p.is_ok()); assert!(db.get(b"k1").unwrap().is_none()); From daabb5d1eb6af74e6c6a88369e3f6f2d8c9daebe Mon Sep 17 00:00:00 2001 From: siddontang Date: Wed, 22 Jun 2016 13:39:32 +0800 Subject: [PATCH 2/3] support writebatch is_empty --- src/rocksdb.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/rocksdb.rs b/src/rocksdb.rs index 8d8da3f..b2ebc46 100644 --- a/src/rocksdb.rs +++ b/src/rocksdb.rs @@ -763,6 +763,10 @@ impl WriteBatch { pub fn count(&self) -> usize { unsafe { rocksdb_ffi::rocksdb_writebatch_count(self.inner) as usize } } + + pub fn is_empty(&self) -> bool { + self.count() == 0 + } } impl Default for WriteBatch { @@ -979,8 +983,10 @@ fn writebatch_works() { let batch = WriteBatch::default(); assert!(db.get(b"k1").unwrap().is_none()); assert_eq!(batch.count(), 0); + assert!(batch.is_empty()); let _ = batch.put(b"k1", b"v1111"); assert_eq!(batch.count(), 1); + assert!(!batch.is_empty()); assert!(db.get(b"k1").unwrap().is_none()); let p = db.write(batch); assert!(p.is_ok()); @@ -992,6 +998,7 @@ fn writebatch_works() { let batch = WriteBatch::default(); let _ = batch.delete(b"k1"); assert_eq!(batch.count(), 1); + assert!(!batch.is_empty()); let p = db.write(batch); assert!(p.is_ok()); assert!(db.get(b"k1").unwrap().is_none()); From 9d9c5566d6903bfd6bb2f0fb4486ab8af512008c Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 27 Oct 2016 11:19:12 +0100 Subject: [PATCH 3/3] Rename count to len --- src/rocksdb.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/rocksdb.rs b/src/rocksdb.rs index b2ebc46..d73722a 100644 --- a/src/rocksdb.rs +++ b/src/rocksdb.rs @@ -760,12 +760,12 @@ impl Writable for DB { } impl WriteBatch { - pub fn count(&self) -> usize { + pub fn len(&self) -> usize { unsafe { rocksdb_ffi::rocksdb_writebatch_count(self.inner) as usize } } pub fn is_empty(&self) -> bool { - self.count() == 0 + self.len() == 0 } } @@ -982,10 +982,10 @@ fn writebatch_works() { // test put let batch = WriteBatch::default(); assert!(db.get(b"k1").unwrap().is_none()); - assert_eq!(batch.count(), 0); + assert_eq!(batch.len(), 0); assert!(batch.is_empty()); let _ = batch.put(b"k1", b"v1111"); - assert_eq!(batch.count(), 1); + assert_eq!(batch.len(), 1); assert!(!batch.is_empty()); assert!(db.get(b"k1").unwrap().is_none()); let p = db.write(batch); @@ -997,7 +997,7 @@ fn writebatch_works() { // test delete let batch = WriteBatch::default(); let _ = batch.delete(b"k1"); - assert_eq!(batch.count(), 1); + assert_eq!(batch.len(), 1); assert!(!batch.is_empty()); let p = db.write(batch); assert!(p.is_ok());