2013-11-27 23:27:02 +01: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.
|
|
|
|
|
|
|
|
#include "dynamic_bloom.h"
|
|
|
|
|
2014-03-28 17:21:20 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "port/port.h"
|
2013-11-27 23:27:02 +01:00
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "util/hash.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
static uint32_t BloomHash(const Slice& key) {
|
|
|
|
return Hash(key.data(), key.size(), 0xbc9f1d34);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-04 22:55:53 +02:00
|
|
|
DynamicBloom::DynamicBloom(uint32_t total_bits, uint32_t cl_per_block,
|
2014-03-28 17:21:20 +01:00
|
|
|
uint32_t num_probes,
|
2014-05-04 22:55:53 +02:00
|
|
|
uint32_t (*hash_func)(const Slice& key),
|
2014-05-05 00:52:23 +02:00
|
|
|
size_t huge_page_tlb_size, Logger* logger)
|
2014-05-04 22:55:53 +02:00
|
|
|
: kBlocked(cl_per_block > 0),
|
|
|
|
kBitsPerBlock(std::min(cl_per_block, num_probes) * CACHE_LINE_SIZE * 8),
|
|
|
|
kTotalBits((kBlocked ? (total_bits + kBitsPerBlock - 1) / kBitsPerBlock *
|
|
|
|
kBitsPerBlock
|
|
|
|
: total_bits + 7) /
|
|
|
|
8 * 8),
|
|
|
|
kNumBlocks(kBlocked ? kTotalBits / kBitsPerBlock : 1),
|
|
|
|
kNumProbes(num_probes),
|
|
|
|
hash_func_(hash_func == nullptr ? &BloomHash : hash_func) {
|
2014-03-28 17:21:20 +01:00
|
|
|
assert(kBlocked ? kTotalBits > 0 : kTotalBits >= kBitsPerBlock);
|
2014-01-15 06:30:13 +01:00
|
|
|
assert(kNumProbes > 0);
|
2013-11-27 23:27:02 +01:00
|
|
|
|
2014-03-28 17:21:20 +01:00
|
|
|
uint32_t sz = kTotalBits / 8;
|
|
|
|
if (kBlocked) {
|
|
|
|
sz += CACHE_LINE_SIZE - 1;
|
|
|
|
}
|
2014-05-04 22:55:53 +02:00
|
|
|
raw_ = reinterpret_cast<unsigned char*>(
|
2014-05-05 00:52:23 +02:00
|
|
|
arena_.AllocateAligned(sz, huge_page_tlb_size, logger));
|
2014-05-04 22:55:53 +02:00
|
|
|
memset(raw_, 0, sz);
|
2014-03-29 01:30:20 +01:00
|
|
|
if (kBlocked && (reinterpret_cast<uint64_t>(raw_) % CACHE_LINE_SIZE)) {
|
2014-03-28 17:21:20 +01:00
|
|
|
data_ = raw_ + CACHE_LINE_SIZE -
|
|
|
|
reinterpret_cast<uint64_t>(raw_) % CACHE_LINE_SIZE;
|
|
|
|
} else {
|
|
|
|
data_ = raw_;
|
|
|
|
}
|
2013-11-27 23:27:02 +01:00
|
|
|
}
|
|
|
|
|
2014-01-15 06:30:13 +01:00
|
|
|
} // rocksdb
|