Skip to content

Commit

Permalink
add WriteBatch::Release() (facebook#11482)
Browse files Browse the repository at this point in the history
Summary:
Together with the existing constructor,
`explicit WriteBatch(std::string&& rep)`, this enables transferring `WriteBatch` via its `std::string` representation. Associated info like KV checksums are dropped but the caller can use `WriteBatch::VerifyChecksum()` before taking ownership if needed.

Pull Request resolved: facebook#11482

Reviewed By: cbi42

Differential Revision: D46233884

Pulled By: ajkr

fbshipit-source-id: 6bc64a6e75fb7bbf61d08c09520fc3705a7b44d8
  • Loading branch information
ajkr authored and facebook-github-bot committed May 27, 2023
1 parent de1dd4c commit 3e7fc88
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
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;
ASSERT_OK(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

0 comments on commit 3e7fc88

Please sign in to comment.