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