Coverage Report

Created: 2025-06-10 13:21

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