作者: 韩晨旭 10225101440 李畅 10225102463
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
4.7 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& cmp)
  18. : comparator_(cmp),
  19. refs_(0),
  20. table_(comparator_, &arena_) {
  21. }
  22. MemTable::~MemTable() {
  23. assert(refs_ == 0);
  24. }
  25. size_t MemTable::ApproximateMemoryUsage() { return arena_.MemoryUsage(); }
  26. int MemTable::KeyComparator::operator()(const char* aptr, const char* bptr)
  27. const {
  28. // Internal keys are encoded as length-prefixed strings.
  29. Slice a = GetLengthPrefixedSlice(aptr);
  30. Slice b = GetLengthPrefixedSlice(bptr);
  31. return comparator.Compare(a, b);
  32. }
  33. // Encode a suitable internal key target for "target" and return it.
  34. // Uses *scratch as scratch space, and the returned pointer will point
  35. // into this scratch space.
  36. static const char* EncodeKey(std::string* scratch, const Slice& target) {
  37. scratch->clear();
  38. PutVarint32(scratch, target.size());
  39. scratch->append(target.data(), target.size());
  40. return scratch->data();
  41. }
  42. class MemTableIterator: public Iterator {
  43. public:
  44. explicit MemTableIterator(MemTable::Table* table) : iter_(table) { }
  45. virtual bool Valid() const { return iter_.Valid(); }
  46. virtual void Seek(const Slice& k) { iter_.Seek(EncodeKey(&tmp_, k)); }
  47. virtual void SeekToFirst() { iter_.SeekToFirst(); }
  48. virtual void SeekToLast() { iter_.SeekToLast(); }
  49. virtual void Next() { iter_.Next(); }
  50. virtual void Prev() { iter_.Prev(); }
  51. virtual Slice key() const { return GetLengthPrefixedSlice(iter_.key()); }
  52. virtual Slice value() const {
  53. Slice key_slice = GetLengthPrefixedSlice(iter_.key());
  54. return GetLengthPrefixedSlice(key_slice.data() + key_slice.size());
  55. }
  56. virtual Status status() const { return Status::OK(); }
  57. private:
  58. MemTable::Table::Iterator iter_;
  59. std::string tmp_; // For passing to EncodeKey
  60. // No copying allowed
  61. MemTableIterator(const MemTableIterator&);
  62. void operator=(const MemTableIterator&);
  63. };
  64. Iterator* MemTable::NewIterator() {
  65. return new MemTableIterator(&table_);
  66. }
  67. void MemTable::Add(SequenceNumber s, ValueType type,
  68. const Slice& key,
  69. const Slice& value) {
  70. // Format of an entry is concatenation of:
  71. // key_size : varint32 of internal_key.size()
  72. // key bytes : char[internal_key.size()]
  73. // tag : uint64((sequence << 8) | type)
  74. // value_size : varint32 of value.size()
  75. // value bytes : char[value.size()]
  76. size_t key_size = key.size();
  77. size_t val_size = value.size();
  78. size_t internal_key_size = key_size + 8;
  79. const size_t encoded_len =
  80. VarintLength(internal_key_size) + internal_key_size +
  81. VarintLength(val_size) + val_size;
  82. char* buf = arena_.Allocate(encoded_len);
  83. char* p = EncodeVarint32(buf, internal_key_size);
  84. memcpy(p, key.data(), key_size);
  85. p += key_size;
  86. EncodeFixed64(p, (s << 8) | type);
  87. p += 8;
  88. p = EncodeVarint32(p, val_size);
  89. memcpy(p, value.data(), val_size);
  90. assert(p + val_size == buf + encoded_len);
  91. table_.Insert(buf);
  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),
  112. key.user_key()) == 0) {
  113. // Correct user key
  114. const uint64_t tag = DecodeFixed64(key_ptr + key_length - 8);
  115. switch (static_cast<ValueType>(tag & 0xff)) {
  116. case kTypeValue: {
  117. Slice v = GetLengthPrefixedSlice(key_ptr + key_length);
  118. value->assign(v.data(), v.size());
  119. return true;
  120. }
  121. case kTypeDeletion:
  122. *s = Status::NotFound(Slice());
  123. return true;
  124. }
  125. }
  126. }
  127. return false;
  128. }
  129. } // namespace leveldb