小组成员:姚凯文(kevinyao0901),姜嘉琪
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.

117 lines
3.6 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* mem, MemTable::Table* table) {
  45. mem_ = mem;
  46. iter_ = new MemTable::Table::Iterator(table);
  47. mem->Ref();
  48. }
  49. virtual ~MemTableIterator() {
  50. delete iter_;
  51. mem_->Unref();
  52. }
  53. virtual bool Valid() const { return iter_->Valid(); }
  54. virtual void Seek(const Slice& k) { iter_->Seek(EncodeKey(&tmp_, k)); }
  55. virtual void SeekToFirst() { iter_->SeekToFirst(); }
  56. virtual void SeekToLast() { iter_->SeekToLast(); }
  57. virtual void Next() { iter_->Next(); }
  58. virtual void Prev() { iter_->Prev(); }
  59. virtual Slice key() const { return GetLengthPrefixedSlice(iter_->key()); }
  60. virtual Slice value() const {
  61. Slice key_slice = GetLengthPrefixedSlice(iter_->key());
  62. return GetLengthPrefixedSlice(key_slice.data() + key_slice.size());
  63. }
  64. virtual Status status() const { return Status::OK(); }
  65. private:
  66. MemTable* mem_;
  67. MemTable::Table::Iterator* iter_;
  68. std::string tmp_; // For passing to EncodeKey
  69. // No copying allowed
  70. MemTableIterator(const MemTableIterator&);
  71. void operator=(const MemTableIterator&);
  72. };
  73. Iterator* MemTable::NewIterator() {
  74. return new MemTableIterator(this, &table_);
  75. }
  76. void MemTable::Add(SequenceNumber s, ValueType type,
  77. const Slice& key,
  78. const Slice& value) {
  79. // Format of an entry is concatenation of:
  80. // key_size : varint32 of internal_key.size()
  81. // key bytes : char[internal_key.size()]
  82. // value_size : varint32 of value.size()
  83. // value bytes : char[value.size()]
  84. size_t key_size = key.size();
  85. size_t val_size = value.size();
  86. size_t internal_key_size = key_size + 8;
  87. const size_t encoded_len =
  88. VarintLength(internal_key_size) + internal_key_size +
  89. VarintLength(val_size) + val_size;
  90. char* buf = arena_.Allocate(encoded_len);
  91. char* p = EncodeVarint32(buf, internal_key_size);
  92. memcpy(p, key.data(), key_size);
  93. p += key_size;
  94. EncodeFixed64(p, (s << 8) | type);
  95. p += 8;
  96. p = EncodeVarint32(p, val_size);
  97. memcpy(p, value.data(), val_size);
  98. assert((p + val_size) - buf == encoded_len);
  99. table_.Insert(buf);
  100. }
  101. }