作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

149 Zeilen
5.3 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/memtable.h"
  5. #include "db/dbformat.h"
  6. #include "leveldb/comparator.h"
  7. #include "leveldb/env.h"
  8. #include "leveldb/iterator.h"
  9. #include "util/coding.h"
  10. namespace leveldb {
  11. static Slice GetLengthPrefixedSlice(const char* data) {
  12. uint32_t len;
  13. const char* p = data;
  14. p = GetVarint32Ptr(p, p + 5, &len); // +5: we assume "p" is not corrupted
  15. return Slice(p, len);
  16. }
  17. MemTable::MemTable(const InternalKeyComparator& comparator)
  18. : comparator_(comparator), refs_(0), table_(comparator_, &arena_) {}
  19. MemTable::~MemTable() { assert(refs_ == 0); }
  20. size_t MemTable::ApproximateMemoryUsage() { return arena_.MemoryUsage(); }
  21. int MemTable::KeyComparator::operator()(const char* aptr,
  22. const char* bptr) const {
  23. // Internal keys are encoded as length-prefixed strings.
  24. Slice a = GetLengthPrefixedSlice(aptr);
  25. Slice b = GetLengthPrefixedSlice(bptr);
  26. return comparator.Compare(a, b);
  27. }
  28. // Encode a suitable internal key target for "target" and return it.
  29. // Uses *scratch as scratch space, and the returned pointer will point
  30. // into this scratch space.
  31. static const char* EncodeKey(std::string* scratch, const Slice& target) {
  32. scratch->clear();
  33. PutVarint32(scratch, target.size());
  34. scratch->append(target.data(), target.size());
  35. return scratch->data();
  36. }
  37. class MemTableIterator : public Iterator {
  38. public:
  39. explicit MemTableIterator(MemTable::Table* table) : iter_(table) {}
  40. MemTableIterator(const MemTableIterator&) = delete;
  41. MemTableIterator& operator=(const MemTableIterator&) = delete;
  42. ~MemTableIterator() override = default;
  43. bool Valid() const override { return iter_.Valid(); }
  44. void Seek(const Slice& k) override { iter_.Seek(EncodeKey(&tmp_, k)); }
  45. void SeekToFirst() override { iter_.SeekToFirst(); }
  46. void SeekToLast() override { iter_.SeekToLast(); }
  47. void Next() override { iter_.Next(); }
  48. void Prev() override { iter_.Prev(); }
  49. Slice key() const override { return GetLengthPrefixedSlice(iter_.key()); }
  50. Slice value() const override {
  51. Slice key_slice = GetLengthPrefixedSlice(iter_.key());
  52. return GetLengthPrefixedSlice(key_slice.data() + key_slice.size());
  53. }
  54. Status status() const override { return Status::OK(); }
  55. private:
  56. MemTable::Table::Iterator iter_;
  57. std::string tmp_; // For passing to EncodeKey
  58. };
  59. Iterator* MemTable::NewIterator() { return new MemTableIterator(&table_); }
  60. void MemTable::Add(SequenceNumber s, ValueType type, const Slice& key,
  61. const Slice& value) {
  62. // Format of an entry is concatenation of:
  63. // key_size : varint32 of internal_key.size()
  64. // key bytes : char[internal_key.size()]
  65. // tag : uint64((sequence << 8) | type)
  66. // value_size : varint32 of value.size()
  67. // value bytes : char[value.size()]
  68. // ttl : uint64(now)
  69. size_t key_size = key.size();
  70. size_t val_size = value.size();
  71. size_t internal_key_size = key_size + 8;
  72. const size_t encoded_len = VarintLength(internal_key_size) +
  73. internal_key_size + VarintLength(val_size) +
  74. val_size;
  75. char* buf = arena_.Allocate(encoded_len);
  76. char* p = EncodeVarint32(buf, internal_key_size);
  77. std::memcpy(p, key.data(), key_size);
  78. p += key_size;
  79. EncodeFixed64(p, (s << 8) | type);
  80. p += 8;
  81. p = EncodeVarint32(p, val_size);
  82. std::memcpy(p, value.data(), val_size);
  83. assert(p + val_size == buf + encoded_len);
  84. table_.Insert(buf);
  85. }
  86. bool MemTable::Get(const LookupKey& key, std::string* value, Status* s) {
  87. Slice memkey = key.memtable_key();
  88. Table::Iterator iter(&table_);
  89. iter.Seek(memkey.data());
  90. while (iter.Valid()) {
  91. // entry format is:
  92. // klength varint32
  93. // userkey char[klength]
  94. // tag uint64
  95. // vlength varint32
  96. // value char[vlength]
  97. // Check that it belongs to same user key. We do not check the
  98. // sequence number since the Seek() call above should have skipped
  99. // all entries with overly large sequence numbers.
  100. const char* entry = iter.key();
  101. uint32_t key_length;
  102. const char* key_ptr = GetVarint32Ptr(entry, entry + 5, &key_length);
  103. if (comparator_.comparator.user_comparator()->Compare(
  104. Slice(key_ptr, key_length - 8), key.user_key()) == 0) {
  105. // Correct user key
  106. const uint64_t tag = DecodeFixed64(key_ptr + key_length - 8);
  107. switch (static_cast<ValueType>(tag & 0xff)) {
  108. // kTypeValue 对应被插入的数据
  109. case kTypeValue: {
  110. Slice v = GetLengthPrefixedSlice(key_ptr + key_length);
  111. uint64_t ttl=*(uint64_t*)(v.data()+v.size()-sizeof(uint64_t)); // 将 TTL 从 new_data 的末尾取出
  112. time_t now = time(nullptr);
  113. // 如果 TTL 超过当前时间,说明数据已经过期
  114. if(ttl < static_cast<uint64_t>(now)){
  115. iter.Next(); // 将 iter 指向下一个键值对
  116. continue;
  117. }
  118. value->assign(v.data(), v.size()-sizeof(uint64_t));
  119. return true;
  120. }
  121. case kTypeDeletion:
  122. *s = Status::NotFound(Slice());
  123. return true;
  124. }
  125. }
  126. else break;
  127. }
  128. return false;
  129. }
  130. } // namespace leveldb