memtable.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // value_size : varint32 of value.size()
  74. // value bytes : char[value.size()]
  75. size_t key_size = key.size();
  76. size_t val_size = value.size();
  77. size_t internal_key_size = key_size + 8;
  78. const size_t encoded_len =
  79. VarintLength(internal_key_size) + internal_key_size +
  80. VarintLength(val_size) + val_size;
  81. char* buf = arena_.Allocate(encoded_len);
  82. char* p = EncodeVarint32(buf, internal_key_size);
  83. memcpy(p, key.data(), key_size);
  84. p += key_size;
  85. EncodeFixed64(p, (s << 8) | type);
  86. p += 8;
  87. p = EncodeVarint32(p, val_size);
  88. memcpy(p, value.data(), val_size);
  89. assert(p + val_size == buf + encoded_len);
  90. table_.Insert(buf);
  91. }
  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),
  111. 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