小组成员:姚凯文(kevinyao0901),姜嘉琪
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

85 行
2.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. #ifndef STORAGE_LEVELDB_DB_MEMTABLE_H_
  5. #define STORAGE_LEVELDB_DB_MEMTABLE_H_
  6. #include <string>
  7. #include "leveldb/db.h"
  8. #include "db/dbformat.h"
  9. #include "db/skiplist.h"
  10. #include "util/arena.h"
  11. namespace leveldb {
  12. class InternalKeyComparator;
  13. class Mutex;
  14. class MemTableIterator;
  15. class MemTable {
  16. public:
  17. // MemTables are reference counted. The initial reference count
  18. // is zero and the caller must call Ref() at least once.
  19. explicit MemTable(const InternalKeyComparator& comparator);
  20. // Increase reference count.
  21. void Ref() { ++refs_; }
  22. // Drop reference count. Delete if no more references exist.
  23. void Unref() {
  24. --refs_;
  25. assert(refs_ >= 0);
  26. if (refs_ <= 0) {
  27. delete this;
  28. }
  29. }
  30. // Returns an estimate of the number of bytes of data in use by this
  31. // data structure.
  32. //
  33. // REQUIRES: external synchronization to prevent simultaneous
  34. // operations on the same MemTable.
  35. size_t ApproximateMemoryUsage();
  36. // Return an iterator that yields the contents of the memtable.
  37. //
  38. // The caller must ensure that the underlying MemTable remains live
  39. // while the returned iterator is live. The keys returned by this
  40. // iterator are internal keys encoded by AppendInternalKey in the
  41. // db/format.{h,cc} module.
  42. Iterator* NewIterator();
  43. // Add an entry into memtable that maps key to value at the
  44. // specified sequence number and with the specified type.
  45. // Typically value will be empty if type==kTypeDeletion.
  46. void Add(SequenceNumber seq, ValueType type,
  47. const Slice& key,
  48. const Slice& value);
  49. private:
  50. ~MemTable(); // Private since only Unref() should be used to delete it
  51. struct KeyComparator {
  52. const InternalKeyComparator comparator;
  53. explicit KeyComparator(const InternalKeyComparator& c) : comparator(c) { }
  54. int operator()(const char* a, const char* b) const;
  55. };
  56. friend class MemTableIterator;
  57. friend class MemTableBackwardIterator;
  58. typedef SkipList<const char*, KeyComparator> Table;
  59. KeyComparator comparator_;
  60. int refs_;
  61. Arena arena_;
  62. Table table_;
  63. // No copying allowed
  64. MemTable(const MemTable&);
  65. void operator=(const MemTable&);
  66. };
  67. }
  68. #endif // STORAGE_LEVELDB_DB_MEMTABLE_H_