Skip to content

Commit 69e2bd2

Browse files
committed
LevelDB: Add WriteBatch::ApproximateSize().
This can be used to report metrics on LevelDB usage. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=156934930
1 parent a53934a commit 69e2bd2

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

db/write_batch.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ void WriteBatch::Clear() {
3939
rep_.resize(kHeader);
4040
}
4141

42+
size_t WriteBatch::ApproximateSize() {
43+
return rep_.size();
44+
}
45+
4246
Status WriteBatch::Iterate(Handler* handler) const {
4347
Slice input(rep_);
4448
if (input.size() < kHeader) {

db/write_batch_test.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,23 @@ TEST(WriteBatchTest, Append) {
113113
PrintContents(&b1));
114114
}
115115

116+
TEST(WriteBatchTest, ApproximateSize) {
117+
WriteBatch batch;
118+
size_t empty_size = batch.ApproximateSize();
119+
120+
batch.Put(Slice("foo"), Slice("bar"));
121+
size_t one_key_size = batch.ApproximateSize();
122+
ASSERT_LT(empty_size, one_key_size);
123+
124+
batch.Put(Slice("baz"), Slice("boo"));
125+
size_t two_keys_size = batch.ApproximateSize();
126+
ASSERT_LT(one_key_size, two_keys_size);
127+
128+
batch.Delete(Slice("box"));
129+
size_t post_delete_size = batch.ApproximateSize();
130+
ASSERT_LT(two_keys_size, post_delete_size);
131+
}
132+
116133
} // namespace leveldb
117134

118135
int main(int argc, char** argv) {

include/leveldb/write_batch.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class WriteBatch {
4242
// Clear all updates buffered in this batch.
4343
void Clear();
4444

45+
// The size of the database changes caused by this batch.
46+
//
47+
// This number is tied to implementation details, and may change across
48+
// releases. It is intended for LevelDB usage metrics.
49+
size_t ApproximateSize();
50+
4551
// Support for iterating over the contents of a batch.
4652
class Handler {
4753
public:

0 commit comments

Comments
 (0)