|
|
|
@ -148,26 +148,20 @@ class BinarySearchIndexReader : public IndexReader { |
|
|
|
|
public: |
|
|
|
|
// Read index from the file and create an intance for
|
|
|
|
|
// `BinarySearchIndexReader`.
|
|
|
|
|
// The return value is a pair, where
|
|
|
|
|
// * first element is the status indicating if the operation succeeded.
|
|
|
|
|
// * second element is the index reader to be created. On failure, this
|
|
|
|
|
// element will be nullptr
|
|
|
|
|
static std::pair<Status, IndexReader*> Create(RandomAccessFile* file, |
|
|
|
|
const BlockHandle& index_handle, |
|
|
|
|
Env* env, |
|
|
|
|
const Comparator* comparator) { |
|
|
|
|
// On success, index_reader will be populated; otherwise it will remain
|
|
|
|
|
// unmodified.
|
|
|
|
|
static Status Create(RandomAccessFile* file, const BlockHandle& index_handle, |
|
|
|
|
Env* env, const Comparator* comparator, |
|
|
|
|
IndexReader** index_reader) { |
|
|
|
|
Block* index_block = nullptr; |
|
|
|
|
auto s = |
|
|
|
|
ReadBlockFromFile(file, ReadOptions(), index_handle, &index_block, env); |
|
|
|
|
auto s = ReadBlockFromFile(file, ReadOptions(), index_handle, |
|
|
|
|
&index_block, env); |
|
|
|
|
|
|
|
|
|
if (!s.ok()) { |
|
|
|
|
// Logically, index_block shouldn't have been populated if any error
|
|
|
|
|
// occurred.
|
|
|
|
|
assert(index_block == nullptr); |
|
|
|
|
return {s, nullptr}; |
|
|
|
|
if (s.ok()) { |
|
|
|
|
*index_reader = new BinarySearchIndexReader(comparator, index_block); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return {s, new BinarySearchIndexReader(comparator, index_block)}; |
|
|
|
|
return s; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
virtual Iterator* NewIterator() override { |
|
|
|
@ -190,12 +184,12 @@ class BinarySearchIndexReader : public IndexReader { |
|
|
|
|
// key.
|
|
|
|
|
class HashIndexReader : public IndexReader { |
|
|
|
|
public: |
|
|
|
|
static std::pair<Status, IndexReader*> Create( |
|
|
|
|
RandomAccessFile* file, const BlockHandle& index_handle, Env* env, |
|
|
|
|
const Comparator* comparator, BlockBasedTable* table, |
|
|
|
|
const SliceTransform* prefix_extractor) { |
|
|
|
|
return {Status::NotSupported("not implemented yet!"), |
|
|
|
|
nullptr}; // not finished
|
|
|
|
|
static Status Create(RandomAccessFile* file, const BlockHandle& index_handle, |
|
|
|
|
Env* env, const Comparator* comparator, |
|
|
|
|
BlockBasedTable* table, |
|
|
|
|
const SliceTransform* prefix_extractor, |
|
|
|
|
IndexReader** index_reader) { |
|
|
|
|
return Status::NotSupported("not implemented yet!"); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -367,7 +361,7 @@ Status BlockBasedTable::Open(const Options& options, const EnvOptions& soptions, |
|
|
|
|
// and with a same life-time as this table object.
|
|
|
|
|
IndexReader* index_reader = nullptr; |
|
|
|
|
// TODO: we never really verify check sum for index block
|
|
|
|
|
std::tie(s, index_reader) = new_table->CreateIndexReader(); |
|
|
|
|
s = new_table->CreateIndexReader(&index_reader); |
|
|
|
|
|
|
|
|
|
if (s.ok()) { |
|
|
|
|
rep->index_reader.reset(index_reader); |
|
|
|
@ -779,7 +773,7 @@ Iterator* BlockBasedTable::NewIndexIterator(const ReadOptions& read_options) |
|
|
|
|
} else { |
|
|
|
|
// Create index reader and put it in the cache.
|
|
|
|
|
Status s; |
|
|
|
|
std::tie(s, index_reader) = CreateIndexReader(); |
|
|
|
|
s = CreateIndexReader(&index_reader); |
|
|
|
|
|
|
|
|
|
if (!s.ok()) { |
|
|
|
|
// make sure if something goes wrong, index_reader shall remain intact.
|
|
|
|
@ -979,7 +973,7 @@ bool BlockBasedTable::TEST_KeyInCache(const ReadOptions& options, |
|
|
|
|
// 3. options
|
|
|
|
|
// 4. internal_comparator
|
|
|
|
|
// 5. index_type
|
|
|
|
|
std::pair<Status, IndexReader*> BlockBasedTable::CreateIndexReader() const { |
|
|
|
|
Status BlockBasedTable::CreateIndexReader(IndexReader** index_reader) const { |
|
|
|
|
// Some old version of block-based tables don't have index type present in
|
|
|
|
|
// table properties. If that's the case we can safely use the kBinarySearch.
|
|
|
|
|
auto index_type = BlockBasedTableOptions::kBinarySearch; |
|
|
|
@ -994,15 +988,14 @@ std::pair<Status, IndexReader*> BlockBasedTable::CreateIndexReader() const { |
|
|
|
|
case BlockBasedTableOptions::kBinarySearch: { |
|
|
|
|
return BinarySearchIndexReader::Create( |
|
|
|
|
rep_->file.get(), rep_->index_handle, rep_->options.env, |
|
|
|
|
&rep_->internal_comparator); |
|
|
|
|
&rep_->internal_comparator, index_reader); |
|
|
|
|
} |
|
|
|
|
default: { |
|
|
|
|
std::string error_message = |
|
|
|
|
"Unrecognized index type: " + std::to_string(rep_->index_type); |
|
|
|
|
// equivalent to assert(false), but more informative.
|
|
|
|
|
assert(!error_message.c_str()); |
|
|
|
|
return {Status::InvalidArgument(error_message.c_str()), |
|
|
|
|
nullptr}; // cannot reach here
|
|
|
|
|
return Status::InvalidArgument(error_message.c_str()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|