2013-10-16 23:59:46 +02: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 23:37:00 +01: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 "table/two_level_iterator.h"
|
|
|
|
|
2013-10-29 01:54:09 +01:00
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "rocksdb/table.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
#include "table/block.h"
|
|
|
|
#include "table/format.h"
|
|
|
|
#include "table/iterator_wrapper.h"
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-03-15 01:00:04 +01:00
|
|
|
typedef Iterator* (*BlockFunction)(void*, const ReadOptions&,
|
2014-01-27 22:53:22 +01:00
|
|
|
const EnvOptions& soptions,
|
|
|
|
const InternalKeyComparator& icomparator,
|
|
|
|
const Slice&, bool for_compaction);
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
class TwoLevelIterator: public Iterator {
|
|
|
|
public:
|
2014-01-27 22:53:22 +01:00
|
|
|
TwoLevelIterator(Iterator* index_iter, BlockFunction block_function,
|
|
|
|
void* arg, const ReadOptions& options,
|
|
|
|
const EnvOptions& soptions,
|
|
|
|
const InternalKeyComparator& internal_comparator,
|
|
|
|
bool for_compaction);
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
virtual ~TwoLevelIterator();
|
|
|
|
|
|
|
|
virtual void Seek(const Slice& target);
|
|
|
|
virtual void SeekToFirst();
|
|
|
|
virtual void SeekToLast();
|
|
|
|
virtual void Next();
|
|
|
|
virtual void Prev();
|
|
|
|
|
|
|
|
virtual bool Valid() const {
|
|
|
|
return data_iter_.Valid();
|
|
|
|
}
|
|
|
|
virtual Slice key() const {
|
|
|
|
assert(Valid());
|
|
|
|
return data_iter_.key();
|
|
|
|
}
|
|
|
|
virtual Slice value() const {
|
|
|
|
assert(Valid());
|
|
|
|
return data_iter_.value();
|
|
|
|
}
|
|
|
|
virtual Status status() const {
|
|
|
|
// It'd be nice if status() returned a const Status& instead of a Status
|
|
|
|
if (!index_iter_.status().ok()) {
|
|
|
|
return index_iter_.status();
|
2013-03-01 03:04:58 +01:00
|
|
|
} else if (data_iter_.iter() != nullptr && !data_iter_.status().ok()) {
|
2011-03-18 23:37:00 +01:00
|
|
|
return data_iter_.status();
|
|
|
|
} else {
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void SaveError(const Status& s) {
|
|
|
|
if (status_.ok() && !s.ok()) status_ = s;
|
|
|
|
}
|
|
|
|
void SkipEmptyDataBlocksForward();
|
|
|
|
void SkipEmptyDataBlocksBackward();
|
|
|
|
void SetDataIterator(Iterator* data_iter);
|
|
|
|
void InitDataBlock();
|
|
|
|
|
|
|
|
BlockFunction block_function_;
|
|
|
|
void* arg_;
|
|
|
|
const ReadOptions options_;
|
2013-03-15 01:00:04 +01:00
|
|
|
const EnvOptions& soptions_;
|
2014-01-27 22:53:22 +01:00
|
|
|
const InternalKeyComparator& internal_comparator_;
|
2011-03-18 23:37:00 +01:00
|
|
|
Status status_;
|
|
|
|
IteratorWrapper index_iter_;
|
2013-03-01 03:04:58 +01:00
|
|
|
IteratorWrapper data_iter_; // May be nullptr
|
|
|
|
// If data_iter_ is non-nullptr, then "data_block_handle_" holds the
|
2011-03-18 23:37:00 +01:00
|
|
|
// "index_value" passed to block_function_ to create the data_iter_.
|
|
|
|
std::string data_block_handle_;
|
2013-05-18 00:53:01 +02:00
|
|
|
bool for_compaction_;
|
2011-03-18 23:37:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TwoLevelIterator::TwoLevelIterator(
|
2014-01-27 22:53:22 +01:00
|
|
|
Iterator* index_iter, BlockFunction block_function, void* arg,
|
|
|
|
const ReadOptions& options, const EnvOptions& soptions,
|
|
|
|
const InternalKeyComparator& internal_comparator, bool for_compaction)
|
2011-03-18 23:37:00 +01:00
|
|
|
: block_function_(block_function),
|
|
|
|
arg_(arg),
|
|
|
|
options_(options),
|
2013-03-15 01:00:04 +01:00
|
|
|
soptions_(soptions),
|
2014-01-27 22:53:22 +01:00
|
|
|
internal_comparator_(internal_comparator),
|
2011-03-18 23:37:00 +01:00
|
|
|
index_iter_(index_iter),
|
2013-05-18 00:53:01 +02:00
|
|
|
data_iter_(nullptr),
|
2014-01-27 22:53:22 +01:00
|
|
|
for_compaction_(for_compaction) {}
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
TwoLevelIterator::~TwoLevelIterator() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::Seek(const Slice& target) {
|
|
|
|
index_iter_.Seek(target);
|
|
|
|
InitDataBlock();
|
2013-03-01 03:04:58 +01:00
|
|
|
if (data_iter_.iter() != nullptr) data_iter_.Seek(target);
|
2011-03-18 23:37:00 +01:00
|
|
|
SkipEmptyDataBlocksForward();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::SeekToFirst() {
|
|
|
|
index_iter_.SeekToFirst();
|
|
|
|
InitDataBlock();
|
2013-03-01 03:04:58 +01:00
|
|
|
if (data_iter_.iter() != nullptr) data_iter_.SeekToFirst();
|
2011-03-18 23:37:00 +01:00
|
|
|
SkipEmptyDataBlocksForward();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::SeekToLast() {
|
|
|
|
index_iter_.SeekToLast();
|
|
|
|
InitDataBlock();
|
2013-03-01 03:04:58 +01:00
|
|
|
if (data_iter_.iter() != nullptr) data_iter_.SeekToLast();
|
2011-03-18 23:37:00 +01:00
|
|
|
SkipEmptyDataBlocksBackward();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::Next() {
|
|
|
|
assert(Valid());
|
|
|
|
data_iter_.Next();
|
|
|
|
SkipEmptyDataBlocksForward();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::Prev() {
|
|
|
|
assert(Valid());
|
|
|
|
data_iter_.Prev();
|
|
|
|
SkipEmptyDataBlocksBackward();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TwoLevelIterator::SkipEmptyDataBlocksForward() {
|
2013-11-06 23:11:52 +01:00
|
|
|
while (data_iter_.iter() == nullptr || (!data_iter_.Valid() &&
|
|
|
|
!data_iter_.status().IsIncomplete())) {
|
2011-03-18 23:37:00 +01:00
|
|
|
// Move to next block
|
|
|
|
if (!index_iter_.Valid()) {
|
2013-03-01 03:04:58 +01:00
|
|
|
SetDataIterator(nullptr);
|
2011-03-18 23:37:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
index_iter_.Next();
|
|
|
|
InitDataBlock();
|
2013-03-01 03:04:58 +01:00
|
|
|
if (data_iter_.iter() != nullptr) data_iter_.SeekToFirst();
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::SkipEmptyDataBlocksBackward() {
|
2013-11-06 23:11:52 +01:00
|
|
|
while (data_iter_.iter() == nullptr || (!data_iter_.Valid() &&
|
|
|
|
!data_iter_.status().IsIncomplete())) {
|
2011-03-18 23:37:00 +01:00
|
|
|
// Move to next block
|
|
|
|
if (!index_iter_.Valid()) {
|
2013-03-01 03:04:58 +01:00
|
|
|
SetDataIterator(nullptr);
|
2011-03-18 23:37:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
index_iter_.Prev();
|
|
|
|
InitDataBlock();
|
2013-03-01 03:04:58 +01:00
|
|
|
if (data_iter_.iter() != nullptr) data_iter_.SeekToLast();
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::SetDataIterator(Iterator* data_iter) {
|
2013-03-01 03:04:58 +01:00
|
|
|
if (data_iter_.iter() != nullptr) SaveError(data_iter_.status());
|
2011-03-18 23:37:00 +01:00
|
|
|
data_iter_.Set(data_iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::InitDataBlock() {
|
|
|
|
if (!index_iter_.Valid()) {
|
2013-03-01 03:04:58 +01:00
|
|
|
SetDataIterator(nullptr);
|
2011-03-18 23:37:00 +01:00
|
|
|
} else {
|
|
|
|
Slice handle = index_iter_.value();
|
2013-03-01 03:04:58 +01:00
|
|
|
if (data_iter_.iter() != nullptr
|
|
|
|
&& handle.compare(data_block_handle_) == 0) {
|
2011-03-18 23:37:00 +01:00
|
|
|
// data_iter_ is already constructed with this iterator, so
|
|
|
|
// no need to change anything
|
|
|
|
} else {
|
2014-01-27 22:53:22 +01:00
|
|
|
Iterator* iter =
|
|
|
|
(*block_function_)(arg_, options_, soptions_, internal_comparator_,
|
|
|
|
handle, for_compaction_);
|
2011-03-18 23:37:00 +01:00
|
|
|
data_block_handle_.assign(handle.data(), handle.size());
|
|
|
|
SetDataIterator(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-31 18:22:06 +01:00
|
|
|
} // namespace
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2014-01-27 22:53:22 +01:00
|
|
|
Iterator* NewTwoLevelIterator(Iterator* index_iter,
|
|
|
|
BlockFunction block_function, void* arg,
|
|
|
|
const ReadOptions& options,
|
|
|
|
const EnvOptions& soptions,
|
|
|
|
const InternalKeyComparator& internal_comparator,
|
|
|
|
bool for_compaction) {
|
|
|
|
return new TwoLevelIterator(index_iter, block_function, arg, options,
|
|
|
|
soptions, internal_comparator, for_compaction);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|