[RocksDB] Expose count for WriteBatch

Summary: As title. Exposed a Count function that returns the number of updates in a batch. Could be handy for replication sequence number check.

Test Plan: make check;

Reviewers: emayanke, sheki, dhruba

CC: leveldb

Differential Revision: https://reviews.facebook.net/D11523
main
Haobo Xu 12 years ago
parent 34ef873290
commit 71e0f695c1
  1. 4
      db/write_batch.cc
  2. 6
      db/write_batch_test.cc
  3. 5
      include/leveldb/write_batch.h

@ -45,6 +45,10 @@ void WriteBatch::Clear() {
rep_.resize(kHeader); rep_.resize(kHeader);
} }
int WriteBatch::Count() const {
return WriteBatchInternal::Count(this);
}
Status WriteBatch::Iterate(Handler* handler) const { Status WriteBatch::Iterate(Handler* handler) const {
Slice input(rep_); Slice input(rep_);
if (input.size() < kHeader) { if (input.size() < kHeader) {

@ -67,6 +67,7 @@ TEST(WriteBatchTest, Empty) {
WriteBatch batch; WriteBatch batch;
ASSERT_EQ("", PrintContents(&batch)); ASSERT_EQ("", PrintContents(&batch));
ASSERT_EQ(0, WriteBatchInternal::Count(&batch)); ASSERT_EQ(0, WriteBatchInternal::Count(&batch));
ASSERT_EQ(0, batch.Count());
} }
TEST(WriteBatchTest, Multiple) { TEST(WriteBatchTest, Multiple) {
@ -81,6 +82,7 @@ TEST(WriteBatchTest, Multiple) {
"Delete(box)@101" "Delete(box)@101"
"Put(foo, bar)@100", "Put(foo, bar)@100",
PrintContents(&batch)); PrintContents(&batch));
ASSERT_EQ(3, batch.Count());
} }
TEST(WriteBatchTest, Corruption) { TEST(WriteBatchTest, Corruption) {
@ -103,16 +105,19 @@ TEST(WriteBatchTest, Append) {
WriteBatchInternal::Append(&b1, &b2); WriteBatchInternal::Append(&b1, &b2);
ASSERT_EQ("", ASSERT_EQ("",
PrintContents(&b1)); PrintContents(&b1));
ASSERT_EQ(0, b1.Count());
b2.Put("a", "va"); b2.Put("a", "va");
WriteBatchInternal::Append(&b1, &b2); WriteBatchInternal::Append(&b1, &b2);
ASSERT_EQ("Put(a, va)@200", ASSERT_EQ("Put(a, va)@200",
PrintContents(&b1)); PrintContents(&b1));
ASSERT_EQ(1, b1.Count());
b2.Clear(); b2.Clear();
b2.Put("b", "vb"); b2.Put("b", "vb");
WriteBatchInternal::Append(&b1, &b2); WriteBatchInternal::Append(&b1, &b2);
ASSERT_EQ("Put(a, va)@200" ASSERT_EQ("Put(a, va)@200"
"Put(b, vb)@201", "Put(b, vb)@201",
PrintContents(&b1)); PrintContents(&b1));
ASSERT_EQ(2, b1.Count());
b2.Delete("foo"); b2.Delete("foo");
WriteBatchInternal::Append(&b1, &b2); WriteBatchInternal::Append(&b1, &b2);
ASSERT_EQ("Put(a, va)@200" ASSERT_EQ("Put(a, va)@200"
@ -120,6 +125,7 @@ TEST(WriteBatchTest, Append) {
"Put(b, vb)@201" "Put(b, vb)@201"
"Delete(foo)@203", "Delete(foo)@203",
PrintContents(&b1)); PrintContents(&b1));
ASSERT_EQ(4, b1.Count());
} }
} // namespace leveldb } // namespace leveldb

@ -59,9 +59,12 @@ class WriteBatch {
}; };
Status Iterate(Handler* handler) const; Status Iterate(Handler* handler) const;
// Retrive the serialized version of this batch. // Retrieve the serialized version of this batch.
std::string Data() { return rep_; } std::string Data() { return rep_; }
// Returns the number of updates in the batch
int Count() const;
// Constructor with a serialized string object // Constructor with a serialized string object
WriteBatch(std::string rep): rep_(rep) {} WriteBatch(std::string rep): rep_(rep) {}

Loading…
Cancel
Save