10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
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.

173 Zeilen
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. #include "ctime"
  11. #include "iostream"
  12. namespace leveldb {
  13. static Slice GetLengthPrefixedSlice(const char* data) {
  14. uint32_t len;
  15. const char* p = data;
  16. p = GetVarint32Ptr(p, p + 5, &len); // +5: we assume "p" is not corrupted
  17. return Slice(p, len);
  18. }
  19. MemTable::MemTable(const InternalKeyComparator& comparator)
  20. : comparator_(comparator), refs_(0), table_(comparator_, &arena_) {}
  21. MemTable::~MemTable() { assert(refs_ == 0); }
  22. size_t MemTable::ApproximateMemoryUsage() { return arena_.MemoryUsage(); }
  23. int MemTable::KeyComparator::operator()(const char* aptr,
  24. const char* bptr) const {
  25. // Internal keys are encoded as length-prefixed strings.
  26. Slice a = GetLengthPrefixedSlice(aptr);
  27. Slice b = GetLengthPrefixedSlice(bptr);
  28. return comparator.Compare(a, b);
  29. }
  30. // Encode a suitable internal key target for "target" and return it.
  31. // Uses *scratch as scratch space, and the returned pointer will point
  32. // into this scratch space.
  33. static const char* EncodeKey(std::string* scratch, const Slice& target) {
  34. scratch->clear();
  35. PutVarint32(scratch, target.size());
  36. scratch->append(target.data(), target.size());
  37. return scratch->data();
  38. }
  39. class MemTableIterator : public Iterator {
  40. public:
  41. explicit MemTableIterator(MemTable::Table* table) : iter_(table) {}
  42. MemTableIterator(const MemTableIterator&) = delete;
  43. MemTableIterator& operator=(const MemTableIterator&) = delete;
  44. ~MemTableIterator() override = default;
  45. bool Valid() const override { return iter_.Valid(); }
  46. void Seek(const Slice& k) override { iter_.Seek(EncodeKey(&tmp_, k)); }
  47. void SeekToFirst() override { iter_.SeekToFirst(); }
  48. void SeekToLast() override { iter_.SeekToLast(); }
  49. void Next() override { iter_.Next(); }
  50. void Prev() override { iter_.Prev(); }
  51. Slice key() const override { return GetLengthPrefixedSlice(iter_.key()); }
  52. Slice value() const override {
  53. Slice key_slice = GetLengthPrefixedSlice(iter_.key());
  54. return GetLengthPrefixedSlice(key_slice.data() + key_slice.size());
  55. }
  56. Status status() const override { return Status::OK(); }
  57. private:
  58. MemTable::Table::Iterator iter_;
  59. std::string tmp_; // For passing to EncodeKey
  60. };
  61. Iterator* MemTable::NewIterator() { return new MemTableIterator(&table_); }
  62. void MemTable::Add(SequenceNumber s, ValueType type, const Slice& key,
  63. const Slice& value, uint64_t deadTime) {
  64. // Format of an entry is concatenation of:
  65. // key_size : varint32 of internal_key.size()
  66. // key bytes : char[internal_key.size()]
  67. // tag : uint64((sequence << 8) | type)
  68. // value_size : varint32 of value.size()
  69. // value bytes : char[value.size()]
  70. //变成
  71. // key_size : varint32 of internal_key.size()
  72. // key bytes : char[internal_key.size()]
  73. // (deadTime) : uint64(可能有)
  74. // tag : uint64((sequence << 8) | havettl << 1 | type)
  75. // value_size : varint32 of value.size()
  76. // value bytes : char[value.size()]
  77. size_t key_size = key.size();
  78. size_t val_size = value.size();
  79. size_t internal_key_size = key_size + 8;
  80. if(deadTime != 0) internal_key_size += 8;
  81. const size_t encoded_len = VarintLength(internal_key_size) +
  82. internal_key_size + VarintLength(val_size) +
  83. val_size;
  84. char* buf = arena_.Allocate(encoded_len);
  85. char* p = EncodeVarint32(buf, internal_key_size);
  86. std::memcpy(p, key.data(), key_size);
  87. p += key_size;
  88. if(deadTime == 0){
  89. EncodeFixed64(p, (s << 8) | type);
  90. } else {
  91. EncodeFixed64(p, deadTime);
  92. p += 8;
  93. EncodeFixed64(p, (s << 8) | 0b10 | type);
  94. }
  95. p += 8;
  96. p = EncodeVarint32(p, val_size);
  97. std::memcpy(p, value.data(), val_size);
  98. assert(p + val_size == buf + encoded_len);
  99. table_.Insert(buf);
  100. std::cout << "insert:" << key.ToString() << std::endl;
  101. }
  102. bool MemTable::Get(const LookupKey& key, std::string* value, Status* s) {
  103. Slice memkey = key.memtable_key();
  104. Table::Iterator iter(&table_);
  105. iter.Seek(memkey.data());
  106. std::cout << "search:" << key.user_key().ToString() << " valid?" << iter.Valid();
  107. if (iter.Valid()) {
  108. // entry format is:
  109. // klength varint32
  110. // userkey char[klength]
  111. // (deadTime) uint64
  112. // tag uint64
  113. // vlength varint32
  114. // value char[vlength]
  115. // Check that it belongs to same user key. We do not check the
  116. // sequence number since the Seek() call above should have skipped
  117. // all entries with overly large sequence numbers.
  118. const char* entry = iter.key();
  119. uint32_t key_length;
  120. const char* key_ptr = GetVarint32Ptr(entry, entry + 5, &key_length);
  121. std::cout << " get:" << ExtractUserKey(Slice(key_ptr, key_length)).ToString() << std::endl;
  122. if (comparator_.comparator.user_comparator()->Compare(
  123. ExtractUserKey(Slice(key_ptr, key_length)), key.user_key()) == 0) {
  124. // Correct user key
  125. const uint64_t tag = DecodeFixed64(key_ptr + key_length - 8);
  126. switch (static_cast<ValueType>(tag & 0x01)) {
  127. case kTypeValue: {
  128. // uint8_t havettl = (tag & 0xff) >> 1;
  129. // if(havettl){
  130. // time_t nowTime;
  131. // time(&nowTime);
  132. // assert(nowTime > 0);
  133. // const uint64_t deadTime = DecodeFixed64(key_ptr + key_length - 16);
  134. // if(static_cast<uint64_t>(nowTime) >= deadTime){ //过期了
  135. // std::cout << nowTime << "dead:" << deadTime << std::endl;
  136. // *s = Status::NotFound(Slice());
  137. // return true; //todo:之前有没过期的key
  138. // }
  139. // }
  140. Slice v = GetLengthPrefixedSlice(key_ptr + key_length);
  141. value->assign(v.data(), v.size());
  142. return true;
  143. }
  144. case kTypeDeletion:
  145. *s = Status::NotFound(Slice());
  146. return true;
  147. }
  148. }
  149. }
  150. return false;
  151. }
  152. } // namespace leveldb