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.
|
|
|
|
|
|
|
|
#include "table/filter_block.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"
|
|
|
|
#include "util/hash.h"
|
|
|
|
#include "util/logging.h"
|
|
|
|
#include "util/testharness.h"
|
|
|
|
#include "util/testutil.h"
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2012-04-17 17:36:46 +02:00
|
|
|
|
|
|
|
// For testing: emit an array with one hash value per key
|
|
|
|
class TestHashFilter : public FilterPolicy {
|
|
|
|
public:
|
|
|
|
virtual const char* Name() const {
|
|
|
|
return "TestHashFilter";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void CreateFilter(const Slice* keys, int n, std::string* dst) const {
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
uint32_t h = Hash(keys[i].data(), keys[i].size(), 1);
|
|
|
|
PutFixed32(dst, h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool KeyMayMatch(const Slice& key, const Slice& filter) const {
|
|
|
|
uint32_t h = Hash(key.data(), key.size(), 1);
|
2012-11-06 21:02:18 +01:00
|
|
|
for (unsigned int i = 0; i + 4 <= filter.size(); i += 4) {
|
2012-04-17 17:36:46 +02:00
|
|
|
if (h == DecodeFixed32(filter.data() + i)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class FilterBlockTest {
|
|
|
|
public:
|
|
|
|
TestHashFilter policy_;
|
2013-08-13 23:04:56 +02:00
|
|
|
Options options_;
|
|
|
|
|
|
|
|
FilterBlockTest() {
|
|
|
|
options_ = Options();
|
|
|
|
options_.filter_policy = &policy_;
|
|
|
|
}
|
2012-04-17 17:36:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(FilterBlockTest, EmptyBuilder) {
|
2013-08-13 23:04:56 +02:00
|
|
|
FilterBlockBuilder builder(options_);
|
2012-04-17 17:36:46 +02:00
|
|
|
Slice block = builder.Finish();
|
|
|
|
ASSERT_EQ("\\x00\\x00\\x00\\x00\\x0b", EscapeString(block));
|
2013-08-13 23:04:56 +02:00
|
|
|
FilterBlockReader reader(options_, block);
|
2012-04-17 17:36:46 +02:00
|
|
|
ASSERT_TRUE(reader.KeyMayMatch(0, "foo"));
|
|
|
|
ASSERT_TRUE(reader.KeyMayMatch(100000, "foo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FilterBlockTest, SingleChunk) {
|
2013-08-13 23:04:56 +02:00
|
|
|
FilterBlockBuilder builder(options_);
|
2012-04-17 17:36:46 +02:00
|
|
|
builder.StartBlock(100);
|
|
|
|
builder.AddKey("foo");
|
|
|
|
builder.AddKey("bar");
|
|
|
|
builder.AddKey("box");
|
|
|
|
builder.StartBlock(200);
|
|
|
|
builder.AddKey("box");
|
|
|
|
builder.StartBlock(300);
|
|
|
|
builder.AddKey("hello");
|
|
|
|
Slice block = builder.Finish();
|
2013-08-13 23:04:56 +02:00
|
|
|
FilterBlockReader reader(options_, block);
|
2012-04-17 17:36:46 +02:00
|
|
|
ASSERT_TRUE(reader.KeyMayMatch(100, "foo"));
|
|
|
|
ASSERT_TRUE(reader.KeyMayMatch(100, "bar"));
|
|
|
|
ASSERT_TRUE(reader.KeyMayMatch(100, "box"));
|
|
|
|
ASSERT_TRUE(reader.KeyMayMatch(100, "hello"));
|
|
|
|
ASSERT_TRUE(reader.KeyMayMatch(100, "foo"));
|
|
|
|
ASSERT_TRUE(! reader.KeyMayMatch(100, "missing"));
|
|
|
|
ASSERT_TRUE(! reader.KeyMayMatch(100, "other"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FilterBlockTest, MultiChunk) {
|
2013-08-13 23:04:56 +02:00
|
|
|
FilterBlockBuilder builder(options_);
|
2012-04-17 17:36:46 +02:00
|
|
|
|
|
|
|
// First filter
|
|
|
|
builder.StartBlock(0);
|
|
|
|
builder.AddKey("foo");
|
|
|
|
builder.StartBlock(2000);
|
|
|
|
builder.AddKey("bar");
|
|
|
|
|
|
|
|
// Second filter
|
|
|
|
builder.StartBlock(3100);
|
|
|
|
builder.AddKey("box");
|
|
|
|
|
|
|
|
// Third filter is empty
|
|
|
|
|
|
|
|
// Last filter
|
|
|
|
builder.StartBlock(9000);
|
|
|
|
builder.AddKey("box");
|
|
|
|
builder.AddKey("hello");
|
|
|
|
|
|
|
|
Slice block = builder.Finish();
|
2013-08-13 23:04:56 +02:00
|
|
|
FilterBlockReader reader(options_, block);
|
2012-04-17 17:36:46 +02:00
|
|
|
|
|
|
|
// Check first filter
|
|
|
|
ASSERT_TRUE(reader.KeyMayMatch(0, "foo"));
|
|
|
|
ASSERT_TRUE(reader.KeyMayMatch(2000, "bar"));
|
|
|
|
ASSERT_TRUE(! reader.KeyMayMatch(0, "box"));
|
|
|
|
ASSERT_TRUE(! reader.KeyMayMatch(0, "hello"));
|
|
|
|
|
|
|
|
// Check second filter
|
|
|
|
ASSERT_TRUE(reader.KeyMayMatch(3100, "box"));
|
|
|
|
ASSERT_TRUE(! reader.KeyMayMatch(3100, "foo"));
|
|
|
|
ASSERT_TRUE(! reader.KeyMayMatch(3100, "bar"));
|
|
|
|
ASSERT_TRUE(! reader.KeyMayMatch(3100, "hello"));
|
|
|
|
|
|
|
|
// Check third filter (empty)
|
|
|
|
ASSERT_TRUE(! reader.KeyMayMatch(4100, "foo"));
|
|
|
|
ASSERT_TRUE(! reader.KeyMayMatch(4100, "bar"));
|
|
|
|
ASSERT_TRUE(! reader.KeyMayMatch(4100, "box"));
|
|
|
|
ASSERT_TRUE(! reader.KeyMayMatch(4100, "hello"));
|
|
|
|
|
|
|
|
// Check last filter
|
|
|
|
ASSERT_TRUE(reader.KeyMayMatch(9000, "box"));
|
|
|
|
ASSERT_TRUE(reader.KeyMayMatch(9000, "hello"));
|
|
|
|
ASSERT_TRUE(! reader.KeyMayMatch(9000, "foo"));
|
|
|
|
ASSERT_TRUE(! reader.KeyMayMatch(9000, "bar"));
|
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|
2012-04-17 17:36:46 +02:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2013-10-04 06:49:15 +02:00
|
|
|
return rocksdb::test::RunAllTests();
|
2012-04-17 17:36:46 +02:00
|
|
|
}
|