作者: 韩晨旭 10225101440 李畅 10225102463
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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