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 Reserve method for fixed-size write batches #48

Open
wants to merge 1 commit into
base: bitcoin-fork
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions db/write_batch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ void WriteBatch::Clear() {
rep_.resize(kHeader);
}

void WriteBatch::Reserve(size_t size) {
rep_.reserve(size);
}

size_t WriteBatch::ApproximateSize() const { return rep_.size(); }

Status WriteBatch::Iterate(Handler* handler) const {
Expand Down
10 changes: 10 additions & 0 deletions db/write_batch_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ TEST(WriteBatchTest, ApproximateSize) {
WriteBatch batch;
size_t empty_size = batch.ApproximateSize();

// Reserve space for multiple operations
size_t expected_size = 35;
batch.Reserve(expected_size);

// The approximate size should still be the same as empty
// since Reserve doesn't affect the content size
ASSERT_EQ(empty_size, batch.ApproximateSize());

batch.Put(Slice("foo"), Slice("bar"));
size_t one_key_size = batch.ApproximateSize();
ASSERT_LT(empty_size, one_key_size);
Expand All @@ -130,6 +138,8 @@ TEST(WriteBatchTest, ApproximateSize) {
batch.Delete(Slice("box"));
size_t post_delete_size = batch.ApproximateSize();
ASSERT_LT(two_keys_size, post_delete_size);

ASSERT_EQ(expected_size, batch.ApproximateSize());
}

} // namespace leveldb
Expand Down
3 changes: 3 additions & 0 deletions include/leveldb/write_batch.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class LEVELDB_EXPORT WriteBatch {
// Clear all updates buffered in this batch.
void Clear();

// Preallocate memory for a batch containing a sequence of updates.
void Reserve(size_t size);

// The size of the database changes caused by this batch.
//
// This number is tied to implementation details, and may change across
Expand Down