10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

172 rindas
5.9 KiB

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