作者: 韩晨旭 10225101440 李畅 10225102463
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

213 satır
7.1 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. // Maximum level to which a new compacted memtable is pushed if it
  25. // does not create overlap. We try to push to level 2 to avoid the
  26. // relatively expensive level 0=>1 compactions and to avoid some
  27. // expensive manifest file operations. We do not push all the way to
  28. // the largest level since that can generate a lot of wasted disk
  29. // space if the same key space is being repeatedly overwritten.
  30. static const int kMaxMemCompactLevel = 2;
  31. }
  32. class InternalKey;
  33. // Value types encoded as the last component of internal keys.
  34. // DO NOT CHANGE THESE ENUM VALUES: they are embedded in the on-disk
  35. // data structures.
  36. enum ValueType {
  37. kTypeDeletion = 0x0,
  38. kTypeValue = 0x1,
  39. };
  40. // kValueTypeForSeek defines the ValueType that should be passed when
  41. // constructing a ParsedInternalKey object for seeking to a particular
  42. // sequence number (since we sort sequence numbers in decreasing order
  43. // and the value type is embedded as the low 8 bits in the sequence
  44. // number in internal keys, we need to use the highest-numbered
  45. // ValueType, not the lowest).
  46. static const ValueType kValueTypeForSeek = kTypeValue;
  47. typedef uint64_t SequenceNumber;
  48. // We leave eight bits empty at the bottom so a type and sequence#
  49. // can be packed together into 64-bits.
  50. static const SequenceNumber kMaxSequenceNumber =
  51. ((0x1ull << 56) - 1);
  52. struct ParsedInternalKey {
  53. Slice user_key;
  54. SequenceNumber sequence;
  55. ValueType type;
  56. ParsedInternalKey() { } // Intentionally left uninitialized (for speed)
  57. ParsedInternalKey(const Slice& u, const SequenceNumber& seq, ValueType t)
  58. : user_key(u), sequence(seq), type(t) { }
  59. std::string DebugString() const;
  60. };
  61. // Return the length of the encoding of "key".
  62. inline size_t InternalKeyEncodingLength(const ParsedInternalKey& key) {
  63. return key.user_key.size() + 8;
  64. }
  65. // Append the serialization of "key" to *result.
  66. extern void AppendInternalKey(std::string* result,
  67. 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. extern bool ParseInternalKey(const Slice& internal_key,
  73. ParsedInternalKey* result);
  74. // Returns the user key portion of an internal key.
  75. inline Slice ExtractUserKey(const Slice& internal_key) {
  76. assert(internal_key.size() >= 8);
  77. return Slice(internal_key.data(), internal_key.size() - 8);
  78. }
  79. inline ValueType ExtractValueType(const Slice& internal_key) {
  80. assert(internal_key.size() >= 8);
  81. const size_t n = internal_key.size();
  82. uint64_t num = DecodeFixed64(internal_key.data() + n - 8);
  83. unsigned char c = num & 0xff;
  84. return static_cast<ValueType>(c);
  85. }
  86. // A comparator for internal keys that uses a specified comparator for
  87. // the user key portion and breaks ties by decreasing sequence number.
  88. class InternalKeyComparator : public Comparator {
  89. private:
  90. const Comparator* user_comparator_;
  91. public:
  92. explicit InternalKeyComparator(const Comparator* c) : user_comparator_(c) { }
  93. virtual const char* Name() const;
  94. virtual int Compare(const Slice& a, const Slice& b) const;
  95. virtual void FindShortestSeparator(
  96. std::string* start,
  97. const Slice& limit) const;
  98. virtual void FindShortSuccessor(std::string* key) const;
  99. const Comparator* user_comparator() const { return user_comparator_; }
  100. int Compare(const InternalKey& a, const InternalKey& b) const;
  101. };
  102. // Modules in this directory should keep internal keys wrapped inside
  103. // the following class instead of plain strings so that we do not
  104. // incorrectly use string comparisons instead of an InternalKeyComparator.
  105. class InternalKey {
  106. private:
  107. std::string rep_;
  108. public:
  109. InternalKey() { } // Leave rep_ as empty to indicate it is invalid
  110. InternalKey(const Slice& user_key, SequenceNumber s, ValueType t) {
  111. AppendInternalKey(&rep_, ParsedInternalKey(user_key, s, t));
  112. }
  113. void DecodeFrom(const Slice& s) { rep_.assign(s.data(), s.size()); }
  114. Slice Encode() const {
  115. assert(!rep_.empty());
  116. return rep_;
  117. }
  118. Slice user_key() const { return ExtractUserKey(rep_); }
  119. void SetFrom(const ParsedInternalKey& p) {
  120. rep_.clear();
  121. AppendInternalKey(&rep_, p);
  122. }
  123. void Clear() { rep_.clear(); }
  124. };
  125. inline int InternalKeyComparator::Compare(
  126. const InternalKey& a, const InternalKey& b) const {
  127. return Compare(a.Encode(), b.Encode());
  128. }
  129. inline bool ParseInternalKey(const Slice& internal_key,
  130. ParsedInternalKey* result) {
  131. const size_t n = internal_key.size();
  132. if (n < 8) return false;
  133. uint64_t num = DecodeFixed64(internal_key.data() + n - 8);
  134. unsigned char c = num & 0xff;
  135. result->sequence = num >> 8;
  136. result->type = static_cast<ValueType>(c);
  137. result->user_key = Slice(internal_key.data(), n - 8);
  138. return (c <= static_cast<unsigned char>(kTypeValue));
  139. }
  140. // A helper class useful for DBImpl::Get()
  141. class LookupKey {
  142. public:
  143. // Initialize *this for looking up user_key at a snapshot with
  144. // the specified sequence number.
  145. LookupKey(const Slice& user_key, SequenceNumber sequence);
  146. ~LookupKey();
  147. // Return a key suitable for lookup in a MemTable.
  148. Slice memtable_key() const { return Slice(start_, end_ - start_); }
  149. // Return an internal key (suitable for passing to an internal iterator)
  150. Slice internal_key() const { return Slice(kstart_, end_ - kstart_); }
  151. // Return the user key
  152. Slice user_key() const { return Slice(kstart_, end_ - kstart_ - 8); }
  153. private:
  154. // We construct a char array of the form:
  155. // klength varint32 <-- start_
  156. // userkey char[klength] <-- kstart_
  157. // tag uint64
  158. // <-- end_
  159. // The array is a suitable MemTable key.
  160. // The suffix starting with "userkey" can be used as an InternalKey.
  161. const char* start_;
  162. const char* kstart_;
  163. const char* end_;
  164. char space_[200]; // Avoid allocation for short keys
  165. // No copying allowed
  166. LookupKey(const LookupKey&);
  167. void operator=(const LookupKey&);
  168. };
  169. inline LookupKey::~LookupKey() {
  170. if (start_ != space_) delete[] start_;
  171. }
  172. }
  173. #endif // STORAGE_LEVELDB_DB_FORMAT_H_