Skip to content

Commit 1aeafec

Browse files
committed
Make the Create() function comform the convention
Summary: Moved "Return multiple values" a more conventional way.
1 parent 16d4e45 commit 1aeafec

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

table/block_based_table_reader.cc

+21-28
Original file line numberDiff line numberDiff line change
@@ -148,26 +148,20 @@ class BinarySearchIndexReader : public IndexReader {
148148
public:
149149
// Read index from the file and create an intance for
150150
// `BinarySearchIndexReader`.
151-
// The return value is a pair, where
152-
// * first element is the status indicating if the operation succeeded.
153-
// * second element is the index reader to be created. On failure, this
154-
// element will be nullptr
155-
static std::pair<Status, IndexReader*> Create(RandomAccessFile* file,
156-
const BlockHandle& index_handle,
157-
Env* env,
158-
const Comparator* comparator) {
151+
// On success, index_reader will be populated; otherwise it will remain
152+
// unmodified.
153+
static Status Create(RandomAccessFile* file, const BlockHandle& index_handle,
154+
Env* env, const Comparator* comparator,
155+
IndexReader** index_reader) {
159156
Block* index_block = nullptr;
160-
auto s =
161-
ReadBlockFromFile(file, ReadOptions(), index_handle, &index_block, env);
157+
auto s = ReadBlockFromFile(file, ReadOptions(), index_handle,
158+
&index_block, env);
162159

163-
if (!s.ok()) {
164-
// Logically, index_block shouldn't have been populated if any error
165-
// occurred.
166-
assert(index_block == nullptr);
167-
return {s, nullptr};
160+
if (s.ok()) {
161+
*index_reader = new BinarySearchIndexReader(comparator, index_block);
168162
}
169163

170-
return {s, new BinarySearchIndexReader(comparator, index_block)};
164+
return s;
171165
}
172166

173167
virtual Iterator* NewIterator() override {
@@ -190,12 +184,12 @@ class BinarySearchIndexReader : public IndexReader {
190184
// key.
191185
class HashIndexReader : public IndexReader {
192186
public:
193-
static std::pair<Status, IndexReader*> Create(
194-
RandomAccessFile* file, const BlockHandle& index_handle, Env* env,
195-
const Comparator* comparator, BlockBasedTable* table,
196-
const SliceTransform* prefix_extractor) {
197-
return {Status::NotSupported("not implemented yet!"),
198-
nullptr}; // not finished
187+
static Status Create(RandomAccessFile* file, const BlockHandle& index_handle,
188+
Env* env, const Comparator* comparator,
189+
BlockBasedTable* table,
190+
const SliceTransform* prefix_extractor,
191+
IndexReader** index_reader) {
192+
return Status::NotSupported("not implemented yet!");
199193
}
200194
};
201195

@@ -367,7 +361,7 @@ Status BlockBasedTable::Open(const Options& options, const EnvOptions& soptions,
367361
// and with a same life-time as this table object.
368362
IndexReader* index_reader = nullptr;
369363
// TODO: we never really verify check sum for index block
370-
std::tie(s, index_reader) = new_table->CreateIndexReader();
364+
s = new_table->CreateIndexReader(&index_reader);
371365

372366
if (s.ok()) {
373367
rep->index_reader.reset(index_reader);
@@ -779,7 +773,7 @@ Iterator* BlockBasedTable::NewIndexIterator(const ReadOptions& read_options)
779773
} else {
780774
// Create index reader and put it in the cache.
781775
Status s;
782-
std::tie(s, index_reader) = CreateIndexReader();
776+
s = CreateIndexReader(&index_reader);
783777

784778
if (!s.ok()) {
785779
// make sure if something goes wrong, index_reader shall remain intact.
@@ -979,7 +973,7 @@ bool BlockBasedTable::TEST_KeyInCache(const ReadOptions& options,
979973
// 3. options
980974
// 4. internal_comparator
981975
// 5. index_type
982-
std::pair<Status, IndexReader*> BlockBasedTable::CreateIndexReader() const {
976+
Status BlockBasedTable::CreateIndexReader(IndexReader** index_reader) const {
983977
// Some old version of block-based tables don't have index type present in
984978
// table properties. If that's the case we can safely use the kBinarySearch.
985979
auto index_type = BlockBasedTableOptions::kBinarySearch;
@@ -994,15 +988,14 @@ std::pair<Status, IndexReader*> BlockBasedTable::CreateIndexReader() const {
994988
case BlockBasedTableOptions::kBinarySearch: {
995989
return BinarySearchIndexReader::Create(
996990
rep_->file.get(), rep_->index_handle, rep_->options.env,
997-
&rep_->internal_comparator);
991+
&rep_->internal_comparator, index_reader);
998992
}
999993
default: {
1000994
std::string error_message =
1001995
"Unrecognized index type: " + std::to_string(rep_->index_type);
1002996
// equivalent to assert(false), but more informative.
1003997
assert(!error_message.c_str());
1004-
return {Status::InvalidArgument(error_message.c_str()),
1005-
nullptr}; // cannot reach here
998+
return Status::InvalidArgument(error_message.c_str());
1006999
}
10071000
}
10081001
}

table/block_based_table_reader.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class BlockBasedTable : public TableReader {
164164

165165
void ReadMeta(const Footer& footer);
166166
void ReadFilter(const Slice& filter_handle_value);
167-
std::pair<Status, IndexReader*> CreateIndexReader() const;
167+
Status CreateIndexReader(IndexReader** index_reader) const;
168168

169169
// Read the meta block from sst.
170170
static Status ReadMetaBlock(

0 commit comments

Comments
 (0)