YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/table/fixed_size_filter_block_test.cc
Line
Count
Source
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#include <string>
15
16
#include <gtest/gtest.h>
17
18
#include "yb/rocksdb/table/fixed_size_filter_block.h"
19
20
#include "yb/rocksdb/filter_policy.h"
21
#include "yb/rocksdb/util/hash.h"
22
#include "yb/rocksdb/env.h"
23
24
#include "yb/rocksdb/util/testutil.h"
25
26
namespace rocksdb {
27
28
class FixedSizeFilterTest : public RocksDBTest {
29
 public:
30
  BlockBasedTableOptions table_options_;
31
32
3
  FixedSizeFilterTest() {
33
3
    table_options_.filter_policy.reset(NewFixedSizeFilterPolicy(
34
3
        FilterPolicy::kDefaultFixedSizeFilterBits, FilterPolicy::kDefaultFixedSizeFilterErrorRate,
35
3
        nullptr));
36
3
  }
37
};
38
39
1
TEST_F(FixedSizeFilterTest, SingleChunk) {
40
1
  FixedSizeFilterBlockBuilder builder(nullptr, table_options_);
41
1
  builder.StartBlock(0);
42
1
  builder.Add("foo");
43
1
  builder.Add("bar");
44
1
  builder.Add("box");
45
1
  builder.Add("hello");
46
47
1
  BlockContents block(builder.Finish(), false, kNoCompression);
48
1
  FixedSizeFilterBlockReader reader(nullptr, table_options_, true, std::move(block));
49
1
  ASSERT_TRUE(reader.KeyMayMatch("foo"));
50
1
  ASSERT_TRUE(reader.KeyMayMatch("bar"));
51
1
  ASSERT_TRUE(reader.KeyMayMatch("box"));
52
1
  ASSERT_TRUE(reader.KeyMayMatch("hello"));
53
1
  ASSERT_TRUE(!reader.KeyMayMatch("missing"));
54
1
  ASSERT_TRUE(!reader.KeyMayMatch("other"));
55
1
}
56
57
1
TEST_F(FixedSizeFilterTest, MultipleChunks) {
58
1
  FixedSizeFilterBlockBuilder builder(nullptr, table_options_);
59
60
  // First block
61
1
  builder.StartBlock(0);
62
1
  builder.Add("a1");
63
1
  builder.Add("b1");
64
1
  builder.Add("c1");
65
1
  builder.Add("foo");
66
1
  builder.Add("bar");
67
68
1
  BlockContents block1(builder.Finish(), false, kNoCompression);
69
70
  // Second block
71
1
  builder.StartBlock(0);
72
1
  builder.Add("a2");
73
1
  builder.Add("b2");
74
1
  builder.Add("c2");
75
1
  builder.Add("foo");
76
1
  builder.Add("bar");
77
78
1
  BlockContents block2(builder.Finish(), false, kNoCompression);
79
80
  // Third block (empty block)
81
1
  builder.StartBlock(0);
82
1
  BlockContents block3(builder.Finish(), false, kNoCompression);
83
84
  // Check first block
85
1
  FixedSizeFilterBlockReader reader1(nullptr, table_options_, true, std::move(block1));
86
1
  ASSERT_TRUE(reader1.KeyMayMatch("a1"));
87
1
  ASSERT_TRUE(reader1.KeyMayMatch("b1"));
88
1
  ASSERT_TRUE(reader1.KeyMayMatch("c1"));
89
1
  ASSERT_TRUE(reader1.KeyMayMatch("foo"));
90
1
  ASSERT_TRUE(reader1.KeyMayMatch("bar"));
91
1
  ASSERT_TRUE(!reader1.KeyMayMatch("a2"));
92
1
  ASSERT_TRUE(!reader1.KeyMayMatch("b2"));
93
1
  ASSERT_TRUE(!reader1.KeyMayMatch("c2"));
94
1
  ASSERT_TRUE(!reader1.KeyMayMatch("missing"));
95
1
  ASSERT_TRUE(!reader1.KeyMayMatch("other"));
96
97
  // Check second block
98
1
  FixedSizeFilterBlockReader reader2(nullptr, table_options_, true, std::move(block2));
99
1
  ASSERT_TRUE(reader2.KeyMayMatch("a2"));
100
1
  ASSERT_TRUE(reader2.KeyMayMatch("b2"));
101
1
  ASSERT_TRUE(reader2.KeyMayMatch("c2"));
102
1
  ASSERT_TRUE(reader2.KeyMayMatch("foo"));
103
1
  ASSERT_TRUE(reader2.KeyMayMatch("bar"));
104
1
  ASSERT_TRUE(!reader2.KeyMayMatch("a1"));
105
1
  ASSERT_TRUE(!reader2.KeyMayMatch("b1"));
106
1
  ASSERT_TRUE(!reader2.KeyMayMatch("c1"));
107
1
  ASSERT_TRUE(!reader2.KeyMayMatch("missing"));
108
1
  ASSERT_TRUE(!reader2.KeyMayMatch("other"));
109
110
  // Check third block
111
1
  FixedSizeFilterBlockReader reader3(nullptr, table_options_, true, std::move(block3));
112
1
  ASSERT_TRUE(!reader3.KeyMayMatch("foo"));
113
1
  ASSERT_TRUE(!reader3.KeyMayMatch("bar"));
114
1
  ASSERT_TRUE(!reader3.KeyMayMatch("a1"));
115
1
  ASSERT_TRUE(!reader3.KeyMayMatch("b1"));
116
1
  ASSERT_TRUE(!reader3.KeyMayMatch("c1"));
117
1
  ASSERT_TRUE(!reader3.KeyMayMatch("a2"));
118
1
  ASSERT_TRUE(!reader3.KeyMayMatch("b2"));
119
1
  ASSERT_TRUE(!reader3.KeyMayMatch("c2"));
120
1
  ASSERT_TRUE(!reader3.KeyMayMatch("missing"));
121
1
  ASSERT_TRUE(!reader3.KeyMayMatch("other"));
122
1
}
123
124
1
TEST_F(FixedSizeFilterTest, ConcurrentReads) {
125
1
  FixedSizeFilterBlockBuilder builder(nullptr, table_options_);
126
1
  builder.StartBlock(0);
127
1
  builder.Add("foo");
128
1
  builder.Add("bar");
129
1
  builder.Add("fox");
130
131
1
  BlockContents block(builder.Finish(), false, kNoCompression);
132
133
  // Multiple readers on the same block should not matter
134
1
  FixedSizeFilterBlockReader reader1(nullptr, table_options_, true, std::move(block));
135
1
  ASSERT_TRUE(reader1.KeyMayMatch("foo"));
136
1
  ASSERT_TRUE(reader1.KeyMayMatch("bar"));
137
1
  ASSERT_TRUE(reader1.KeyMayMatch("fox"));
138
1
  ASSERT_TRUE(!reader1.KeyMayMatch("other"));
139
1
  ASSERT_TRUE(!reader1.KeyMayMatch("missing"));
140
141
1
  FixedSizeFilterBlockReader reader2(nullptr, table_options_, true, std::move(block));
142
1
  ASSERT_TRUE(reader2.KeyMayMatch("foo"));
143
1
  ASSERT_TRUE(reader2.KeyMayMatch("bar"));
144
1
  ASSERT_TRUE(reader2.KeyMayMatch("fox"));
145
1
  ASSERT_TRUE(!reader2.KeyMayMatch("other"));
146
1
  ASSERT_TRUE(!reader2.KeyMayMatch("missing"));
147
1
}
148
149
}  // namespace rocksdb
150
151
13.2k
int main(int argc, char** argv) {
152
13.2k
  ::testing::InitGoogleTest(&argc, argv);
153
13.2k
  return RUN_ALL_TESTS();
154
13.2k
}