]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
5 | |
6 | #ifndef ROCKSDB_LITE | |
7 | #include <string> | |
8 | ||
f67539c2 | 9 | #include "db/db_impl/db_impl.h" |
7c673cae FG |
10 | #include "db/db_test_util.h" |
11 | #include "rocksdb/options.h" | |
12 | #include "rocksdb/table.h" | |
f67539c2 | 13 | #include "test_util/testharness.h" |
7c673cae | 14 | |
f67539c2 | 15 | namespace ROCKSDB_NAMESPACE { |
7c673cae FG |
16 | class OptionsFileTest : public testing::Test { |
17 | public: | |
11fdf7f2 | 18 | OptionsFileTest() : dbname_(test::PerThreadDBPath("options_file_test")) {} |
7c673cae FG |
19 | |
20 | std::string dbname_; | |
21 | }; | |
22 | ||
23 | namespace { | |
24 | void UpdateOptionsFiles(DB* db, | |
25 | std::unordered_set<std::string>* filename_history, | |
26 | int* options_files_count) { | |
27 | std::vector<std::string> filenames; | |
20effc67 | 28 | EXPECT_OK(db->GetEnv()->GetChildren(db->GetName(), &filenames)); |
7c673cae FG |
29 | uint64_t number; |
30 | FileType type; | |
31 | *options_files_count = 0; | |
32 | for (auto filename : filenames) { | |
33 | if (ParseFileName(filename, &number, &type) && type == kOptionsFile) { | |
34 | filename_history->insert(filename); | |
35 | (*options_files_count)++; | |
36 | } | |
37 | } | |
38 | } | |
39 | ||
40 | // Verify whether the current Options Files are the latest ones. | |
41 | void VerifyOptionsFileName( | |
42 | DB* db, const std::unordered_set<std::string>& past_filenames) { | |
43 | std::vector<std::string> filenames; | |
44 | std::unordered_set<std::string> current_filenames; | |
20effc67 | 45 | EXPECT_OK(db->GetEnv()->GetChildren(db->GetName(), &filenames)); |
7c673cae FG |
46 | uint64_t number; |
47 | FileType type; | |
48 | for (auto filename : filenames) { | |
49 | if (ParseFileName(filename, &number, &type) && type == kOptionsFile) { | |
50 | current_filenames.insert(filename); | |
51 | } | |
52 | } | |
53 | for (auto past_filename : past_filenames) { | |
54 | if (current_filenames.find(past_filename) != current_filenames.end()) { | |
55 | continue; | |
56 | } | |
57 | for (auto filename : current_filenames) { | |
58 | ASSERT_GT(filename, past_filename); | |
59 | } | |
60 | } | |
61 | } | |
1e59de90 | 62 | } // anonymous namespace |
7c673cae FG |
63 | |
64 | TEST_F(OptionsFileTest, NumberOfOptionsFiles) { | |
65 | const int kReopenCount = 20; | |
66 | Options opt; | |
67 | opt.create_if_missing = true; | |
1e59de90 | 68 | ASSERT_OK(DestroyDB(dbname_, opt)); |
7c673cae FG |
69 | std::unordered_set<std::string> filename_history; |
70 | DB* db; | |
71 | for (int i = 0; i < kReopenCount; ++i) { | |
72 | ASSERT_OK(DB::Open(opt, dbname_, &db)); | |
73 | int num_options_files = 0; | |
74 | UpdateOptionsFiles(db, &filename_history, &num_options_files); | |
75 | ASSERT_GT(num_options_files, 0); | |
76 | ASSERT_LE(num_options_files, 2); | |
77 | // Make sure we always keep the latest option files. | |
78 | VerifyOptionsFileName(db, filename_history); | |
79 | delete db; | |
80 | } | |
81 | } | |
82 | ||
83 | TEST_F(OptionsFileTest, OptionsFileName) { | |
84 | const uint64_t kOptionsFileNum = 12345; | |
85 | uint64_t number; | |
86 | FileType type; | |
87 | ||
88 | auto options_file_name = OptionsFileName("", kOptionsFileNum); | |
89 | ASSERT_TRUE(ParseFileName(options_file_name, &number, &type, nullptr)); | |
90 | ASSERT_EQ(type, kOptionsFile); | |
91 | ASSERT_EQ(number, kOptionsFileNum); | |
92 | ||
93 | const uint64_t kTempOptionsFileNum = 54352; | |
94 | auto temp_options_file_name = TempOptionsFileName("", kTempOptionsFileNum); | |
95 | ASSERT_TRUE(ParseFileName(temp_options_file_name, &number, &type, nullptr)); | |
96 | ASSERT_NE(temp_options_file_name.find(kTempFileNameSuffix), | |
97 | std::string::npos); | |
98 | ASSERT_EQ(type, kTempFile); | |
99 | ASSERT_EQ(number, kTempOptionsFileNum); | |
100 | } | |
f67539c2 | 101 | } // namespace ROCKSDB_NAMESPACE |
7c673cae FG |
102 | |
103 | int main(int argc, char** argv) { | |
104 | #if !(defined NDEBUG) || !defined(OS_WIN) | |
1e59de90 | 105 | ROCKSDB_NAMESPACE::port::InstallStackTraceHandler(); |
7c673cae FG |
106 | ::testing::InitGoogleTest(&argc, argv); |
107 | return RUN_ALL_TESTS(); | |
108 | #else | |
109 | return 0; | |
110 | #endif // !(defined NDEBUG) || !defined(OS_WIN) | |
111 | } | |
112 | #else | |
113 | ||
114 | #include <cstdio> | |
115 | ||
11fdf7f2 | 116 | int main(int /*argc*/, char** /*argv*/) { |
7c673cae FG |
117 | printf("Skipped as Options file is not supported in RocksDBLite.\n"); |
118 | return 0; | |
119 | } | |
120 | #endif // !ROCKSDB_LITE |