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.

169 rivejä
5.6 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 <cstdio>
  6. #include <sstream>
  7. #include "port/port.h"
  8. #include "util/coding.h"
  9. namespace leveldb {
  10. static uint64_t PackSequenceAndTypeAndTtlAndLookup(
  11. uint64_t seq, ValueType t, bool havettl, bool islookup) {
  12. assert(seq <= kMaxSequenceNumber);
  13. assert(t <= kValueTypeForSeek);
  14. return (seq << 8) | (islookup << 2) | (havettl << 1) | t;
  15. }
  16. //下面有两个调这个函数的没改,也许也要修改标志位?
  17. static uint64_t PackSequenceAndType(uint64_t seq, ValueType t) {
  18. assert(seq <= kMaxSequenceNumber);
  19. assert(t <= kValueTypeForSeek);
  20. return (seq << 8) | t;
  21. }
  22. void AppendInternalKey(std::string* result, const ParsedInternalKey& key) {
  23. result->append(key.user_key.data(), key.user_key.size());
  24. if(key.deadTime != 0)
  25. PutFixed64(result, key.deadTime);
  26. PutFixed64(result, PackSequenceAndTypeAndTtlAndLookup(
  27. key.sequence, key.type, (key.deadTime != 0), false));
  28. }
  29. std::string ParsedInternalKey::DebugString() const {
  30. std::ostringstream ss;
  31. ss << '\'' << EscapeString(user_key.ToString()) << "' @ " << sequence << " : "
  32. << static_cast<int>(type);
  33. return ss.str();
  34. }
  35. std::string InternalKey::DebugString() const {
  36. ParsedInternalKey parsed;
  37. if (ParseInternalKey(rep_, &parsed)) {
  38. return parsed.DebugString();
  39. }
  40. std::ostringstream ss;
  41. ss << "(bad)" << EscapeString(rep_);
  42. return ss.str();
  43. }
  44. const char* InternalKeyComparator::Name() const {
  45. return "leveldb.InternalKeyComparator";
  46. }
  47. int InternalKeyComparator::Compare(const Slice& akey, const Slice& bkey) const {
  48. // Order by:
  49. // increasing user key (according to user-supplied comparator)
  50. // decreasing sequence number
  51. // decreasing type (though sequence# should be enough to disambiguate)
  52. //目前看调用时都是a为node, b为key,万一有不是的,逻辑还得补充
  53. //for debug
  54. // std::string a = ExtractUserKey(akey).ToString();
  55. // std::string b = ExtractUserKey(bkey).ToString();
  56. int r = user_comparator_->Compare(ExtractUserKey(akey), ExtractUserKey(bkey));
  57. if (r == 0) {
  58. const uint64_t atag = DecodeFixed64(akey.data() + akey.size() - 8);
  59. const uint64_t btag = DecodeFixed64(bkey.data() + bkey.size() - 8);
  60. const uint64_t aseq = atag >> 8;
  61. const uint64_t bseq = btag >> 8;
  62. if (aseq > bseq) {
  63. r = -1;
  64. return r;
  65. }
  66. //原本应该找到了,新加判断
  67. if((btag & 0b100) && (atag & 0b10)){ //一个是查询键,另一个有ttl
  68. const uint64_t atime = DecodeFixed64(akey.data() + akey.size() - 16);
  69. const uint64_t btime = DecodeFixed64(bkey.data() + bkey.size() - 16);
  70. if(atime <= btime){//过期了继续找
  71. r = -1;
  72. return r;
  73. }
  74. }
  75. if (aseq < bseq) {
  76. r = +1;
  77. }
  78. }
  79. return r;
  80. }
  81. void InternalKeyComparator::FindShortestSeparator(std::string* start,
  82. const Slice& limit) const {
  83. // Attempt to shorten the user portion of the key
  84. Slice user_start = ExtractUserKey(*start);
  85. Slice user_limit = ExtractUserKey(limit);
  86. std::string tmp(user_start.data(), user_start.size());
  87. user_comparator_->FindShortestSeparator(&tmp, user_limit);
  88. if (tmp.size() < user_start.size() &&
  89. user_comparator_->Compare(user_start, tmp) < 0) {
  90. // User key has become shorter physically, but larger logically.
  91. // Tack on the earliest possible number to the shortened user key.
  92. PutFixed64(&tmp,
  93. PackSequenceAndType(kMaxSequenceNumber, kValueTypeForSeek));
  94. assert(this->Compare(*start, tmp) < 0);
  95. assert(this->Compare(tmp, limit) < 0);
  96. start->swap(tmp);
  97. }
  98. }
  99. void InternalKeyComparator::FindShortSuccessor(std::string* key) const {
  100. Slice user_key = ExtractUserKey(*key);
  101. std::string tmp(user_key.data(), user_key.size());
  102. user_comparator_->FindShortSuccessor(&tmp);
  103. if (tmp.size() < user_key.size() &&
  104. user_comparator_->Compare(user_key, tmp) < 0) {
  105. // User key has become shorter physically, but larger logically.
  106. // Tack on the earliest possible number to the shortened user key.
  107. PutFixed64(&tmp,
  108. PackSequenceAndType(kMaxSequenceNumber, kValueTypeForSeek));
  109. assert(this->Compare(*key, tmp) < 0);
  110. key->swap(tmp);
  111. }
  112. }
  113. const char* InternalFilterPolicy::Name() const { return user_policy_->Name(); }
  114. void InternalFilterPolicy::CreateFilter(const Slice* keys, int n,
  115. std::string* dst) const {
  116. // We rely on the fact that the code in table.cc does not mind us
  117. // adjusting keys[].
  118. Slice* mkey = const_cast<Slice*>(keys);
  119. for (int i = 0; i < n; i++) {
  120. mkey[i] = ExtractUserKey(keys[i]);
  121. // TODO(sanjay): Suppress dups?
  122. }
  123. user_policy_->CreateFilter(keys, n, dst);
  124. }
  125. bool InternalFilterPolicy::KeyMayMatch(const Slice& key, const Slice& f) const {
  126. return user_policy_->KeyMayMatch(ExtractUserKey(key), f);
  127. }
  128. LookupKey::LookupKey(const Slice& user_key, SequenceNumber s, uint64_t nowTime) {
  129. size_t usize = user_key.size();
  130. size_t needed = usize + 21; // A conservative estimate
  131. char* dst;
  132. if (needed <= sizeof(space_)) {
  133. dst = space_;
  134. } else {
  135. dst = new char[needed];
  136. }
  137. start_ = dst;
  138. dst = EncodeVarint32(dst, usize + 16);
  139. kstart_ = dst;
  140. std::memcpy(dst, user_key.data(), usize);
  141. dst += usize;
  142. EncodeFixed64(dst, nowTime);
  143. dst += 8;
  144. EncodeFixed64(dst, PackSequenceAndTypeAndTtlAndLookup(s, kValueTypeForSeek, 0, true));
  145. dst += 8;
  146. end_ = dst;
  147. }
  148. } // namespace leveldb