| 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 "leveldb/db.h" |
| 19 |
|
| #include "db/dbformat.h" |
| 20 |
|
| #include "db/memtable.h" |
| 21 |
|
| #include "db/write_batch_internal.h" |
| 22 |
|
| #include "util/coding.h" |
| 23 |
|
| |
| 24 |
|
| namespace leveldb { |
| 25 |
|
| |
| 26 |
22 |
| WriteBatch::WriteBatch() { |
| 27 |
22 |
▶ | Clear(); |
Branch: 1 branch
| Branch | True | False | Total |
| Clear(); |
22 |
0 |
22 |
|
| 28 |
22 |
| } |
| 29 |
|
| |
| 30 |
21 |
| WriteBatch::~WriteBatch() { } |
| 31 |
|
| |
| 32 |
0 |
| WriteBatch::Handler::~Handler() { } |
| 33 |
|
| |
| 34 |
22 |
| void WriteBatch::Clear() { |
| 35 |
22 |
| rep_.clear(); |
| 36 |
22 |
| rep_.resize(12); |
| 37 |
22 |
| } |
| 38 |
|
| |
| 39 |
31 |
| Status WriteBatch::Iterate(Handler* handler) const { |
| 40 |
31 |
| Slice input(rep_); |
| 41 |
31 |
▶ | if (input.size() < 12) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (input.size() < 12) { |
0 |
31 |
31 |
|
| 42 |
0 |
▶ | return Status::Corruption("malformed WriteBatch (too small)"); |
Branch: 1 branch
| Branch | True | False | Total |
| return Status::Corruption("malformed WriteBatch (too small)"); |
0 |
0 |
0 |
|
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
31 |
| input.remove_prefix(12); |
| 46 |
31 |
| Slice key, value; |
| 47 |
31 |
| int found = 0; |
| 48 |
62 |
▶ | while (!input.empty()) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!input.empty()) { |
31 |
31 |
62 |
|
| 49 |
31 |
| found++; |
| 50 |
31 |
| char tag = input[0]; |
| 51 |
31 |
| input.remove_prefix(1); |
| 52 |
31 |
| switch (tag) { |
| 53 |
16 |
| case kTypeValue: |
| 54 |
32 |
▶ | if (GetLengthPrefixedSlice(&input, &key) && |
Branch: 3 branches
| Branch | True | False | Total |
| if (GetLengthPrefixedSlice(&input, &key) && |
16 |
0 |
16 |
| if (GetLengthPrefixedSlice(&input, &key) && |
16 |
0 |
16 |
| if (GetLengthPrefixedSlice(&input, &key) && |
16 |
0 |
16 |
|
| 55 |
16 |
▶ | GetLengthPrefixedSlice(&input, &value)) { |
Branch: 2 branches
| Branch | True | False | Total |
| GetLengthPrefixedSlice(&input, &value)) { |
16 |
0 |
16 |
| GetLengthPrefixedSlice(&input, &value)) { |
16 |
0 |
16 |
|
| 56 |
16 |
▶ | handler->Put(key, value); |
Branch: 1 branch
| Branch | True | False | Total |
| handler->Put(key, value); |
16 |
0 |
16 |
|
| 57 |
|
| } else { |
| 58 |
0 |
▶ | return Status::Corruption("bad WriteBatch Put"); |
Branch: 1 branch
| Branch | True | False | Total |
| return Status::Corruption("bad WriteBatch Put"); |
0 |
0 |
0 |
|
| 59 |
|
| } |
| 60 |
16 |
| break; |
| 61 |
15 |
| case kTypeDeletion: |
| 62 |
15 |
▶ | if (GetLengthPrefixedSlice(&input, &key)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (GetLengthPrefixedSlice(&input, &key)) { |
15 |
0 |
15 |
| if (GetLengthPrefixedSlice(&input, &key)) { |
15 |
0 |
15 |
|
| 63 |
15 |
▶ | handler->Delete(key); |
Branch: 1 branch
| Branch | True | False | Total |
| handler->Delete(key); |
15 |
0 |
15 |
|
| 64 |
|
| } else { |
| 65 |
0 |
▶ | return Status::Corruption("bad WriteBatch Delete"); |
Branch: 1 branch
| Branch | True | False | Total |
| return Status::Corruption("bad WriteBatch Delete"); |
0 |
0 |
0 |
|
| 66 |
|
| } |
| 67 |
15 |
| break; |
| 68 |
0 |
| default: |
| 69 |
0 |
▶ | return Status::Corruption("unknown WriteBatch tag"); |
Branch: 1 branch
| Branch | True | False | Total |
| return Status::Corruption("unknown WriteBatch tag"); |
0 |
0 |
0 |
|
| 70 |
|
| } |
| 71 |
|
| } |
| 72 |
31 |
▶ | if (found != WriteBatchInternal::Count(this)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (found != WriteBatchInternal::Count(this)) { |
31 |
0 |
31 |
| if (found != WriteBatchInternal::Count(this)) { |
0 |
31 |
31 |
|
| 73 |
0 |
▶ | return Status::Corruption("WriteBatch has wrong count"); |
Branch: 1 branch
| Branch | True | False | Total |
| return Status::Corruption("WriteBatch has wrong count"); |
0 |
0 |
0 |
|
| 74 |
|
| } else { |
| 75 |
31 |
| return Status::OK(); |
| 76 |
|
| } |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
80 |
| int WriteBatchInternal::Count(const WriteBatch* b) { |
| 80 |
80 |
| return DecodeFixed32(b->rep_.data() + 8); |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
18 |
| void WriteBatchInternal::SetCount(WriteBatch* b, int n) { |
| 84 |
18 |
| EncodeFixed32(&b->rep_[8], n); |
| 85 |
18 |
| } |
| 86 |
|
| |
| 87 |
44 |
| SequenceNumber WriteBatchInternal::Sequence(const WriteBatch* b) { |
| 88 |
44 |
| return SequenceNumber(DecodeFixed64(b->rep_.data())); |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
18 |
| void WriteBatchInternal::SetSequence(WriteBatch* b, SequenceNumber seq) { |
| 92 |
18 |
| EncodeFixed64(&b->rep_[0], seq); |
| 93 |
18 |
| } |
| 94 |
|
| |
| 95 |
9 |
| void WriteBatch::Put(const Slice& key, const Slice& value) { |
| 96 |
9 |
| WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1); |
| 97 |
9 |
| rep_.push_back(static_cast<char>(kTypeValue)); |
| 98 |
9 |
| PutLengthPrefixedSlice(&rep_, key); |
| 99 |
9 |
| PutLengthPrefixedSlice(&rep_, value); |
| 100 |
9 |
| } |
| 101 |
|
| |
| 102 |
9 |
| void WriteBatch::Delete(const Slice& key) { |
| 103 |
9 |
| WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1); |
| 104 |
9 |
| rep_.push_back(static_cast<char>(kTypeDeletion)); |
| 105 |
9 |
| PutLengthPrefixedSlice(&rep_, key); |
| 106 |
9 |
| } |
| 107 |
|
| |
| 108 |
|
| namespace { |
| 109 |
|
| class MemTableInserter : public WriteBatch::Handler { |
| 110 |
|
| public: |
| 111 |
|
| SequenceNumber sequence_; |
| 112 |
|
| MemTable* mem_; |
| 113 |
|
| |
| 114 |
16 |
| virtual void Put(const Slice& key, const Slice& value) { |
| 115 |
16 |
| mem_->Add(sequence_, kTypeValue, key, value); |
| 116 |
16 |
| sequence_++; |
| 117 |
16 |
| } |
| 118 |
15 |
| virtual void Delete(const Slice& key) { |
| 119 |
15 |
▶ | mem_->Add(sequence_, kTypeDeletion, key, Slice()); |
Branch: 1 branch
| Branch | True | False | Total |
| mem_->Add(sequence_, kTypeDeletion, key, Slice()); |
15 |
0 |
15 |
|
| 120 |
15 |
| sequence_++; |
| 121 |
15 |
| } |
| 122 |
|
| }; |
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
31 |
| Status WriteBatchInternal::InsertInto(const WriteBatch* b, |
| 126 |
|
| MemTable* memtable) { |
| 127 |
31 |
| MemTableInserter inserter; |
| 128 |
31 |
▶ | inserter.sequence_ = WriteBatchInternal::Sequence(b); |
Branch: 1 branch
| Branch | True | False | Total |
| inserter.sequence_ = WriteBatchInternal::Sequence(b); |
31 |
0 |
31 |
|
| 129 |
31 |
| inserter.mem_ = memtable; |
| 130 |
62 |
▶ | return b->Iterate(&inserter); |
Branch: 1 branch
| Branch | True | False | Total |
| return b->Iterate(&inserter); |
31 |
0 |
31 |
|
| 131 |
31 |
| } |
| 132 |
|
| |
| 133 |
13 |
| void WriteBatchInternal::SetContents(WriteBatch* b, const Slice& contents) { |
| 134 |
13 |
▶ | assert(contents.size() >= 12); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(contents.size() >= 12); |
0 |
13 |
13 |
|
| 135 |
13 |
| b->rep_.assign(contents.data(), contents.size()); |
| 136 |
13 |
| } |
| 137 |
|
| |
| 138 |
|
| } |