作者: 谢瑞阳 10225101483 徐翔宇 10225101535
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.

88 lines
2.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. #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. It is safe to call when MemTable is being modified.
  32. size_t ApproximateMemoryUsage();
  33. // Return an iterator that yields the contents of the memtable.
  34. //
  35. // The caller must ensure that the underlying MemTable remains live
  36. // while the returned iterator is live. The keys returned by this
  37. // iterator are internal keys encoded by AppendInternalKey in the
  38. // db/format.{h,cc} module.
  39. Iterator* NewIterator();
  40. // Add an entry into memtable that maps key to value at the
  41. // specified sequence number and with the specified type.
  42. // Typically value will be empty if type==kTypeDeletion.
  43. void Add(SequenceNumber seq, ValueType type,
  44. const Slice& key,
  45. const Slice& value);
  46. // If memtable contains a value for key, store it in *value and return true.
  47. // If memtable contains a deletion for key, store a NotFound() error
  48. // in *status and return true.
  49. // Else, return false.
  50. bool Get(const LookupKey& key, std::string* value, Status* s);
  51. private:
  52. ~MemTable(); // Private since only Unref() should be used to delete it
  53. struct KeyComparator {
  54. const InternalKeyComparator comparator;
  55. explicit KeyComparator(const InternalKeyComparator& c) : comparator(c) { }
  56. int operator()(const char* a, const char* b) const;
  57. };
  58. friend class MemTableIterator;
  59. friend class MemTableBackwardIterator;
  60. typedef SkipList<const char*, KeyComparator> Table;
  61. KeyComparator comparator_;
  62. int refs_;
  63. Arena arena_;
  64. Table table_;
  65. // No copying allowed
  66. MemTable(const MemTable&);
  67. void operator=(const MemTable&);
  68. };
  69. } // namespace leveldb
  70. #endif // STORAGE_LEVELDB_DB_MEMTABLE_H_