1 // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
5 // A filter block is stored near the end of a Table file. It contains
6 // filters (e.g., bloom filters) for all data blocks in the table combined
7 // into a single filter block.
9 #ifndef STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_
10 #define STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_
16 #include "leveldb/slice.h"
17 #include "util/hash.h"
23 // A FilterBlockBuilder is used to construct all of the filters for a
24 // particular Table. It generates a single string which is stored as
25 // a special block in the Table.
27 // The sequence of calls to FilterBlockBuilder must match the regexp:
28 // (StartBlock AddKey*)* Finish
29 class FilterBlockBuilder {
31 explicit FilterBlockBuilder(const FilterPolicy*);
33 void StartBlock(uint64_t block_offset);
34 void AddKey(const Slice& key);
38 void GenerateFilter();
40 const FilterPolicy* policy_;
41 std::string keys_; // Flattened key contents
42 std::vector<size_t> start_; // Starting index in keys_ of each key
43 std::string result_; // Filter data computed so far
44 std::vector<Slice> tmp_keys_; // policy_->CreateFilter() argument
45 std::vector<uint32_t> filter_offsets_;
48 FilterBlockBuilder(const FilterBlockBuilder&);
49 void operator=(const FilterBlockBuilder&);
52 class FilterBlockReader {
54 // REQUIRES: "contents" and *policy must stay live while *this is live.
55 FilterBlockReader(const FilterPolicy* policy, const Slice& contents);
56 bool KeyMayMatch(uint64_t block_offset, const Slice& key);
59 const FilterPolicy* policy_;
60 const char* data_; // Pointer to filter data (at block-start)
61 const char* offset_; // Pointer to beginning of offset array (at block-end)
62 size_t num_; // Number of entries in offset array
63 size_t base_lg_; // Encoding parameter (see kFilterBaseLg in .cc file)
68 #endif // STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_