LCOV - code coverage report
Current view: top level - src/leveldb/db - write_batch.cc (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 77 95 81.1 %
Date: 2022-04-21 14:51:19 Functions: 15 18 83.3 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 12 18 66.7 %

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

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a