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).
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.
13 #include "rocksdb/sst_dump_tool.h"
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"
24 const uint32_t optLength
= 100;
27 static std::string
MakeKey(int i
) {
29 snprintf(buf
, sizeof(buf
), "k_%04d", i
);
30 InternalKey
key(std::string(buf
), 0, ValueType::kTypeValue
);
31 return key
.Encode().ToString();
34 static std::string
MakeValue(int i
) {
36 snprintf(buf
, sizeof(buf
), "v_%04d", i
);
37 InternalKey
key(std::string(buf
), 0, ValueType::kTypeValue
);
38 return key
.Encode().ToString();
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
));
46 unique_ptr
<WritableFile
> file
;
47 Env
* env
= Env::Default();
48 EnvOptions env_options
;
49 ReadOptions read_options
;
51 const ImmutableCFOptions
imoptions(opts
);
52 const MutableCFOptions
moptions(opts
);
53 rocksdb::InternalKeyComparator
ikc(opts
.comparator
);
54 unique_ptr
<TableBuilder
> tb
;
56 ASSERT_OK(env
->NewWritableFile(file_name
, &file
, env_options
));
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(
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
,
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
));
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
);
92 // Test for sst dump tool "raw" mode
93 class SSTDumpToolTest
: public testing::Test
{
97 BlockBasedTableOptions table_options_
;
99 SSTDumpToolTest() { testDir_
= test::TmpDir(); }
101 ~SSTDumpToolTest() {}
103 std::string
MakeFilePath(const std::string
& file_name
) const {
104 std::string
path(testDir_
);
105 path
.append("/").append(file_name
);
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
];
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());
121 TEST_F(SSTDumpToolTest
, EmptyFilter
) {
122 std::string file_path
= MakeFilePath("rocksdb_sst_test.sst");
123 createSST(file_path
, table_options_
);
126 PopulateCommandArgs(file_path
, "--command=raw", usage
);
128 rocksdb::SSTDumpTool tool
;
129 ASSERT_TRUE(!tool
.Run(3, usage
));
132 for (int i
= 0; i
< 3; i
++) {
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_
);
143 PopulateCommandArgs(file_path
, "--command=raw", usage
);
145 rocksdb::SSTDumpTool tool
;
146 ASSERT_TRUE(!tool
.Run(3, usage
));
149 for (int i
= 0; i
< 3; i
++) {
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_
);
160 PopulateCommandArgs(file_path
, "--command=raw", usage
);
162 rocksdb::SSTDumpTool tool
;
163 ASSERT_TRUE(!tool
.Run(3, usage
));
166 for (int i
= 0; i
< 3; i
++) {
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_
);
177 PopulateCommandArgs(file_path
, "--show_properties", usage
);
179 rocksdb::SSTDumpTool tool
;
180 ASSERT_TRUE(!tool
.Run(3, usage
));
183 for (int i
= 0; i
< 3; i
++) {
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_
);
194 PopulateCommandArgs(file_path
, "--command=recompress", usage
);
196 rocksdb::SSTDumpTool tool
;
197 ASSERT_TRUE(!tool
.Run(3, usage
));
200 for (int i
= 0; i
< 3; i
++) {
204 } // namespace rocksdb
206 int main(int argc
, char** argv
) {
207 ::testing::InitGoogleTest(&argc
, argv
);
208 return RUN_ALL_TESTS();
214 int main(int /*argc*/, char** /*argv*/) {
215 fprintf(stderr
, "SKIPPED as SSTDumpTool is not supported in ROCKSDB_LITE\n");
219 #endif // !ROCKSDB_LITE return RUN_ALL_TESTS();