Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

230 wiersze
7.7 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/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. extern void AppendInternalKey(std::string* result,
  70. 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. extern bool ParseInternalKey(const Slice& internal_key,
  76. ParsedInternalKey* result);
  77. // Returns the user key portion of an internal key.
  78. inline Slice ExtractUserKey(const Slice& internal_key) {
  79. assert(internal_key.size() >= 8);
  80. return Slice(internal_key.data(), internal_key.size() - 8);
  81. }
  82. inline ValueType ExtractValueType(const Slice& internal_key) {
  83. assert(internal_key.size() >= 8);
  84. const size_t n = internal_key.size();
  85. uint64_t num = DecodeFixed64(internal_key.data() + n - 8);
  86. unsigned char c = num & 0xff;
  87. return static_cast<ValueType>(c);
  88. }
  89. // A comparator for internal keys that uses a specified comparator for
  90. // the user key portion and breaks ties by decreasing sequence number.
  91. class InternalKeyComparator : public Comparator {
  92. private:
  93. const Comparator* user_comparator_;
  94. public:
  95. explicit InternalKeyComparator(const Comparator* c) : user_comparator_(c) { }
  96. virtual const char* Name() const;
  97. virtual int Compare(const Slice& a, const Slice& b) const;
  98. virtual void FindShortestSeparator(
  99. std::string* start,
  100. const Slice& limit) const;
  101. virtual void FindShortSuccessor(std::string* key) const;
  102. const Comparator* user_comparator() const { return user_comparator_; }
  103. int Compare(const InternalKey& a, const InternalKey& b) const;
  104. };
  105. // Filter policy wrapper that converts from internal keys to user keys
  106. class InternalFilterPolicy : public FilterPolicy {
  107. private:
  108. const FilterPolicy* const user_policy_;
  109. public:
  110. explicit InternalFilterPolicy(const FilterPolicy* p) : user_policy_(p) { }
  111. virtual const char* Name() const;
  112. virtual void CreateFilter(const Slice* keys, int n, std::string* dst) const;
  113. virtual bool KeyMayMatch(const Slice& key, const Slice& filter) const;
  114. };
  115. // Modules in this directory should keep internal keys wrapped inside
  116. // the following class instead of plain strings so that we do not
  117. // incorrectly use string comparisons instead of an InternalKeyComparator.
  118. class InternalKey {
  119. private:
  120. std::string rep_;
  121. public:
  122. InternalKey() { } // Leave rep_ as empty to indicate it is invalid
  123. InternalKey(const Slice& user_key, SequenceNumber s, ValueType t) {
  124. AppendInternalKey(&rep_, ParsedInternalKey(user_key, s, t));
  125. }
  126. void DecodeFrom(const Slice& s) { rep_.assign(s.data(), s.size()); }
  127. Slice Encode() const {
  128. assert(!rep_.empty());
  129. return rep_;
  130. }
  131. Slice user_key() const { return ExtractUserKey(rep_); }
  132. void SetFrom(const ParsedInternalKey& p) {
  133. rep_.clear();
  134. AppendInternalKey(&rep_, p);
  135. }
  136. void Clear() { rep_.clear(); }
  137. std::string DebugString() const;
  138. };
  139. inline int InternalKeyComparator::Compare(
  140. const InternalKey& a, const InternalKey& b) const {
  141. return Compare(a.Encode(), b.Encode());
  142. }
  143. inline bool ParseInternalKey(const Slice& internal_key,
  144. ParsedInternalKey* result) {
  145. const size_t n = internal_key.size();
  146. if (n < 8) return false;
  147. uint64_t num = DecodeFixed64(internal_key.data() + n - 8);
  148. unsigned char c = num & 0xff;
  149. result->sequence = num >> 8;
  150. result->type = static_cast<ValueType>(c);
  151. result->user_key = Slice(internal_key.data(), n - 8);
  152. return (c <= static_cast<unsigned char>(kTypeValue));
  153. }
  154. // A helper class useful for DBImpl::Get()
  155. class LookupKey {
  156. public:
  157. // Initialize *this for looking up user_key at a snapshot with
  158. // the specified sequence number.
  159. LookupKey(const Slice& user_key, SequenceNumber sequence);
  160. ~LookupKey();
  161. // Return a key suitable for lookup in a MemTable.
  162. Slice memtable_key() const { return Slice(start_, end_ - start_); }
  163. // Return an internal key (suitable for passing to an internal iterator)
  164. Slice internal_key() const { return Slice(kstart_, end_ - kstart_); }
  165. // Return the user key
  166. Slice user_key() const { return Slice(kstart_, end_ - kstart_ - 8); }
  167. private:
  168. // We construct a char array of the form:
  169. // klength varint32 <-- start_
  170. // userkey char[klength] <-- kstart_
  171. // tag uint64
  172. // <-- end_
  173. // The array is a suitable MemTable key.
  174. // The suffix starting with "userkey" can be used as an InternalKey.
  175. const char* start_;
  176. const char* kstart_;
  177. const char* end_;
  178. char space_[200]; // Avoid allocation for short keys
  179. // No copying allowed
  180. LookupKey(const LookupKey&);
  181. void operator=(const LookupKey&);
  182. };
  183. inline LookupKey::~LookupKey() {
  184. if (start_ != space_) delete[] start_;
  185. }
  186. } // namespace leveldb
  187. #endif // STORAGE_LEVELDB_DB_FORMAT_H_