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/binary_search_index_reader.h"
10 
11 namespace ROCKSDB_NAMESPACE {
Create(const BlockBasedTable * table,const ReadOptions & ro,FilePrefetchBuffer * prefetch_buffer,bool use_cache,bool prefetch,bool pin,BlockCacheLookupContext * lookup_context,std::unique_ptr<IndexReader> * index_reader)12 Status BinarySearchIndexReader::Create(
13     const BlockBasedTable* table, const ReadOptions& ro,
14     FilePrefetchBuffer* prefetch_buffer, bool use_cache, bool prefetch,
15     bool pin, BlockCacheLookupContext* lookup_context,
16     std::unique_ptr<IndexReader>* index_reader) {
17   assert(table != nullptr);
18   assert(table->get_rep());
19   assert(!pin || prefetch);
20   assert(index_reader != nullptr);
21 
22   CachableEntry<Block> index_block;
23   if (prefetch || !use_cache) {
24     const Status s =
25         ReadIndexBlock(table, prefetch_buffer, ro, use_cache,
26                        /*get_context=*/nullptr, lookup_context, &index_block);
27     if (!s.ok()) {
28       return s;
29     }
30 
31     if (use_cache && !pin) {
32       index_block.Reset();
33     }
34   }
35 
36   index_reader->reset(
37       new BinarySearchIndexReader(table, std::move(index_block)));
38 
39   return Status::OK();
40 }
41 
NewIterator(const ReadOptions & read_options,bool,IndexBlockIter * iter,GetContext * get_context,BlockCacheLookupContext * lookup_context)42 InternalIteratorBase<IndexValue>* BinarySearchIndexReader::NewIterator(
43     const ReadOptions& read_options, bool /* disable_prefix_seek */,
44     IndexBlockIter* iter, GetContext* get_context,
45     BlockCacheLookupContext* lookup_context) {
46   const BlockBasedTable::Rep* rep = table()->get_rep();
47   const bool no_io = (read_options.read_tier == kBlockCacheTier);
48   CachableEntry<Block> index_block;
49   const Status s =
50       GetOrReadIndexBlock(no_io, get_context, lookup_context, &index_block);
51   if (!s.ok()) {
52     if (iter != nullptr) {
53       iter->Invalidate(s);
54       return iter;
55     }
56 
57     return NewErrorInternalIterator<IndexValue>(s);
58   }
59 
60   Statistics* kNullStats = nullptr;
61   // We don't return pinned data from index blocks, so no need
62   // to set `block_contents_pinned`.
63   auto it = index_block.GetValue()->NewIndexIterator(
64       internal_comparator()->user_comparator(),
65       rep->get_global_seqno(BlockType::kIndex), iter, kNullStats, true,
66       index_has_first_key(), index_key_includes_seq(), index_value_is_full());
67 
68   assert(it != nullptr);
69   index_block.TransferTo(it);
70 
71   return it;
72 }
73 }  // namespace ROCKSDB_NAMESPACE
74