dbformat.h 7.4 KB

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