2012-04-17 17:36:46 +02:00
|
|
|
// Copyright (c) 2012 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
//
|
|
|
|
// A filter block is stored near the end of a Table file. It contains
|
|
|
|
// filters (e.g., bloom filters) for all data blocks in the table combined
|
|
|
|
// into a single filter block.
|
|
|
|
|
|
|
|
#ifndef STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_
|
|
|
|
#define STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "rocksdb/slice_transform.h"
|
2012-04-17 17:36:46 +02:00
|
|
|
#include "util/hash.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
class FilterPolicy;
|
|
|
|
|
|
|
|
// A FilterBlockBuilder is used to construct all of the filters for a
|
|
|
|
// particular Table. It generates a single string which is stored as
|
|
|
|
// a special block in the Table.
|
|
|
|
//
|
|
|
|
// The sequence of calls to FilterBlockBuilder must match the regexp:
|
|
|
|
// (StartBlock AddKey*)* Finish
|
|
|
|
class FilterBlockBuilder {
|
|
|
|
public:
|
2013-08-13 23:04:56 +02:00
|
|
|
explicit FilterBlockBuilder(const Options& opt);
|
2012-04-17 17:36:46 +02:00
|
|
|
|
|
|
|
void StartBlock(uint64_t block_offset);
|
|
|
|
void AddKey(const Slice& key);
|
|
|
|
Slice Finish();
|
|
|
|
|
|
|
|
private:
|
2013-08-13 23:04:56 +02:00
|
|
|
bool SamePrefix(const Slice &key1, const Slice &key2) const;
|
2012-04-17 17:36:46 +02:00
|
|
|
void GenerateFilter();
|
|
|
|
|
|
|
|
const FilterPolicy* policy_;
|
2013-08-13 23:04:56 +02:00
|
|
|
const SliceTransform* prefix_extractor_;
|
|
|
|
bool whole_key_filtering_;
|
|
|
|
const Comparator* comparator_;
|
|
|
|
|
|
|
|
std::string entries_; // Flattened entry contents
|
|
|
|
std::vector<size_t> start_; // Starting index in entries_ of each entry
|
|
|
|
std::string result_; // Filter data computed so far
|
|
|
|
std::vector<Slice> tmp_entries_; // policy_->CreateFilter() argument
|
2012-04-17 17:36:46 +02:00
|
|
|
std::vector<uint32_t> filter_offsets_;
|
|
|
|
|
|
|
|
// No copying allowed
|
|
|
|
FilterBlockBuilder(const FilterBlockBuilder&);
|
|
|
|
void operator=(const FilterBlockBuilder&);
|
|
|
|
};
|
|
|
|
|
|
|
|
class FilterBlockReader {
|
|
|
|
public:
|
|
|
|
// REQUIRES: "contents" and *policy must stay live while *this is live.
|
2013-08-13 23:04:56 +02:00
|
|
|
FilterBlockReader(const Options& opt, const Slice& contents);
|
2012-04-17 17:36:46 +02:00
|
|
|
bool KeyMayMatch(uint64_t block_offset, const Slice& key);
|
2013-08-13 23:04:56 +02:00
|
|
|
bool PrefixMayMatch(uint64_t block_offset, const Slice& prefix);
|
2012-04-17 17:36:46 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
const FilterPolicy* policy_;
|
2013-08-13 23:04:56 +02:00
|
|
|
const SliceTransform* prefix_extractor_;
|
|
|
|
bool whole_key_filtering_;
|
2012-04-17 17:36:46 +02:00
|
|
|
const char* data_; // Pointer to filter data (at block-start)
|
|
|
|
const char* offset_; // Pointer to beginning of offset array (at block-end)
|
|
|
|
size_t num_; // Number of entries in offset array
|
|
|
|
size_t base_lg_; // Encoding parameter (see kFilterBaseLg in .cc file)
|
2013-08-13 23:04:56 +02:00
|
|
|
|
|
|
|
bool MayMatch(uint64_t block_offset, const Slice& entry);
|
2012-04-17 17:36:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_
|