diff --git a/db/write_batch.cc b/db/write_batch.cc index 3c773d24a..386e7ce1f 100644 --- a/db/write_batch.cc +++ b/db/write_batch.cc @@ -48,20 +48,6 @@ WriteBatch::~WriteBatch() { } WriteBatch::Handler::~Handler() { } -void WriteBatch::Handler::Put(const Slice& key, const Slice& value) { - // you need to either implement Put or PutCF - throw std::runtime_error("Handler::Put not implemented!"); -} - -void WriteBatch::Handler::Merge(const Slice& key, const Slice& value) { - throw std::runtime_error("Handler::Merge not implemented!"); -} - -void WriteBatch::Handler::Delete(const Slice& key) { - // you need to either implement Delete or DeleteCF - throw std::runtime_error("Handler::Delete not implemented!"); -} - void WriteBatch::Handler::LogData(const Slice& blob) { // If the user has not specified something to do with blobs, then we ignore // them. diff --git a/db/write_batch_test.cc b/db/write_batch_test.cc index e28d02aef..c51d1750f 100644 --- a/db/write_batch_test.cc +++ b/db/write_batch_test.cc @@ -187,6 +187,39 @@ namespace { }; } +TEST(WriteBatchTest, MergeNotImplemented) { + WriteBatch batch; + batch.Merge(Slice("foo"), Slice("bar")); + ASSERT_EQ(1, batch.Count()); + ASSERT_EQ("Merge(foo, bar)@0", + PrintContents(&batch)); + + WriteBatch::Handler handler; + ASSERT_OK(batch.Iterate(&handler)); +} + +TEST(WriteBatchTest, PutNotImplemented) { + WriteBatch batch; + batch.Put(Slice("k1"), Slice("v1")); + ASSERT_EQ(1, batch.Count()); + ASSERT_EQ("Put(k1, v1)@0", + PrintContents(&batch)); + + WriteBatch::Handler handler; + ASSERT_OK(batch.Iterate(&handler)); +} + +TEST(WriteBatchTest, DeleteNotImplemented) { + WriteBatch batch; + batch.Delete(Slice("k2")); + ASSERT_EQ(1, batch.Count()); + ASSERT_EQ("Delete(k2)@0", + PrintContents(&batch)); + + WriteBatch::Handler handler; + ASSERT_OK(batch.Iterate(&handler)); +} + TEST(WriteBatchTest, Blob) { WriteBatch batch; batch.Put(Slice("k1"), Slice("v1")); diff --git a/include/rocksdb/write_batch.h b/include/rocksdb/write_batch.h index db440be02..462a54a59 100644 --- a/include/rocksdb/write_batch.h +++ b/include/rocksdb/write_batch.h @@ -105,10 +105,11 @@ class WriteBatch { return Status::InvalidArgument( "non-default column family and PutCF not implemented"); } - virtual void Put(const Slice& key, const Slice& value); + virtual void Put(const Slice& key, const Slice& value) {} + // Merge and LogData are not pure virtual. Otherwise, we would break // existing clients of Handler on a source code level. The default - // implementation of Merge simply throws a runtime exception. + // implementation of Merge does nothing. virtual Status MergeCF(uint32_t column_family_id, const Slice& key, const Slice& value) { if (column_family_id == 0) { @@ -118,7 +119,8 @@ class WriteBatch { return Status::InvalidArgument( "non-default column family and MergeCF not implemented"); } - virtual void Merge(const Slice& key, const Slice& value); + virtual void Merge(const Slice& key, const Slice& value) {} + // The default implementation of LogData does nothing. virtual void LogData(const Slice& blob); virtual Status DeleteCF(uint32_t column_family_id, const Slice& key) { @@ -129,7 +131,8 @@ class WriteBatch { return Status::InvalidArgument( "non-default column family and DeleteCF not implemented"); } - virtual void Delete(const Slice& key); + virtual void Delete(const Slice& key) {} + // Continue is called by WriteBatch::Iterate. If it returns false, // iteration is halted. Otherwise, it continues iterating. The default // implementation always returns true.