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 #pragma once
10 
11 #include "table/block_based/block_based_table_reader.h"
12 
13 #include "table/block_based/reader_common.h"
14 
15 namespace ROCKSDB_NAMESPACE {
16 // Encapsulates common functionality for the various index reader
17 // implementations. Provides access to the index block regardless of whether
18 // it is owned by the reader or stored in the cache, or whether it is pinned
19 // in the cache or not.
20 class BlockBasedTable::IndexReaderCommon : public BlockBasedTable::IndexReader {
21  public:
IndexReaderCommon(const BlockBasedTable * t,CachableEntry<Block> && index_block)22   IndexReaderCommon(const BlockBasedTable* t,
23                     CachableEntry<Block>&& index_block)
24       : table_(t), index_block_(std::move(index_block)) {
25     assert(table_ != nullptr);
26   }
27 
28  protected:
29   static Status ReadIndexBlock(const BlockBasedTable* table,
30                                FilePrefetchBuffer* prefetch_buffer,
31                                const ReadOptions& read_options, bool use_cache,
32                                GetContext* get_context,
33                                BlockCacheLookupContext* lookup_context,
34                                CachableEntry<Block>* index_block);
35 
table()36   const BlockBasedTable* table() const { return table_; }
37 
internal_comparator()38   const InternalKeyComparator* internal_comparator() const {
39     assert(table_ != nullptr);
40     assert(table_->get_rep() != nullptr);
41 
42     return &table_->get_rep()->internal_comparator;
43   }
44 
index_has_first_key()45   bool index_has_first_key() const {
46     assert(table_ != nullptr);
47     assert(table_->get_rep() != nullptr);
48     return table_->get_rep()->index_has_first_key;
49   }
50 
index_key_includes_seq()51   bool index_key_includes_seq() const {
52     assert(table_ != nullptr);
53     assert(table_->get_rep() != nullptr);
54     return table_->get_rep()->index_key_includes_seq;
55   }
56 
index_value_is_full()57   bool index_value_is_full() const {
58     assert(table_ != nullptr);
59     assert(table_->get_rep() != nullptr);
60     return table_->get_rep()->index_value_is_full;
61   }
62 
cache_index_blocks()63   bool cache_index_blocks() const {
64     assert(table_ != nullptr);
65     assert(table_->get_rep() != nullptr);
66     return table_->get_rep()->table_options.cache_index_and_filter_blocks;
67   }
68 
69   Status GetOrReadIndexBlock(bool no_io, GetContext* get_context,
70                              BlockCacheLookupContext* lookup_context,
71                              CachableEntry<Block>* index_block) const;
72 
ApproximateIndexBlockMemoryUsage()73   size_t ApproximateIndexBlockMemoryUsage() const {
74     assert(!index_block_.GetOwnValue() || index_block_.GetValue() != nullptr);
75     return index_block_.GetOwnValue()
76                ? index_block_.GetValue()->ApproximateMemoryUsage()
77                : 0;
78   }
79 
80  private:
81   const BlockBasedTable* table_;
82   CachableEntry<Block> index_block_;
83 };
84 
85 }  // namespace ROCKSDB_NAMESPACE
86