1
0
mirror of https://github.com/google/leveldb.git synced 2025-04-25 14:00:27 +08:00

Add Reserve method for fixed-size write batches

This commit is contained in:
Lőrinc 2025-03-24 14:32:30 +01:00
parent ac691084fd
commit 88841d904c
3 changed files with 17 additions and 0 deletions

View File

@ -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 {

View File

@ -116,6 +116,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);
@ -127,6 +135,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

View File

@ -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