Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add WriteBatch::Release() #11482

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## Unreleased
### New Features
* Add a new option OptimisticTransactionDBOptions::shared_lock_buckets that enables sharing mutexes for validating transactions between DB instances, for better balancing memory efficiency and validation contention across DB instances. Different column families and DBs also now use different hash seeds in this validation, so that the same set of key names will not contend across DBs or column families.
* Add `WriteBatch::Release()` that releases the batch's serialized data to the caller.

### Public API Changes
* Add `WaitForCompact()` to wait for all flush and compactions jobs to finish. Jobs to wait include the unscheduled (queued, but not scheduled yet).
Expand Down
6 changes: 6 additions & 0 deletions db/write_batch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ size_t WriteBatch::GetProtectionBytesPerKey() const {
return 0;
}

std::string WriteBatch::Release() {
std::string ret = std::move(rep_);
Clear();
return ret;
}

bool WriteBatch::HasPut() const {
return (ComputeContentFlags() & ContentFlags::HAS_PUT) != 0;
}
Expand Down
16 changes: 16 additions & 0 deletions db/write_batch_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,22 @@ TEST_F(WriteBatchTest, SingleDeletion) {
ASSERT_EQ(2u, batch.Count());
}

TEST_F(WriteBatchTest, OwnershipTransfer) {
Random rnd(301);
WriteBatch put_batch;
put_batch.Put(rnd.RandomString(16) /* key */,
rnd.RandomString(1024) /* value */);

// (1) Verify `Release()` transfers string data ownership
const char* expected_data = put_batch.Data().data();
std::string batch_str = put_batch.Release();
ASSERT_EQ(expected_data, batch_str.data());

// (2) Verify constructor transfers string data ownership
WriteBatch move_batch(std::move(batch_str));
ASSERT_EQ(expected_data, move_batch.Data().data());
}

namespace {
struct TestHandler : public WriteBatch::Handler {
std::string seen;
Expand Down
3 changes: 3 additions & 0 deletions include/rocksdb/write_batch.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ class WriteBatch : public WriteBatchBase {
// Retrieve the serialized version of this batch.
const std::string& Data() const { return rep_; }

// Release the serialized data and clear this batch.
std::string Release();

// Retrieve data size of the batch.
size_t GetDataSize() const { return rep_.size(); }

Expand Down