LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
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.

146 lines
5.1 KiB

1 month ago
1 month ago
1 month ago
1 month ago
  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()],[real_value,timestamp(uint64)]
  68. //
  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);//value包含timestamp,timestamp,8字节
  83. assert(p + val_size == buf + encoded_len);
  84. table_.Insert(buf);
  85. }
  86. /**
  87. * @descrption: ttl问题场景keyttl的相对大小于版本号的相对大小不同
  88. * @param key
  89. * @param value
  90. * @param s
  91. * @return
  92. */
  93. bool MemTable::Get(const LookupKey& key, std::string* value, Status* s) {
  94. Slice memkey = key.memtable_key();
  95. Table::Iterator iter(&table_);
  96. iter.Seek(memkey.data());
  97. if (iter.Valid()) {
  98. // entry format is:
  99. // klength varint32
  100. // userkey char[klength]
  101. // tag uint64
  102. // vlength varint32
  103. // value char[vlength]
  104. // Check that it belongs to same user key. We do not check the
  105. // sequence number since the Seek() call above should have skipped
  106. // all entries with overly large sequence numbers.
  107. const char* entry = iter.key();
  108. uint32_t key_length;
  109. const char* key_ptr = GetVarint32Ptr(entry, entry + 5, &key_length);
  110. if (comparator_.comparator.user_comparator()->Compare(
  111. Slice(key_ptr, key_length - 8), key.user_key()) == 0) {
  112. // Correct user key
  113. const uint64_t tag = DecodeFixed64(key_ptr + key_length - 8);
  114. switch (static_cast<ValueType>(tag & 0xff)) {
  115. case kTypeValue: {
  116. Slice v = GetLengthPrefixedSlice(key_ptr + key_length);
  117. value->assign(v.data(), v.size());
  118. return true;
  119. }
  120. case kTypeDeletion:
  121. *s = Status::NotFound(Slice());
  122. return true;
  123. }
  124. }
  125. }
  126. return false;
  127. }
  128. } // namespace leveldb