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 "table/two_level_iterator.h"
|
|
|
|
|
2011-03-30 18:35:40 +00:00
|
|
|
#include "leveldb/table.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "table/block.h"
|
|
|
|
#include "table/format.h"
|
|
|
|
#include "table/iterator_wrapper.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
typedef Iterator* (*BlockFunction)(void*, const ReadOptions&, const Slice&);
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
class TwoLevelIterator : public Iterator {
|
2011-03-18 22:37:00 +00:00
|
|
|
public:
|
2019-05-02 11:01:00 -07:00
|
|
|
TwoLevelIterator(Iterator* index_iter, BlockFunction block_function,
|
|
|
|
void* arg, const ReadOptions& options);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
~TwoLevelIterator() override;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
void Seek(const Slice& target) override;
|
|
|
|
void SeekToFirst() override;
|
|
|
|
void SeekToLast() override;
|
|
|
|
void Next() override;
|
|
|
|
void Prev() override;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
bool Valid() const override { return data_iter_.Valid(); }
|
|
|
|
Slice key() const override {
|
2011-03-18 22:37:00 +00:00
|
|
|
assert(Valid());
|
|
|
|
return data_iter_.key();
|
|
|
|
}
|
2019-05-09 14:00:07 -07:00
|
|
|
Slice value() const override {
|
2011-03-18 22:37:00 +00:00
|
|
|
assert(Valid());
|
|
|
|
return data_iter_.value();
|
|
|
|
}
|
2019-05-09 14:00:07 -07:00
|
|
|
Status status() const override {
|
2011-03-18 22:37:00 +00:00
|
|
|
// It'd be nice if status() returned a const Status& instead of a Status
|
|
|
|
if (!index_iter_.status().ok()) {
|
|
|
|
return index_iter_.status();
|
2018-04-10 16:18:06 -07:00
|
|
|
} else if (data_iter_.iter() != nullptr && !data_iter_.status().ok()) {
|
2011-03-18 22:37:00 +00: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_;
|
|
|
|
Status status_;
|
|
|
|
IteratorWrapper index_iter_;
|
2019-05-02 11:01:00 -07:00
|
|
|
IteratorWrapper data_iter_; // May be nullptr
|
2018-04-10 16:18:06 -07:00
|
|
|
// If data_iter_ is non-null, then "data_block_handle_" holds the
|
2011-03-18 22:37:00 +00:00
|
|
|
// "index_value" passed to block_function_ to create the data_iter_.
|
|
|
|
std::string data_block_handle_;
|
|
|
|
};
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
TwoLevelIterator::TwoLevelIterator(Iterator* index_iter,
|
|
|
|
BlockFunction block_function, void* arg,
|
|
|
|
const ReadOptions& options)
|
2011-03-18 22:37:00 +00:00
|
|
|
: block_function_(block_function),
|
|
|
|
arg_(arg),
|
|
|
|
options_(options),
|
|
|
|
index_iter_(index_iter),
|
2019-05-02 11:01:00 -07:00
|
|
|
data_iter_(nullptr) {}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-05-04 17:40:21 -07:00
|
|
|
TwoLevelIterator::~TwoLevelIterator() = default;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
void TwoLevelIterator::Seek(const Slice& target) {
|
|
|
|
index_iter_.Seek(target);
|
|
|
|
InitDataBlock();
|
2018-04-10 16:18:06 -07:00
|
|
|
if (data_iter_.iter() != nullptr) data_iter_.Seek(target);
|
2011-03-18 22:37:00 +00:00
|
|
|
SkipEmptyDataBlocksForward();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::SeekToFirst() {
|
|
|
|
index_iter_.SeekToFirst();
|
|
|
|
InitDataBlock();
|
2018-04-10 16:18:06 -07:00
|
|
|
if (data_iter_.iter() != nullptr) data_iter_.SeekToFirst();
|
2011-03-18 22:37:00 +00:00
|
|
|
SkipEmptyDataBlocksForward();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::SeekToLast() {
|
|
|
|
index_iter_.SeekToLast();
|
|
|
|
InitDataBlock();
|
2018-04-10 16:18:06 -07:00
|
|
|
if (data_iter_.iter() != nullptr) data_iter_.SeekToLast();
|
2011-03-18 22:37:00 +00:00
|
|
|
SkipEmptyDataBlocksBackward();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::Next() {
|
|
|
|
assert(Valid());
|
|
|
|
data_iter_.Next();
|
|
|
|
SkipEmptyDataBlocksForward();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::Prev() {
|
|
|
|
assert(Valid());
|
|
|
|
data_iter_.Prev();
|
|
|
|
SkipEmptyDataBlocksBackward();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::SkipEmptyDataBlocksForward() {
|
2018-04-10 16:18:06 -07:00
|
|
|
while (data_iter_.iter() == nullptr || !data_iter_.Valid()) {
|
2011-03-18 22:37:00 +00:00
|
|
|
// Move to next block
|
|
|
|
if (!index_iter_.Valid()) {
|
2018-04-10 16:18:06 -07:00
|
|
|
SetDataIterator(nullptr);
|
2011-03-18 22:37:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
index_iter_.Next();
|
|
|
|
InitDataBlock();
|
2018-04-10 16:18:06 -07:00
|
|
|
if (data_iter_.iter() != nullptr) data_iter_.SeekToFirst();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::SkipEmptyDataBlocksBackward() {
|
2018-04-10 16:18:06 -07:00
|
|
|
while (data_iter_.iter() == nullptr || !data_iter_.Valid()) {
|
2011-03-18 22:37:00 +00:00
|
|
|
// Move to next block
|
|
|
|
if (!index_iter_.Valid()) {
|
2018-04-10 16:18:06 -07:00
|
|
|
SetDataIterator(nullptr);
|
2011-03-18 22:37:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
index_iter_.Prev();
|
|
|
|
InitDataBlock();
|
2018-04-10 16:18:06 -07:00
|
|
|
if (data_iter_.iter() != nullptr) data_iter_.SeekToLast();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::SetDataIterator(Iterator* data_iter) {
|
2018-04-10 16:18:06 -07:00
|
|
|
if (data_iter_.iter() != nullptr) SaveError(data_iter_.status());
|
2011-03-18 22:37:00 +00:00
|
|
|
data_iter_.Set(data_iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoLevelIterator::InitDataBlock() {
|
|
|
|
if (!index_iter_.Valid()) {
|
2018-04-10 16:18:06 -07:00
|
|
|
SetDataIterator(nullptr);
|
2011-03-18 22:37:00 +00:00
|
|
|
} else {
|
|
|
|
Slice handle = index_iter_.value();
|
2019-05-02 11:01:00 -07:00
|
|
|
if (data_iter_.iter() != nullptr &&
|
|
|
|
handle.compare(data_block_handle_) == 0) {
|
2011-03-18 22:37:00 +00:00
|
|
|
// data_iter_ is already constructed with this iterator, so
|
|
|
|
// no need to change anything
|
|
|
|
} else {
|
|
|
|
Iterator* iter = (*block_function_)(arg_, options_, handle);
|
|
|
|
data_block_handle_.assign(handle.data(), handle.size());
|
|
|
|
SetDataIterator(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
Iterator* NewTwoLevelIterator(Iterator* index_iter,
|
|
|
|
BlockFunction block_function, void* arg,
|
|
|
|
const ReadOptions& options) {
|
2011-03-18 22:37:00 +00:00
|
|
|
return new TwoLevelIterator(index_iter, block_function, arg, options);
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace leveldb
|