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