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.

106 regels
3.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 += user_key.ToString();
  25. result += buf;
  26. return result;
  27. }
  28. const char* InternalKeyComparator::Name() const {
  29. return "leveldb.InternalKeyComparator";
  30. }
  31. int InternalKeyComparator::Compare(const Slice& akey, const Slice& bkey) const {
  32. // Order by:
  33. // increasing user key (according to user-supplied comparator)
  34. // decreasing sequence number
  35. // decreasing type (though sequence# should be enough to disambiguate)
  36. int r = user_comparator_->Compare(ExtractUserKey(akey), ExtractUserKey(bkey));
  37. if (r == 0) {
  38. const uint64_t anum = DecodeFixed64(akey.data() + akey.size() - 8);
  39. const uint64_t bnum = DecodeFixed64(bkey.data() + bkey.size() - 8);
  40. if (anum > bnum) {
  41. r = -1;
  42. } else if (anum < bnum) {
  43. r = +1;
  44. }
  45. }
  46. return r;
  47. }
  48. void InternalKeyComparator::FindShortestSeparator(
  49. std::string* start,
  50. const Slice& limit) const {
  51. // Attempt to shorten the user portion of the key
  52. Slice user_start = ExtractUserKey(*start);
  53. Slice user_limit = ExtractUserKey(limit);
  54. std::string tmp(user_start.data(), user_start.size());
  55. user_comparator_->FindShortestSeparator(&tmp, user_limit);
  56. if (user_comparator_->Compare(*start, tmp) < 0) {
  57. // User key has become larger. Tack on the earliest possible
  58. // number to the shortened user key.
  59. PutFixed64(&tmp, PackSequenceAndType(kMaxSequenceNumber,kValueTypeForSeek));
  60. assert(this->Compare(*start, tmp) < 0);
  61. assert(this->Compare(tmp, limit) < 0);
  62. start->swap(tmp);
  63. }
  64. }
  65. void InternalKeyComparator::FindShortSuccessor(std::string* key) const {
  66. Slice user_key = ExtractUserKey(*key);
  67. std::string tmp(user_key.data(), user_key.size());
  68. user_comparator_->FindShortSuccessor(&tmp);
  69. if (user_comparator_->Compare(user_key, tmp) < 0) {
  70. // User key has become larger. Tack on the earliest possible
  71. // number to the shortened user key.
  72. PutFixed64(&tmp, PackSequenceAndType(kMaxSequenceNumber,kValueTypeForSeek));
  73. assert(this->Compare(*key, tmp) < 0);
  74. key->swap(tmp);
  75. }
  76. }
  77. LookupKey::LookupKey(const Slice& user_key, SequenceNumber s) {
  78. size_t usize = user_key.size();
  79. size_t needed = usize + 13; // A conservative estimate
  80. char* dst;
  81. if (needed <= sizeof(space_)) {
  82. dst = space_;
  83. } else {
  84. dst = new char[needed];
  85. }
  86. start_ = dst;
  87. dst = EncodeVarint32(dst, usize + 8);
  88. kstart_ = dst;
  89. memcpy(dst, user_key.data(), usize);
  90. dst += usize;
  91. EncodeFixed64(dst, PackSequenceAndType(s, kValueTypeForSeek));
  92. dst += 8;
  93. end_ = dst;
  94. }
  95. }