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 
7 #include "table/block_based/filter_block_reader_common.h"
8 #include "monitoring/perf_context_imp.h"
9 #include "table/block_based/block_based_table_reader.h"
10 #include "table/block_based/parsed_full_filter_block.h"
11 
12 namespace ROCKSDB_NAMESPACE {
13 
14 template <typename TBlocklike>
ReadFilterBlock(const BlockBasedTable * table,FilePrefetchBuffer * prefetch_buffer,const ReadOptions & read_options,bool use_cache,GetContext * get_context,BlockCacheLookupContext * lookup_context,CachableEntry<TBlocklike> * filter_block)15 Status FilterBlockReaderCommon<TBlocklike>::ReadFilterBlock(
16     const BlockBasedTable* table, FilePrefetchBuffer* prefetch_buffer,
17     const ReadOptions& read_options, bool use_cache, GetContext* get_context,
18     BlockCacheLookupContext* lookup_context,
19     CachableEntry<TBlocklike>* filter_block) {
20   PERF_TIMER_GUARD(read_filter_block_nanos);
21 
22   assert(table);
23   assert(filter_block);
24   assert(filter_block->IsEmpty());
25 
26   const BlockBasedTable::Rep* const rep = table->get_rep();
27   assert(rep);
28 
29   const Status s =
30       table->RetrieveBlock(prefetch_buffer, read_options, rep->filter_handle,
31                            UncompressionDict::GetEmptyDict(), filter_block,
32                            BlockType::kFilter, get_context, lookup_context,
33                            /* for_compaction */ false, use_cache);
34 
35   return s;
36 }
37 
38 template <typename TBlocklike>
39 const SliceTransform*
table_prefix_extractor() const40 FilterBlockReaderCommon<TBlocklike>::table_prefix_extractor() const {
41   assert(table_);
42 
43   const BlockBasedTable::Rep* const rep = table_->get_rep();
44   assert(rep);
45 
46   return rep->prefix_filtering ? rep->table_prefix_extractor.get() : nullptr;
47 }
48 
49 template <typename TBlocklike>
whole_key_filtering() const50 bool FilterBlockReaderCommon<TBlocklike>::whole_key_filtering() const {
51   assert(table_);
52   assert(table_->get_rep());
53 
54   return table_->get_rep()->whole_key_filtering;
55 }
56 
57 template <typename TBlocklike>
cache_filter_blocks() const58 bool FilterBlockReaderCommon<TBlocklike>::cache_filter_blocks() const {
59   assert(table_);
60   assert(table_->get_rep());
61 
62   return table_->get_rep()->table_options.cache_index_and_filter_blocks;
63 }
64 
65 template <typename TBlocklike>
GetOrReadFilterBlock(bool no_io,GetContext * get_context,BlockCacheLookupContext * lookup_context,CachableEntry<TBlocklike> * filter_block) const66 Status FilterBlockReaderCommon<TBlocklike>::GetOrReadFilterBlock(
67     bool no_io, GetContext* get_context,
68     BlockCacheLookupContext* lookup_context,
69     CachableEntry<TBlocklike>* filter_block) const {
70   assert(filter_block);
71 
72   if (!filter_block_.IsEmpty()) {
73     filter_block->SetUnownedValue(filter_block_.GetValue());
74     return Status::OK();
75   }
76 
77   ReadOptions read_options;
78   if (no_io) {
79     read_options.read_tier = kBlockCacheTier;
80   }
81 
82   return ReadFilterBlock(table_, nullptr /* prefetch_buffer */, read_options,
83                          cache_filter_blocks(), get_context, lookup_context,
84                          filter_block);
85 }
86 
87 template <typename TBlocklike>
ApproximateFilterBlockMemoryUsage() const88 size_t FilterBlockReaderCommon<TBlocklike>::ApproximateFilterBlockMemoryUsage()
89     const {
90   assert(!filter_block_.GetOwnValue() || filter_block_.GetValue() != nullptr);
91   return filter_block_.GetOwnValue()
92              ? filter_block_.GetValue()->ApproximateMemoryUsage()
93              : 0;
94 }
95 
96 // Explicitly instantiate templates for both "blocklike" types we use.
97 // This makes it possible to keep the template definitions in the .cc file.
98 template class FilterBlockReaderCommon<BlockContents>;
99 template class FilterBlockReaderCommon<Block>;
100 template class FilterBlockReaderCommon<ParsedFullFilterBlock>;
101 
102 }  // namespace ROCKSDB_NAMESPACE
103