memtable.h 4.2 KB
Newer Older
J
jorlow@chromium.org 已提交
1 2 3 4 5 6 7 8
// 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>
9
#include "leveldb/db.h"
J
jorlow@chromium.org 已提交
10 11
#include "db/dbformat.h"
#include "db/skiplist.h"
12
#include "db/version_set.h"
J
jorlow@chromium.org 已提交
13 14 15 16 17 18 19 20 21 22
#include "util/arena.h"

namespace leveldb {

class InternalKeyComparator;
class Mutex;
class MemTableIterator;

class MemTable {
 public:
23 24
  // MemTables are reference counted.  The initial reference count
  // is zero and the caller must call Ref() at least once.
25 26
  explicit MemTable(const InternalKeyComparator& comparator,
                    int numlevel = 7);
27 28 29 30 31 32 33 34 35 36 37 38

  // 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;
    }
  }
J
jorlow@chromium.org 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51

  // 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
52
  // db/dbformat.{h,cc} module.
J
jorlow@chromium.org 已提交
53 54 55 56 57 58 59 60 61
  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);

62 63 64
  // 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.
65 66 67 68
  // If memtable contains Merge operation as the most recent entry for a key,
  //   and the merge process does not stop (not reaching a value or delete),
  //   store the current merged result in value and MergeInProgress in s.
  //   return false
69
  // Else, return false.
70 71
  bool Get(const LookupKey& key, std::string* value, Status* s,
          const Options& options);
72

73 74 75
  // Returns the edits area that is needed for flushing the memtable
  VersionEdit* GetEdits() { return &edit_; }

76 77 78 79
  // Returns the sequence number of the first element that was inserted
  // into the memtable
  SequenceNumber GetFirstSequenceNumber() { return first_seqno_; }

80 81 82 83 84 85 86 87
  // Returns the logfile number that can be safely deleted when this
  // memstore is flushed to storage
  uint64_t GetLogNumber() { return mem_logfile_number_; }

  // Sets the logfile number that can be safely deleted when this
  // memstore is flushed to storage
  void SetLogNumber(uint64_t num) { mem_logfile_number_ = num; }

J
jorlow@chromium.org 已提交
88
 private:
89 90
  ~MemTable();  // Private since only Unref() should be used to delete it

J
jorlow@chromium.org 已提交
91 92 93 94 95 96 97
  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;
98
  friend class MemTableList;
J
jorlow@chromium.org 已提交
99 100 101 102

  typedef SkipList<const char*, KeyComparator> Table;

  KeyComparator comparator_;
103
  int refs_;
J
jorlow@chromium.org 已提交
104 105 106
  Arena arena_;
  Table table_;

107
  // These are used to manage memtable flushes to storage
A
Abhishek Kona 已提交
108
  bool flush_in_progress_; // started the flush
109 110 111 112 113 114 115
  bool flush_completed_;   // finished the flush
  uint64_t file_number_;    // filled up after flush is complete

  // The udpates to be applied to the transaction log when this
  // memtable is flushed to storage.
  VersionEdit edit_;

116 117 118
  // The sequence number of the kv that was inserted first
  SequenceNumber first_seqno_;

119 120 121
  // The log files earlier than this number can be deleted.
  uint64_t mem_logfile_number_;

J
jorlow@chromium.org 已提交
122 123 124 125 126
  // No copying allowed
  MemTable(const MemTable&);
  void operator=(const MemTable&);
};

H
Hans Wennborg 已提交
127
}  // namespace leveldb
J
jorlow@chromium.org 已提交
128 129

#endif  // STORAGE_LEVELDB_DB_MEMTABLE_H_