小组成员: 曹可心-10223903406 朴祉燕-10224602413
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.

91 line
2.7 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. // If memtable contains a value for key, store it in *value and return true.
  50. // If memtable contains a deletion for key, store a NotFound() error
  51. // in *status and return true.
  52. // Else, return false.
  53. bool Get(const LookupKey& key, std::string* value, Status* s);
  54. private:
  55. ~MemTable(); // Private since only Unref() should be used to delete it
  56. struct KeyComparator {
  57. const InternalKeyComparator comparator;
  58. explicit KeyComparator(const InternalKeyComparator& c) : comparator(c) { }
  59. int operator()(const char* a, const char* b) const;
  60. };
  61. friend class MemTableIterator;
  62. friend class MemTableBackwardIterator;
  63. typedef SkipList<const char*, KeyComparator> Table;
  64. KeyComparator comparator_;
  65. int refs_;
  66. Arena arena_;
  67. Table table_;
  68. // No copying allowed
  69. MemTable(const MemTable&);
  70. void operator=(const MemTable&);
  71. };
  72. } // namespace leveldb
  73. #endif // STORAGE_LEVELDB_DB_MEMTABLE_H_