/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/options_file_test.cc
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | |
21 | | #ifndef ROCKSDB_LITE |
22 | | #include <string> |
23 | | |
24 | | #include "yb/rocksdb/db/db_impl.h" |
25 | | #include "yb/rocksdb/db/db_test_util.h" |
26 | | #include "yb/rocksdb/options.h" |
27 | | #include "yb/rocksdb/table.h" |
28 | | #include "yb/rocksdb/util/testharness.h" |
29 | | |
30 | | #include "yb/rocksdb/util/testutil.h" |
31 | | |
32 | | namespace rocksdb { |
33 | | class OptionsFileTest : public RocksDBTest { |
34 | | public: |
35 | 2 | OptionsFileTest() : dbname_(test::TmpDir() + "/options_file_test") {} |
36 | | |
37 | | std::string dbname_; |
38 | | }; |
39 | | |
40 | | namespace { |
41 | | void UpdateOptionsFiles(DB* db, |
42 | | std::unordered_set<std::string>* filename_history, |
43 | 20 | int* options_files_count) { |
44 | 20 | std::vector<std::string> filenames; |
45 | 20 | ASSERT_OK(db->GetEnv()->GetChildren(db->GetName(), &filenames)); |
46 | 20 | uint64_t number; |
47 | 20 | FileType type; |
48 | 20 | *options_files_count = 0; |
49 | 389 | for (auto filename : filenames) { |
50 | 389 | if (ParseFileName(filename, &number, &type) && type == kOptionsFile) { |
51 | 39 | filename_history->insert(filename); |
52 | 39 | (*options_files_count)++; |
53 | 39 | } |
54 | 389 | } |
55 | 20 | } |
56 | | |
57 | | // Verify whether the current Options Files are the latest ones. |
58 | | void VerifyOptionsFileName( |
59 | 20 | DB* db, const std::unordered_set<std::string>& past_filenames) { |
60 | 20 | std::vector<std::string> filenames; |
61 | 20 | std::unordered_set<std::string> current_filenames; |
62 | 20 | ASSERT_OK(db->GetEnv()->GetChildren(db->GetName(), &filenames)); |
63 | 20 | uint64_t number; |
64 | 20 | FileType type; |
65 | 389 | for (auto filename : filenames) { |
66 | 389 | if (ParseFileName(filename, &number, &type) && type == kOptionsFile) { |
67 | 39 | current_filenames.insert(filename); |
68 | 39 | } |
69 | 389 | } |
70 | 210 | for (auto past_filename : past_filenames) { |
71 | 210 | if (current_filenames.find(past_filename) != current_filenames.end()) { |
72 | 39 | continue; |
73 | 39 | } |
74 | 342 | for (auto filename : current_filenames) { |
75 | 342 | ASSERT_GT(filename, past_filename); |
76 | 342 | } |
77 | 171 | } |
78 | 20 | } |
79 | | } // namespace |
80 | | |
81 | 1 | TEST_F(OptionsFileTest, NumberOfOptionsFiles) { |
82 | 1 | const int kReopenCount = 20; |
83 | 1 | Options opt; |
84 | 1 | opt.create_if_missing = true; |
85 | 1 | ASSERT_OK(DestroyDB(dbname_, opt)); |
86 | 1 | std::unordered_set<std::string> filename_history; |
87 | 1 | DB* db; |
88 | 21 | for (int i = 0; i < kReopenCount; ++i) { |
89 | 20 | ASSERT_OK(DB::Open(opt, dbname_, &db)); |
90 | 20 | int num_options_files = 0; |
91 | 20 | UpdateOptionsFiles(db, &filename_history, &num_options_files); |
92 | 20 | ASSERT_GT(num_options_files, 0); |
93 | 20 | ASSERT_LE(num_options_files, 2); |
94 | | // Make sure we always keep the latest option files. |
95 | 20 | VerifyOptionsFileName(db, filename_history); |
96 | 20 | delete db; |
97 | 20 | } |
98 | 1 | } |
99 | | |
100 | 1 | TEST_F(OptionsFileTest, OptionsFileName) { |
101 | 1 | const uint64_t kOptionsFileNum = 12345; |
102 | 1 | uint64_t number; |
103 | 1 | FileType type; |
104 | | |
105 | 1 | auto options_file_name = OptionsFileName("", kOptionsFileNum); |
106 | 1 | ASSERT_TRUE(ParseFileName(options_file_name, &number, &type, nullptr)); |
107 | 1 | ASSERT_EQ(type, kOptionsFile); |
108 | 1 | ASSERT_EQ(number, kOptionsFileNum); |
109 | | |
110 | 1 | const uint64_t kTempOptionsFileNum = 54352; |
111 | 1 | auto temp_options_file_name = TempOptionsFileName("", kTempOptionsFileNum); |
112 | 1 | ASSERT_TRUE(ParseFileName(temp_options_file_name, &number, &type, nullptr)); |
113 | 1 | ASSERT_NE(temp_options_file_name.find(kTempFileNameSuffix), |
114 | 1 | std::string::npos); |
115 | 1 | ASSERT_EQ(type, kTempFile); |
116 | 1 | ASSERT_EQ(number, kTempOptionsFileNum); |
117 | 1 | } |
118 | | } // namespace rocksdb |
119 | | |
120 | 13.2k | int main(int argc, char** argv) { |
121 | 13.2k | #if !(defined NDEBUG) || !defined(OS_WIN) |
122 | 13.2k | ::testing::InitGoogleTest(&argc, argv); |
123 | 13.2k | return RUN_ALL_TESTS(); |
124 | | #else |
125 | | return 0; |
126 | | #endif // !(defined NDEBUG) || !defined(OS_WIN) |
127 | 13.2k | } |
128 | | #else |
129 | | |
130 | | #include <cstdio> |
131 | | |
132 | | int main(int argc, char** argv) { |
133 | | printf("Skipped as Options file is not supported in RocksDBLite.\n"); |
134 | | return 0; |
135 | | } |
136 | | #endif // !ROCKSDB_LITE |