db/dbformat.h

filter: Statementstmt 81.4% 22 / 27 Branchbran 30.0% 3 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 67.5% 25 / 37 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/leveldb/table_builder.h ↑ Summary util/posix_logger.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 #ifndef STORAGE_LEVELDB_DB_FORMAT_H_
6 #define STORAGE_LEVELDB_DB_FORMAT_H_
7  
8 #include <stdio.h>
9 #include "leveldb/comparator.h"
10 #include "leveldb/db.h"
11 #include "leveldb/slice.h"
12 #include "leveldb/table_builder.h"
13 #include "util/coding.h"
14 #include "util/logging.h"
15  
16 namespace leveldb {
17  
18 // Grouping of constants. We may want to make some of these
19 // parameters set via options.
20 namespace config {
21 static const int kNumLevels = 7;
22  
23 // Level-0 compaction is started when we hit this many files.
24 static const int kL0_CompactionTrigger = 4;
25  
26 // Soft limit on number of level-0 files. We slow down writes at this point.
27 static const int kL0_SlowdownWritesTrigger = 8;
28  
29 // Maximum number of level-0 files. We stop writes at this point.
30 static const int kL0_StopWritesTrigger = 12;
31  
32 // Maximum level to which a new compacted memtable is pushed if it
33 // does not create overlap. We try to push to level 2 to avoid the
34 // relatively expensive level 0=>1 compactions and to avoid some
35 // expensive manifest file operations. We do not push all the way to
36 // the largest level since that can generate a lot of wasted disk
37 // space if the same key space is being repeatedly overwritten.
38 static const int kMaxMemCompactLevel = 2;
39  
40 }
41  
42 class InternalKey;
43  
44 // Value types encoded as the last component of internal keys.
45 // DO NOT CHANGE THESE ENUM VALUES: they are embedded in the on-disk
46 // data structures.
47 enum ValueType {
48 kTypeDeletion = 0x0,
49 kTypeValue = 0x1
50 };
51 // kValueTypeForSeek defines the ValueType that should be passed when
52 // constructing a ParsedInternalKey object for seeking to a particular
53 // sequence number (since we sort sequence numbers in decreasing order
54 // and the value type is embedded as the low 8 bits in the sequence
55 // number in internal keys, we need to use the highest-numbered
56 // ValueType, not the lowest).
57 static const ValueType kValueTypeForSeek = kTypeValue;
58  
59 typedef uint64_t SequenceNumber;
60  
61 // We leave eight bits empty at the bottom so a type and sequence#
62 // can be packed together into 64-bits.
63 static const SequenceNumber kMaxSequenceNumber =
64 ((0x1ull << 56) - 1);
65  
66 struct ParsedInternalKey {
67 Slice user_key;
68 SequenceNumber sequence;
69 ValueType type;
70  
71 95 ParsedInternalKey() { } // Intentionally left uninitialized (for speed)
72 6 ParsedInternalKey(const Slice& u, const SequenceNumber& seq, ValueType t)
73 6 : user_key(u), sequence(seq), type(t) { }
74 std::string DebugString() const;
75 };
76  
77 // Return the length of the encoding of "key".
78 inline size_t InternalKeyEncodingLength(const ParsedInternalKey& key) {
79 return key.user_key.size() + 8;
80 }
81  
82 // Append the serialization of "key" to *result.
83 extern void AppendInternalKey(std::string* result,
84 const ParsedInternalKey& key);
85  
86 // Attempt to parse an internal key from "internal_key". On success,
87 // stores the parsed data in "*result", and returns true.
88 //
89 // On error, returns false, leaves "*result" in an undefined state.
90 extern bool ParseInternalKey(const Slice& internal_key,
91 ParsedInternalKey* result);
92  
93 // Returns the user key portion of an internal key.
94 341 inline Slice ExtractUserKey(const Slice& internal_key) {
95 341 ▶ assert(internal_key.size() >= 8);
Branch: 1 branch
BranchTrueFalseTotal
assert(internal_key.size() >= 8); 0 341 341
96 341 return Slice(internal_key.data(), internal_key.size() - 8);
97 }
98  
99 inline ValueType ExtractValueType(const Slice& internal_key) {
100 assert(internal_key.size() >= 8);
101 const size_t n = internal_key.size();
102 uint64_t num = DecodeFixed64(internal_key.data() + n - 8);
103 unsigned char c = num & 0xff;
104 return static_cast<ValueType>(c);
105 }
106  
107 // A comparator for internal keys that uses a specified comparator for
108 // the user key portion and breaks ties by decreasing sequence number.
109 class InternalKeyComparator : public Comparator {
110 private:
111 const Comparator* user_comparator_;
112 public:
113 4 explicit InternalKeyComparator(const Comparator* c) : user_comparator_(c) { }
114 virtual const char* Name() const;
115 virtual int Compare(const Slice& a, const Slice& b) const;
116 virtual void FindShortestSeparator(
117 std::string* start,
118 const Slice& limit) const;
119 virtual void FindShortSuccessor(std::string* key) const;
120  
121 30 const Comparator* user_comparator() const { return user_comparator_; }
122  
123 int Compare(const InternalKey& a, const InternalKey& b) const;
124 };
125  
126 // Modules in this directory should keep internal keys wrapped inside
127 // the following class instead of plain strings so that we do not
128 // incorrectly use string comparisons instead of an InternalKeyComparator.
129 class InternalKey {
130 private:
131 std::string rep_;
132 public:
133 16 InternalKey() { } // Leave rep_ as empty to indicate it is invalid
134 0 InternalKey(const Slice& user_key, SequenceNumber s, ValueType t) {
135 0 ▶ AppendInternalKey(&rep_, ParsedInternalKey(user_key, s, t));
Branch: 1 branch
BranchTrueFalseTotal
AppendInternalKey(&rep_, ParsedInternalKey(user_key, s, t)); 0 0 0
136 0 }
137  
138 0 void DecodeFrom(const Slice& s) { rep_.assign(s.data(), s.size()); }
139 Slice Encode() const {
140 assert(!rep_.empty());
141 return rep_;
142 }
143  
144 2 Slice user_key() const { return ExtractUserKey(rep_); }
145  
146 void SetFrom(const ParsedInternalKey& p) {
147 rep_.clear();
148 AppendInternalKey(&rep_, p);
149 }
150  
151 0 void Clear() { rep_.clear(); }
152 };
153  
154 inline int InternalKeyComparator::Compare(
155 const InternalKey& a, const InternalKey& b) const {
156 return Compare(a.Encode(), b.Encode());
157 }
158  
159 95 inline bool ParseInternalKey(const Slice& internal_key,
160 ParsedInternalKey* result) {
161 95 const size_t n = internal_key.size();
162 95 ▶ if (n < 8) return false;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 8) return false; 0 95 95
163 95 uint64_t num = DecodeFixed64(internal_key.data() + n - 8);
164 95 unsigned char c = num & 0xff;
165 95 result->sequence = num >> 8;
166 95 result->type = static_cast<ValueType>(c);
167 95 result->user_key = Slice(internal_key.data(), n - 8);
168 95 return (c <= static_cast<unsigned char>(kTypeValue));
169 }
170  
171 // A helper class useful for DBImpl::Get()
172 class LookupKey {
173 public:
174 // Initialize *this for looking up user_key at a snapshot with
175 // the specified sequence number.
176 LookupKey(const Slice& user_key, SequenceNumber sequence);
177  
178 ~LookupKey();
179  
180 // Return a key suitable for lookup in a MemTable.
181 Slice memtable_key() const { return Slice(start_, end_ - start_); }
182  
183 // Return an internal key (suitable for passing to an internal iterator)
184 Slice internal_key() const { return Slice(kstart_, end_ - kstart_); }
185  
186 // Return the user key
187 Slice user_key() const { return Slice(kstart_, end_ - kstart_ - 8); }
188  
189 private:
190 // We construct a char array of the form:
191 // klength varint32 <-- start_
192 // userkey char[klength] <-- kstart_
193 // tag uint64
194 // <-- end_
195 // The array is a suitable MemTable key.
196 // The suffix starting with "userkey" can be used as an InternalKey.
197 const char* start_;
198 const char* kstart_;
199 const char* end_;
200 char space_[200]; // Avoid allocation for short keys
201  
202 // No copying allowed
203 LookupKey(const LookupKey&);
204 void operator=(const LookupKey&);
205 };
206  
207 8 inline LookupKey::~LookupKey() {
208 8 ▶ if (start_ != space_) delete[] start_;
Branch: 2 branches
BranchTrueFalseTotal
if (start_ != space_) delete[] start_; 0 8 8
if (start_ != space_) delete[] start_; 0 0 0
209 8 }
210  
211 }
212  
213 #endif // STORAGE_LEVELDB_DB_FORMAT_H_
« include/leveldb/table_builder.h ↑ Summary util/posix_logger.h »