Make the Create() function comform the convention

Summary:

Moved "Return multiple values" a more conventional way.
main
kailiu 11 years ago
parent 16d4e45c12
commit 1aeafeccac
  1. 49
      table/block_based_table_reader.cc
  2. 2
      table/block_based_table_reader.h

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

@ -164,7 +164,7 @@ class BlockBasedTable : public TableReader {
void ReadMeta(const Footer& footer); void ReadMeta(const Footer& footer);
void ReadFilter(const Slice& filter_handle_value); void ReadFilter(const Slice& filter_handle_value);
std::pair<Status, IndexReader*> CreateIndexReader() const; Status CreateIndexReader(IndexReader** index_reader) const;
// Read the meta block from sst. // Read the meta block from sst.
static Status ReadMetaBlock( static Status ReadMetaBlock(

Loading…
Cancel
Save