memtable.h 6.3 KB
Newer Older
1 2 3 4 5
//  Copyright (c) 2013, Facebook, Inc.  All rights reserved.
//  This source code is licensed under the BSD-style license found in the
//  LICENSE file in the root directory of this source tree. An additional grant
//  of patent rights can be found in the PATENTS file in the same directory.
//
J
jorlow@chromium.org 已提交
6 7 8 9
// 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.

10
#pragma once
J
jorlow@chromium.org 已提交
11
#include <string>
J
Jim Paton 已提交
12
#include <memory>
13
#include <deque>
J
jorlow@chromium.org 已提交
14 15
#include "db/dbformat.h"
#include "db/skiplist.h"
16
#include "db/version_set.h"
17 18
#include "rocksdb/db.h"
#include "rocksdb/memtablerep.h"
X
Xing Jin 已提交
19
#include "util/arena_impl.h"
J
jorlow@chromium.org 已提交
20

21
namespace rocksdb {
J
jorlow@chromium.org 已提交
22 23 24

class Mutex;
class MemTableIterator;
25
class MergeContext;
J
jorlow@chromium.org 已提交
26 27 28

class MemTable {
 public:
J
Jim Paton 已提交
29 30 31 32 33 34
  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;
  };

35 36
  // MemTables are reference counted.  The initial reference count
  // is zero and the caller must call Ref() at least once.
37 38
  explicit MemTable(const InternalKeyComparator& comparator,
                    const Options& options = Options());
39

40 41
  ~MemTable();

42 43 44
  // Increase reference count.
  void Ref() { ++refs_; }

45 46 47
  // Drop reference count.
  // If the refcount goes to zero return this memtable, otherwise return null
  MemTable* Unref() {
48 49 50
    --refs_;
    assert(refs_ >= 0);
    if (refs_ <= 0) {
51
      return this;
52
    }
53
    return nullptr;
54
  }
J
jorlow@chromium.org 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67

  // 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
68
  // db/dbformat.{h,cc} module.
J
Jim Paton 已提交
69
  //
70 71 72 73 74 75 76 77
  // If options.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
  // specific prefix.
  // If options.prefix is not supplied and options.prefix_seek is set, the
  // iterator is not bound to a specific prefix. However, the semantics of
  // Seek is changed - the result might only include keys with the same prefix
  // as the seek-key.
  Iterator* NewIterator(const ReadOptions& options = ReadOptions());
J
jorlow@chromium.org 已提交
78 79 80 81 82 83 84 85

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

86 87 88
  // 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.
89
  // If memtable contains Merge operation as the most recent entry for a key,
90
  //   and the merge process does not stop (not reaching a value or delete),
91 92
  //   prepend the current merge operand to *operands.
  //   store MergeInProgress in s, and return false.
93
  // Else, return false.
94
  bool Get(const LookupKey& key, std::string* value, Status* s,
95
           MergeContext& merge_context, const Options& options);
96

97 98 99 100 101 102 103 104 105 106
  // Update the value and return status ok,
  //   if key exists in current memtable
  //     if new sizeof(new_value) <= sizeof(old_value) &&
  //       old_value for that key is a put i.e. kTypeValue
  //     else return false, and status - NotUpdatable()
  //   else return false, and status - NotFound()
  bool Update(SequenceNumber seq, ValueType type,
              const Slice& key,
              const Slice& value);

107 108 109 110 111
  // Returns the number of successive merge entries starting from the newest
  // entry for the key up to the last non-merge entry or last entry for the
  // key in the memtable.
  size_t CountSuccessiveMergeEntries(const LookupKey& key);

112 113 114
  // Returns the edits area that is needed for flushing the memtable
  VersionEdit* GetEdits() { return &edit_; }

115 116 117 118
  // Returns the sequence number of the first element that was inserted
  // into the memtable
  SequenceNumber GetFirstSequenceNumber() { return first_seqno_; }

119 120 121 122 123 124 125 126
  // 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; }

127 128 129 130 131 132 133 134
  // 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 已提交
135 136 137
  // Notify the underlying storage that no more items will be added
  void MarkImmutable() { table_->MarkReadOnly(); }

J
jorlow@chromium.org 已提交
138 139 140
 private:
  friend class MemTableIterator;
  friend class MemTableBackwardIterator;
141
  friend class MemTableList;
J
jorlow@chromium.org 已提交
142 143

  KeyComparator comparator_;
144
  int refs_;
X
Xing Jin 已提交
145
  ArenaImpl arena_impl_;
146
  unique_ptr<MemTableRep> table_;
J
jorlow@chromium.org 已提交
147

148
  // These are used to manage memtable flushes to storage
A
Abhishek Kona 已提交
149
  bool flush_in_progress_; // started the flush
150 151 152 153 154 155 156
  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_;

157 158 159
  // The sequence number of the kv that was inserted first
  SequenceNumber first_seqno_;

160
  // The log files earlier than this number can be deleted.
161 162 163 164
  uint64_t mem_next_logfile_number_;

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

167 168 169
  // rw locks for inplace updates
  std::vector<port::RWMutex> locks_;

J
jorlow@chromium.org 已提交
170 171 172
  // No copying allowed
  MemTable(const MemTable&);
  void operator=(const MemTable&);
173 174 175

  // Get the lock associated for the key
  port::RWMutex* GetLock(const Slice& key);
J
jorlow@chromium.org 已提交
176 177
};

178
}  // namespace rocksdb