小组成员:陈予曈,朱陈媛
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.

173 lines
6.2 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. // 添加所需头文件-柠
  11. #include <iostream>
  12. #include <sstream>
  13. #include <iomanip>
  14. #include <ctime>
  15. #include <chrono>
  16. namespace leveldb {
  17. static Slice GetLengthPrefixedSlice(const char* data) {
  18. uint32_t len;
  19. const char* p = data;
  20. p = GetVarint32Ptr(p, p + 5, &len); // +5: we assume "p" is not corrupted
  21. return Slice(p, len);
  22. }
  23. MemTable::MemTable(const InternalKeyComparator& comparator)
  24. : comparator_(comparator), refs_(0), table_(comparator_, &arena_) {}
  25. MemTable::~MemTable() { assert(refs_ == 0); }
  26. size_t MemTable::ApproximateMemoryUsage() { return arena_.MemoryUsage(); }
  27. int MemTable::KeyComparator::operator()(const char* aptr,
  28. const char* bptr) const {
  29. // Internal keys are encoded as length-prefixed strings.
  30. Slice a = GetLengthPrefixedSlice(aptr);
  31. Slice b = GetLengthPrefixedSlice(bptr);
  32. return comparator.Compare(a, b);
  33. }
  34. // Encode a suitable internal key target for "target" and return it.
  35. // Uses *scratch as scratch space, and the returned pointer will point
  36. // into this scratch space.
  37. static const char* EncodeKey(std::string* scratch, const Slice& target) {
  38. scratch->clear();
  39. PutVarint32(scratch, target.size());
  40. scratch->append(target.data(), target.size());
  41. return scratch->data();
  42. }
  43. class MemTableIterator : public Iterator {
  44. public:
  45. explicit MemTableIterator(MemTable::Table* table) : iter_(table) {}
  46. MemTableIterator(const MemTableIterator&) = delete;
  47. MemTableIterator& operator=(const MemTableIterator&) = delete;
  48. ~MemTableIterator() override = default;
  49. bool Valid() const override { return iter_.Valid(); }
  50. void Seek(const Slice& k) override { iter_.Seek(EncodeKey(&tmp_, k)); }
  51. void SeekToFirst() override { iter_.SeekToFirst(); }
  52. void SeekToLast() override { iter_.SeekToLast(); }
  53. void Next() override { iter_.Next(); }
  54. void Prev() override { iter_.Prev(); }
  55. Slice key() const override { return GetLengthPrefixedSlice(iter_.key()); }
  56. Slice value() const override {
  57. Slice key_slice = GetLengthPrefixedSlice(iter_.key());
  58. return GetLengthPrefixedSlice(key_slice.data() + key_slice.size());
  59. }
  60. Status status() const override { return Status::OK(); }
  61. private:
  62. MemTable::Table::Iterator iter_;
  63. std::string tmp_; // For passing to EncodeKey
  64. };
  65. Iterator* MemTable::NewIterator() { return new MemTableIterator(&table_); }
  66. void MemTable::Add(SequenceNumber s, ValueType type, const Slice& key,
  67. const Slice& value) {
  68. // Format of an entry is concatenation of:
  69. // key_size : varint32 of internal_key.size()
  70. // key bytes : char[internal_key.size()]
  71. // tag : uint64((sequence << 8) | type)
  72. // value_size : varint32 of value.size()
  73. // value bytes : char[value.size()]
  74. size_t key_size = key.size();
  75. size_t val_size = value.size();
  76. size_t internal_key_size = key_size + 8;
  77. const size_t encoded_len = VarintLength(internal_key_size) +
  78. internal_key_size + VarintLength(val_size) +
  79. val_size;
  80. char* buf = arena_.Allocate(encoded_len);
  81. char* p = EncodeVarint32(buf, internal_key_size);
  82. std::memcpy(p, key.data(), key_size);
  83. p += key_size;
  84. EncodeFixed64(p, (s << 8) | type);
  85. p += 8;
  86. p = EncodeVarint32(p, val_size);
  87. std::memcpy(p, value.data(), val_size);
  88. assert(p + val_size == buf + encoded_len);
  89. table_.Insert(buf);
  90. }
  91. //修改memtable读取逻辑-陈予曈
  92. bool MemTable::Get(const LookupKey& key, std::string* value, Status* s) {
  93. Slice memkey = key.memtable_key();
  94. Table::Iterator iter(&table_);
  95. iter.Seek(memkey.data());
  96. if (iter.Valid()) {
  97. // entry format is:
  98. // klength varint32
  99. // userkey char[klength]
  100. // tag uint64
  101. // vlength varint32
  102. // value char[vlength]
  103. // Check that it belongs to same user key. We do not check the
  104. // sequence number since the Seek() call above should have skipped
  105. // all entries with overly large sequence numbers.
  106. const char* entry = iter.key();
  107. uint32_t key_length;
  108. const char* key_ptr = GetVarint32Ptr(entry, entry + 5, &key_length);
  109. if (comparator_.comparator.user_comparator()->Compare(
  110. Slice(key_ptr, key_length - 8), key.user_key()) == 0) {
  111. // Correct user key
  112. const uint64_t tag = DecodeFixed64(key_ptr + key_length - 8);
  113. switch (static_cast<ValueType>(tag & 0xff)) {
  114. case kTypeValue: {
  115. Slice v = GetLengthPrefixedSlice(key_ptr + key_length);
  116. // 数据过期则读取不到-陈予曈
  117. std::string value_with_ttl(v.data(), v.size());
  118. if (value_with_ttl.size() >= 19) {
  119. std::string expiration_time_str = value_with_ttl.substr(value_with_ttl.size() - 19); // 提取过期时间戳
  120. std::tm tm = {};
  121. char* res = strptime(expiration_time_str.c_str(), "%Y-%m-%d %H:%M:%S", &tm);
  122. if (res == nullptr) { //解析时间戳失败
  123. value->assign(v.data(), v.size()-19);
  124. return true;
  125. } else {
  126. std::time_t expiration_time = std::mktime(&tm);
  127. std::time_t current_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
  128. if (expiration_time <= current_time) { //数据过期
  129. //std::cerr << "notfound_mem" << std::endl;
  130. *s = Status::NotFound(Slice());
  131. value->assign(v.data(), 0);
  132. }
  133. else //数据未过期
  134. {
  135. value->assign(v.data(), v.size()-19);
  136. }
  137. return true;
  138. }
  139. } else { //时间戳信息不存在
  140. value->assign(v.data(), v.size());
  141. return true;
  142. }
  143. value->assign(v.data(), v.size());
  144. return true;
  145. }
  146. case kTypeDeletion:
  147. *s = Status::NotFound(Slice());
  148. return true;
  149. }
  150. }
  151. }
  152. return false;
  153. }
  154. } // namespace leveldb