LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
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.

87 lines
2.6 KiB

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