2011-03-19 06:37:00 +08:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#ifndef STORAGE_LEVELDB_DB_MEMTABLE_H_
|
|
|
|
#define STORAGE_LEVELDB_DB_MEMTABLE_H_
|
|
|
|
|
|
|
|
#include <string>
|
2011-03-31 02:35:40 +08:00
|
|
|
#include "leveldb/db.h"
|
2011-03-19 06:37:00 +08:00
|
|
|
#include "db/dbformat.h"
|
|
|
|
#include "db/skiplist.h"
|
|
|
|
#include "util/arena.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
class InternalKeyComparator;
|
|
|
|
class Mutex;
|
|
|
|
class MemTableIterator;
|
|
|
|
|
|
|
|
class MemTable {
|
|
|
|
public:
|
2011-05-21 10:17:43 +08:00
|
|
|
// MemTables are reference counted. The initial reference count
|
|
|
|
// is zero and the caller must call Ref() at least once.
|
2011-03-19 06:37:00 +08:00
|
|
|
explicit MemTable(const InternalKeyComparator& comparator);
|
2011-05-21 10:17:43 +08:00
|
|
|
|
|
|
|
// Increase reference count.
|
|
|
|
void Ref() { ++refs_; }
|
|
|
|
|
|
|
|
// Drop reference count. Delete if no more references exist.
|
|
|
|
void Unref() {
|
|
|
|
--refs_;
|
|
|
|
assert(refs_ >= 0);
|
|
|
|
if (refs_ <= 0) {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
}
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
// Returns an estimate of the number of bytes of data in use by this
|
|
|
|
// data structure.
|
|
|
|
//
|
|
|
|
// REQUIRES: external synchronization to prevent simultaneous
|
|
|
|
// operations on the same MemTable.
|
|
|
|
size_t ApproximateMemoryUsage();
|
|
|
|
|
|
|
|
// Return an iterator that yields the contents of the memtable.
|
|
|
|
//
|
|
|
|
// The caller must ensure that the underlying MemTable remains live
|
|
|
|
// while the returned iterator is live. The keys returned by this
|
|
|
|
// iterator are internal keys encoded by AppendInternalKey in the
|
|
|
|
// db/format.{h,cc} module.
|
|
|
|
Iterator* NewIterator();
|
|
|
|
|
|
|
|
// Add an entry into memtable that maps key to value at the
|
|
|
|
// specified sequence number and with the specified type.
|
|
|
|
// Typically value will be empty if type==kTypeDeletion.
|
|
|
|
void Add(SequenceNumber seq, ValueType type,
|
|
|
|
const Slice& key,
|
|
|
|
const Slice& value);
|
|
|
|
|
2011-06-22 10:36:45 +08:00
|
|
|
// If memtable contains a value for key, store it in *value and return true.
|
|
|
|
// If memtable contains a deletion for key, store a NotFound() error
|
|
|
|
// in *status and return true.
|
|
|
|
// Else, return false.
|
|
|
|
bool Get(const LookupKey& key, std::string* value, Status* s);
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
private:
|
2011-05-21 10:17:43 +08:00
|
|
|
~MemTable(); // Private since only Unref() should be used to delete it
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
struct KeyComparator {
|
|
|
|
const InternalKeyComparator comparator;
|
|
|
|
explicit KeyComparator(const InternalKeyComparator& c) : comparator(c) { }
|
|
|
|
int operator()(const char* a, const char* b) const;
|
|
|
|
};
|
|
|
|
friend class MemTableIterator;
|
|
|
|
friend class MemTableBackwardIterator;
|
|
|
|
|
|
|
|
typedef SkipList<const char*, KeyComparator> Table;
|
|
|
|
|
|
|
|
KeyComparator comparator_;
|
2011-05-21 10:17:43 +08:00
|
|
|
int refs_;
|
2011-03-19 06:37:00 +08:00
|
|
|
Arena arena_;
|
|
|
|
Table table_;
|
|
|
|
|
|
|
|
// No copying allowed
|
|
|
|
MemTable(const MemTable&);
|
|
|
|
void operator=(const MemTable&);
|
|
|
|
};
|
|
|
|
|
2011-11-01 01:22:06 +08:00
|
|
|
} // namespace leveldb
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
#endif // STORAGE_LEVELDB_DB_MEMTABLE_H_
|