小组成员:姚凯文(kevinyao0901),姜嘉琪
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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