]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/tools/sst_dump_test.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / rocksdb / tools / sst_dump_test.cc
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).
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);
52 const MutableCFOptions moptions(opts);
53 rocksdb::InternalKeyComparator ikc(opts.comparator);
54 unique_ptr<TableBuilder> tb;
55
56 ASSERT_OK(env->NewWritableFile(file_name, &file, env_options));
57
58 opts.table_factory = tf;
59 std::vector<std::unique_ptr<IntTblPropCollectorFactory> >
60 int_tbl_prop_collector_factories;
61 std::unique_ptr<WritableFileWriter> file_writer(
62 new WritableFileWriter(std::move(file), file_name, EnvOptions()));
63 std::string column_family_name;
64 int unknown_level = -1;
65 tb.reset(opts.table_factory->NewTableBuilder(
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),
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 {
94 std::string testDir_;
95
96 public:
97 BlockBasedTableOptions table_options_;
98
99 SSTDumpToolTest() { testDir_ = test::TmpDir(); }
100
101 ~SSTDumpToolTest() {}
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 }
119 };
120
121 TEST_F(SSTDumpToolTest, EmptyFilter) {
122 std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
123 createSST(file_path, table_options_);
124
125 char* usage[3];
126 PopulateCommandArgs(file_path, "--command=raw", usage);
127
128 rocksdb::SSTDumpTool tool;
129 ASSERT_TRUE(!tool.Run(3, usage));
130
131 cleanup(file_path);
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));
139 std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
140 createSST(file_path, table_options_);
141
142 char* usage[3];
143 PopulateCommandArgs(file_path, "--command=raw", usage);
144
145 rocksdb::SSTDumpTool tool;
146 ASSERT_TRUE(!tool.Run(3, usage));
147
148 cleanup(file_path);
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));
156 std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
157 createSST(file_path, table_options_);
158
159 char* usage[3];
160 PopulateCommandArgs(file_path, "--command=raw", usage);
161
162 rocksdb::SSTDumpTool tool;
163 ASSERT_TRUE(!tool.Run(3, usage));
164
165 cleanup(file_path);
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));
173 std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
174 createSST(file_path, table_options_);
175
176 char* usage[3];
177 PopulateCommandArgs(file_path, "--show_properties", usage);
178
179 rocksdb::SSTDumpTool tool;
180 ASSERT_TRUE(!tool.Run(3, usage));
181
182 cleanup(file_path);
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));
190 std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
191 createSST(file_path, table_options_);
192
193 char* usage[3];
194 PopulateCommandArgs(file_path, "--command=recompress", usage);
195
196 rocksdb::SSTDumpTool tool;
197 ASSERT_TRUE(!tool.Run(3, usage));
198
199 cleanup(file_path);
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
214 int main(int /*argc*/, char** /*argv*/) {
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();