LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
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.

235 lines
7.7 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  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. typedef uint64_t TIMESTAMP;
  51. static const TIMESTAMP kTSLength = sizeof(TIMESTAMP);
  52. // We leave eight bits empty at the bottom so a type and sequence#
  53. // can be packed together into 64-bits.
  54. static const SequenceNumber kMaxSequenceNumber = ((0x1ull << 56) - 1);
  55. //static const uint32_t kTSLength = sizeof(uint64_t ); // size of timestamp
  56. struct ParsedInternalKey {
  57. Slice user_key;
  58. SequenceNumber sequence;
  59. ValueType type;
  60. ParsedInternalKey() {} // Intentionally left uninitialized (for speed)
  61. ParsedInternalKey(const Slice& u, const SequenceNumber& seq, ValueType t)
  62. : user_key(u), sequence(seq), type(t) {}
  63. std::string DebugString() const;
  64. };
  65. // Return the length of the encoding of "key".
  66. inline size_t InternalKeyEncodingLength(const ParsedInternalKey& key) {
  67. return key.user_key.size() + 8;
  68. }
  69. // Append the serialization of "key" to *result.
  70. void AppendInternalKey(std::string* result, const ParsedInternalKey& key);
  71. // Attempt to parse an internal key from "internal_key". On success,
  72. // stores the parsed data in "*result", and returns true.
  73. //
  74. // On error, returns false, leaves "*result" in an undefined state.
  75. bool ParseInternalKey(const Slice& internal_key, ParsedInternalKey* result);
  76. // Returns the user key portion of an internal key.
  77. inline Slice ExtractUserKey(const Slice& internal_key) {
  78. assert(internal_key.size() >= 8);
  79. return Slice(internal_key.data(), internal_key.size() - 8);
  80. }
  81. // A comparator for internal keys that uses a specified comparator for
  82. // the user key portion and breaks ties by decreasing sequence number.
  83. class InternalKeyComparator : public Comparator {
  84. private:
  85. const Comparator* user_comparator_;
  86. public:
  87. explicit InternalKeyComparator(const Comparator* c) : user_comparator_(c) {}
  88. const char* Name() const override;
  89. int Compare(const Slice& a, const Slice& b) const override;
  90. void FindShortestSeparator(std::string* start,
  91. const Slice& limit) const override;
  92. void FindShortSuccessor(std::string* key) const override;
  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. const char* Name() const override;
  103. void CreateFilter(const Slice* keys, int n, std::string* dst) const override;
  104. bool KeyMayMatch(const Slice& key, const Slice& filter) const override;
  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. bool DecodeFrom(const Slice& s) {
  118. rep_.assign(s.data(), s.size());
  119. return !rep_.empty();
  120. }
  121. Slice Encode() const {
  122. assert(!rep_.empty());
  123. return rep_;
  124. }
  125. Slice user_key() const { return ExtractUserKey(rep_); }
  126. void SetFrom(const ParsedInternalKey& p) {
  127. rep_.clear();
  128. AppendInternalKey(&rep_, p);
  129. }
  130. void Clear() { rep_.clear(); }
  131. std::string DebugString() const;
  132. };
  133. inline int InternalKeyComparator::Compare(const InternalKey& a,
  134. const InternalKey& b) const {
  135. return Compare(a.Encode(), b.Encode());
  136. }
  137. inline bool ParseInternalKey(const Slice& internal_key,
  138. ParsedInternalKey* result) {
  139. const size_t n = internal_key.size();
  140. if (n < 8) return false;
  141. uint64_t num = DecodeFixed64(internal_key.data() + n - 8);
  142. uint8_t c = num & 0xff;
  143. result->sequence = num >> 8;
  144. result->type = static_cast<ValueType>(c);
  145. result->user_key = Slice(internal_key.data(), n - 8);
  146. return (c <= static_cast<uint8_t>(kTypeValue));
  147. }
  148. // A helper class useful for DBImpl::Get()
  149. class LookupKey {
  150. public:
  151. // Initialize *this for looking up user_key at a snapshot with
  152. // the specified sequence number.
  153. LookupKey(const Slice& user_key, SequenceNumber sequence);
  154. LookupKey(const LookupKey&) = delete;
  155. LookupKey& operator=(const LookupKey&) = delete;
  156. ~LookupKey();
  157. // Return a key suitable for lookup in a MemTable.
  158. Slice memtable_key() const { return Slice(start_, end_ - start_); }
  159. // Return an internal key (suitable for passing to an internal iterator)
  160. Slice internal_key() const { return Slice(kstart_, end_ - kstart_); }
  161. // Return the user key
  162. Slice user_key() const { return Slice(kstart_, end_ - kstart_ - 8); }
  163. private:
  164. // We construct a char array of the form:
  165. // klength varint32 <-- start_
  166. // userkey char[klength] <-- kstart_
  167. // tag uint64
  168. // <-- end_
  169. // The array is a suitable MemTable key.
  170. // The suffix starting with "userkey" can be used as an InternalKey.
  171. const char* start_;
  172. const char* kstart_;
  173. const char* end_;
  174. char space_[200]; // Avoid allocation for short keys
  175. };
  176. inline LookupKey::~LookupKey() {
  177. if (start_ != space_) delete[] start_;
  178. }
  179. } // namespace leveldb
  180. #endif // STORAGE_LEVELDB_DB_DBFORMAT_H_