memtable.h 5.1 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>
J
Jim Paton 已提交
9
#include <memory>
10
#include <deque>
J
jorlow@chromium.org 已提交
11 12
#include "db/dbformat.h"
#include "db/skiplist.h"
13
#include "db/version_set.h"
14 15
#include "rocksdb/db.h"
#include "rocksdb/memtablerep.h"
X
Xing Jin 已提交
16
#include "util/arena_impl.h"
J
jorlow@chromium.org 已提交
17 18 19 20 21 22 23 24

namespace leveldb {

class Mutex;
class MemTableIterator;

class MemTable {
 public:
J
Jim Paton 已提交
25 26 27 28 29 30
  struct KeyComparator : public MemTableRep::KeyComparator {
    const InternalKeyComparator comparator;
    explicit KeyComparator(const InternalKeyComparator& c) : comparator(c) { }
    virtual int operator()(const char* a, const char* b) const;
  };

31 32
  // MemTables are reference counted.  The initial reference count
  // is zero and the caller must call Ref() at least once.
X
Xing Jin 已提交
33 34 35 36 37
  explicit MemTable(
    const InternalKeyComparator& comparator,
    std::shared_ptr<MemTableRepFactory> table_factory,
    int numlevel = 7,
    const Options& options = Options());
38 39 40 41 42 43 44 45 46 47 48 49

  // 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 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62

  // 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
63
  // db/dbformat.{h,cc} module.
J
Jim Paton 已提交
64 65 66 67 68
  //
  // If a prefix is supplied, it is passed to the underlying MemTableRep as a
  // hint that the iterator only need to support access to keys with that
  // prefix.
  Iterator* NewIterator(const Slice* prefix = nullptr);
J
jorlow@chromium.org 已提交
69 70 71 72 73 74 75 76

  // 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);

77 78 79
  // 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.
80
  // If memtable contains Merge operation as the most recent entry for a key,
81
  //   and the merge process does not stop (not reaching a value or delete),
82 83
  //   prepend the current merge operand to *operands.
  //   store MergeInProgress in s, and return false.
84
  // Else, return false.
85
  bool Get(const LookupKey& key, std::string* value, Status* s,
86
           std::deque<std::string>* operands, const Options& options);
87

88 89 90
  // Returns the edits area that is needed for flushing the memtable
  VersionEdit* GetEdits() { return &edit_; }

91 92 93 94
  // Returns the sequence number of the first element that was inserted
  // into the memtable
  SequenceNumber GetFirstSequenceNumber() { return first_seqno_; }

95 96 97 98 99 100 101 102
  // Returns the next active logfile number when this memtable is about to
  // be flushed to storage
  uint64_t GetNextLogNumber() { return mem_next_logfile_number_; }

  // Sets the next active logfile number when this memtable is about to
  // be flushed to storage
  void SetNextLogNumber(uint64_t num) { mem_next_logfile_number_ = num; }

103 104 105 106 107 108 109 110
  // 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
Jim Paton 已提交
111 112 113
  // Notify the underlying storage that no more items will be added
  void MarkImmutable() { table_->MarkReadOnly(); }

J
jorlow@chromium.org 已提交
114
 private:
115
  ~MemTable();  // Private since only Unref() should be used to delete it
J
jorlow@chromium.org 已提交
116 117
  friend class MemTableIterator;
  friend class MemTableBackwardIterator;
118
  friend class MemTableList;
J
jorlow@chromium.org 已提交
119 120

  KeyComparator comparator_;
121
  int refs_;
X
Xing Jin 已提交
122
  ArenaImpl arena_impl_;
J
Jim Paton 已提交
123
  shared_ptr<MemTableRep> table_;
J
jorlow@chromium.org 已提交
124

125
  // These are used to manage memtable flushes to storage
A
Abhishek Kona 已提交
126
  bool flush_in_progress_; // started the flush
127 128 129 130 131 132 133
  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_;

134 135 136
  // The sequence number of the kv that was inserted first
  SequenceNumber first_seqno_;

137
  // The log files earlier than this number can be deleted.
138 139 140 141
  uint64_t mem_next_logfile_number_;

  // The log file that backs this memtable (to be deleted when
  // memtable flush is done)
142 143
  uint64_t mem_logfile_number_;

J
jorlow@chromium.org 已提交
144 145 146 147 148
  // No copying allowed
  MemTable(const MemTable&);
  void operator=(const MemTable&);
};

H
Hans Wennborg 已提交
149
}  // namespace leveldb
J
jorlow@chromium.org 已提交
150 151

#endif  // STORAGE_LEVELDB_DB_MEMTABLE_H_