Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

205 Zeilen
6.6 KiB

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