1 //  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 //
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
9 #include "table/block_based/index_reader_common.h"
10 
11 namespace ROCKSDB_NAMESPACE {
ReadIndexBlock(const BlockBasedTable * table,FilePrefetchBuffer * prefetch_buffer,const ReadOptions & read_options,bool use_cache,GetContext * get_context,BlockCacheLookupContext * lookup_context,CachableEntry<Block> * index_block)12 Status BlockBasedTable::IndexReaderCommon::ReadIndexBlock(
13     const BlockBasedTable* table, FilePrefetchBuffer* prefetch_buffer,
14     const ReadOptions& read_options, bool use_cache, GetContext* get_context,
15     BlockCacheLookupContext* lookup_context,
16     CachableEntry<Block>* index_block) {
17   PERF_TIMER_GUARD(read_index_block_nanos);
18 
19   assert(table != nullptr);
20   assert(index_block != nullptr);
21   assert(index_block->IsEmpty());
22 
23   const Rep* const rep = table->get_rep();
24   assert(rep != nullptr);
25 
26   const Status s = table->RetrieveBlock(
27       prefetch_buffer, read_options, rep->footer.index_handle(),
28       UncompressionDict::GetEmptyDict(), index_block, BlockType::kIndex,
29       get_context, lookup_context, /* for_compaction */ false, use_cache);
30 
31   return s;
32 }
33 
GetOrReadIndexBlock(bool no_io,GetContext * get_context,BlockCacheLookupContext * lookup_context,CachableEntry<Block> * index_block) const34 Status BlockBasedTable::IndexReaderCommon::GetOrReadIndexBlock(
35     bool no_io, GetContext* get_context,
36     BlockCacheLookupContext* lookup_context,
37     CachableEntry<Block>* index_block) const {
38   assert(index_block != nullptr);
39 
40   if (!index_block_.IsEmpty()) {
41     index_block->SetUnownedValue(index_block_.GetValue());
42     return Status::OK();
43   }
44 
45   ReadOptions read_options;
46   if (no_io) {
47     read_options.read_tier = kBlockCacheTier;
48   }
49 
50   return ReadIndexBlock(table_, /*prefetch_buffer=*/nullptr, read_options,
51                         cache_index_blocks(), get_context, lookup_context,
52                         index_block);
53 }
54 }  // namespace ROCKSDB_NAMESPACE
55