作者: 谢瑞阳 10225101483 徐翔宇 10225101535
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

204 行
7.0 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. }
  19. class InternalKey;
  20. // Value types encoded as the last component of internal keys.
  21. // DO NOT CHANGE THESE ENUM VALUES: they are embedded in the on-disk
  22. // data structures.
  23. enum ValueType {
  24. kTypeDeletion = 0x0,
  25. kTypeValue = 0x1,
  26. kTypeLargeValueRef = 0x2,
  27. };
  28. // kValueTypeForSeek defines the ValueType that should be passed when
  29. // constructing a ParsedInternalKey object for seeking to a particular
  30. // sequence number (since we sort sequence numbers in decreasing order
  31. // and the value type is embedded as the low 8 bits in the sequence
  32. // number in internal keys, we need to use the highest-numbered
  33. // ValueType, not the lowest).
  34. static const ValueType kValueTypeForSeek = kTypeLargeValueRef;
  35. typedef uint64_t SequenceNumber;
  36. // We leave eight bits empty at the bottom so a type and sequence#
  37. // can be packed together into 64-bits.
  38. static const SequenceNumber kMaxSequenceNumber =
  39. ((0x1ull << 56) - 1);
  40. struct ParsedInternalKey {
  41. Slice user_key;
  42. SequenceNumber sequence;
  43. ValueType type;
  44. ParsedInternalKey() { } // Intentionally left uninitialized (for speed)
  45. ParsedInternalKey(const Slice& u, const SequenceNumber& seq, ValueType t)
  46. : user_key(u), sequence(seq), type(t) { }
  47. std::string DebugString() const;
  48. };
  49. // Return the length of the encoding of "key".
  50. inline size_t InternalKeyEncodingLength(const ParsedInternalKey& key) {
  51. return key.user_key.size() + 8;
  52. }
  53. // Append the serialization of "key" to *result.
  54. extern void AppendInternalKey(std::string* result,
  55. const ParsedInternalKey& key);
  56. // Attempt to parse an internal key from "internal_key". On success,
  57. // stores the parsed data in "*result", and returns true.
  58. //
  59. // On error, returns false, leaves "*result" in an undefined state.
  60. extern bool ParseInternalKey(const Slice& internal_key,
  61. ParsedInternalKey* result);
  62. // Returns the user key portion of an internal key.
  63. inline Slice ExtractUserKey(const Slice& internal_key) {
  64. assert(internal_key.size() >= 8);
  65. return Slice(internal_key.data(), internal_key.size() - 8);
  66. }
  67. inline ValueType ExtractValueType(const Slice& internal_key) {
  68. assert(internal_key.size() >= 8);
  69. const size_t n = internal_key.size();
  70. uint64_t num = DecodeFixed64(internal_key.data() + n - 8);
  71. unsigned char c = num & 0xff;
  72. return static_cast<ValueType>(c);
  73. }
  74. // A comparator for internal keys that uses a specified comparator for
  75. // the user key portion and breaks ties by decreasing sequence number.
  76. class InternalKeyComparator : public Comparator {
  77. private:
  78. const Comparator* user_comparator_;
  79. public:
  80. explicit InternalKeyComparator(const Comparator* c) : user_comparator_(c) { }
  81. virtual const char* Name() const;
  82. virtual int Compare(const Slice& a, const Slice& b) const;
  83. virtual void FindShortestSeparator(
  84. std::string* start,
  85. const Slice& limit) const;
  86. virtual void FindShortSuccessor(std::string* key) const;
  87. const Comparator* user_comparator() const { return user_comparator_; }
  88. int Compare(const InternalKey& a, const InternalKey& b) const;
  89. };
  90. // Modules in this directory should keep internal keys wrapped inside
  91. // the following class instead of plain strings so that we do not
  92. // incorrectly use string comparisons instead of an InternalKeyComparator.
  93. class InternalKey {
  94. private:
  95. std::string rep_;
  96. public:
  97. InternalKey() { } // Leave rep_ as empty to indicate it is invalid
  98. InternalKey(const Slice& user_key, SequenceNumber s, ValueType t) {
  99. AppendInternalKey(&rep_, ParsedInternalKey(user_key, s, t));
  100. }
  101. void DecodeFrom(const Slice& s) { rep_.assign(s.data(), s.size()); }
  102. Slice Encode() const {
  103. assert(!rep_.empty());
  104. return rep_;
  105. }
  106. Slice user_key() const { return ExtractUserKey(rep_); }
  107. void SetFrom(const ParsedInternalKey& p) {
  108. rep_.clear();
  109. AppendInternalKey(&rep_, p);
  110. }
  111. void Clear() { rep_.clear(); }
  112. };
  113. inline int InternalKeyComparator::Compare(
  114. const InternalKey& a, const InternalKey& b) const {
  115. return Compare(a.Encode(), b.Encode());
  116. }
  117. // LargeValueRef is a 160-bit hash value (20 bytes), plus an 8 byte
  118. // uncompressed size, and a 1 byte CompressionType code. An
  119. // encoded form of it is embedded in the filenames of large value
  120. // files stored in the database, and the raw binary form is stored as
  121. // the iter->value() result for values of type kTypeLargeValueRef in
  122. // the table and log files that make up the database.
  123. struct LargeValueRef {
  124. char data[29];
  125. // Initialize a large value ref for the given data
  126. static LargeValueRef Make(const Slice& data,
  127. CompressionType compression_type);
  128. // Initialize a large value ref from a serialized, 29-byte reference value
  129. static LargeValueRef FromRef(const Slice& ref) {
  130. LargeValueRef result;
  131. assert(ref.size() == sizeof(result.data));
  132. memcpy(result.data, ref.data(), sizeof(result.data));
  133. return result;
  134. }
  135. // Return the number of bytes in a LargeValueRef (not the
  136. // number of bytes in the value referenced).
  137. static size_t ByteSize() { return sizeof(LargeValueRef().data); }
  138. // Return the number of bytes in the value referenced by "*this".
  139. uint64_t ValueSize() const { return DecodeFixed64(&data[20]); }
  140. CompressionType compression_type() const {
  141. return static_cast<CompressionType>(data[28]);
  142. }
  143. bool operator==(const LargeValueRef& b) const {
  144. return memcmp(data, b.data, sizeof(data)) == 0;
  145. }
  146. bool operator<(const LargeValueRef& b) const {
  147. return memcmp(data, b.data, sizeof(data)) < 0;
  148. }
  149. };
  150. // Convert the large value ref to a human-readable string suitable
  151. // for embedding in a large value filename.
  152. extern std::string LargeValueRefToFilenameString(const LargeValueRef& h);
  153. // Parse the large value filename string in "input" and store it in
  154. // "*h". If successful, returns true. Otherwise returns false.
  155. extern bool FilenameStringToLargeValueRef(const Slice& in, LargeValueRef* ref);
  156. inline bool ParseInternalKey(const Slice& internal_key,
  157. ParsedInternalKey* result) {
  158. const size_t n = internal_key.size();
  159. if (n < 8) return false;
  160. uint64_t num = DecodeFixed64(internal_key.data() + n - 8);
  161. unsigned char c = num & 0xff;
  162. result->sequence = num >> 8;
  163. result->type = static_cast<ValueType>(c);
  164. result->user_key = Slice(internal_key.data(), n - 8);
  165. return (c <= static_cast<unsigned char>(kTypeLargeValueRef));
  166. }
  167. }
  168. #endif // STORAGE_LEVELDB_DB_FORMAT_H_