作者: 韩晨旭 10225101440 李畅 10225102463
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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