From b551104e0441a7e806c7fc667e33a6e582575a03 Mon Sep 17 00:00:00 2001 From: siddontang Date: Wed, 3 May 2017 10:54:07 -0700 Subject: [PATCH] support PopSavePoint for WriteBatch Summary: Try to fix https://github.com/facebook/rocksdb/issues/1969 Closes https://github.com/facebook/rocksdb/pull/2170 Differential Revision: D4907333 Pulled By: yiwu-arbug fbshipit-source-id: 417b420ff668e6c2fd0dad42a94c57385012edc5 --- HISTORY.md | 3 +++ db/c.cc | 4 ++++ db/c_test.c | 3 +++ db/write_batch.cc | 11 +++++++++++ db/write_batch_test.cc | 13 +++++++++++++ include/rocksdb/c.h | 2 ++ include/rocksdb/utilities/write_batch_with_index.h | 6 ++++++ include/rocksdb/write_batch.h | 6 ++++++ include/rocksdb/write_batch_base.h | 6 ++++++ .../write_batch_with_index.cc | 4 ++++ 10 files changed, 58 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 804321151..168496b4a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,8 @@ # Rocksdb Change Log ## Unreleased +### Public API Change +* Introduce WriteBatch::PopSavePoint to pop the most recent save point explicitly. + ### New Features * DB::ResetStats() to reset internal stats. * Statistics::Reset() to reset user stats. diff --git a/db/c.cc b/db/c.cc index 1ccc39a41..be2700e42 100644 --- a/db/c.cc +++ b/db/c.cc @@ -1362,6 +1362,10 @@ void rocksdb_writebatch_rollback_to_save_point(rocksdb_writebatch_t* b, SaveError(errptr, b->rep.RollbackToSavePoint()); } +void rocksdb_writebatch_pop_save_point(rocksdb_writebatch_t* b, char** errptr) { + SaveError(errptr, b->rep.PopSavePoint()); +} + rocksdb_writebatch_wi_t* rocksdb_writebatch_wi_create(size_t reserved_bytes, unsigned char overwrite_key) { rocksdb_writebatch_wi_t* b = new rocksdb_writebatch_wi_t; b->rep = new WriteBatchWithIndex(BytewiseComparator(), reserved_bytes, overwrite_key); diff --git a/db/c_test.c b/db/c_test.c index 29ff03a25..2ecf18d6c 100644 --- a/db/c_test.c +++ b/db/c_test.c @@ -587,10 +587,13 @@ int main(int argc, char** argv) { { rocksdb_writebatch_t* wb = rocksdb_writebatch_create(); rocksdb_writebatch_set_save_point(wb); + rocksdb_writebatch_set_save_point(wb); const char* k_list[2] = {"z", "ap"}; const size_t k_sizes[2] = {1, 2}; const char* v_list[3] = {"x", "y", "z"}; const size_t v_sizes[3] = {1, 1, 1}; + rocksdb_writebatch_pop_save_point(wb, &err); + CheckNoError(err); rocksdb_writebatch_putv(wb, 2, k_list, k_sizes, 3, v_list, v_sizes); rocksdb_writebatch_rollback_to_save_point(wb, &err); CheckNoError(err); diff --git a/db/write_batch.cc b/db/write_batch.cc index e80330e2d..b1bb6b06c 100644 --- a/db/write_batch.cc +++ b/db/write_batch.cc @@ -803,6 +803,17 @@ Status WriteBatch::RollbackToSavePoint() { return Status::OK(); } +Status WriteBatch::PopSavePoint() { + if (save_points_ == nullptr || save_points_->stack.size() == 0) { + return Status::NotFound(); + } + + // Pop the most recent savepoint off the stack + save_points_->stack.pop(); + + return Status::OK(); +} + class MemTableInserter : public WriteBatch::Handler { SequenceNumber sequence_; diff --git a/db/write_batch_test.cc b/db/write_batch_test.cc index f5349be06..2c44b7ffa 100644 --- a/db/write_batch_test.cc +++ b/db/write_batch_test.cc @@ -861,6 +861,19 @@ TEST_F(WriteBatchTest, SavePointTest) { s = batch2.RollbackToSavePoint(); ASSERT_TRUE(s.IsNotFound()); ASSERT_EQ("", PrintContents(&batch2)); + + WriteBatch batch3; + + s = batch3.PopSavePoint(); + ASSERT_TRUE(s.IsNotFound()); + ASSERT_EQ("", PrintContents(&batch3)); + + batch3.SetSavePoint(); + batch3.Delete("A"); + + s = batch3.PopSavePoint(); + ASSERT_OK(s); + ASSERT_EQ("Delete(A)@0", PrintContents(&batch3)); } TEST_F(WriteBatchTest, MemoryLimitTest) { diff --git a/include/rocksdb/c.h b/include/rocksdb/c.h index 7f528b7e7..3a5aee990 100644 --- a/include/rocksdb/c.h +++ b/include/rocksdb/c.h @@ -457,6 +457,8 @@ extern ROCKSDB_LIBRARY_API void rocksdb_writebatch_set_save_point( rocksdb_writebatch_t*); extern ROCKSDB_LIBRARY_API void rocksdb_writebatch_rollback_to_save_point( rocksdb_writebatch_t*, char** errptr); +extern ROCKSDB_LIBRARY_API void rocksdb_writebatch_pop_save_point( + rocksdb_writebatch_t*, char** errptr); /* Write batch with index */ diff --git a/include/rocksdb/utilities/write_batch_with_index.h b/include/rocksdb/utilities/write_batch_with_index.h index 3e9381ab5..ba6d67057 100644 --- a/include/rocksdb/utilities/write_batch_with_index.h +++ b/include/rocksdb/utilities/write_batch_with_index.h @@ -207,6 +207,12 @@ class WriteBatchWithIndex : public WriteBatchBase { // or other Status on corruption. Status RollbackToSavePoint() override; + // Pop the most recent save point. + // If there is no previous call to SetSavePoint(), Status::NotFound() + // will be returned. + // Otherwise returns Status::OK(). + Status PopSavePoint() override; + void SetMaxBytes(size_t max_bytes) override; private: diff --git a/include/rocksdb/write_batch.h b/include/rocksdb/write_batch.h index c60ac1796..d60fa8cc8 100644 --- a/include/rocksdb/write_batch.h +++ b/include/rocksdb/write_batch.h @@ -166,6 +166,12 @@ class WriteBatch : public WriteBatchBase { // Otherwise returns Status::OK(). Status RollbackToSavePoint() override; + // Pop the most recent save point. + // If there is no previous call to SetSavePoint(), Status::NotFound() + // will be returned. + // Otherwise returns Status::OK(). + Status PopSavePoint() override; + // Support for iterating over the contents of a batch. class Handler { public: diff --git a/include/rocksdb/write_batch_base.h b/include/rocksdb/write_batch_base.h index 9278f8670..8b6279292 100644 --- a/include/rocksdb/write_batch_base.h +++ b/include/rocksdb/write_batch_base.h @@ -112,6 +112,12 @@ class WriteBatchBase { // Clear(). virtual Status RollbackToSavePoint() = 0; + // Pop the most recent save point. + // If there is no previous call to SetSavePoint(), Status::NotFound() + // will be returned. + // Otherwise returns Status::OK(). + virtual Status PopSavePoint() = 0; + // Sets the maximum size of the write batch in bytes. 0 means no limit. virtual void SetMaxBytes(size_t max_bytes) = 0; }; diff --git a/utilities/write_batch_with_index/write_batch_with_index.cc b/utilities/write_batch_with_index/write_batch_with_index.cc index 3e3bf778c..9a8e5db78 100644 --- a/utilities/write_batch_with_index/write_batch_with_index.cc +++ b/utilities/write_batch_with_index/write_batch_with_index.cc @@ -832,6 +832,10 @@ Status WriteBatchWithIndex::RollbackToSavePoint() { return s; } +Status WriteBatchWithIndex::PopSavePoint() { + return rep->write_batch.PopSavePoint(); +} + void WriteBatchWithIndex::SetMaxBytes(size_t max_bytes) { rep->write_batch.SetMaxBytes(max_bytes); }