1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
10 #include "rocksdb/slice_transform.h"
12 #include "rocksdb/db.h"
13 #include "rocksdb/env.h"
14 #include "rocksdb/filter_policy.h"
15 #include "rocksdb/statistics.h"
16 #include "rocksdb/table.h"
17 #include "util/testharness.h"
21 class SliceTransformTest
: public testing::Test
{};
23 TEST_F(SliceTransformTest
, CapPrefixTransform
) {
27 std::unique_ptr
<const SliceTransform
> transform
;
29 transform
.reset(NewCappedPrefixTransform(6));
30 ASSERT_EQ(transform
->Transform(s
).ToString(), "abcdef");
31 ASSERT_TRUE(transform
->SameResultWhenAppended("123456"));
32 ASSERT_TRUE(transform
->SameResultWhenAppended("1234567"));
33 ASSERT_TRUE(!transform
->SameResultWhenAppended("12345"));
35 transform
.reset(NewCappedPrefixTransform(8));
36 ASSERT_EQ(transform
->Transform(s
).ToString(), "abcdefge");
38 transform
.reset(NewCappedPrefixTransform(10));
39 ASSERT_EQ(transform
->Transform(s
).ToString(), "abcdefge");
41 transform
.reset(NewCappedPrefixTransform(0));
42 ASSERT_EQ(transform
->Transform(s
).ToString(), "");
44 transform
.reset(NewCappedPrefixTransform(0));
45 ASSERT_EQ(transform
->Transform("").ToString(), "");
48 class SliceTransformDBTest
: public testing::Test
{
55 SliceTransformDBTest() : env_(Env::Default()), db_(nullptr) {
56 dbname_
= test::PerThreadDBPath("slice_transform_db_test");
57 EXPECT_OK(DestroyDB(dbname_
, last_options_
));
60 ~SliceTransformDBTest() override
{
62 EXPECT_OK(DestroyDB(dbname_
, last_options_
));
65 DB
* db() { return db_
; }
67 // Return the current option configuration.
68 Options
* GetOptions() { return &last_options_
; }
70 void DestroyAndReopen() {
71 // Destroy using last options
73 ASSERT_OK(TryReopen());
79 ASSERT_OK(DestroyDB(dbname_
, last_options_
));
85 last_options_
.create_if_missing
= true;
87 return DB::Open(last_options_
, dbname_
, &db_
);
90 Options last_options_
;
94 uint64_t TestGetTickerCount(const Options
& options
, Tickers ticker_type
) {
95 return options
.statistics
->getTickerCount(ticker_type
);
99 TEST_F(SliceTransformDBTest
, CapPrefix
) {
100 last_options_
.prefix_extractor
.reset(NewCappedPrefixTransform(8));
101 last_options_
.statistics
= rocksdb::CreateDBStatistics();
102 BlockBasedTableOptions bbto
;
103 bbto
.filter_policy
.reset(NewBloomFilterPolicy(10, false));
104 bbto
.whole_key_filtering
= false;
105 last_options_
.table_factory
.reset(NewBlockBasedTableFactory(bbto
));
106 ASSERT_OK(TryReopen());
112 ASSERT_OK(db()->Put(wo
, "barbarbar", "foo"));
113 ASSERT_OK(db()->Put(wo
, "barbarbar2", "foo2"));
114 ASSERT_OK(db()->Put(wo
, "foo", "bar"));
115 ASSERT_OK(db()->Put(wo
, "foo3", "bar3"));
116 ASSERT_OK(db()->Flush(fo
));
118 std::unique_ptr
<Iterator
> iter(db()->NewIterator(ro
));
121 ASSERT_OK(iter
->status());
122 ASSERT_TRUE(iter
->Valid());
123 ASSERT_EQ(iter
->value().ToString(), "bar");
124 ASSERT_EQ(TestGetTickerCount(last_options_
, BLOOM_FILTER_PREFIX_USEFUL
), 0U);
127 ASSERT_OK(iter
->status());
128 ASSERT_TRUE(!iter
->Valid());
129 ASSERT_EQ(TestGetTickerCount(last_options_
, BLOOM_FILTER_PREFIX_USEFUL
), 1U);
131 iter
->Seek("barbarbar");
132 ASSERT_OK(iter
->status());
133 ASSERT_TRUE(iter
->Valid());
134 ASSERT_EQ(iter
->value().ToString(), "foo");
135 ASSERT_EQ(TestGetTickerCount(last_options_
, BLOOM_FILTER_PREFIX_USEFUL
), 1U);
137 iter
->Seek("barfoofoo");
138 ASSERT_OK(iter
->status());
139 ASSERT_TRUE(!iter
->Valid());
140 ASSERT_EQ(TestGetTickerCount(last_options_
, BLOOM_FILTER_PREFIX_USEFUL
), 2U);
142 iter
->Seek("foobarbar");
143 ASSERT_OK(iter
->status());
144 ASSERT_TRUE(!iter
->Valid());
145 ASSERT_EQ(TestGetTickerCount(last_options_
, BLOOM_FILTER_PREFIX_USEFUL
), 3U);
148 } // namespace rocksdb
150 int main(int argc
, char** argv
) {
151 ::testing::InitGoogleTest(&argc
, argv
);
152 return RUN_ALL_TESTS();