]>
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 | // Copyright (c) 2012 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. | |
9 | ||
10 | #ifndef ROCKSDB_LITE | |
11 | ||
12 | #include <stdint.h> | |
13 | #include "rocksdb/sst_dump_tool.h" | |
14 | ||
15 | #include "rocksdb/filter_policy.h" | |
16 | #include "table/block_based_table_factory.h" | |
17 | #include "table/table_builder.h" | |
18 | #include "util/file_reader_writer.h" | |
19 | #include "util/testharness.h" | |
20 | #include "util/testutil.h" | |
21 | ||
22 | namespace rocksdb { | |
23 | ||
24 | const uint32_t optLength = 100; | |
25 | ||
26 | namespace { | |
27 | static std::string MakeKey(int i) { | |
28 | char buf[100]; | |
29 | snprintf(buf, sizeof(buf), "k_%04d", i); | |
30 | InternalKey key(std::string(buf), 0, ValueType::kTypeValue); | |
31 | return key.Encode().ToString(); | |
32 | } | |
33 | ||
34 | static std::string MakeValue(int i) { | |
35 | char buf[100]; | |
36 | snprintf(buf, sizeof(buf), "v_%04d", i); | |
37 | InternalKey key(std::string(buf), 0, ValueType::kTypeValue); | |
38 | return key.Encode().ToString(); | |
39 | } | |
40 | ||
494da23a TL |
41 | void createSST(const Options& opts, const std::string& file_name) { |
42 | Env* env = opts.env; | |
43 | EnvOptions env_options(opts); | |
7c673cae | 44 | ReadOptions read_options; |
7c673cae | 45 | const ImmutableCFOptions imoptions(opts); |
11fdf7f2 | 46 | const MutableCFOptions moptions(opts); |
7c673cae | 47 | rocksdb::InternalKeyComparator ikc(opts.comparator); |
494da23a | 48 | std::unique_ptr<TableBuilder> tb; |
7c673cae | 49 | |
494da23a | 50 | std::unique_ptr<WritableFile> file; |
11fdf7f2 TL |
51 | ASSERT_OK(env->NewWritableFile(file_name, &file, env_options)); |
52 | ||
7c673cae FG |
53 | std::vector<std::unique_ptr<IntTblPropCollectorFactory> > |
54 | int_tbl_prop_collector_factories; | |
11fdf7f2 TL |
55 | std::unique_ptr<WritableFileWriter> file_writer( |
56 | new WritableFileWriter(std::move(file), file_name, EnvOptions())); | |
7c673cae FG |
57 | std::string column_family_name; |
58 | int unknown_level = -1; | |
59 | tb.reset(opts.table_factory->NewTableBuilder( | |
11fdf7f2 TL |
60 | TableBuilderOptions( |
61 | imoptions, moptions, ikc, &int_tbl_prop_collector_factories, | |
494da23a TL |
62 | CompressionType::kNoCompression, 0 /* sample_for_compression */, |
63 | CompressionOptions(), false /* skip_filters */, column_family_name, | |
64 | unknown_level), | |
7c673cae FG |
65 | TablePropertiesCollectorFactory::Context::kUnknownColumnFamily, |
66 | file_writer.get())); | |
67 | ||
68 | // Populate slightly more than 1K keys | |
69 | uint32_t num_keys = 1024; | |
70 | for (uint32_t i = 0; i < num_keys; i++) { | |
71 | tb->Add(MakeKey(i), MakeValue(i)); | |
72 | } | |
73 | tb->Finish(); | |
74 | file_writer->Close(); | |
75 | } | |
76 | ||
494da23a TL |
77 | void cleanup(const Options& opts, const std::string& file_name) { |
78 | Env* env = opts.env; | |
7c673cae FG |
79 | env->DeleteFile(file_name); |
80 | std::string outfile_name = file_name.substr(0, file_name.length() - 4); | |
81 | outfile_name.append("_dump.txt"); | |
82 | env->DeleteFile(outfile_name); | |
83 | } | |
84 | } // namespace | |
85 | ||
86 | // Test for sst dump tool "raw" mode | |
87 | class SSTDumpToolTest : public testing::Test { | |
11fdf7f2 TL |
88 | std::string testDir_; |
89 | ||
7c673cae | 90 | public: |
11fdf7f2 | 91 | SSTDumpToolTest() { testDir_ = test::TmpDir(); } |
7c673cae | 92 | |
494da23a | 93 | ~SSTDumpToolTest() override {} |
11fdf7f2 TL |
94 | |
95 | std::string MakeFilePath(const std::string& file_name) const { | |
96 | std::string path(testDir_); | |
97 | path.append("/").append(file_name); | |
98 | return path; | |
99 | } | |
100 | ||
101 | template <std::size_t N> | |
102 | void PopulateCommandArgs(const std::string& file_path, const char* command, | |
103 | char* (&usage)[N]) const { | |
104 | for (int i = 0; i < static_cast<int>(N); ++i) { | |
105 | usage[i] = new char[optLength]; | |
106 | } | |
107 | snprintf(usage[0], optLength, "./sst_dump"); | |
108 | snprintf(usage[1], optLength, "%s", command); | |
109 | snprintf(usage[2], optLength, "--file=%s", file_path.c_str()); | |
110 | } | |
7c673cae FG |
111 | }; |
112 | ||
113 | TEST_F(SSTDumpToolTest, EmptyFilter) { | |
494da23a | 114 | Options opts; |
11fdf7f2 | 115 | std::string file_path = MakeFilePath("rocksdb_sst_test.sst"); |
494da23a | 116 | createSST(opts, file_path); |
7c673cae FG |
117 | |
118 | char* usage[3]; | |
11fdf7f2 | 119 | PopulateCommandArgs(file_path, "--command=raw", usage); |
7c673cae FG |
120 | |
121 | rocksdb::SSTDumpTool tool; | |
494da23a | 122 | ASSERT_TRUE(!tool.Run(3, usage, opts)); |
7c673cae | 123 | |
494da23a | 124 | cleanup(opts, file_path); |
7c673cae FG |
125 | for (int i = 0; i < 3; i++) { |
126 | delete[] usage[i]; | |
127 | } | |
128 | } | |
129 | ||
130 | TEST_F(SSTDumpToolTest, FilterBlock) { | |
494da23a TL |
131 | Options opts; |
132 | BlockBasedTableOptions table_opts; | |
133 | table_opts.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, true)); | |
134 | opts.table_factory.reset(new BlockBasedTableFactory(table_opts)); | |
11fdf7f2 | 135 | std::string file_path = MakeFilePath("rocksdb_sst_test.sst"); |
494da23a | 136 | createSST(opts, file_path); |
7c673cae FG |
137 | |
138 | char* usage[3]; | |
11fdf7f2 | 139 | PopulateCommandArgs(file_path, "--command=raw", usage); |
7c673cae FG |
140 | |
141 | rocksdb::SSTDumpTool tool; | |
494da23a | 142 | ASSERT_TRUE(!tool.Run(3, usage, opts)); |
7c673cae | 143 | |
494da23a | 144 | cleanup(opts, file_path); |
7c673cae FG |
145 | for (int i = 0; i < 3; i++) { |
146 | delete[] usage[i]; | |
147 | } | |
148 | } | |
149 | ||
150 | TEST_F(SSTDumpToolTest, FullFilterBlock) { | |
494da23a TL |
151 | Options opts; |
152 | BlockBasedTableOptions table_opts; | |
153 | table_opts.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false)); | |
154 | opts.table_factory.reset(new BlockBasedTableFactory(table_opts)); | |
11fdf7f2 | 155 | std::string file_path = MakeFilePath("rocksdb_sst_test.sst"); |
494da23a | 156 | createSST(opts, file_path); |
7c673cae FG |
157 | |
158 | char* usage[3]; | |
11fdf7f2 | 159 | PopulateCommandArgs(file_path, "--command=raw", usage); |
7c673cae FG |
160 | |
161 | rocksdb::SSTDumpTool tool; | |
494da23a | 162 | ASSERT_TRUE(!tool.Run(3, usage, opts)); |
7c673cae | 163 | |
494da23a | 164 | cleanup(opts, file_path); |
7c673cae FG |
165 | for (int i = 0; i < 3; i++) { |
166 | delete[] usage[i]; | |
167 | } | |
168 | } | |
169 | ||
170 | TEST_F(SSTDumpToolTest, GetProperties) { | |
494da23a TL |
171 | Options opts; |
172 | BlockBasedTableOptions table_opts; | |
173 | table_opts.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false)); | |
174 | opts.table_factory.reset(new BlockBasedTableFactory(table_opts)); | |
11fdf7f2 | 175 | std::string file_path = MakeFilePath("rocksdb_sst_test.sst"); |
494da23a | 176 | createSST(opts, file_path); |
7c673cae FG |
177 | |
178 | char* usage[3]; | |
11fdf7f2 | 179 | PopulateCommandArgs(file_path, "--show_properties", usage); |
7c673cae FG |
180 | |
181 | rocksdb::SSTDumpTool tool; | |
494da23a | 182 | ASSERT_TRUE(!tool.Run(3, usage, opts)); |
7c673cae | 183 | |
494da23a | 184 | cleanup(opts, file_path); |
7c673cae FG |
185 | for (int i = 0; i < 3; i++) { |
186 | delete[] usage[i]; | |
187 | } | |
188 | } | |
189 | ||
190 | TEST_F(SSTDumpToolTest, CompressedSizes) { | |
494da23a TL |
191 | Options opts; |
192 | BlockBasedTableOptions table_opts; | |
193 | table_opts.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false)); | |
194 | opts.table_factory.reset(new BlockBasedTableFactory(table_opts)); | |
11fdf7f2 | 195 | std::string file_path = MakeFilePath("rocksdb_sst_test.sst"); |
494da23a | 196 | createSST(opts, file_path); |
7c673cae FG |
197 | |
198 | char* usage[3]; | |
11fdf7f2 | 199 | PopulateCommandArgs(file_path, "--command=recompress", usage); |
7c673cae | 200 | |
7c673cae | 201 | rocksdb::SSTDumpTool tool; |
494da23a TL |
202 | ASSERT_TRUE(!tool.Run(3, usage, opts)); |
203 | ||
204 | cleanup(opts, file_path); | |
205 | for (int i = 0; i < 3; i++) { | |
206 | delete[] usage[i]; | |
207 | } | |
208 | } | |
209 | ||
210 | TEST_F(SSTDumpToolTest, MemEnv) { | |
211 | std::unique_ptr<Env> env(NewMemEnv(Env::Default())); | |
212 | Options opts; | |
213 | opts.env = env.get(); | |
214 | std::string file_path = MakeFilePath("rocksdb_sst_test.sst"); | |
215 | createSST(opts, file_path); | |
216 | ||
217 | char* usage[3]; | |
218 | PopulateCommandArgs(file_path, "--command=verify_checksum", usage); | |
219 | ||
220 | rocksdb::SSTDumpTool tool; | |
221 | ASSERT_TRUE(!tool.Run(3, usage, opts)); | |
7c673cae | 222 | |
494da23a | 223 | cleanup(opts, file_path); |
7c673cae FG |
224 | for (int i = 0; i < 3; i++) { |
225 | delete[] usage[i]; | |
226 | } | |
227 | } | |
494da23a | 228 | |
7c673cae FG |
229 | } // namespace rocksdb |
230 | ||
231 | int main(int argc, char** argv) { | |
232 | ::testing::InitGoogleTest(&argc, argv); | |
233 | return RUN_ALL_TESTS(); | |
234 | } | |
235 | ||
236 | #else | |
237 | #include <stdio.h> | |
238 | ||
11fdf7f2 | 239 | int main(int /*argc*/, char** /*argv*/) { |
7c673cae FG |
240 | fprintf(stderr, "SKIPPED as SSTDumpTool is not supported in ROCKSDB_LITE\n"); |
241 | return 0; | |
242 | } | |
243 | ||
244 | #endif // !ROCKSDB_LITE return RUN_ALL_TESTS(); |