partitioned_filter_block.h 6.2 KB
Newer Older
M
Maysam Yabandeh 已提交
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
S
Siying Dong 已提交
2 3 4
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).
M
Maysam Yabandeh 已提交
5 6 7 8 9

#pragma once

#include <list>
#include <string>
M
Maysam Yabandeh 已提交
10
#include <unordered_map>
M
Maysam Yabandeh 已提交
11
#include "db/dbformat.h"
12
#include "index_builder.h"
M
Maysam Yabandeh 已提交
13 14 15
#include "rocksdb/options.h"
#include "rocksdb/slice.h"
#include "rocksdb/slice_transform.h"
16
#include "table/block_based/block.h"
17
#include "table/block_based/filter_block_reader_common.h"
18
#include "table/block_based/full_filter_block.h"
M
Maysam Yabandeh 已提交
19
#include "util/autovector.h"
M
Maysam Yabandeh 已提交
20

21
namespace ROCKSDB_NAMESPACE {
M
Maysam Yabandeh 已提交
22

M
Maysam Yabandeh 已提交
23
class PartitionedFilterBlockBuilder : public FullFilterBlockBuilder {
M
Maysam Yabandeh 已提交
24
 public:
M
Maysam Yabandeh 已提交
25 26 27
  explicit PartitionedFilterBlockBuilder(
      const SliceTransform* prefix_extractor, bool whole_key_filtering,
      FilterBitsBuilder* filter_bits_builder, int index_block_restart_interval,
28
      const bool use_value_delta_encoding,
29 30
      PartitionedIndexBuilder* const p_index_builder,
      const uint32_t partition_size);
M
Maysam Yabandeh 已提交
31

M
Maysam Yabandeh 已提交
32
  virtual ~PartitionedFilterBlockBuilder();
M
Maysam Yabandeh 已提交
33

M
Maysam Yabandeh 已提交
34
  void AddKey(const Slice& key) override;
35
  void Add(const Slice& key) override;
M
Maysam Yabandeh 已提交
36

M
Maysam Yabandeh 已提交
37 38
  virtual Slice Finish(const BlockHandle& last_partition_block_handle,
                       Status* status) override;
M
Maysam Yabandeh 已提交
39 40

 private:
M
Maysam Yabandeh 已提交
41 42
  // Filter data
  BlockBuilder index_on_filter_block_builder_;  // top-level index builder
43 44
  BlockBuilder
      index_on_filter_block_builder_without_seq_;  // same for user keys
M
Maysam Yabandeh 已提交
45
  struct FilterEntry {
M
Maysam Yabandeh 已提交
46
    std::string key;
M
Maysam Yabandeh 已提交
47
    Slice filter;
M
Maysam Yabandeh 已提交
48
  };
M
Maysam Yabandeh 已提交
49 50 51 52
  std::list<FilterEntry> filters;  // list of partitioned indexes and their keys
  std::unique_ptr<IndexBuilder> value;
  std::vector<std::unique_ptr<const char[]>> filter_gc;
  bool finishing_filters =
M
Maysam Yabandeh 已提交
53
      false;  // true if Finish is called once but not complete yet.
M
Maysam Yabandeh 已提交
54
  // The policy of when cut a filter block and Finish it
55
  void MaybeCutAFilterBlock(const Slice* next_key);
56 57 58 59
  // Currently we keep the same number of partitions for filters and indexes.
  // This would allow for some potentioal optimizations in future. If such
  // optimizations did not realize we can use different number of partitions and
  // eliminate p_index_builder_
M
Maysam Yabandeh 已提交
60
  PartitionedIndexBuilder* const p_index_builder_;
61 62 63 64
  // The desired number of keys per partition
  uint32_t keys_per_partition_;
  // The number of keys added to the last partition so far
  uint32_t keys_added_to_partition_;
65
  BlockHandle last_encoded_handle_;
M
Maysam Yabandeh 已提交
66 67
};

68
class PartitionedFilterBlockReader : public FilterBlockReaderCommon<Block> {
M
Maysam Yabandeh 已提交
69
 public:
70 71 72 73
  PartitionedFilterBlockReader(const BlockBasedTable* t,
                               CachableEntry<Block>&& filter_block);

