]>
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 | ||
41 | void createSST(const std::string& file_name, | |
42 | const BlockBasedTableOptions& table_options) { | |
43 | std::shared_ptr<rocksdb::TableFactory> tf; | |
44 | tf.reset(new rocksdb::BlockBasedTableFactory(table_options)); | |
45 | ||
46 | unique_ptr<WritableFile> file; | |
47 | Env* env = Env::Default(); | |
48 | EnvOptions env_options; | |
49 | ReadOptions read_options; | |
50 | Options opts; | |
51 | const ImmutableCFOptions imoptions(opts); | |
11fdf7f2 | 52 | const MutableCFOptions moptions(opts); |
7c673cae FG |
53 | rocksdb::InternalKeyComparator ikc(opts.comparator); |
54 | unique_ptr<TableBuilder> tb; | |
55 | ||
11fdf7f2 TL |
56 | ASSERT_OK(env->NewWritableFile(file_name, &file, env_options)); |
57 | ||
7c673cae FG |
58 | opts.table_factory = tf; |
59 | std::vector<std::unique_ptr<IntTblPropCollectorFactory> > | |
60 | int_tbl_prop_collector_factories; | |
11fdf7f2 TL |
61 | std::unique_ptr<WritableFileWriter> file_writer( |
62 | new WritableFileWriter(std::move(file), file_name, EnvOptions())); | |
7c673cae FG |
63 | std::string column_family_name; |
64 | int unknown_level = -1; | |
65 | tb.reset(opts.table_factory->NewTableBuilder( | |
11fdf7f2 TL |
66 | TableBuilderOptions( |
67 | imoptions, moptions, ikc, &int_tbl_prop_collector_factories, | |
68 | CompressionType::kNoCompression, CompressionOptions(), | |
69 | nullptr /* compression_dict */, false /* skip_filters */, | |
70 | column_family_name, unknown_level), | |
7c673cae FG |
71 | TablePropertiesCollectorFactory::Context::kUnknownColumnFamily, |
72 | file_writer.get())); | |
73 | ||
74 | // Populate slightly more than 1K keys | |
75 | uint32_t num_keys = 1024; | |
76 | for (uint32_t i = 0; i < num_keys; i++) { | |
77 | tb->Add(MakeKey(i), MakeValue(i)); | |
78 | } | |
79 | tb->Finish(); | |
80 | file_writer->Close(); | |
81 | } | |
82 | ||
83 | void cleanup(const std::string& file_name) { | |
84 | Env* env = Env::Default(); | |
85 | env->DeleteFile(file_name); | |
86 | std::string outfile_name = file_name.substr(0, file_name.length() - 4); | |
87 | outfile_name.append("_dump.txt"); | |
88 | env->DeleteFile(outfile_name); | |
89 | } | |
90 | } // namespace | |
91 | ||
92 | // Test for sst dump tool "raw" mode | |
93 | class SSTDumpToolTest : public testing::Test { | |
11fdf7f2 TL |
94 | std::string testDir_; |
95 | ||
7c673cae FG |
96 | public: |
97 | BlockBasedTableOptions table_options_; | |
98 | ||
11fdf7f2 | 99 | SSTDumpToolTest() { testDir_ = test::TmpDir(); } |
7c673cae FG |
100 | |
101 | ~SSTDumpToolTest() {} | |
11fdf7f2 TL |
102 | |
103 | std::string MakeFilePath(const std::string& file_name) const { | |
104 | std::string path(testDir_); | |
105 | path.append("/").append(file_name); | |
106 | return path; | |
107 | } | |
108 | ||
109 | template <std::size_t N> | |
110 | void PopulateCommandArgs(const std::string& file_path, const char* command, | |
111 | char* (&usage)[N]) const { | |
112 | for (int i = 0; i < static_cast<int>(N); ++i) { | |
113 | usage[i] = new char[optLength]; | |
114 | } | |
115 | snprintf(usage[0], optLength, "./sst_dump"); | |
116 | snprintf(usage[1], optLength, "%s", command); | |
117 | snprintf(usage[2], optLength, "--file=%s", file_path.c_str()); | |
118 | } | |
7c673cae FG |
119 | }; |
120 | ||
121 | TEST_F(SSTDumpToolTest, EmptyFilter) { | |
11fdf7f2 TL |
122 | std::string file_path = MakeFilePath("rocksdb_sst_test.sst"); |
123 | createSST(file_path, table_options_); | |
7c673cae FG |
124 | |
125 | char* usage[3]; | |
11fdf7f2 | 126 | PopulateCommandArgs(file_path, "--command=raw", usage); |
7c673cae FG |
127 | |
128 | rocksdb::SSTDumpTool tool; | |
129 | ASSERT_TRUE(!tool.Run(3, usage)); | |
130 | ||
11fdf7f2 | 131 | cleanup(file_path); |
7c673cae FG |
132 | for (int i = 0; i < 3; i++) { |
133 | delete[] usage[i]; | |
134 | } | |
135 | } | |
136 | ||
137 | TEST_F(SSTDumpToolTest, FilterBlock) { | |
138 | table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, true)); | |
11fdf7f2 TL |
139 | std::string file_path = MakeFilePath("rocksdb_sst_test.sst"); |
140 | createSST(file_path, table_options_); | |
7c673cae FG |
141 | |
142 | char* usage[3]; | |
11fdf7f2 | 143 | PopulateCommandArgs(file_path, "--command=raw", usage); |
7c673cae FG |
144 | |
145 | rocksdb::SSTDumpTool tool; | |
146 | ASSERT_TRUE(!tool.Run(3, usage)); | |
147 | ||
11fdf7f2 | 148 | cleanup(file_path); |
7c673cae FG |
149 | for (int i = 0; i < 3; i++) { |
150 | delete[] usage[i]; | |
151 | } | |
152 | } | |
153 | ||
154 | TEST_F(SSTDumpToolTest, FullFilterBlock) { | |
155 | table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false)); | |
11fdf7f2 TL |
156 | std::string file_path = MakeFilePath("rocksdb_sst_test.sst"); |
157 | createSST(file_path, table_options_); | |
7c673cae FG |
158 | |
159 | char* usage[3]; | |
11fdf7f2 | 160 | PopulateCommandArgs(file_path, "--command=raw", usage); |
7c673cae FG |
161 | |
162 | rocksdb::SSTDumpTool tool; | |
163 | ASSERT_TRUE(!tool.Run(3, usage)); | |
164 | ||
11fdf7f2 | 165 | cleanup(file_path); |
7c673cae FG |
166 | for (int i = 0; i < 3; i++) { |
167 | delete[] usage[i]; | |
168 | } | |
169 | } | |
170 | ||
171 | TEST_F(SSTDumpToolTest, GetProperties) { | |
172 | table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false)); | |
11fdf7f2 TL |
173 | std::string file_path = MakeFilePath("rocksdb_sst_test.sst"); |
174 | createSST(file_path, table_options_); | |
7c673cae FG |
175 | |
176 | char* usage[3]; | |
11fdf7f2 | 177 | PopulateCommandArgs(file_path, "--show_properties", usage); |
7c673cae FG |
178 | |
179 | rocksdb::SSTDumpTool tool; | |
180 | ASSERT_TRUE(!tool.Run(3, usage)); | |
181 | ||
11fdf7f2 | 182 | cleanup(file_path); |
7c673cae FG |
183 | for (int i = 0; i < 3; i++) { |
184 | delete[] usage[i]; | |
185 | } | |
186 | } | |
187 | ||
188 | TEST_F(SSTDumpToolTest, CompressedSizes) { | |
189 | table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false)); | |
11fdf7f2 TL |
190 | std::string file_path = MakeFilePath("rocksdb_sst_test.sst"); |
191 | createSST(file_path, table_options_); | |
7c673cae FG |
192 | |
193 | char* usage[3]; | |
11fdf7f2 | 194 | PopulateCommandArgs(file_path, "--command=recompress", usage); |
7c673cae | 195 | |
7c673cae FG |
196 | rocksdb::SSTDumpTool tool; |
197 | ASSERT_TRUE(!tool.Run(3, usage)); | |
198 | ||
11fdf7f2 | 199 | cleanup(file_path); |
7c673cae FG |
200 | for (int i = 0; i < 3; i++) { |
201 | delete[] usage[i]; | |
202 | } | |
203 | } | |
204 | } // namespace rocksdb | |
205 | ||
206 | int main(int argc, char** argv) { | |
207 | ::testing::InitGoogleTest(&argc, argv); | |
208 | return RUN_ALL_TESTS(); | |
209 | } | |
210 | ||
211 | #else | |
212 | #include <stdio.h> | |
213 | ||
11fdf7f2 | 214 | int main(int /*argc*/, char** /*argv*/) { |
7c673cae FG |
215 | fprintf(stderr, "SKIPPED as SSTDumpTool is not supported in ROCKSDB_LITE\n"); |
216 | return 0; | |
217 | } | |
218 | ||
219 | #endif // !ROCKSDB_LITE return RUN_ALL_TESTS(); |