2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
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.
|
|
|
|
|
2013-10-05 07:32:05 +02:00
|
|
|
#pragma once
|
2013-11-13 07:46:51 +01:00
|
|
|
|
|
|
|
#include <memory>
|
2012-04-17 17:36:46 +02:00
|
|
|
#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"
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2012-04-17 17:36:46 +02:00
|
|
|
|
|
|
|
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:
|
2014-01-27 22:53:22 +01:00
|
|
|
explicit FilterBlockBuilder(const Options& opt,
|
|
|
|
const Comparator* internal_comparator);
|
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();
|
|
|
|
|
2014-02-11 02:04:44 +01:00
|
|
|
// important: all of these might point to invalid addresses
|
|
|
|
// at the time of destruction of this filter block. destructor
|
|
|
|
// should NOT dereference them.
|
2012-04-17 17:36:46 +02:00
|
|
|
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-11-13 07:46:51 +01:00
|
|
|
FilterBlockReader(
|
|
|
|
const Options& opt,
|
|
|
|
const Slice& contents,
|
|
|
|
bool delete_contents_after_use = false);
|
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);
|
2014-08-05 20:27:34 +02:00
|
|
|
size_t ApproximateMemoryUsage() const;
|
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-11-13 07:46:51 +01:00
|
|
|
std::unique_ptr<const char[]> filter_data;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
}
|