full_filter_block.h 5.5 KB
Newer Older
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).
5 6 7 8 9 10 11 12

#pragma once

#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <string>
#include <vector>
13

14
#include "db/dbformat.h"
15 16 17
#include "rocksdb/options.h"
#include "rocksdb/slice.h"
#include "rocksdb/slice_transform.h"
18
#include "table/block_based/filter_block_reader_common.h"
19
#include "table/block_based/parsed_full_filter_block.h"
20
#include "util/hash.h"
21

22
namespace ROCKSDB_NAMESPACE {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

class FilterPolicy;
class FilterBitsBuilder;
class FilterBitsReader;

// A FullFilterBlockBuilder is used to construct a full filter for a
// particular Table.  It generates a single string which is stored as
// a special block in the Table.
// The format of full filter block is:
// +----------------------------------------------------------------+
// |              full filter for all keys in sst file              |
// +----------------------------------------------------------------+
// The full filter can be very large. At the end of it, we put
// num_probes: how many hash functions are used in bloom filter
//
class FullFilterBlockBuilder : public FilterBlockBuilder {
 public:
  explicit FullFilterBlockBuilder(const SliceTransform* prefix_extractor,
41
                                  bool whole_key_filtering,
42
                                  FilterBitsBuilder* filter_bits_builder);
43 44 45 46
  // No copying allowed
  FullFilterBlockBuilder(const FullFilterBlockBuilder&) = delete;
  void operator=(const FullFilterBlockBuilder&) = delete;

47 48 49 50 51
  // bits_builder is created in filter_policy, it should be passed in here
  // directly. and be deleted here
  ~FullFilterBlockBuilder() {}

  virtual bool IsBlockBased() override { return false; }
A
Andrew Kryczka 已提交
52
  virtual void StartBlock(uint64_t /*block_offset*/) override {}
53
  virtual void Add(const Slice& key) override;
54
  virtual size_t NumAdded() const override { return num_added_; }
M
Maysam Yabandeh 已提交
55 56 57 58 59 60
  virtual Slice Finish(const BlockHandle& tmp, Status* status) override;
  using FilterBlockBuilder::Finish;

 protected:
  virtual void AddKey(const Slice& key);
  std::unique_ptr<FilterBitsBuilder> filter_bits_builder_;
61
  virtual void Reset();
62 63
  void AddPrefix(const Slice& key);
  const SliceTransform* prefix_extractor() { return prefix_extractor_; }
64 65 66 67 68 69 70

 private:
  // important: all of these might point to invalid addresses
  // at the time of destruction of this filter block. destructor
  // should NOT dereference them.
  const SliceTransform* prefix_extractor_;
  bool whole_key_filtering_;
71
  bool last_whole_key_recorded_;
72
  std::string last_whole_key_str_;
73
  bool last_prefix_recorded_;
74
  std::string last_prefix_str_;
75 76

  uint32_t num_added_;
I
Igor Canadi 已提交
77
  std::unique_ptr<const char[]> filter_data_;
78 79 80 81 82

};

// A FilterBlockReader is used to parse filter from SST table.
// KeyMayMatch and PrefixMayMatch would trigger filter checking
83 84
class FullFilterBlockReader
    : public FilterBlockReaderCommon<ParsedFullFilterBlock> {
85
 public:
86
  FullFilterBlockReader(const BlockBasedTable* t,
87
                        CachableEntry<ParsedFullFilterBlock>&& filter_block);
88 89 90 91 92

  static std::unique_ptr<FilterBlockReader> Create(
      const BlockBasedTable* table, FilePrefetchBuffer* prefetch_buffer,
      bool use_cache, bool prefetch, bool pin,
      BlockCacheLookupContext* lookup_context);
93 94 95 96 97

  bool IsBlockBased() override { return false; }

  bool KeyMayMatch(const Slice& key, const SliceTransform* prefix_extractor,
                   uint64_t block_offset, const bool no_io,
98 99
                   const Slice* const const_ikey_ptr, GetContext* get_context,
                   BlockCacheLookupContext* lookup_context) override;
100 101 102 103 104

  bool PrefixMayMatch(const Slice& prefix,
                      const SliceTransform* prefix_extractor,
                      uint64_t block_offset, const bool no_io,
                      const Slice* const const_ikey_ptr,
105 106
                      GetContext* get_context,
                      BlockCacheLookupContext* lookup_context) override;
107 108 109 110

  void KeysMayMatch(MultiGetRange* range,
                    const SliceTransform* prefix_extractor,
                    uint64_t block_offset, const bool no_io,
111
                    BlockCacheLookupContext* lookup_context) override;
112 113 114 115

  void PrefixesMayMatch(MultiGetRange* range,
                        const SliceTransform* prefix_extractor,
                        uint64_t block_offset, const bool no_io,
116
                        BlockCacheLookupContext* lookup_context) override;
117 118 119 120 121
  size_t ApproximateMemoryUsage() const override;
  bool RangeMayExist(const Slice* iterate_upper_bound, const Slice& user_key,
                     const SliceTransform* prefix_extractor,
                     const Comparator* comparator,
                     const Slice* const const_ikey_ptr, bool* filter_checked,
122
                     bool need_upper_bound_check, bool no_io,
123 124 125 126 127 128
                     BlockCacheLookupContext* lookup_context) override;

 private:
  bool MayMatch(const Slice& entry, bool no_io, GetContext* get_context,
                BlockCacheLookupContext* lookup_context) const;
  void MayMatch(MultiGetRange* range, bool no_io,
129
                const SliceTransform* prefix_extractor,
130 131 132
                BlockCacheLookupContext* lookup_context) const;
  bool IsFilterCompatible(const Slice* iterate_upper_bound, const Slice& prefix,
                          const Comparator* comparator) const;
133 134

 private:
135 136
  bool full_length_enabled_;
  size_t prefix_extractor_full_length_;
137 138
};

139
}  // namespace ROCKSDB_NAMESPACE