Coverage Report

Created: 2026-04-09 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/leveldb/db/write_batch.cc
Line
Count
Source
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
1.68M
WriteBatch::WriteBatch() { Clear(); }
30
31
1.68M
WriteBatch::~WriteBatch() = default;
32
33
2.52M
WriteBatch::Handler::~Handler() = default;
34
35
1.68M
void WriteBatch::Clear() {
36
1.68M
  rep_.clear();
37
1.68M
  rep_.resize(kHeader);
38
1.68M
}
39
40
0
size_t WriteBatch::ApproximateSize() const { return rep_.size(); }
41
42
2.52M
Status WriteBatch::Iterate(Handler* handler) const {
43
2.52M
  Slice input(rep_);
44
2.52M
  if (input.size() < kHeader) {
45
0
    return Status::Corruption("malformed WriteBatch (too small)");
46
0
  }
47
48
2.52M
  input.remove_prefix(kHeader);
49
2.52M
  Slice key, value;
50
2.52M
  int found = 0;
51
5.04M
  while (!input.empty()) {
52
2.52M
    found++;
53
2.52M
    char tag = input[0];
54
2.52M
    input.remove_prefix(1);
55
2.52M
    switch (tag) {
56
76.8k
      case kTypeValue:
57
76.8k
        if (GetLengthPrefixedSlice(&input, &key) &&
58
76.8k
            GetLengthPrefixedSlice(&input, &value)) {
59
76.8k
          handler->Put(key, value);
60
76.8k
        } else {
61
0
          return Status::Corruption("bad WriteBatch Put");
62
0
        }
63
76.8k
        break;
64
2.44M
      case kTypeDeletion:
65
2.44M
        if (GetLengthPrefixedSlice(&input, &key)) {
66
2.44M
          handler->Delete(key);
67
2.44M
        } else {
68
0
          return Status::Corruption("bad WriteBatch Delete");
69
0
        }
70
2.44M
        break;
71
2.44M
      default:
72
0
        return Status::Corruption("unknown WriteBatch tag");
73
2.52M
    }
74
2.52M
  }
75
2.52M
  if (found != WriteBatchInternal::Count(this)) {
76
0
    return Status::Corruption("WriteBatch has wrong count");
77
2.52M
  } else {
78
2.52M
    return Status::OK();
79
2.52M
  }
80
2.52M
}
81
82
6.50M
int WriteBatchInternal::Count(const WriteBatch* b) {
83
6.50M
  return DecodeFixed32(b->rep_.data() + 8);
84
6.50M
}
85
86
1.45M
void WriteBatchInternal::SetCount(WriteBatch* b, int n) {
87
1.45M
  EncodeFixed32(&b->rep_[8], n);
88
1.45M
}
89
90
3.58M
SequenceNumber WriteBatchInternal::Sequence(const WriteBatch* b) {
91
3.58M
  return SequenceNumber(DecodeFixed64(b->rep_.data()));
92
3.58M
}
93
94
1.45M
void WriteBatchInternal::SetSequence(WriteBatch* b, SequenceNumber seq) {
95
1.45M
  EncodeFixed64(&b->rep_[0], seq);
96
1.45M
}
97
98
46.5k
void WriteBatch::Put(const Slice& key, const Slice& value) {
99
46.5k
  WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
100
46.5k
  rep_.push_back(static_cast<char>(kTypeValue));
101
46.5k
  PutLengthPrefixedSlice(&rep_, key);
102
46.5k
  PutLengthPrefixedSlice(&rep_, value);
103
46.5k
}
104
105
1.41M
void WriteBatch::Delete(const Slice& key) {
106
1.41M
  WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
107
1.41M
  rep_.push_back(static_cast<char>(kTypeDeletion));
108
1.41M
  PutLengthPrefixedSlice(&rep_, key);
109
1.41M
}
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.8k
  void Put(const Slice& key, const Slice& value) override {
122
76.8k
    mem_->Add(sequence_, kTypeValue, key, value);
123
76.8k
    sequence_++;
124
76.8k
  }
125
2.44M
  void Delete(const Slice& key) override {
126
2.44M
    mem_->Add(sequence_, kTypeDeletion, key, Slice());
127
2.44M
    sequence_++;
128
2.44M
  }
129
};
130
}  // namespace
131
132
2.52M
Status WriteBatchInternal::InsertInto(const WriteBatch* b, MemTable* memtable) {
133
2.52M
  MemTableInserter inserter;
134
2.52M
  inserter.sequence_ = WriteBatchInternal::Sequence(b);
135
2.52M
  inserter.mem_ = memtable;
136
2.52M
  return b->Iterate(&inserter);
137
2.52M
}
138
139
1.06M
void WriteBatchInternal::SetContents(WriteBatch* b, const Slice& contents) {
140
1.06M
  assert(contents.size() >= kHeader);
141
1.06M
  b->rep_.assign(contents.data(), contents.size());
142
1.06M
}
143
144
0
void WriteBatchInternal::Append(WriteBatch* dst, const WriteBatch* src) {
145
0
  SetCount(dst, Count(dst) + Count(src));
146
  assert(src->rep_.size() >= kHeader);
147
0
  dst->rep_.append(src->rep_.data() + kHeader, src->rep_.size() - kHeader);
148
0
}
149
150
}  // namespace leveldb