Coverage Report

Created: 2026-02-14 07:08

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