小组成员:10215300402-朱维清 & 10222140408 谷杰
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

136 linhas
4.4 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. #include "db/dbformat.h"
  5. #include <cstdio>
  6. #include <sstream>
  7. #include "port/port.h"
  8. #include "util/coding.h"
  9. namespace leveldb {
  10. static uint64_t PackSequenceAndType(uint64_t seq, ValueType t) {
  11. assert(seq <= kMaxSequenceNumber);
  12. assert(t <= kValueTypeForSeek);
  13. return (seq << 8) | t;
  14. }
  15. void AppendInternalKey(std::string* result, const ParsedInternalKey& key) {
  16. result->append(key.user_key.data(), key.user_key.size());
  17. PutFixed64(result, PackSequenceAndType(key.sequence, key.type));
  18. }
  19. std::string ParsedInternalKey::DebugString() const {
  20. std::ostringstream ss;
  21. ss << '\'' << EscapeString(user_key.ToString()) << "' @ " << sequence << " : "
  22. << static_cast<int>(type);
  23. return ss.str();
  24. }
  25. std::string InternalKey::DebugString() const {
  26. ParsedInternalKey parsed;
  27. if (ParseInternalKey(rep_, &parsed)) {
  28. return parsed.DebugString();
  29. }
  30. std::ostringstream ss;
  31. ss << "(bad)" << EscapeString(rep_);
  32. return ss.str();
  33. }
  34. const char* InternalKeyComparator::Name() const {
  35. return "leveldb.InternalKeyComparator";
  36. }
  37. int InternalKeyComparator::Compare(const Slice& akey, const Slice& bkey) const {
  38. // Order by:
  39. // increasing user key (according to user-supplied comparator)
  40. // decreasing sequence number
  41. // decreasing type (though sequence# should be enough to disambiguate)
  42. int r = user_comparator_->Compare(ExtractUserKey(akey), ExtractUserKey(bkey));
  43. if (r == 0) {
  44. const uint64_t anum = DecodeFixed64(akey.data() + akey.size() - 8);
  45. const uint64_t bnum = DecodeFixed64(bkey.data() + bkey.size() - 8);
  46. if (anum > bnum) {
  47. r = -1;
  48. } else if (anum < bnum) {
  49. r = +1;
  50. }
  51. }
  52. return r;
  53. }
  54. void InternalKeyComparator::FindShortestSeparator(std::string* start,
  55. const Slice& limit) const {
  56. // Attempt to shorten the user portion of the key
  57. Slice user_start = ExtractUserKey(*start);
  58. Slice user_limit = ExtractUserKey(limit);
  59. std::string tmp(user_start.data(), user_start.size());
  60. user_comparator_->FindShortestSeparator(&tmp, user_limit);
  61. if (tmp.size() < user_start.size() &&
  62. user_comparator_->Compare(user_start, tmp) < 0) {
  63. // User key has become shorter physically, but larger logically.
  64. // Tack on the earliest possible number to the shortened user key.
  65. PutFixed64(&tmp,
  66. PackSequenceAndType(kMaxSequenceNumber, kValueTypeForSeek));
  67. assert(this->Compare(*start, tmp) < 0);
  68. assert(this->Compare(tmp, limit) < 0);
  69. start->swap(tmp);
  70. }
  71. }
  72. void InternalKeyComparator::FindShortSuccessor(std::string* key) const {
  73. Slice user_key = ExtractUserKey(*key);
  74. std::string tmp(user_key.data(), user_key.size());
  75. user_comparator_->FindShortSuccessor(&tmp);
  76. if (tmp.size() < user_key.size() &&
  77. user_comparator_->Compare(user_key, tmp) < 0) {
  78. // User key has become shorter physically, but larger logically.
  79. // Tack on the earliest possible number to the shortened user key.
  80. PutFixed64(&tmp,
  81. PackSequenceAndType(kMaxSequenceNumber, kValueTypeForSeek));
  82. assert(this->Compare(*key, tmp) < 0);
  83. key->swap(tmp);
  84. }
  85. }
  86. const char* InternalFilterPolicy::Name() const { return user_policy_->Name(); }
  87. void InternalFilterPolicy::CreateFilter(const Slice* keys, int n,
  88. std::string* dst) const {
  89. // We rely on the fact that the code in table.cc does not mind us
  90. // adjusting keys[].
  91. Slice* mkey = const_cast<Slice*>(keys);
  92. for (int i = 0; i < n; i++) {
  93. mkey[i] = ExtractUserKey(keys[i]);
  94. // TODO(sanjay): Suppress dups?
  95. }
  96. user_policy_->CreateFilter(keys, n, dst);
  97. }
  98. bool InternalFilterPolicy::KeyMayMatch(const Slice& key, const Slice& f) const {
  99. return user_policy_->KeyMayMatch(ExtractUserKey(key), f);
  100. }
  101. LookupKey::LookupKey(const Slice& user_key, SequenceNumber s) {
  102. size_t usize = user_key.size();
  103. size_t needed = usize + 13; // A conservative estimate
  104. char* dst;
  105. if (needed <= sizeof(space_)) {
  106. dst = space_;
  107. } else {
  108. dst = new char[needed];
  109. }
  110. start_ = dst;
  111. dst = EncodeVarint32(dst, usize + 8);
  112. kstart_ = dst;
  113. std::memcpy(dst, user_key.data(), usize);
  114. dst += usize;
  115. EncodeFixed64(dst, PackSequenceAndType(s, kValueTypeForSeek));
  116. dst += 8;
  117. end_ = dst;
  118. }
  119. } // namespace leveldb