filter_block_test.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include "table/filter_block.h"
  5. #include "leveldb/filter_policy.h"
  6. #include "util/coding.h"
  7. #include "util/hash.h"
  8. #include "util/logging.h"
  9. #include "util/testharness.h"
  10. #include "util/testutil.h"
  11. namespace leveldb {
  12. // For testing: emit an array with one hash value per key
  13. class TestHashFilter : public FilterPolicy {
  14. public:
  15. virtual const char* Name() const {
  16. return "TestHashFilter";
  17. }
  18. virtual void CreateFilter(const Slice* keys, int n, std::string* dst) const {
  19. for (int i = 0; i < n; i++) {
  20. uint32_t h = Hash(keys[i].data(), keys[i].size(), 1);
  21. PutFixed32(dst, h);
  22. }
  23. }
  24. virtual bool KeyMayMatch(const Slice& key, const Slice& filter) const {
  25. uint32_t h = Hash(key.data(), key.size(), 1);
  26. for (size_t i = 0; i + 4 <= filter.size(); i += 4) {
  27. if (h == DecodeFixed32(filter.data() + i)) {
  28. return true;
  29. }
  30. }
  31. return false;
  32. }
  33. };
  34. class FilterBlockTest {
  35. public:
  36. TestHashFilter policy_;
  37. };
  38. TEST(FilterBlockTest, EmptyBuilder) {
  39. FilterBlockBuilder builder(&policy_);
  40. Slice block = builder.Finish();
  41. ASSERT_EQ("\\x00\\x00\\x00\\x00\\x0b", EscapeString(block));
  42. FilterBlockReader reader(&policy_, block);
  43. ASSERT_TRUE(reader.KeyMayMatch(0, "foo"));
  44. ASSERT_TRUE(reader.KeyMayMatch(100000, "foo"));
  45. }
  46. TEST(FilterBlockTest, SingleChunk) {
  47. FilterBlockBuilder builder(&policy_);
  48. builder.StartBlock(100);
  49. builder.AddKey("foo");
  50. builder.AddKey("bar");
  51. builder.AddKey("box");
  52. builder.StartBlock(200);
  53. builder.AddKey("box");
  54. builder.StartBlock(300);
  55. builder.AddKey("hello");
  56. Slice block = builder.Finish();
  57. FilterBlockReader reader(&policy_, block);
  58. ASSERT_TRUE(reader.KeyMayMatch(100, "foo"));
  59. ASSERT_TRUE(reader.KeyMayMatch(100, "bar"));
  60. ASSERT_TRUE(reader.KeyMayMatch(100, "box"));
  61. ASSERT_TRUE(reader.KeyMayMatch(100, "hello"));
  62. ASSERT_TRUE(reader.KeyMayMatch(100, "foo"));
  63. ASSERT_TRUE(! reader.KeyMayMatch(100, "missing"));
  64. ASSERT_TRUE(! reader.KeyMayMatch(100, "other"));
  65. }
  66. TEST(FilterBlockTest, MultiChunk) {
  67. FilterBlockBuilder builder(&policy_);
  68. // First filter
  69. builder.StartBlock(0);
  70. builder.AddKey("foo");
  71. builder.StartBlock(2000);
  72. builder.AddKey("bar");
  73. // Second filter
  74. builder.StartBlock(3100);
  75. builder.AddKey("box");
  76. // Third filter is empty
  77. // Last filter
  78. builder.StartBlock(9000);
  79. builder.AddKey("box");
  80. builder.AddKey("hello");
  81. Slice block = builder.Finish();
  82. FilterBlockReader reader(&policy_, block);
  83. // Check first filter
  84. ASSERT_TRUE(reader.KeyMayMatch(0, "foo"));
  85. ASSERT_TRUE(reader.KeyMayMatch(2000, "bar"));
  86. ASSERT_TRUE(! reader.KeyMayMatch(0, "box"));
  87. ASSERT_TRUE(! reader.KeyMayMatch(0, "hello"));
  88. // Check second filter
  89. ASSERT_TRUE(reader.KeyMayMatch(3100, "box"));
  90. ASSERT_TRUE(! reader.KeyMayMatch(3100, "foo"));
  91. ASSERT_TRUE(! reader.KeyMayMatch(3100, "bar"));
  92. ASSERT_TRUE(! reader.KeyMayMatch(3100, "hello"));
  93. // Check third filter (empty)
  94. ASSERT_TRUE(! reader.KeyMayMatch(4100, "foo"));
  95. ASSERT_TRUE(! reader.KeyMayMatch(4100, "bar"));
  96. ASSERT_TRUE(! reader.KeyMayMatch(4100, "box"));
  97. ASSERT_TRUE(! reader.KeyMayMatch(4100, "hello"));
  98. // Check last filter
  99. ASSERT_TRUE(reader.KeyMayMatch(9000, "box"));
  100. ASSERT_TRUE(reader.KeyMayMatch(9000, "hello"));
  101. ASSERT_TRUE(! reader.KeyMayMatch(9000, "foo"));
  102. ASSERT_TRUE(! reader.KeyMayMatch(9000, "bar"));
  103. }
  104. } // namespace leveldb
  105. int main(int argc, char** argv) {
  106. return leveldb::test::RunAllTests();
  107. }