10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

173 рядки
6.1 KiB

4 місяці тому
4 місяці тому
4 місяці тому
  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. const uint64_t atime = DecodeFixed64(akey.data() + akey.size() - 16);
  67. const uint64_t btime = DecodeFixed64(bkey.data() + bkey.size() - 16);
  68. //原本应该找到了,新加判断
  69. // if((btag & 0b100) && (atag & 0b10)){ //一个是查询键,另一个有ttl
  70. // const uint64_t atime = DecodeFixed64(akey.data() + akey.size() - 16);
  71. // const uint64_t btime = DecodeFixed64(bkey.data() + bkey.size() - 16);
  72. // std::cout<<"atime:"<<atime<<" btime:"<<btime<<" "<<aseq<<" "<<bseq<<" "<<btag<<" "<<atag<<std::endl;
  73. // if(atime <= btime){//过期了继续找
  74. // r = -1;
  75. // return r;
  76. // }
  77. // }
  78. if (aseq < bseq) {
  79. r = +1;
  80. }
  81. }
  82. return r;
  83. }
  84. void InternalKeyComparator::FindShortestSeparator(std::string* start,
  85. const Slice& limit) const {
  86. // Attempt to shorten the user portion of the key
  87. Slice user_start = ExtractUserKey(*start);
  88. Slice user_limit = ExtractUserKey(limit);
  89. std::string tmp(user_start.data(), user_start.size());
  90. user_comparator_->FindShortestSeparator(&tmp, user_limit);
  91. if (tmp.size() < user_start.size() &&
  92. user_comparator_->Compare(user_start, tmp) < 0) {
  93. // User key has become shorter physically, but larger logically.
  94. // Tack on the earliest possible number to the shortened user key.
  95. PutFixed64(&tmp,
  96. PackSequenceAndType(kMaxSequenceNumber, kValueTypeForSeek));
  97. assert(this->Compare(*start, tmp) < 0);
  98. assert(this->Compare(tmp, limit) < 0);
  99. start->swap(tmp);
  100. }
  101. }
  102. void InternalKeyComparator::FindShortSuccessor(std::string* key) const {
  103. Slice user_key = ExtractUserKey(*key);
  104. std::string tmp(user_key.data(), user_key.size());
  105. user_comparator_->FindShortSuccessor(&tmp);
  106. if (tmp.size() < user_key.size() &&
  107. user_comparator_->Compare(user_key, tmp) < 0) {
  108. // User key has become shorter physically, but larger logically.
  109. // Tack on the earliest possible number to the shortened user key.
  110. PutFixed64(&tmp,
  111. PackSequenceAndType(kMaxSequenceNumber, kValueTypeForSeek));
  112. assert(this->Compare(*key, tmp) < 0);
  113. key->swap(tmp);
  114. }
  115. }
  116. const char* InternalFilterPolicy::Name() const { return user_policy_->Name(); }
  117. void InternalFilterPolicy::CreateFilter(const Slice* keys, int n,
  118. std::string* dst) const {
  119. // We rely on the fact that the code in table.cc does not mind us
  120. // adjusting keys[].
  121. Slice* mkey = const_cast<Slice*>(keys);
  122. for (int i = 0; i < n; i++) {
  123. mkey[i] = ExtractUserKey(keys[i]);
  124. // TODO(sanjay): Suppress dups?
  125. }
  126. user_policy_->CreateFilter(keys, n, dst);
  127. }
  128. bool InternalFilterPolicy::KeyMayMatch(const Slice& key, const Slice& f) const {
  129. return user_policy_->KeyMayMatch(ExtractUserKey(key), f);
  130. }
  131. LookupKey::LookupKey(const Slice& user_key, SequenceNumber s, uint64_t nowTime) {
  132. size_t usize = user_key.size();
  133. size_t needed = usize + 21; // A conservative estimate
  134. char* dst;
  135. if (needed <= sizeof(space_)) {
  136. dst = space_;
  137. } else {
  138. dst = new char[needed];
  139. }
  140. start_ = dst;
  141. dst = EncodeVarint32(dst, usize + 16);
  142. kstart_ = dst;
  143. std::memcpy(dst, user_key.data(), usize);
  144. dst += usize;
  145. EncodeFixed64(dst, nowTime);
  146. dst += 8;
  147. // EncodeFixed64(dst, PackSequenceAndTypeAndTtlAndLookup(s, kValueTypeForSeek, 0, true));
  148. EncodeFixed64(dst, PackSequenceAndTypeAndTtlAndLookup(s, kValueTypeForSeek, 1, false));
  149. dst += 8;
  150. end_ = dst;
  151. printf("lookupkey tag:%lx\n",PackSequenceAndTypeAndTtlAndLookup(s, kValueTypeForSeek, 1, false));
  152. }
  153. } // namespace leveldb