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.

155 satır
5.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. }
  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. };
  27. // kValueTypeForSeek defines the ValueType that should be passed when
  28. // constructing a ParsedInternalKey object for seeking to a particular
  29. // sequence number (since we sort sequence numbers in decreasing order
  30. // and the value type is embedded as the low 8 bits in the sequence
  31. // number in internal keys, we need to use the highest-numbered
  32. // ValueType, not the lowest).
  33. static const ValueType kValueTypeForSeek = kTypeValue;
  34. typedef uint64_t SequenceNumber;
  35. // We leave eight bits empty at the bottom so a type and sequence#
  36. // can be packed together into 64-bits.
  37. static const SequenceNumber kMaxSequenceNumber =
  38. ((0x1ull << 56) - 1);
  39. struct ParsedInternalKey {
  40. Slice user_key;
  41. SequenceNumber sequence;
  42. ValueType type;
  43. ParsedInternalKey() { } // Intentionally left uninitialized (for speed)
  44. ParsedInternalKey(const Slice& u, const SequenceNumber& seq, ValueType t)
  45. : user_key(u), sequence(seq), type(t) { }
  46. std::string DebugString() const;
  47. };
  48. // Return the length of the encoding of "key".
  49. inline size_t InternalKeyEncodingLength(const ParsedInternalKey& key) {
  50. return key.user_key.size() + 8;
  51. }
  52. // Append the serialization of "key" to *result.
  53. extern void AppendInternalKey(std::string* result,
  54. const ParsedInternalKey& key);
  55. // Attempt to parse an internal key from "internal_key". On success,
  56. // stores the parsed data in "*result", and returns true.
  57. //
  58. // On error, returns false, leaves "*result" in an undefined state.
  59. extern bool ParseInternalKey(const Slice& internal_key,
  60. ParsedInternalKey* result);
  61. // Returns the user key portion of an internal key.
  62. inline Slice ExtractUserKey(const Slice& internal_key) {
  63. assert(internal_key.size() >= 8);
  64. return Slice(internal_key.data(), internal_key.size() - 8);
  65. }
  66. inline ValueType ExtractValueType(const Slice& internal_key) {
  67. assert(internal_key.size() >= 8);
  68. const size_t n = internal_key.size();
  69. uint64_t num = DecodeFixed64(internal_key.data() + n - 8);
  70. unsigned char c = num & 0xff;
  71. return static_cast<ValueType>(c);
  72. }
  73. // A comparator for internal keys that uses a specified comparator for
  74. // the user key portion and breaks ties by decreasing sequence number.
  75. class InternalKeyComparator : public Comparator {
  76. private:
  77. const Comparator* user_comparator_;
  78. public:
  79. explicit InternalKeyComparator(const Comparator* c) : user_comparator_(c) { }
  80. virtual const char* Name() const;
  81. virtual int Compare(const Slice& a, const Slice& b) const;
  82. virtual void FindShortestSeparator(
  83. std::string* start,
  84. const Slice& limit) const;
  85. virtual void FindShortSuccessor(std::string* key) const;
  86. const Comparator* user_comparator() const { return user_comparator_; }
  87. int Compare(const InternalKey& a, const InternalKey& b) const;
  88. };
  89. // Modules in this directory should keep internal keys wrapped inside
  90. // the following class instead of plain strings so that we do not
  91. // incorrectly use string comparisons instead of an InternalKeyComparator.
  92. class InternalKey {
  93. private:
  94. std::string rep_;
  95. public:
  96. InternalKey() { } // Leave rep_ as empty to indicate it is invalid
  97. InternalKey(const Slice& user_key, SequenceNumber s, ValueType t) {
  98. AppendInternalKey(&rep_, ParsedInternalKey(user_key, s, t));
  99. }
  100. void DecodeFrom(const Slice& s) { rep_.assign(s.data(), s.size()); }
  101. Slice Encode() const {
  102. assert(!rep_.empty());
  103. return rep_;
  104. }
  105. Slice user_key() const { return ExtractUserKey(rep_); }
  106. void SetFrom(const ParsedInternalKey& p) {
  107. rep_.clear();
  108. AppendInternalKey(&rep_, p);
  109. }
  110. void Clear() { rep_.clear(); }
  111. };
  112. inline int InternalKeyComparator::Compare(
  113. const InternalKey& a, const InternalKey& b) const {
  114. return Compare(a.Encode(), b.Encode());
  115. }
  116. inline bool ParseInternalKey(const Slice& internal_key,
  117. ParsedInternalKey* result) {
  118. const size_t n = internal_key.size();
  119. if (n < 8) return false;
  120. uint64_t num = DecodeFixed64(internal_key.data() + n - 8);
  121. unsigned char c = num & 0xff;
  122. result->sequence = num >> 8;
  123. result->type = static_cast<ValueType>(c);
  124. result->user_key = Slice(internal_key.data(), n - 8);
  125. return (c <= static_cast<unsigned char>(kTypeValue));
  126. }
  127. }
  128. #endif // STORAGE_LEVELDB_DB_FORMAT_H_