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.
|
|
|
|
|
2014-12-23 22:24:07 +01:00
|
|
|
#include <algorithm>
|
2014-09-08 19:37:05 +02:00
|
|
|
#include "table/block_based_filter_block.h"
|
2012-04-17 17:36:46 +02:00
|
|
|
|
2013-08-13 23:04:56 +02:00
|
|
|
#include "db/dbformat.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/filter_policy.h"
|
2012-04-17 17:36:46 +02:00
|
|
|
#include "util/coding.h"
|
2015-04-24 04:17:57 +02:00
|
|
|
#include "util/string_util.h"
|
2012-04-17 17:36:46 +02:00
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2012-04-17 17:36:46 +02:00
|
|
|
|
2014-09-08 19:37:05 +02:00
|
|
|
namespace {
|
|
|
|
bool SamePrefix(const SliceTransform* prefix_extractor,
|
|
|
|
const Slice& key1, const Slice& key2) {
|
|
|
|
if (!prefix_extractor->InDomain(key1) &&
|
|
|
|
!prefix_extractor->InDomain(key2)) {
|
|
|
|
return true;
|
|
|
|
} else if (!prefix_extractor->InDomain(key1) ||
|
|
|
|
!prefix_extractor->InDomain(key2)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return (prefix_extractor->Transform(key1) ==
|
|
|
|
prefix_extractor->Transform(key2));
|
|
|
|
}
|
|
|
|
}
|
2014-12-23 22:24:07 +01:00
|
|
|
|
|
|
|
void AppendItem(std::string* props, const std::string& key,
|
|
|
|
const std::string& value) {
|
|
|
|
char cspace = ' ';
|
|
|
|
std::string value_str("");
|
|
|
|
size_t i = 0;
|
|
|
|
const size_t dataLength = 64;
|
|
|
|
const size_t tabLength = 2;
|
|
|
|
const size_t offLength = 16;
|
|
|
|
|
|
|
|
value_str.append(&value[i], std::min(size_t(dataLength), value.size()));
|
|
|
|
i += dataLength;
|
|
|
|
while (i < value.size()) {
|
|
|
|
value_str.append("\n");
|
|
|
|
value_str.append(offLength, cspace);
|
|
|
|
value_str.append(&value[i], std::min(size_t(dataLength), value.size() - i));
|
|
|
|
i += dataLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string result("");
|
|
|
|
if (key.size() < (offLength - tabLength))
|
|
|
|
result.append(size_t((offLength - tabLength)) - key.size(), cspace);
|
|
|
|
result.append(key);
|
|
|
|
|
|
|
|
props->append(result + ": " + value_str + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class TKey>
|
|
|
|
void AppendItem(std::string* props, const TKey& key, const std::string& value) {
|
2015-04-24 04:17:57 +02:00
|
|
|
std::string key_str = rocksdb::ToString(key);
|
2014-12-23 22:24:07 +01:00
|
|
|
AppendItem(props, key_str, value);
|
|
|
|
}
|
2014-09-08 19:37:05 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2012-04-17 17:36:46 +02:00
|
|
|
// See doc/table_format.txt for an explanation of the filter block format.
|
|
|
|
|
|
|
|
// Generate new filter every 2KB of data
|
|
|
|
static const size_t kFilterBaseLg = 11;
|
|
|
|
static const size_t kFilterBase = 1 << kFilterBaseLg;
|
|
|
|
|
2014-09-08 19:37:05 +02:00
|
|
|
BlockBasedFilterBlockBuilder::BlockBasedFilterBlockBuilder(
|
|
|
|
const SliceTransform* prefix_extractor,
|
|
|
|
const BlockBasedTableOptions& table_opt)
|
2014-08-25 23:22:05 +02:00
|
|
|
: policy_(table_opt.filter_policy.get()),
|
2014-09-05 01:18:36 +02:00
|
|
|
prefix_extractor_(prefix_extractor),
|
2014-09-08 19:37:05 +02:00
|
|
|
whole_key_filtering_(table_opt.whole_key_filtering) {
|
|
|
|
assert(policy_);
|
|
|
|
}
|
2012-04-17 17:36:46 +02:00
|
|
|
|
2014-09-08 19:37:05 +02:00
|
|
|
void BlockBasedFilterBlockBuilder::StartBlock(uint64_t block_offset) {
|
2012-04-17 17:36:46 +02:00
|
|
|
uint64_t filter_index = (block_offset / kFilterBase);
|
|
|
|
assert(filter_index >= filter_offsets_.size());
|
|
|
|
while (filter_index > filter_offsets_.size()) {
|
|
|
|
GenerateFilter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 19:37:05 +02:00
|
|
|
void BlockBasedFilterBlockBuilder::Add(const Slice& key) {
|
|
|
|
added_to_start_ = 0;
|
|
|
|
if (whole_key_filtering_) {
|
|
|
|
AddKey(key);
|
|
|
|
added_to_start_ = 1;
|
|
|
|
}
|
|
|
|
if (prefix_extractor_ && prefix_extractor_->InDomain(key)) {
|
|
|
|
AddPrefix(key);
|
2013-08-13 23:04:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 19:37:05 +02:00
|
|
|
// Add key to filter if needed
|
|
|
|
inline void BlockBasedFilterBlockBuilder::AddKey(const Slice& key) {
|
|
|
|
start_.push_back(entries_.size());
|
|
|
|
entries_.append(key.data(), key.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add prefix to filter if needed
|
|
|
|
inline void BlockBasedFilterBlockBuilder::AddPrefix(const Slice& key) {
|
2013-08-23 23:49:57 +02:00
|
|
|
// get slice for most recently added entry
|
2013-08-13 23:04:56 +02:00
|
|
|
Slice prev;
|
2014-09-08 19:37:05 +02:00
|
|
|
if (start_.size() > added_to_start_) {
|
|
|
|
size_t prev_start = start_[start_.size() - 1 - added_to_start_];
|
Fix two nasty use-after-free-bugs
Summary:
These bugs were caught by ASAN crash test.
1. The first one, in table/filter_block.cc is very nasty. We first reference entries_ and store the reference to Slice prev. Then, we call entries_.append(), which can change the reference. The Slice prev now points to junk.
2. The second one is a bug in a test, so it's not very serious. Once we set read_opts.prefix, we never clear it, so some other function might still reference it.
Test Plan: asan crash test now runs more than 5 mins. Before, it failed immediately. I will run the full one, but the full one takes quite some time (5 hours)
Reviewers: dhruba, haobo, kailiu
Reviewed By: dhruba
CC: leveldb
Differential Revision: https://reviews.facebook.net/D14223
2013-11-20 06:01:48 +01:00
|
|
|
const char* base = entries_.data() + prev_start;
|
|
|
|
size_t length = entries_.size() - prev_start;
|
|
|
|
prev = Slice(base, length);
|
|
|
|
}
|
|
|
|
|
2014-09-08 19:37:05 +02:00
|
|
|
// this assumes prefix(prefix(key)) == prefix(key), as the last
|
|
|
|
// entry in entries_ may be either a key or prefix, and we use
|
|
|
|
// prefix(last entry) to get the prefix of the last key.
|
|
|
|
if (prev.size() == 0 || !SamePrefix(prefix_extractor_, key, prev)) {
|
|
|
|
Slice prefix = prefix_extractor_->Transform(key);
|
|
|
|
start_.push_back(entries_.size());
|
|
|
|
entries_.append(prefix.data(), prefix.size());
|
2013-08-13 23:04:56 +02:00
|
|
|
}
|
2012-04-17 17:36:46 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 19:37:05 +02:00
|
|
|
Slice BlockBasedFilterBlockBuilder::Finish() {
|
2012-04-17 17:36:46 +02:00
|
|
|
if (!start_.empty()) {
|
|
|
|
GenerateFilter();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append array of per-filter offsets
|
2014-11-11 22:47:22 +01:00
|
|
|
const uint32_t array_offset = static_cast<uint32_t>(result_.size());
|
2012-04-17 17:36:46 +02:00
|
|
|
for (size_t i = 0; i < filter_offsets_.size(); i++) {
|
|
|
|
PutFixed32(&result_, filter_offsets_[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
PutFixed32(&result_, array_offset);
|
|
|
|
result_.push_back(kFilterBaseLg); // Save encoding parameter in result
|
|
|
|
return Slice(result_);
|
|
|
|
}
|
|
|
|
|
2014-09-08 19:37:05 +02:00
|
|
|
void BlockBasedFilterBlockBuilder::GenerateFilter() {
|
2013-08-13 23:04:56 +02:00
|
|
|
const size_t num_entries = start_.size();
|
|
|
|
if (num_entries == 0) {
|
2012-04-17 17:36:46 +02:00
|
|
|
// Fast path if there are no keys for this filter
|
2014-11-11 22:47:22 +01:00
|
|
|
filter_offsets_.push_back(static_cast<uint32_t>(result_.size()));
|
2012-04-17 17:36:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make list of keys from flattened key structure
|
2013-08-13 23:04:56 +02:00
|
|
|
start_.push_back(entries_.size()); // Simplify length computation
|
|
|
|
tmp_entries_.resize(num_entries);
|
|
|
|
for (size_t i = 0; i < num_entries; i++) {
|
|
|
|
const char* base = entries_.data() + start_[i];
|
2014-09-08 19:37:05 +02:00
|
|
|
size_t length = start_[i + 1] - start_[i];
|
2013-08-13 23:04:56 +02:00
|
|
|
tmp_entries_[i] = Slice(base, length);
|
2012-04-17 17:36:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate filter for current set of keys and append to result_.
|
2014-11-11 22:47:22 +01:00
|
|
|
filter_offsets_.push_back(static_cast<uint32_t>(result_.size()));
|
|
|
|
policy_->CreateFilter(&tmp_entries_[0], static_cast<int>(num_entries),
|
|
|
|
&result_);
|
2012-04-17 17:36:46 +02:00
|
|
|
|
2013-08-13 23:04:56 +02:00
|
|
|
tmp_entries_.clear();
|
|
|
|
entries_.clear();
|
2012-04-17 17:36:46 +02:00
|
|
|
start_.clear();
|
|
|
|
}
|
|
|
|
|
2014-09-08 19:37:05 +02:00
|
|
|
BlockBasedFilterBlockReader::BlockBasedFilterBlockReader(
|
2014-09-05 01:18:36 +02:00
|
|
|
const SliceTransform* prefix_extractor,
|
2015-02-05 02:03:57 +01:00
|
|
|
const BlockBasedTableOptions& table_opt, bool whole_key_filtering,
|
|
|
|
BlockContents&& contents)
|
2014-08-25 23:22:05 +02:00
|
|
|
: policy_(table_opt.filter_policy.get()),
|
2014-09-05 01:18:36 +02:00
|
|
|
prefix_extractor_(prefix_extractor),
|
2015-02-05 02:03:57 +01:00
|
|
|
whole_key_filtering_(whole_key_filtering),
|
2013-03-01 03:04:58 +01:00
|
|
|
data_(nullptr),
|
|
|
|
offset_(nullptr),
|
2012-04-17 17:36:46 +02:00
|
|
|
num_(0),
|
2014-09-18 01:45:58 +02:00
|
|
|
base_lg_(0),
|
|
|
|
contents_(std::move(contents)) {
|
2014-09-08 19:37:05 +02:00
|
|
|
assert(policy_);
|
2014-09-18 01:45:58 +02:00
|
|
|
size_t n = contents_.data.size();
|
2012-04-17 17:36:46 +02:00
|
|
|
if (n < 5) return; // 1 byte for base_lg_ and 4 for start of offset array
|
2014-09-18 01:45:58 +02:00
|
|
|
base_lg_ = contents_.data[n - 1];
|
|
|
|
uint32_t last_word = DecodeFixed32(contents_.data.data() + n - 5);
|
2012-04-17 17:36:46 +02:00
|
|
|
if (last_word > n - 5) return;
|
2014-09-18 01:45:58 +02:00
|
|
|
data_ = contents_.data.data();
|
2012-04-17 17:36:46 +02:00
|
|
|
offset_ = data_ + last_word;
|
|
|
|
num_ = (n - 5 - last_word) / 4;
|
2014-08-16 00:05:09 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 19:37:05 +02:00
|
|
|
bool BlockBasedFilterBlockReader::KeyMayMatch(const Slice& key,
|
|
|
|
uint64_t block_offset) {
|
|
|
|
assert(block_offset != kNotValid);
|
2013-08-13 23:04:56 +02:00
|
|
|
if (!whole_key_filtering_) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-08 19:37:05 +02:00
|
|
|
return MayMatch(key, block_offset);
|
2013-08-13 23:04:56 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 19:37:05 +02:00
|
|
|
bool BlockBasedFilterBlockReader::PrefixMayMatch(const Slice& prefix,
|
|
|
|
uint64_t block_offset) {
|
|
|
|
assert(block_offset != kNotValid);
|
2013-08-13 23:04:56 +02:00
|
|
|
if (!prefix_extractor_) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-08 19:37:05 +02:00
|
|
|
return MayMatch(prefix, block_offset);
|
2013-08-13 23:04:56 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 19:37:05 +02:00
|
|
|
bool BlockBasedFilterBlockReader::MayMatch(const Slice& entry,
|
|
|
|
uint64_t block_offset) {
|
2012-04-17 17:36:46 +02:00
|
|
|
uint64_t index = block_offset >> base_lg_;
|
|
|
|
if (index < num_) {
|
2014-09-08 19:37:05 +02:00
|
|
|
uint32_t start = DecodeFixed32(offset_ + index * 4);
|
|
|
|
uint32_t limit = DecodeFixed32(offset_ + index * 4 + 4);
|
2014-02-03 22:48:30 +01:00
|
|
|
if (start <= limit && limit <= (uint32_t)(offset_ - data_)) {
|
2012-04-17 17:36:46 +02:00
|
|
|
Slice filter = Slice(data_ + start, limit - start);
|
2013-08-13 23:04:56 +02:00
|
|
|
return policy_->KeyMayMatch(entry, filter);
|
2012-04-17 17:36:46 +02:00
|
|
|
} else if (start == limit) {
|
2013-08-13 23:04:56 +02:00
|
|
|
// Empty filters do not match any entries
|
2012-04-17 17:36:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true; // Errors are treated as potential matches
|
|
|
|
}
|
|
|
|
|
2014-09-08 19:37:05 +02:00
|
|
|
size_t BlockBasedFilterBlockReader::ApproximateMemoryUsage() const {
|
2014-08-05 20:27:34 +02:00
|
|
|
return num_ * 4 + 5 + (offset_ - data_);
|
|
|
|
}
|
2014-12-23 22:24:07 +01:00
|
|
|
|
|
|
|
std::string BlockBasedFilterBlockReader::ToString() const {
|
|
|
|
std::string result, filter_meta;
|
|
|
|
result.reserve(1024);
|
|
|
|
|
|
|
|
std::string s_bo("Block offset"), s_hd("Hex dump"), s_fb("# filter blocks");
|
2015-04-24 04:17:57 +02:00
|
|
|
AppendItem(&result, s_fb, rocksdb::ToString(num_));
|
2014-12-23 22:24:07 +01:00
|
|
|
AppendItem(&result, s_bo, s_hd);
|
|
|
|
|
|
|
|
for (size_t index = 0; index < num_; index++) {
|
|
|
|
uint32_t start = DecodeFixed32(offset_ + index * 4);
|
|
|
|
uint32_t limit = DecodeFixed32(offset_ + index * 4 + 4);
|
|
|
|
|
|
|
|
if (start != limit) {
|
2015-04-24 04:17:57 +02:00
|
|
|
result.append(" filter block # " + rocksdb::ToString(index + 1) + "\n");
|
2014-12-23 22:24:07 +01:00
|
|
|
Slice filter = Slice(data_ + start, limit - start);
|
|
|
|
AppendItem(&result, start, filter.ToString(true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2012-04-17 17:36:46 +02:00
|
|
|
}
|
2014-12-23 22:24:07 +01:00
|
|
|
} // namespace rocksdb
|