memtable.h 2.6 KB

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