Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

87 lignes
2.9 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. }