Coverage Report

Created: 2025-07-18 07:11

/src/leveldb/db/write_batch.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file. See the AUTHORS file for names of contributors.
4
//
5
// WriteBatch::rep_ :=
6
//    sequence: fixed64
7
//    count: fixed32
8
//    data: record[count]
9
// record :=
10
//    kTypeValue varstring varstring         |
11
//    kTypeDeletion varstring
12
// varstring :=
13
//    len: varint32
14
//    data: uint8[len]
15
16
#include "leveldb/write_batch.h"
17
18
#include "db/dbformat.h"
19
#include "db/memtable.h"
20
#include "db/write_batch_internal.h"
21
#include "leveldb/db.h"
22
#include "util/coding.h"
23
24
namespace leveldb {
25
26
// WriteBatch header has an 8-byte sequence number followed by a 4-byte count.
27
static const size_t kHeader = 12;
28
29
2.31M
WriteBatch::WriteBatch() { Clear(); }
30
31
2.31M
WriteBatch::~WriteBatch() = default;
32
33
3.57M
WriteBatch::Handler::~Handler() = default;
34
35
2.31M
void WriteBatch::Clear() {
36
2.31M
  rep_.clear();
37
2.31M
  rep_.resize(kHeader);
38
2.31M
}
39
40
0
size_t WriteBatch::ApproximateSize() const { return rep_.size(); }
41
42
3.57M
Status WriteBatch::Iterate(Handler* handler) const {
43
3.57M
  Slice input(rep_);
44
3.57M
  if (input.size() < kHeader) {
45
0
    return Status::Corruption("malformed WriteBatch (too small)");
46
0
  }
47
48
3.57M
  input.remove_prefix(kHeader);
49
3.57M
  Slice key, value;
50
3.57M
  int found = 0;
51
7.14M
  while (!input.empty()) {
52
3.57M
    found++;
53
3.57M
    char tag = input[0];
54
3.57M
    input.remove_prefix(1);
55
3.57M
    switch (tag) {
56
76.6k
      case kTypeValue:
57
76.6k
        if (GetLengthPrefixedSlice(&input, &key) &&
58
76.6k
            GetLengthPrefixedSlice(&input, &value)) {
59
76.6k
          handler->Put(key, value);
60
76.6k
        } else {
61
0
          return Status::Corruption("bad WriteBatch Put");
62
0
        }
63
76.6k
        break;
64
3.49M
      case kTypeDeletion:
65
3.49M
        if (GetLengthPrefixedSlice(&input, &key)) {
66
3.49M
          handler->Delete(key);
67
3.49M
        } else {
68
0
          return Status::Corruption("bad WriteBatch Delete");
69
0
        }
70
3.49M
        break;
71
3.49M
      default:
72
0
        return Status::Corruption("unknown WriteBatch tag");
73
3.57M
    }
74
3.57M
  }
75
3.57M
  if (found != WriteBatchInternal::Count(this)) {
76
0
    return Status::Corruption("WriteBatch has wrong count");
77
3.57M
  } else {
78
3.57M
    return Status::OK();
79
3.57M
  }
80
3.57M
}
81
82
9.22M
int WriteBatchInternal::Count(const WriteBatch* b) {
83
9.22M
  return DecodeFixed32(b->rep_.data() + 8);
84
9.22M
}
85
86
2.07M
void WriteBatchInternal::SetCount(WriteBatch* b, int n) {
87
2.07M
  EncodeFixed32(&b->rep_[8], n);
88
2.07M
}
89
90
5.06M
SequenceNumber WriteBatchInternal::Sequence(const WriteBatch* b) {
91
5.06M
  return SequenceNumber(DecodeFixed64(b->rep_.data()));
92
5.06M
}
93
94
2.07M
void WriteBatchInternal::SetSequence(WriteBatch* b, SequenceNumber seq) {
95
2.07M
  EncodeFixed64(&b->rep_[0], seq);
96
2.07M
}
97
98
45.6k
void WriteBatch::Put(const Slice& key, const Slice& value) {
99
45.6k
  WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
100
45.6k
  rep_.push_back(static_cast<char>(kTypeValue));
101
45.6k
  PutLengthPrefixedSlice(&rep_, key);
102
45.6k
  PutLengthPrefixedSlice(&rep_, value);
103
45.6k
}
104
105
2.03M
void WriteBatch::Delete(const Slice& key) {
106
2.03M
  WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
107
2.03M
  rep_.push_back(static_cast<char>(kTypeDeletion));
108
2.03M
  PutLengthPrefixedSlice(&rep_, key);
109
2.03M
}
110
111
0
void WriteBatch::Append(const WriteBatch& source) {
112
0
  WriteBatchInternal::Append(this, &source);
113
0
}
114
115
namespace {
116
class MemTableInserter : public WriteBatch::Handler {
117
 public:
118
  SequenceNumber sequence_;
119
  MemTable* mem_;
120
121
76.6k
  void Put(const Slice& key, const Slice& value) override {
122
76.6k
    mem_->Add(sequence_, kTypeValue, key, value);
123
76.6k
    sequence_++;
124
76.6k
  }
125
3.49M
  void Delete(const Slice& key) override {
126
3.49M
    mem_->Add(sequence_, kTypeDeletion, key, Slice());
127
3.49M
    sequence_++;
128
3.49M
  }
129
};
130
}  // namespace
131
132
3.57M
Status WriteBatchInternal::InsertInto(const WriteBatch* b, MemTable* memtable) {
133
3.57M
  MemTableInserter inserter;
134
3.57M
  inserter.sequence_ = WriteBatchInternal::Sequence(b);
135
3.57M
  inserter.mem_ = memtable;
136
3.57M
  return b->Iterate(&inserter);
137
3.57M
}
138
139
1.49M
void WriteBatchInternal::SetContents(WriteBatch* b, const Slice& contents) {
140
1.49M
  assert(contents.size() >= kHeader);
141
1.49M
  b->rep_.assign(contents.data(), contents.size());
142
1.49M
}
143
144
0
void WriteBatchInternal::Append(WriteBatch* dst, const WriteBatch* src) {
145
0
  SetCount(dst, Count(dst) + Count(src));
146
0
  assert(src->rep_.size() >= kHeader);
147
0
  dst->rep_.append(src->rep_.data() + kHeader, src->rep_.size() - kHeader);
148
0
}
149
150
}  // namespace leveldb