2013-10-16 21:59:46 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2011-03-18 22:37:00 +00: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.
|
|
|
|
|
|
|
|
#include "db/db_iter.h"
|
2013-05-28 21:00:10 +00:00
|
|
|
#include <stdexcept>
|
[RocksDB] [MergeOperator] The new Merge Interface! Uses merge sequences.
Summary:
Here are the major changes to the Merge Interface. It has been expanded
to handle cases where the MergeOperator is not associative. It does so by stacking
up merge operations while scanning through the key history (i.e.: during Get() or
Compaction), until a valid Put/Delete/end-of-history is encountered; it then
applies all of the merge operations in the correct sequence starting with the
base/sentinel value.
I have also introduced an "AssociativeMerge" function which allows the user to
take advantage of associative merge operations (such as in the case of counters).
The implementation will always attempt to merge the operations/operands themselves
together when they are encountered, and will resort to the "stacking" method if
and only if the "associative-merge" fails.
This implementation is conjectured to allow MergeOperator to handle the general
case, while still providing the user with the ability to take advantage of certain
efficiencies in their own merge-operator / data-structure.
NOTE: This is a preliminary diff. This must still go through a lot of review,
revision, and testing. Feedback welcome!
Test Plan:
-This is a preliminary diff. I have only just begun testing/debugging it.
-I will be testing this with the existing MergeOperator use-cases and unit-tests
(counters, string-append, and redis-lists)
-I will be "desk-checking" and walking through the code with the help gdb.
-I will find a way of stress-testing the new interface / implementation using
db_bench, db_test, merge_test, and/or db_stress.
-I will ensure that my tests cover all cases: Get-Memtable,
Get-Immutable-Memtable, Get-from-Disk, Iterator-Range-Scan, Flush-Memtable-to-L0,
Compaction-L0-L1, Compaction-Ln-L(n+1), Put/Delete found, Put/Delete not-found,
end-of-history, end-of-file, etc.
-A lot of feedback from the reviewers.
Reviewers: haobo, dhruba, zshao, emayanke
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D11499
2013-08-06 03:14:32 +00:00
|
|
|
#include <deque>
|
2014-07-15 23:10:18 +00:00
|
|
|
#include <string>
|
2014-08-08 16:44:14 +00:00
|
|
|
#include <limits>
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
#include "db/filename.h"
|
|
|
|
#include "db/dbformat.h"
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "rocksdb/iterator.h"
|
|
|
|
#include "rocksdb/merge_operator.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "port/port.h"
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
#include "util/arena.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "util/logging.h"
|
|
|
|
#include "util/mutexlock.h"
|
2013-10-02 17:28:25 +00:00
|
|
|
#include "util/perf_context_imp.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
static void DumpInternalIter(Iterator* iter) {
|
|
|
|
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
|
|
|
|
ParsedInternalKey k;
|
|
|
|
if (!ParseInternalKey(iter->key(), &k)) {
|
|
|
|
fprintf(stderr, "Corrupt '%s'\n", EscapeString(iter->key()).c_str());
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "@ '%s'\n", k.DebugString().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Memtables and sstables that make the DB representation contain
|
|
|
|
// (userkey,seq,type) => uservalue entries. DBIter
|
|
|
|
// combines multiple entries for the same userkey found in the DB
|
|
|
|
// representation into a single entry while accounting for sequence
|
|
|
|
// numbers, deletion markers, overwrites, etc.
|
|
|
|
class DBIter: public Iterator {
|
|
|
|
public:
|
2013-03-21 22:59:47 +00:00
|
|
|
// The following is grossly complicated. TODO: clean it up
|
2011-03-25 20:27:43 +00:00
|
|
|
// Which direction is the iterator currently moving?
|
|
|
|
// (1) When moving forward, the internal iterator is positioned at
|
|
|
|
// the exact entry that yields this->key(), this->value()
|
|
|
|
// (2) When moving backwards, the internal iterator is positioned
|
|
|
|
// just before all entries whose user key == this->key().
|
|
|
|
enum Direction {
|
|
|
|
kForward,
|
|
|
|
kReverse
|
|
|
|
};
|
|
|
|
|
2014-09-08 22:04:34 +00:00
|
|
|
DBIter(Env* env, const ImmutableCFOptions& ioptions,
|
|
|
|
const Comparator* cmp, Iterator* iter, SequenceNumber s,
|
|
|
|
bool arena_mode, uint64_t max_sequential_skip_in_iterations,
|
2014-09-04 17:48:24 +00:00
|
|
|
const Slice* iterate_upper_bound = nullptr)
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
: arena_mode_(arena_mode),
|
|
|
|
env_(env),
|
2014-09-08 22:04:34 +00:00
|
|
|
logger_(ioptions.info_log),
|
2011-03-18 22:37:00 +00:00
|
|
|
user_comparator_(cmp),
|
2014-09-08 22:04:34 +00:00
|
|
|
user_merge_operator_(ioptions.merge_operator),
|
2011-03-18 22:37:00 +00:00
|
|
|
iter_(iter),
|
|
|
|
sequence_(s),
|
2011-03-25 20:27:43 +00:00
|
|
|
direction_(kForward),
|
2013-03-21 22:59:47 +00:00
|
|
|
valid_(false),
|
2013-05-28 21:00:10 +00:00
|
|
|
current_entry_is_merged_(false),
|
2014-09-08 22:04:34 +00:00
|
|
|
statistics_(ioptions.statistics),
|
2014-09-04 17:48:24 +00:00
|
|
|
iterate_upper_bound_(iterate_upper_bound) {
|
2014-07-28 19:05:36 +00:00
|
|
|
RecordTick(statistics_, NO_ITERATORS);
|
2014-09-08 22:04:34 +00:00
|
|
|
prefix_extractor_ = ioptions.prefix_extractor;
|
|
|
|
max_skip_ = max_sequential_skip_in_iterations;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
virtual ~DBIter() {
|
2013-05-28 21:00:10 +00:00
|
|
|
RecordTick(statistics_, NO_ITERATORS, -1);
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
if (!arena_mode_) {
|
|
|
|
delete iter_;
|
|
|
|
} else {
|
|
|
|
iter_->~Iterator();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virtual void SetIter(Iterator* iter) {
|
|
|
|
assert(iter_ == nullptr);
|
|
|
|
iter_ = iter;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2015-02-26 19:28:41 +00:00
|
|
|
virtual bool Valid() const override { return valid_; }
|
|
|
|
virtual Slice key() const override {
|
2011-03-18 22:37:00 +00:00
|
|
|
assert(valid_);
|
2014-04-01 21:45:30 +00:00
|
|
|
return saved_key_.GetKey();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2015-02-26 19:28:41 +00:00
|
|
|
virtual Slice value() const override {
|
2011-03-18 22:37:00 +00:00
|
|
|
assert(valid_);
|
2013-03-21 22:59:47 +00:00
|
|
|
return (direction_ == kForward && !current_entry_is_merged_) ?
|
|
|
|
iter_->value() : saved_value_;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2015-02-26 19:28:41 +00:00
|
|
|
virtual Status status() const override {
|
2011-03-18 22:37:00 +00:00
|
|
|
if (status_.ok()) {
|
|
|
|
return iter_->status();
|
|
|
|
} else {
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
virtual void Next() override;
|
|
|
|
virtual void Prev() override;
|
|
|
|
virtual void Seek(const Slice& target) override;
|
|
|
|
virtual void SeekToFirst() override;
|
|
|
|
virtual void SeekToLast() override;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
private:
|
2014-07-15 23:10:18 +00:00
|
|
|
void PrevInternal();
|
|
|
|
void FindParseableKey(ParsedInternalKey* ikey, Direction direction);
|
|
|
|
bool FindValueForCurrentKey();
|
|
|
|
bool FindValueForCurrentKeyUsingSeek();
|
|
|
|
void FindPrevUserKey();
|
|
|
|
void FindNextUserKey();
|
2013-11-18 19:32:54 +00:00
|
|
|
inline void FindNextUserEntry(bool skipping);
|
|
|
|
void FindNextUserEntryInternal(bool skipping);
|
2011-03-25 20:27:43 +00:00
|
|
|
bool ParseKey(ParsedInternalKey* key);
|
2013-03-21 22:59:47 +00:00
|
|
|
void MergeValuesNewToOld();
|
2011-03-25 20:27:43 +00:00
|
|
|
|
|
|
|
inline void ClearSavedValue() {
|
|
|
|
if (saved_value_.capacity() > 1048576) {
|
|
|
|
std::string empty;
|
|
|
|
swap(empty, saved_value_);
|
|
|
|
} else {
|
|
|
|
saved_value_.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 17:48:24 +00:00
|
|
|
const SliceTransform* prefix_extractor_;
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
bool arena_mode_;
|
2011-03-18 22:37:00 +00:00
|
|
|
Env* const env_;
|
2013-12-03 19:17:58 +00:00
|
|
|
Logger* logger_;
|
2011-03-18 22:37:00 +00:00
|
|
|
const Comparator* const user_comparator_;
|
2013-03-21 22:59:47 +00:00
|
|
|
const MergeOperator* const user_merge_operator_;
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
Iterator* iter_;
|
2011-03-18 22:37:00 +00:00
|
|
|
SequenceNumber const sequence_;
|
2011-03-25 20:27:43 +00:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
Status status_;
|
2014-07-15 23:10:18 +00:00
|
|
|
IterKey saved_key_;
|
|
|
|
std::string saved_value_;
|
2011-03-25 20:27:43 +00:00
|
|
|
Direction direction_;
|
2011-03-18 22:37:00 +00:00
|
|
|
bool valid_;
|
2013-03-21 22:59:47 +00:00
|
|
|
bool current_entry_is_merged_;
|
2013-11-22 22:14:05 +00:00
|
|
|
Statistics* statistics_;
|
2013-07-28 18:53:08 +00:00
|
|
|
uint64_t max_skip_;
|
2014-09-04 17:48:24 +00:00
|
|
|
const Slice* iterate_upper_bound_;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// No copying allowed
|
|
|
|
DBIter(const DBIter&);
|
|
|
|
void operator=(const DBIter&);
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool DBIter::ParseKey(ParsedInternalKey* ikey) {
|
|
|
|
if (!ParseInternalKey(iter_->key(), ikey)) {
|
|
|
|
status_ = Status::Corruption("corrupted internal key in DBIter");
|
2014-10-30 23:54:34 +00:00
|
|
|
Log(InfoLogLevel::ERROR_LEVEL,
|
|
|
|
logger_, "corrupted internal key in DBIter: %s",
|
2013-03-21 22:59:47 +00:00
|
|
|
iter_->key().ToString(true).c_str());
|
2011-03-18 22:37:00 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
void DBIter::Next() {
|
|
|
|
assert(valid_);
|
|
|
|
|
2014-07-15 23:10:18 +00:00
|
|
|
if (direction_ == kReverse) {
|
|
|
|
FindNextUserKey();
|
2011-03-25 20:27:43 +00:00
|
|
|
direction_ = kForward;
|
|
|
|
if (!iter_->Valid()) {
|
|
|
|
iter_->SeekToFirst();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-25 20:27:43 +00:00
|
|
|
|
2013-03-21 22:59:47 +00:00
|
|
|
// If the current value is merged, we might already hit end of iter_
|
|
|
|
if (!iter_->Valid()) {
|
|
|
|
valid_ = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FindNextUserEntry(true /* skipping the current user key */);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 22:59:47 +00:00
|
|
|
// PRE: saved_key_ has the current user key if skipping
|
|
|
|
// POST: saved_key_ should have the next user key if valid_,
|
|
|
|
// if the current entry is a result of merge
|
|
|
|
// current_entry_is_merged_ => true
|
|
|
|
// saved_value_ => the merged value
|
|
|
|
//
|
|
|
|
// NOTE: In between, saved_key_ can point to a user key that has
|
|
|
|
// a delete marker
|
2013-11-18 19:32:54 +00:00
|
|
|
inline void DBIter::FindNextUserEntry(bool skipping) {
|
2014-08-22 22:28:58 +00:00
|
|
|
PERF_TIMER_GUARD(find_next_user_entry_time);
|
2013-11-18 19:32:54 +00:00
|
|
|
FindNextUserEntryInternal(skipping);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actual implementation of DBIter::FindNextUserEntry()
|
|
|
|
void DBIter::FindNextUserEntryInternal(bool skipping) {
|
2011-03-25 20:27:43 +00:00
|
|
|
// Loop until we hit an acceptable entry to yield
|
|
|
|
assert(iter_->Valid());
|
|
|
|
assert(direction_ == kForward);
|
2013-03-21 22:59:47 +00:00
|
|
|
current_entry_is_merged_ = false;
|
2013-07-28 18:53:08 +00:00
|
|
|
uint64_t num_skipped = 0;
|
2011-03-25 20:27:43 +00:00
|
|
|
do {
|
2011-03-18 22:37:00 +00:00
|
|
|
ParsedInternalKey ikey;
|
2014-09-04 17:48:24 +00:00
|
|
|
|
|
|
|
if (ParseKey(&ikey)) {
|
|
|
|
if (iterate_upper_bound_ != nullptr &&
|
|
|
|
ikey.user_key.compare(*iterate_upper_bound_) >= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ikey.sequence <= sequence_) {
|
|
|
|
if (skipping &&
|
|
|
|
user_comparator_->Compare(ikey.user_key, saved_key_.GetKey()) <= 0) {
|
|
|
|
num_skipped++; // skip this entry
|
|
|
|
PERF_COUNTER_ADD(internal_key_skipped_count, 1);
|
|
|
|
} else {
|
|
|
|
switch (ikey.type) {
|
|
|
|
case kTypeDeletion:
|
|
|
|
// Arrange to skip all upcoming entries for this key since
|
|
|
|
// they are hidden by this deletion.
|
|
|
|
saved_key_.SetKey(ikey.user_key);
|
|
|
|
skipping = true;
|
|
|
|
num_skipped = 0;
|
|
|
|
PERF_COUNTER_ADD(internal_delete_skipped_count, 1);
|
|
|
|
break;
|
|
|
|
case kTypeValue:
|
|
|
|
valid_ = true;
|
|
|
|
saved_key_.SetKey(ikey.user_key);
|
|
|
|
return;
|
|
|
|
case kTypeMerge:
|
|
|
|
// By now, we are sure the current ikey is going to yield a value
|
|
|
|
saved_key_.SetKey(ikey.user_key);
|
|
|
|
current_entry_is_merged_ = true;
|
|
|
|
valid_ = true;
|
|
|
|
MergeValuesNewToOld(); // Go to a different state machine
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
2013-03-21 22:59:47 +00:00
|
|
|
}
|
2011-03-25 20:27:43 +00:00
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2013-07-28 18:53:08 +00:00
|
|
|
// If we have sequentially iterated via numerous keys and still not
|
|
|
|
// found the next user-key, then it is better to seek so that we can
|
2015-04-25 09:14:27 +00:00
|
|
|
// avoid too many key comparisons. We seek to the last occurrence of
|
2013-07-28 18:53:08 +00:00
|
|
|
// our current key by looking for sequence number 0.
|
|
|
|
if (skipping && num_skipped > max_skip_) {
|
|
|
|
num_skipped = 0;
|
|
|
|
std::string last_key;
|
2014-04-01 21:45:30 +00:00
|
|
|
AppendInternalKey(&last_key, ParsedInternalKey(saved_key_.GetKey(), 0,
|
|
|
|
kValueTypeForSeek));
|
2013-07-28 18:53:08 +00:00
|
|
|
iter_->Seek(last_key);
|
|
|
|
RecordTick(statistics_, NUMBER_OF_RESEEKS_IN_ITERATION);
|
|
|
|
} else {
|
|
|
|
iter_->Next();
|
|
|
|
}
|
2011-03-25 20:27:43 +00:00
|
|
|
} while (iter_->Valid());
|
|
|
|
valid_ = false;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 22:59:47 +00:00
|
|
|
// Merge values of the same user key starting from the current iter_ position
|
|
|
|
// Scan from the newer entries to older entries.
|
|
|
|
// PRE: iter_->key() points to the first merge type entry
|
|
|
|
// saved_key_ stores the user key
|
|
|
|
// POST: saved_value_ has the merged value for the user key
|
|
|
|
// iter_ points to the next entry (or invalid)
|
|
|
|
void DBIter::MergeValuesNewToOld() {
|
2013-08-19 18:42:47 +00:00
|
|
|
if (!user_merge_operator_) {
|
2014-10-30 23:54:34 +00:00
|
|
|
Log(InfoLogLevel::ERROR_LEVEL,
|
|
|
|
logger_, "Options::merge_operator is null.");
|
2014-12-04 19:11:11 +00:00
|
|
|
status_ = Status::InvalidArgument("user_merge_operator_ must be set.");
|
|
|
|
valid_ = false;
|
|
|
|
return;
|
2013-08-19 18:42:47 +00:00
|
|
|
}
|
2013-03-21 22:59:47 +00:00
|
|
|
|
[RocksDB] [MergeOperator] The new Merge Interface! Uses merge sequences.
Summary:
Here are the major changes to the Merge Interface. It has been expanded
to handle cases where the MergeOperator is not associative. It does so by stacking
up merge operations while scanning through the key history (i.e.: during Get() or
Compaction), until a valid Put/Delete/end-of-history is encountered; it then
applies all of the merge operations in the correct sequence starting with the
base/sentinel value.
I have also introduced an "AssociativeMerge" function which allows the user to
take advantage of associative merge operations (such as in the case of counters).
The implementation will always attempt to merge the operations/operands themselves
together when they are encountered, and will resort to the "stacking" method if
and only if the "associative-merge" fails.
This implementation is conjectured to allow MergeOperator to handle the general
case, while still providing the user with the ability to take advantage of certain
efficiencies in their own merge-operator / data-structure.
NOTE: This is a preliminary diff. This must still go through a lot of review,
revision, and testing. Feedback welcome!
Test Plan:
-This is a preliminary diff. I have only just begun testing/debugging it.
-I will be testing this with the existing MergeOperator use-cases and unit-tests
(counters, string-append, and redis-lists)
-I will be "desk-checking" and walking through the code with the help gdb.
-I will find a way of stress-testing the new interface / implementation using
db_bench, db_test, merge_test, and/or db_stress.
-I will ensure that my tests cover all cases: Get-Memtable,
Get-Immutable-Memtable, Get-from-Disk, Iterator-Range-Scan, Flush-Memtable-to-L0,
Compaction-L0-L1, Compaction-Ln-L(n+1), Put/Delete found, Put/Delete not-found,
end-of-history, end-of-file, etc.
-A lot of feedback from the reviewers.
Reviewers: haobo, dhruba, zshao, emayanke
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D11499
2013-08-06 03:14:32 +00:00
|
|
|
// Start the merge process by pushing the first operand
|
|
|
|
std::deque<std::string> operands;
|
|
|
|
operands.push_front(iter_->value().ToString());
|
2013-03-21 22:59:47 +00:00
|
|
|
|
|
|
|
ParsedInternalKey ikey;
|
|
|
|
for (iter_->Next(); iter_->Valid(); iter_->Next()) {
|
|
|
|
if (!ParseKey(&ikey)) {
|
|
|
|
// skip corrupted key
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-04-01 21:45:30 +00:00
|
|
|
if (user_comparator_->Compare(ikey.user_key, saved_key_.GetKey()) != 0) {
|
2013-03-21 22:59:47 +00:00
|
|
|
// hit the next user key, stop right here
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kTypeDeletion == ikey.type) {
|
|
|
|
// hit a delete with the same user key, stop right here
|
|
|
|
// iter_ is positioned after delete
|
|
|
|
iter_->Next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kTypeValue == ikey.type) {
|
[RocksDB] [MergeOperator] The new Merge Interface! Uses merge sequences.
Summary:
Here are the major changes to the Merge Interface. It has been expanded
to handle cases where the MergeOperator is not associative. It does so by stacking
up merge operations while scanning through the key history (i.e.: during Get() or
Compaction), until a valid Put/Delete/end-of-history is encountered; it then
applies all of the merge operations in the correct sequence starting with the
base/sentinel value.
I have also introduced an "AssociativeMerge" function which allows the user to
take advantage of associative merge operations (such as in the case of counters).
The implementation will always attempt to merge the operations/operands themselves
together when they are encountered, and will resort to the "stacking" method if
and only if the "associative-merge" fails.
This implementation is conjectured to allow MergeOperator to handle the general
case, while still providing the user with the ability to take advantage of certain
efficiencies in their own merge-operator / data-structure.
NOTE: This is a preliminary diff. This must still go through a lot of review,
revision, and testing. Feedback welcome!
Test Plan:
-This is a preliminary diff. I have only just begun testing/debugging it.
-I will be testing this with the existing MergeOperator use-cases and unit-tests
(counters, string-append, and redis-lists)
-I will be "desk-checking" and walking through the code with the help gdb.
-I will find a way of stress-testing the new interface / implementation using
db_bench, db_test, merge_test, and/or db_stress.
-I will ensure that my tests cover all cases: Get-Memtable,
Get-Immutable-Memtable, Get-from-Disk, Iterator-Range-Scan, Flush-Memtable-to-L0,
Compaction-L0-L1, Compaction-Ln-L(n+1), Put/Delete found, Put/Delete not-found,
end-of-history, end-of-file, etc.
-A lot of feedback from the reviewers.
Reviewers: haobo, dhruba, zshao, emayanke
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D11499
2013-08-06 03:14:32 +00:00
|
|
|
// hit a put, merge the put value with operands and store the
|
|
|
|
// final result in saved_value_. We are done!
|
|
|
|
// ignore corruption if there is any.
|
2014-11-06 19:14:28 +00:00
|
|
|
const Slice val = iter_->value();
|
2015-03-03 18:59:36 +00:00
|
|
|
{
|
|
|
|
StopWatchNano timer(env_, statistics_ != nullptr);
|
|
|
|
PERF_TIMER_GUARD(merge_operator_time_nanos);
|
|
|
|
user_merge_operator_->FullMerge(ikey.user_key, &val, operands,
|
|
|
|
&saved_value_, logger_);
|
|
|
|
RecordTick(statistics_, MERGE_OPERATION_TOTAL_TIME,
|
|
|
|
timer.ElapsedNanos());
|
|
|
|
}
|
2013-03-21 22:59:47 +00:00
|
|
|
// iter_ is positioned after put
|
|
|
|
iter_->Next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kTypeMerge == ikey.type) {
|
[RocksDB] [MergeOperator] The new Merge Interface! Uses merge sequences.
Summary:
Here are the major changes to the Merge Interface. It has been expanded
to handle cases where the MergeOperator is not associative. It does so by stacking
up merge operations while scanning through the key history (i.e.: during Get() or
Compaction), until a valid Put/Delete/end-of-history is encountered; it then
applies all of the merge operations in the correct sequence starting with the
base/sentinel value.
I have also introduced an "AssociativeMerge" function which allows the user to
take advantage of associative merge operations (such as in the case of counters).
The implementation will always attempt to merge the operations/operands themselves
together when they are encountered, and will resort to the "stacking" method if
and only if the "associative-merge" fails.
This implementation is conjectured to allow MergeOperator to handle the general
case, while still providing the user with the ability to take advantage of certain
efficiencies in their own merge-operator / data-structure.
NOTE: This is a preliminary diff. This must still go through a lot of review,
revision, and testing. Feedback welcome!
Test Plan:
-This is a preliminary diff. I have only just begun testing/debugging it.
-I will be testing this with the existing MergeOperator use-cases and unit-tests
(counters, string-append, and redis-lists)
-I will be "desk-checking" and walking through the code with the help gdb.
-I will find a way of stress-testing the new interface / implementation using
db_bench, db_test, merge_test, and/or db_stress.
-I will ensure that my tests cover all cases: Get-Memtable,
Get-Immutable-Memtable, Get-from-Disk, Iterator-Range-Scan, Flush-Memtable-to-L0,
Compaction-L0-L1, Compaction-Ln-L(n+1), Put/Delete found, Put/Delete not-found,
end-of-history, end-of-file, etc.
-A lot of feedback from the reviewers.
Reviewers: haobo, dhruba, zshao, emayanke
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D11499
2013-08-06 03:14:32 +00:00
|
|
|
// hit a merge, add the value as an operand and run associative merge.
|
|
|
|
// when complete, add result to operands and continue.
|
2014-11-06 19:14:28 +00:00
|
|
|
const Slice& val = iter_->value();
|
|
|
|
operands.push_front(val.ToString());
|
2013-03-21 22:59:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 18:59:36 +00:00
|
|
|
{
|
|
|
|
StopWatchNano timer(env_, statistics_ != nullptr);
|
|
|
|
PERF_TIMER_GUARD(merge_operator_time_nanos);
|
|
|
|
// we either exhausted all internal keys under this user key, or hit
|
|
|
|
// a deletion marker.
|
|
|
|
// feed null as the existing value to the merge operator, such that
|
|
|
|
// client can differentiate this scenario and do things accordingly.
|
|
|
|
user_merge_operator_->FullMerge(saved_key_.GetKey(), nullptr, operands,
|
|
|
|
&saved_value_, logger_);
|
|
|
|
RecordTick(statistics_, MERGE_OPERATION_TOTAL_TIME, timer.ElapsedNanos());
|
|
|
|
}
|
2013-03-21 22:59:47 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
void DBIter::Prev() {
|
|
|
|
assert(valid_);
|
2014-07-15 23:10:18 +00:00
|
|
|
if (direction_ == kForward) {
|
|
|
|
FindPrevUserKey();
|
|
|
|
direction_ = kReverse;
|
|
|
|
}
|
|
|
|
PrevInternal();
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2014-07-15 23:10:18 +00:00
|
|
|
void DBIter::PrevInternal() {
|
|
|
|
if (!iter_->Valid()) {
|
|
|
|
valid_ = false;
|
|
|
|
return;
|
2013-03-21 22:59:47 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 23:10:18 +00:00
|
|
|
ParsedInternalKey ikey;
|
|
|
|
|
|
|
|
while (iter_->Valid()) {
|
2014-05-30 21:31:55 +00:00
|
|
|
saved_key_.SetKey(ExtractUserKey(iter_->key()));
|
2014-07-15 23:10:18 +00:00
|
|
|
if (FindValueForCurrentKey()) {
|
|
|
|
valid_ = true;
|
2011-03-18 22:37:00 +00:00
|
|
|
if (!iter_->Valid()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-07-15 23:10:18 +00:00
|
|
|
FindParseableKey(&ikey, kReverse);
|
|
|
|
if (user_comparator_->Compare(ikey.user_key, saved_key_.GetKey()) == 0) {
|
|
|
|
FindPrevUserKey();
|
2011-03-25 20:27:43 +00:00
|
|
|
}
|
2014-07-15 23:10:18 +00:00
|
|
|
return;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2014-07-15 23:10:18 +00:00
|
|
|
if (!iter_->Valid()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
FindParseableKey(&ikey, kReverse);
|
|
|
|
if (user_comparator_->Compare(ikey.user_key, saved_key_.GetKey()) == 0) {
|
2011-03-25 20:27:43 +00:00
|
|
|
|
2014-07-15 23:10:18 +00:00
|
|
|
FindPrevUserKey();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We haven't found any key - iterator is not valid
|
|
|
|
assert(!iter_->Valid());
|
|
|
|
valid_ = false;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 23:10:18 +00:00
|
|
|
// This function checks, if the entry with biggest sequence_number <= sequence_
|
|
|
|
// is non kTypeDeletion. If it's not, we save value in saved_value_
|
|
|
|
bool DBIter::FindValueForCurrentKey() {
|
|
|
|
assert(iter_->Valid());
|
|
|
|
// Contains operands for merge operator.
|
|
|
|
std::deque<std::string> operands;
|
|
|
|
// last entry before merge (could be kTypeDeletion or kTypeValue)
|
|
|
|
ValueType last_not_merge_type = kTypeDeletion;
|
|
|
|
ValueType last_key_entry_type = kTypeDeletion;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2014-07-15 23:10:18 +00:00
|
|
|
ParsedInternalKey ikey;
|
|
|
|
FindParseableKey(&ikey, kReverse);
|
|
|
|
|
|
|
|
size_t num_skipped = 0;
|
|
|
|
while (iter_->Valid() && ikey.sequence <= sequence_ &&
|
|
|
|
(user_comparator_->Compare(ikey.user_key, saved_key_.GetKey()) == 0)) {
|
|
|
|
// We iterate too much: let's use Seek() to avoid too much key comparisons
|
|
|
|
if (num_skipped >= max_skip_) {
|
|
|
|
return FindValueForCurrentKeyUsingSeek();
|
|
|
|
}
|
|
|
|
|
|
|
|
last_key_entry_type = ikey.type;
|
|
|
|
switch (last_key_entry_type) {
|
|
|
|
case kTypeValue:
|
|
|
|
operands.clear();
|
|
|
|
saved_value_ = iter_->value().ToString();
|
|
|
|
last_not_merge_type = kTypeValue;
|
|
|
|
break;
|
|
|
|
case kTypeDeletion:
|
|
|
|
operands.clear();
|
|
|
|
last_not_merge_type = kTypeDeletion;
|
2014-09-04 17:48:24 +00:00
|
|
|
PERF_COUNTER_ADD(internal_delete_skipped_count, 1);
|
2014-07-15 23:10:18 +00:00
|
|
|
break;
|
|
|
|
case kTypeMerge:
|
|
|
|
assert(user_merge_operator_ != nullptr);
|
|
|
|
operands.push_back(iter_->value().ToString());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
2014-09-04 17:48:24 +00:00
|
|
|
PERF_COUNTER_ADD(internal_key_skipped_count, 1);
|
2014-07-15 23:10:18 +00:00
|
|
|
assert(user_comparator_->Compare(ikey.user_key, saved_key_.GetKey()) == 0);
|
|
|
|
iter_->Prev();
|
|
|
|
++num_skipped;
|
|
|
|
FindParseableKey(&ikey, kReverse);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (last_key_entry_type) {
|
|
|
|
case kTypeDeletion:
|
|
|
|
valid_ = false;
|
|
|
|
return false;
|
|
|
|
case kTypeMerge:
|
|
|
|
if (last_not_merge_type == kTypeDeletion) {
|
2015-03-03 18:59:36 +00:00
|
|
|
StopWatchNano timer(env_, statistics_ != nullptr);
|
|
|
|
PERF_TIMER_GUARD(merge_operator_time_nanos);
|
2014-07-15 23:10:18 +00:00
|
|
|
user_merge_operator_->FullMerge(saved_key_.GetKey(), nullptr, operands,
|
|
|
|
&saved_value_, logger_);
|
2015-03-03 18:59:36 +00:00
|
|
|
RecordTick(statistics_, MERGE_OPERATION_TOTAL_TIME,
|
|
|
|
timer.ElapsedNanos());
|
2013-07-28 18:53:08 +00:00
|
|
|
} else {
|
2014-07-15 23:10:18 +00:00
|
|
|
assert(last_not_merge_type == kTypeValue);
|
|
|
|
std::string last_put_value = saved_value_;
|
|
|
|
Slice temp_slice(last_put_value);
|
2015-03-03 18:59:36 +00:00
|
|
|
{
|
|
|
|
StopWatchNano timer(env_, statistics_ != nullptr);
|
|
|
|
PERF_TIMER_GUARD(merge_operator_time_nanos);
|
|
|
|
user_merge_operator_->FullMerge(saved_key_.GetKey(), &temp_slice,
|
|
|
|
operands, &saved_value_, logger_);
|
|
|
|
RecordTick(statistics_, MERGE_OPERATION_TOTAL_TIME,
|
|
|
|
timer.ElapsedNanos());
|
|
|
|
}
|
2013-07-28 18:53:08 +00:00
|
|
|
}
|
2014-07-15 23:10:18 +00:00
|
|
|
break;
|
|
|
|
case kTypeValue:
|
|
|
|
// do nothing - we've already has value in saved_value_
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
2011-03-25 20:27:43 +00:00
|
|
|
}
|
2014-07-15 23:10:18 +00:00
|
|
|
valid_ = true;
|
|
|
|
return true;
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2014-07-15 23:10:18 +00:00
|
|
|
// This function is used in FindValueForCurrentKey.
|
|
|
|
// We use Seek() function instead of Prev() to find necessary value
|
|
|
|
bool DBIter::FindValueForCurrentKeyUsingSeek() {
|
|
|
|
std::string last_key;
|
|
|
|
AppendInternalKey(&last_key, ParsedInternalKey(saved_key_.GetKey(), sequence_,
|
|
|
|
kValueTypeForSeek));
|
|
|
|
iter_->Seek(last_key);
|
|
|
|
RecordTick(statistics_, NUMBER_OF_RESEEKS_IN_ITERATION);
|
|
|
|
|
|
|
|
// assume there is at least one parseable key for this user key
|
|
|
|
ParsedInternalKey ikey;
|
|
|
|
FindParseableKey(&ikey, kForward);
|
|
|
|
|
|
|
|
if (ikey.type == kTypeValue || ikey.type == kTypeDeletion) {
|
|
|
|
if (ikey.type == kTypeValue) {
|
|
|
|
saved_value_ = iter_->value().ToString();
|
|
|
|
valid_ = true;
|
|
|
|
return true;
|
|
|
|
}
|
2011-03-25 20:27:43 +00:00
|
|
|
valid_ = false;
|
2014-07-15 23:10:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// kTypeMerge. We need to collect all kTypeMerge values and save them
|
|
|
|
// in operands
|
|
|
|
std::deque<std::string> operands;
|
|
|
|
while (iter_->Valid() &&
|
|
|
|
(user_comparator_->Compare(ikey.user_key, saved_key_.GetKey()) == 0) &&
|
|
|
|
ikey.type == kTypeMerge) {
|
|
|
|
operands.push_front(iter_->value().ToString());
|
|
|
|
iter_->Next();
|
|
|
|
FindParseableKey(&ikey, kForward);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!iter_->Valid() ||
|
|
|
|
(user_comparator_->Compare(ikey.user_key, saved_key_.GetKey()) != 0) ||
|
|
|
|
ikey.type == kTypeDeletion) {
|
2015-03-03 18:59:36 +00:00
|
|
|
{
|
|
|
|
StopWatchNano timer(env_, statistics_ != nullptr);
|
|
|
|
PERF_TIMER_GUARD(merge_operator_time_nanos);
|
|
|
|
user_merge_operator_->FullMerge(saved_key_.GetKey(), nullptr, operands,
|
|
|
|
&saved_value_, logger_);
|
|
|
|
RecordTick(statistics_, MERGE_OPERATION_TOTAL_TIME, timer.ElapsedNanos());
|
|
|
|
}
|
2014-07-15 23:10:18 +00:00
|
|
|
// Make iter_ valid and point to saved_key_
|
|
|
|
if (!iter_->Valid() ||
|
|
|
|
(user_comparator_->Compare(ikey.user_key, saved_key_.GetKey()) != 0)) {
|
|
|
|
iter_->Seek(last_key);
|
|
|
|
RecordTick(statistics_, NUMBER_OF_RESEEKS_IN_ITERATION);
|
|
|
|
}
|
2011-03-25 20:27:43 +00:00
|
|
|
valid_ = true;
|
2014-07-15 23:10:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-06 19:14:28 +00:00
|
|
|
const Slice& val = iter_->value();
|
2015-03-03 18:59:36 +00:00
|
|
|
{
|
|
|
|
StopWatchNano timer(env_, statistics_ != nullptr);
|
|
|
|
PERF_TIMER_GUARD(merge_operator_time_nanos);
|
|
|
|
user_merge_operator_->FullMerge(saved_key_.GetKey(), &val, operands,
|
|
|
|
&saved_value_, logger_);
|
|
|
|
RecordTick(statistics_, MERGE_OPERATION_TOTAL_TIME, timer.ElapsedNanos());
|
|
|
|
}
|
2014-07-15 23:10:18 +00:00
|
|
|
valid_ = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used in Next to change directions
|
|
|
|
// Go to next user key
|
|
|
|
// Don't use Seek(),
|
|
|
|
// because next user key will be very close
|
|
|
|
void DBIter::FindNextUserKey() {
|
|
|
|
if (!iter_->Valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ParsedInternalKey ikey;
|
|
|
|
FindParseableKey(&ikey, kForward);
|
|
|
|
while (iter_->Valid() &&
|
2015-07-07 18:36:24 +00:00
|
|
|
user_comparator_->Compare(ikey.user_key, saved_key_.GetKey()) != 0) {
|
2014-07-15 23:10:18 +00:00
|
|
|
iter_->Next();
|
|
|
|
FindParseableKey(&ikey, kForward);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go to previous user_key
|
|
|
|
void DBIter::FindPrevUserKey() {
|
|
|
|
if (!iter_->Valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
size_t num_skipped = 0;
|
|
|
|
ParsedInternalKey ikey;
|
|
|
|
FindParseableKey(&ikey, kReverse);
|
|
|
|
while (iter_->Valid() &&
|
2015-07-07 18:36:24 +00:00
|
|
|
user_comparator_->Compare(ikey.user_key, saved_key_.GetKey()) == 0) {
|
2014-07-15 23:10:18 +00:00
|
|
|
if (num_skipped >= max_skip_) {
|
|
|
|
num_skipped = 0;
|
|
|
|
IterKey last_key;
|
|
|
|
last_key.SetInternalKey(ParsedInternalKey(
|
|
|
|
saved_key_.GetKey(), kMaxSequenceNumber, kValueTypeForSeek));
|
|
|
|
iter_->Seek(last_key.GetKey());
|
|
|
|
RecordTick(statistics_, NUMBER_OF_RESEEKS_IN_ITERATION);
|
|
|
|
}
|
|
|
|
|
|
|
|
iter_->Prev();
|
|
|
|
++num_skipped;
|
|
|
|
FindParseableKey(&ikey, kReverse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip all unparseable keys
|
|
|
|
void DBIter::FindParseableKey(ParsedInternalKey* ikey, Direction direction) {
|
|
|
|
while (iter_->Valid() && !ParseKey(ikey)) {
|
|
|
|
if (direction == kReverse) {
|
|
|
|
iter_->Prev();
|
|
|
|
} else {
|
|
|
|
iter_->Next();
|
|
|
|
}
|
2011-03-25 20:27:43 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
void DBIter::Seek(const Slice& target) {
|
2014-08-13 22:56:37 +00:00
|
|
|
StopWatch sw(env_, statistics_, DB_SEEK);
|
|
|
|
|
2014-09-04 17:48:24 +00:00
|
|
|
// total ordering is not guaranteed if prefix_extractor is set
|
|
|
|
// hence prefix based seeks will not give correct results
|
|
|
|
if (iterate_upper_bound_ != nullptr && prefix_extractor_ != nullptr) {
|
|
|
|
if (!prefix_extractor_->InDomain(*iterate_upper_bound_) ||
|
|
|
|
!prefix_extractor_->InDomain(target) ||
|
|
|
|
prefix_extractor_->Transform(*iterate_upper_bound_).compare(
|
|
|
|
prefix_extractor_->Transform(target)) != 0) {
|
|
|
|
status_ = Status::InvalidArgument("read_options.iterate_*_bound "
|
|
|
|
" and seek target need to have the same prefix.");
|
|
|
|
valid_ = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 21:45:30 +00:00
|
|
|
saved_key_.Clear();
|
|
|
|
// now savved_key is used to store internal key.
|
|
|
|
saved_key_.SetInternalKey(target, sequence_);
|
2014-08-22 22:28:58 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
PERF_TIMER_GUARD(seek_internal_seek_time);
|
|
|
|
iter_->Seek(saved_key_.GetKey());
|
|
|
|
}
|
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
if (iter_->Valid()) {
|
2013-11-20 00:37:34 +00:00
|
|
|
direction_ = kForward;
|
|
|
|
ClearSavedValue();
|
2013-03-21 22:59:47 +00:00
|
|
|
FindNextUserEntry(false /*not skipping */);
|
2011-03-25 20:27:43 +00:00
|
|
|
} else {
|
|
|
|
valid_ = false;
|
|
|
|
}
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
void DBIter::SeekToFirst() {
|
2014-08-08 16:44:14 +00:00
|
|
|
// Don't use iter_::Seek() if we set a prefix extractor
|
|
|
|
// because prefix seek wiil be used.
|
2014-09-04 17:48:24 +00:00
|
|
|
if (prefix_extractor_ != nullptr) {
|
2014-08-08 16:44:14 +00:00
|
|
|
max_skip_ = std::numeric_limits<uint64_t>::max();
|
|
|
|
}
|
2011-03-25 20:27:43 +00:00
|
|
|
direction_ = kForward;
|
|
|
|
ClearSavedValue();
|
2014-08-22 22:28:58 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
PERF_TIMER_GUARD(seek_internal_seek_time);
|
|
|
|
iter_->SeekToFirst();
|
|
|
|
}
|
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
if (iter_->Valid()) {
|
2013-03-21 22:59:47 +00:00
|
|
|
FindNextUserEntry(false /* not skipping */);
|
2011-03-25 20:27:43 +00:00
|
|
|
} else {
|
|
|
|
valid_ = false;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
void DBIter::SeekToLast() {
|
2014-08-08 16:44:14 +00:00
|
|
|
// Don't use iter_::Seek() if we set a prefix extractor
|
|
|
|
// because prefix seek wiil be used.
|
2014-09-04 17:48:24 +00:00
|
|
|
if (prefix_extractor_ != nullptr) {
|
2014-08-08 16:44:14 +00:00
|
|
|
max_skip_ = std::numeric_limits<uint64_t>::max();
|
|
|
|
}
|
2011-03-25 20:27:43 +00:00
|
|
|
direction_ = kReverse;
|
|
|
|
ClearSavedValue();
|
2014-08-22 22:28:58 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
PERF_TIMER_GUARD(seek_internal_seek_time);
|
|
|
|
iter_->SeekToLast();
|
|
|
|
}
|
2015-06-25 16:44:30 +00:00
|
|
|
// When the iterate_upper_bound is set to a value,
|
|
|
|
// it will seek to the last key before the
|
|
|
|
// ReadOptions.iterate_upper_bound
|
|
|
|
if (iter_->Valid() && iterate_upper_bound_ != nullptr) {
|
|
|
|
saved_key_.SetKey(*iterate_upper_bound_);
|
|
|
|
std::string last_key;
|
|
|
|
AppendInternalKey(&last_key,
|
|
|
|
ParsedInternalKey(saved_key_.GetKey(), kMaxSequenceNumber,
|
|
|
|
kValueTypeForSeek));
|
|
|
|
|
|
|
|
iter_->Seek(last_key);
|
2014-07-15 23:10:18 +00:00
|
|
|
|
2015-06-25 16:44:30 +00:00
|
|
|
if (!iter_->Valid()) {
|
|
|
|
iter_->SeekToLast();
|
|
|
|
} else {
|
|
|
|
iter_->Prev();
|
|
|
|
if (!iter_->Valid()) {
|
|
|
|
valid_ = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-15 23:10:18 +00:00
|
|
|
PrevInternal();
|
2011-03-25 20:27:43 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 22:04:34 +00:00
|
|
|
Iterator* NewDBIterator(Env* env, const ImmutableCFOptions& ioptions,
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
const Comparator* user_key_comparator,
|
|
|
|
Iterator* internal_iter,
|
2014-09-04 17:48:24 +00:00
|
|
|
const SequenceNumber& sequence,
|
2014-09-08 22:04:34 +00:00
|
|
|
uint64_t max_sequential_skip_in_iterations,
|
2014-09-04 17:48:24 +00:00
|
|
|
const Slice* iterate_upper_bound) {
|
2014-09-08 22:04:34 +00:00
|
|
|
return new DBIter(env, ioptions, user_key_comparator, internal_iter, sequence,
|
|
|
|
false, max_sequential_skip_in_iterations,
|
|
|
|
iterate_upper_bound);
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
}
|
|
|
|
|
2014-06-03 19:28:58 +00:00
|
|
|
ArenaWrappedDBIter::~ArenaWrappedDBIter() { db_iter_->~DBIter(); }
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
|
|
|
|
void ArenaWrappedDBIter::SetDBIter(DBIter* iter) { db_iter_ = iter; }
|
|
|
|
|
|
|
|
void ArenaWrappedDBIter::SetIterUnderDBIter(Iterator* iter) {
|
|
|
|
static_cast<DBIter*>(db_iter_)->SetIter(iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ArenaWrappedDBIter::Valid() const { return db_iter_->Valid(); }
|
|
|
|
inline void ArenaWrappedDBIter::SeekToFirst() { db_iter_->SeekToFirst(); }
|
|
|
|
inline void ArenaWrappedDBIter::SeekToLast() { db_iter_->SeekToLast(); }
|
|
|
|
inline void ArenaWrappedDBIter::Seek(const Slice& target) {
|
|
|
|
db_iter_->Seek(target);
|
|
|
|
}
|
|
|
|
inline void ArenaWrappedDBIter::Next() { db_iter_->Next(); }
|
|
|
|
inline void ArenaWrappedDBIter::Prev() { db_iter_->Prev(); }
|
|
|
|
inline Slice ArenaWrappedDBIter::key() const { return db_iter_->key(); }
|
|
|
|
inline Slice ArenaWrappedDBIter::value() const { return db_iter_->value(); }
|
|
|
|
inline Status ArenaWrappedDBIter::status() const { return db_iter_->status(); }
|
|
|
|
void ArenaWrappedDBIter::RegisterCleanup(CleanupFunction function, void* arg1,
|
|
|
|
void* arg2) {
|
|
|
|
db_iter_->RegisterCleanup(function, arg1, arg2);
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
ArenaWrappedDBIter* NewArenaWrappedDbIterator(
|
2014-09-08 22:04:34 +00:00
|
|
|
Env* env, const ImmutableCFOptions& ioptions,
|
|
|
|
const Comparator* user_key_comparator,
|
2014-09-04 17:48:24 +00:00
|
|
|
const SequenceNumber& sequence,
|
2014-09-08 22:04:34 +00:00
|
|
|
uint64_t max_sequential_skip_in_iterations,
|
2014-09-04 17:48:24 +00:00
|
|
|
const Slice* iterate_upper_bound) {
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
ArenaWrappedDBIter* iter = new ArenaWrappedDBIter();
|
|
|
|
Arena* arena = iter->GetArena();
|
|
|
|
auto mem = arena->AllocateAligned(sizeof(DBIter));
|
2014-09-08 22:04:34 +00:00
|
|
|
DBIter* db_iter = new (mem) DBIter(env, ioptions, user_key_comparator,
|
|
|
|
nullptr, sequence, true, max_sequential_skip_in_iterations,
|
|
|
|
iterate_upper_bound);
|
2014-09-04 17:48:24 +00:00
|
|
|
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
iter->SetDBIter(db_iter);
|
2014-09-04 17:48:24 +00:00
|
|
|
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
return iter;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|