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

109 line
3.5 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. table_(comparator_, &arena_) {
  20. }
  21. MemTable::~MemTable() {
  22. }
  23. size_t MemTable::ApproximateMemoryUsage() { return arena_.MemoryUsage(); }
  24. int MemTable::KeyComparator::operator()(const char* aptr, const char* bptr)
  25. const {
  26. // Internal keys are encoded as length-prefixed strings.
  27. Slice a = GetLengthPrefixedSlice(aptr);
  28. Slice b = GetLengthPrefixedSlice(bptr);
  29. return comparator.Compare(a, b);
  30. }
  31. // Encode a suitable internal key target for "target" and return it.
  32. // Uses *scratch as scratch space, and the returned pointer will point
  33. // into this scratch space.
  34. static const char* EncodeKey(std::string* scratch, const Slice& target) {
  35. scratch->clear();
  36. PutVarint32(scratch, target.size());
  37. scratch->append(target.data(), target.size());
  38. return scratch->data();
  39. }
  40. class MemTableIterator: public Iterator {
  41. public:
  42. explicit MemTableIterator(MemTable::Table* table) {
  43. iter_ = new MemTable::Table::Iterator(table);
  44. }
  45. virtual ~MemTableIterator() { delete iter_; }
  46. virtual bool Valid() const { return iter_->Valid(); }
  47. virtual void Seek(const Slice& k) { iter_->Seek(EncodeKey(&tmp_, k)); }
  48. virtual void SeekToFirst() { iter_->SeekToFirst(); }
  49. virtual void SeekToLast() { iter_->SeekToLast(); }
  50. virtual void Next() { iter_->Next(); }
  51. virtual void Prev() { iter_->Prev(); }
  52. virtual Slice key() const { return GetLengthPrefixedSlice(iter_->key()); }
  53. virtual Slice value() const {
  54. Slice key_slice = GetLengthPrefixedSlice(iter_->key());
  55. return GetLengthPrefixedSlice(key_slice.data() + key_slice.size());
  56. }
  57. virtual Status status() const { return Status::OK(); }
  58. private:
  59. MemTable::Table::Iterator* iter_;
  60. std::string tmp_; // For passing to EncodeKey
  61. // No copying allowed
  62. MemTableIterator(const MemTableIterator&);
  63. void operator=(const MemTableIterator&);
  64. };
  65. Iterator* MemTable::NewIterator() {
  66. return new MemTableIterator(&table_);
  67. }
  68. void MemTable::Add(SequenceNumber s, ValueType type,
  69. const Slice& key,
  70. const Slice& value) {
  71. // Format of an entry is concatenation of:
  72. // key_size : varint32 of internal_key.size()
  73. // key bytes : char[internal_key.size()]
  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. }