  static std::unique_ptr<FilterBlockReader> Create(
74 75 76
      const BlockBasedTable* table, const ReadOptions& ro,
      FilePrefetchBuffer* prefetch_buffer, bool use_cache, bool prefetch,
      bool pin, BlockCacheLookupContext* lookup_context);
M
Maysam Yabandeh 已提交
77

78 79 80
  bool IsBlockBased() override { return false; }
  bool KeyMayMatch(const Slice& key, const SliceTransform* prefix_extractor,
                   uint64_t block_offset, const bool no_io,
81 82
                   const Slice* const const_ikey_ptr, GetContext* get_context,
                   BlockCacheLookupContext* lookup_context) override;
83 84 85 86 87
  void KeysMayMatch(MultiGetRange* range,
                    const SliceTransform* prefix_extractor,
                    uint64_t block_offset, const bool no_io,
                    BlockCacheLookupContext* lookup_context) override;

88 89 90 91
  bool PrefixMayMatch(const Slice& prefix,
                      const SliceTransform* prefix_extractor,
                      uint64_t block_offset, const bool no_io,
                      const Slice* const const_ikey_ptr,
92 93
                      GetContext* get_context,
                      BlockCacheLookupContext* lookup_context) override;
94 95 96 97
  void PrefixesMayMatch(MultiGetRange* range,
                        const SliceTransform* prefix_extractor,
                        uint64_t block_offset, const bool no_io,
                        BlockCacheLookupContext* lookup_context) override;
98

99
  size_t ApproximateMemoryUsage() const override;
M
Maysam Yabandeh 已提交
100 101

 private:
102 103 104 105 106 107
  BlockHandle GetFilterPartitionHandle(const CachableEntry<Block>& filter_block,
                                       const Slice& entry) const;
  Status GetFilterPartitionBlock(
      FilePrefetchBuffer* prefetch_buffer, const BlockHandle& handle,
      bool no_io, GetContext* get_context,
      BlockCacheLookupContext* lookup_context,
108
      CachableEntry<ParsedFullFilterBlock>* filter_block) const;
109 110 111 112 113 114 115 116 117 118 119

  using FilterFunction = bool (FullFilterBlockReader::*)(
      const Slice& slice, const SliceTransform* prefix_extractor,
      uint64_t block_offset, const bool no_io,
      const Slice* const const_ikey_ptr, GetContext* get_context,
      BlockCacheLookupContext* lookup_context);
  bool MayMatch(const Slice& slice, const SliceTransform* prefix_extractor,
                uint64_t block_offset, bool no_io, const Slice* const_ikey_ptr,
                GetContext* get_context,
                BlockCacheLookupContext* lookup_context,
                FilterFunction filter_function) const;
120 121 122 123 124 125 126 127 128 129 130 131 132
  using FilterManyFunction = void (FullFilterBlockReader::*)(
      MultiGetRange* range, const SliceTransform* prefix_extractor,
      uint64_t block_offset, const bool no_io,
      BlockCacheLookupContext* lookup_context);
  void MayMatch(MultiGetRange* range, const SliceTransform* prefix_extractor,
                uint64_t block_offset, bool no_io,
                BlockCacheLookupContext* lookup_context,
                FilterManyFunction filter_function) const;
  void MayMatchPartition(MultiGetRange* range,
                         const SliceTransform* prefix_extractor,
                         uint64_t block_offset, BlockHandle filter_handle,
                         bool no_io, BlockCacheLookupContext* lookup_context,
                         FilterManyFunction filter_function) const;
133
  void CacheDependencies(const ReadOptions& ro, bool pin) override;
134 135 136 137 138 139

  const InternalKeyComparator* internal_comparator() const;
  bool index_key_includes_seq() const;
  bool index_value_is_full() const;

 protected:
140 141
  std::unordered_map<uint64_t, CachableEntry<ParsedFullFilterBlock>>
      filter_map_;
M
Maysam Yabandeh 已提交
142 143
};

144
}  // namespace ROCKSDB_NAMESPACE