You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 line
7.5 KiB

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