Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
memtable.h
Go to the documentation of this file.
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 #include "leveldb/db.h"
10 #include "db/dbformat.h"
11 #include "db/skiplist.h"
12 #include "util/arena.h"
13 
14 namespace leveldb {
15 
16 class InternalKeyComparator;
17 class Mutex;
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  // Increase reference count.
27  void Ref() { ++refs_; }
28 
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;
35  }
36  }
37 
38  // Returns an estimate of the number of bytes of data in use by this
39  // data structure.
40  //
41  // REQUIRES: external synchronization to prevent simultaneous
42  // operations on the same MemTable.
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.
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,
57  const Slice& key,
58  const Slice& value);
59 
60  // If memtable contains a value for key, store it in *value and return true.
61  // If memtable contains a deletion for key, store a NotFound() error
62  // in *status and return true.
63  // Else, return false.
64  bool Get(const LookupKey& key, std::string* value, Status* s);
65 
66  private:
67  ~MemTable(); // Private since only Unref() should be used to delete it
68 
69  struct KeyComparator {
71  explicit KeyComparator(const InternalKeyComparator& c) : comparator(c) { }
72  int operator()(const char* a, const char* b) const;
73  };
74  friend class MemTableIterator;
76 
78 
80  int refs_;
82  Table table_;
83 
84  // No copying allowed
85  MemTable(const MemTable&);
86  void operator=(const MemTable&);
87 };
88 
89 } // namespace leveldb
90 
91 #endif // STORAGE_LEVELDB_DB_MEMTABLE_H_
int operator()(const char *a, const char *b) const
Definition: memtable.cc:33
size_t ApproximateMemoryUsage()
Definition: memtable.cc:31
std::string * value
Definition: version_set.cc:270
SkipList< const char *, KeyComparator > Table
Definition: memtable.h:77
friend class MemTableBackwardIterator
Definition: memtable.h:75
KeyComparator(const InternalKeyComparator &c)
Definition: memtable.h:71
ValueType
Definition: dbformat.h:51
uint64_t SequenceNumber
Definition: dbformat.h:63
MemTable(const InternalKeyComparator &comparator)
Definition: memtable.cc:21
Iterator * NewIterator()
Definition: memtable.cc:78
bool Get(const LookupKey &key, std::string *value, Status *s)
Definition: memtable.cc:108
const InternalKeyComparator comparator
Definition: memtable.h:70
void Add(SequenceNumber seq, ValueType type, const Slice &key, const Slice &value)
Definition: memtable.cc:82
void operator=(const MemTable &)
KeyComparator comparator_
Definition: memtable.h:79