db/memtable.cc

filter: Statementstmt 97.2% 71 / 73 Branchbran 50.0% 30 / 60 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.9% 101 / 133 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« db/filename.cc ↑ Summary util/random.h »
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 #include "db/memtable.h"
6 #include "db/dbformat.h"
7 #include "leveldb/comparator.h"
8 #include "leveldb/env.h"
9 #include "leveldb/iterator.h"
10 #include "util/coding.h"
11  
12 namespace leveldb {
13  
14 393 static Slice GetLengthPrefixedSlice(const char* data) {
15 uint32_t len;
16 393 const char* p = data;
17 393 ▶ p = GetVarint32Ptr(p, p + 5, &len); // +5: we assume "p" is not corrupted
Branch: 1 branch
BranchTrueFalseTotal
p = GetVarint32Ptr(p, p + 5, &len); // +5: we assume "p" is not corrupted 393 0 393
18 393 return Slice(p, len);
19 }
20  
21 5 MemTable::MemTable(const InternalKeyComparator& cmp)
22 5 : comparator_(cmp),
23 5 refs_(0),
24 5 ▶ table_(comparator_, &arena_) {
Branch: 2 branches
BranchTrueFalseTotal
table_(comparator_, &arena_) { 5 0 5
table_(comparator_, &arena_) { 5 0 5
25 5 }
26  
27 5 MemTable::~MemTable() {
28 5 ▶ assert(refs_ == 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(refs_ == 0); 0 5 5
29 5 }
30  
31 31 size_t MemTable::ApproximateMemoryUsage() { return arena_.MemoryUsage(); }
32  
33 128 int MemTable::KeyComparator::operator()(const char* aptr, const char* bptr)
34 const {
35 // Internal keys are encoded as length-prefixed strings.
36 128 ▶ Slice a = GetLengthPrefixedSlice(aptr);
Branch: 1 branch
BranchTrueFalseTotal
Slice a = GetLengthPrefixedSlice(aptr); 128 0 128
37 128 ▶ Slice b = GetLengthPrefixedSlice(bptr);
Branch: 1 branch
BranchTrueFalseTotal
Slice b = GetLengthPrefixedSlice(bptr); 128 0 128
38 256 ▶ return comparator.Compare(a, b);
Branch: 1 branch
BranchTrueFalseTotal
return comparator.Compare(a, b); 128 0 128
39 }
40  
41 // Encode a suitable internal key target for "target" and return it.
42 // Uses *scratch as scratch space, and the returned pointer will point
43 // into this scratch space.
44 6 static const char* EncodeKey(std::string* scratch, const Slice& target) {
45 6 scratch->clear();
46 6 PutVarint32(scratch, target.size());
47 6 scratch->append(target.data(), target.size());
48 6 return scratch->data();
49 }
50  
51 class MemTableIterator: public Iterator {
52 public:
53 15 explicit MemTableIterator(MemTable::Table* table) : iter_(table) { }
54  
55 120 virtual bool Valid() const { return iter_.Valid(); }
56 6 ▶ virtual void Seek(const Slice& k) { iter_.Seek(EncodeKey(&tmp_, k)); }
Branch: 2 branches
BranchTrueFalseTotal
virtual void Seek(const Slice& k) { iter_.Seek(EncodeKey(&tmp_, k)); } 6 0 6
virtual void Seek(const Slice& k) { iter_.Seek(EncodeKey(&tmp_, k)); } 6 0 6
57 9 virtual void SeekToFirst() { iter_.SeekToFirst(); }
58 0 virtual void SeekToLast() { iter_.SeekToLast(); }
59 83 virtual void Next() { iter_.Next(); }
60 0 virtual void Prev() { iter_.Prev(); }
61 108 virtual Slice key() const { return GetLengthPrefixedSlice(iter_.key()); }
62 13 virtual Slice value() const {
63 13 ▶ Slice key_slice = GetLengthPrefixedSlice(iter_.key());
Branch: 1 branch
BranchTrueFalseTotal
Slice key_slice = GetLengthPrefixedSlice(iter_.key()); 13 0 13
64 26 ▶ return GetLengthPrefixedSlice(key_slice.data() + key_slice.size());
Branch: 1 branch
BranchTrueFalseTotal
return GetLengthPrefixedSlice(key_slice.data() + key_slice.size()); 13 0 13
65 }
66  
67 1 virtual Status status() const { return Status::OK(); }
68  
69 private:
70 MemTable::Table::Iterator iter_;
71 std::string tmp_; // For passing to EncodeKey
72  
73 // No copying allowed
74 MemTableIterator(const MemTableIterator&);
75 void operator=(const MemTableIterator&);
76 };
77  
78 15 Iterator* MemTable::NewIterator() {
79 15 ▶ return new MemTableIterator(&table_);
Branch: 2 branches
BranchTrueFalseTotal
return new MemTableIterator(&table_); 15 0 15
return new MemTableIterator(&table_); 0 0 0
80 }
81  
82 31 void MemTable::Add(SequenceNumber s, ValueType type,
83 const Slice& key,
84 const Slice& value) {
85 // Format of an entry is concatenation of:
86 // key_size : varint32 of internal_key.size()
87 // key bytes : char[internal_key.size()]
88 // value_size : varint32 of value.size()
89 // value bytes : char[value.size()]
90 31 size_t key_size = key.size();
91 31 size_t val_size = value.size();
92 31 size_t internal_key_size = key_size + 8;
93 const size_t encoded_len =
94 31 ▶ VarintLength(internal_key_size) + internal_key_size +
Branch: 1 branch
BranchTrueFalseTotal
VarintLength(internal_key_size) + internal_key_size + 31 0 31
95 31 ▶ VarintLength(val_size) + val_size;
Branch: 1 branch
BranchTrueFalseTotal
VarintLength(val_size) + val_size; 31 0 31
96 31 ▶ char* buf = arena_.Allocate(encoded_len);
Branch: 1 branch
BranchTrueFalseTotal
char* buf = arena_.Allocate(encoded_len); 31 0 31
97 31 ▶ char* p = EncodeVarint32(buf, internal_key_size);
Branch: 1 branch
BranchTrueFalseTotal
char* p = EncodeVarint32(buf, internal_key_size); 31 0 31
98 31 memcpy(p, key.data(), key_size);
99 31 p += key_size;
100 31 ▶ EncodeFixed64(p, (s << 8) | type);
Branch: 1 branch
BranchTrueFalseTotal
EncodeFixed64(p, (s << 8) | type); 31 0 31
101 31 p += 8;
102 31 ▶ p = EncodeVarint32(p, val_size);
Branch: 1 branch
BranchTrueFalseTotal
p = EncodeVarint32(p, val_size); 31 0 31
103 31 memcpy(p, value.data(), val_size);
104 31 ▶ assert((p + val_size) - buf == encoded_len);
Branch: 1 branch
BranchTrueFalseTotal
assert((p + val_size) - buf == encoded_len); 0 31 31
105 31 ▶ table_.Insert(buf);
Branch: 1 branch
BranchTrueFalseTotal
table_.Insert(buf); 31 0 31
106 31 }
107  
108 8 bool MemTable::Get(const LookupKey& key, std::string* value, Status* s) {
109 8 Slice memkey = key.memtable_key();
110 8 Table::Iterator iter(&table_);
111 8 ▶ iter.Seek(memkey.data());
Branch: 1 branch
BranchTrueFalseTotal
iter.Seek(memkey.data()); 8 0 8
112 8 ▶ if (iter.Valid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter.Valid()) { 6 2 8
113 // entry format is:
114 // klength varint32
115 // userkey char[klength]
116 // tag uint64
117 // vlength varint32
118 // value char[vlength]
119 // Check that it belongs to same user key. We do not check the
120 // sequence number since the Seek() call above should have skipped
121 // all entries with overly large sequence numbers.
122 6 const char* entry = iter.key();
123 uint32_t key_length;
124 6 ▶ const char* key_ptr = GetVarint32Ptr(entry, entry+5, &key_length);
Branch: 1 branch
BranchTrueFalseTotal
const char* key_ptr = GetVarint32Ptr(entry, entry+5, &key_length); 6 0 6
125 12 ▶ if (comparator_.comparator.user_comparator()->Compare(
Branch: 1 branch
BranchTrueFalseTotal
if (comparator_.comparator.user_comparator()->Compare( 6 0 6
126 6 Slice(key_ptr, key_length - 8),
127 12 ▶ key.user_key()) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
key.user_key()) == 0) { 6 0 6
128 // Correct user key
129 6 const uint64_t tag = DecodeFixed64(key_ptr + key_length - 8);
130 6 switch (static_cast<ValueType>(tag & 0xff)) {
131 3 case kTypeValue: {
132 3 ▶ Slice v = GetLengthPrefixedSlice(key_ptr + key_length);
Branch: 1 branch
BranchTrueFalseTotal
Slice v = GetLengthPrefixedSlice(key_ptr + key_length); 3 0 3
133 3 ▶ value->assign(v.data(), v.size());
Branch: 1 branch
BranchTrueFalseTotal
value->assign(v.data(), v.size()); 3 0 3
134 3 return true;
135 }
136 3 case kTypeDeletion:
137 3 ▶ *s = Status::NotFound(Slice());
Branch: 2 branches
BranchTrueFalseTotal
*s = Status::NotFound(Slice()); 3 0 3
*s = Status::NotFound(Slice()); 3 0 3
138 3 return true;
139 }
140 }
141 }
142 2 return false;
143 }
144  
145 }
« db/filename.cc ↑ Summary util/random.h »