10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
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.

120 lines
3.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. #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 += 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. LookupKey::LookupKey(const Slice& user_key, SequenceNumber s) {
  91. size_t usize = user_key.size();
  92. size_t needed = usize + 13; // A conservative estimate
  93. char* dst;
  94. if (needed <= sizeof(space_)) {
  95. dst = space_;
  96. } else {
  97. dst = new char[needed];
  98. }
  99. start_ = dst;
  100. dst = EncodeVarint32(dst, usize + 8);
  101. kstart_ = dst;
  102. memcpy(dst, user_key.data(), usize);
  103. dst += usize;
  104. EncodeFixed64(dst, PackSequenceAndType(s, kValueTypeForSeek));
  105. dst += 8;
  106. end_ = dst;
  107. }
  108. } // namespace leveldb