mirror of
https://github.com/facebook/rocksdb.git
synced 2024-11-27 11:43:49 +00:00
36a5f8ed7f
- Replace raw slice comparison with a call to user comparator. Added test for custom comparators. - Fix end of namespace comments. - Fixed bug in picking inputs for a level-0 compaction. When finding overlapping files, the covered range may expand as files are added to the input set. We now correctly expand the range when this happens instead of continuing to use the old range. For example, suppose L0 contains files with the following ranges: F1: a .. d F2: c .. g F3: f .. j and the initial compaction target is F3. We used to search for range f..j which yielded {F2,F3}. However we now expand the range as soon as another file is added. In this case, when F2 is added, we expand the range to c..j and restart the search. That picks up file F1 as well. This change fixes a bug related to deleted keys showing up incorrectly after a compaction as described in Issue 44. (Sync with upstream @25072954)
183 lines
4.6 KiB
C++
183 lines
4.6 KiB
C++
// 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"
|
|
|
|
#include "leveldb/table.h"
|
|
#include "table/block.h"
|
|
#include "table/format.h"
|
|
#include "table/iterator_wrapper.h"
|
|
|
|
namespace leveldb {
|
|
|
|
namespace {
|
|
|
|
typedef Iterator* (*BlockFunction)(void*, const ReadOptions&, const Slice&);
|
|
|
|
class TwoLevelIterator: public Iterator {
|
|
public:
|
|
TwoLevelIterator(
|
|
Iterator* index_iter,
|
|
BlockFunction block_function,
|
|
void* arg,
|
|
const ReadOptions& options);
|
|
|
|
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();
|
|
} else if (data_iter_.iter() != NULL && !data_iter_.status().ok()) {
|
|
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_;
|
|
IteratorWrapper data_iter_; // May be NULL
|
|
// If data_iter_ is non-NULL, then "data_block_handle_" holds the
|
|
// "index_value" passed to block_function_ to create the data_iter_.
|
|
std::string data_block_handle_;
|
|
};
|
|
|
|
TwoLevelIterator::TwoLevelIterator(
|
|
Iterator* index_iter,
|
|
BlockFunction block_function,
|
|
void* arg,
|
|
const ReadOptions& options)
|
|
: block_function_(block_function),
|
|
arg_(arg),
|
|
options_(options),
|
|
index_iter_(index_iter),
|
|
data_iter_(NULL) {
|
|
}
|
|
|
|
TwoLevelIterator::~TwoLevelIterator() {
|
|
}
|
|
|
|
void TwoLevelIterator::Seek(const Slice& target) {
|
|
index_iter_.Seek(target);
|
|
InitDataBlock();
|
|
if (data_iter_.iter() != NULL) data_iter_.Seek(target);
|
|
SkipEmptyDataBlocksForward();
|
|
}
|
|
|
|
void TwoLevelIterator::SeekToFirst() {
|
|
index_iter_.SeekToFirst();
|
|
InitDataBlock();
|
|
if (data_iter_.iter() != NULL) data_iter_.SeekToFirst();
|
|
SkipEmptyDataBlocksForward();
|
|
}
|
|
|
|
void TwoLevelIterator::SeekToLast() {
|
|
index_iter_.SeekToLast();
|
|
InitDataBlock();
|
|
if (data_iter_.iter() != NULL) data_iter_.SeekToLast();
|
|
SkipEmptyDataBlocksBackward();
|
|
}
|
|
|
|
void TwoLevelIterator::Next() {
|
|
assert(Valid());
|
|
data_iter_.Next();
|
|
SkipEmptyDataBlocksForward();
|
|
}
|
|
|
|
void TwoLevelIterator::Prev() {
|
|
assert(Valid());
|
|
data_iter_.Prev();
|
|
SkipEmptyDataBlocksBackward();
|
|
}
|
|
|
|
|
|
void TwoLevelIterator::SkipEmptyDataBlocksForward() {
|
|
while (data_iter_.iter() == NULL || !data_iter_.Valid()) {
|
|
// Move to next block
|
|
if (!index_iter_.Valid()) {
|
|
SetDataIterator(NULL);
|
|
return;
|
|
}
|
|
index_iter_.Next();
|
|
InitDataBlock();
|
|
if (data_iter_.iter() != NULL) data_iter_.SeekToFirst();
|
|
}
|
|
}
|
|
|
|
void TwoLevelIterator::SkipEmptyDataBlocksBackward() {
|
|
while (data_iter_.iter() == NULL || !data_iter_.Valid()) {
|
|
// Move to next block
|
|
if (!index_iter_.Valid()) {
|
|
SetDataIterator(NULL);
|
|
return;
|
|
}
|
|
index_iter_.Prev();
|
|
InitDataBlock();
|
|
if (data_iter_.iter() != NULL) data_iter_.SeekToLast();
|
|
}
|
|
}
|
|
|
|
void TwoLevelIterator::SetDataIterator(Iterator* data_iter) {
|
|
if (data_iter_.iter() != NULL) SaveError(data_iter_.status());
|
|
data_iter_.Set(data_iter);
|
|
}
|
|
|
|
void TwoLevelIterator::InitDataBlock() {
|
|
if (!index_iter_.Valid()) {
|
|
SetDataIterator(NULL);
|
|
} else {
|
|
Slice handle = index_iter_.value();
|
|
if (data_iter_.iter() != NULL && handle.compare(data_block_handle_) == 0) {
|
|
// 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);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Iterator* NewTwoLevelIterator(
|
|
Iterator* index_iter,
|
|
BlockFunction block_function,
|
|
void* arg,
|
|
const ReadOptions& options) {
|
|
return new TwoLevelIterator(index_iter, block_function, arg, options);
|
|
}
|
|
|
|
} // namespace leveldb
|