小组成员:10215300402-朱维清 & 10222140408 谷杰
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

108 satır
3.4 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. // 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. }