YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/tools/sst_dump_test.cc
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
// Copyright (c) 2012 The LevelDB Authors. All rights reserved.
21
// Use of this source code is governed by a BSD-style license that can be
22
// found in the LICENSE file. See the AUTHORS file for names of contributors.
23
24
#ifndef ROCKSDB_LITE
25
26
#include <stdint.h>
27
28
#include "yb/rocksdb/db/dbformat.h"
29
#include "yb/rocksdb/db/filename.h"
30
#include "yb/rocksdb/filter_policy.h"
31
#include "yb/rocksdb/sst_dump_tool.h"
32
#include "yb/rocksdb/table/block_based_table_factory.h"
33
#include "yb/rocksdb/table/table_builder.h"
34
#include "yb/rocksdb/util/file_reader_writer.h"
35
#include "yb/rocksdb/util/testutil.h"
36
37
#include "yb/util/test_util.h"
38
39
namespace rocksdb {
40
41
const uint32_t optLength = 100;
42
43
namespace {
44
5.12k
static std::string MakeKey(int i) {
45
5.12k
  char buf[100];
46
5.12k
  snprintf(buf, sizeof(buf), "k_%04d", i);
47
5.12k
  InternalKey key(std::string(buf), 0, ValueType::kTypeValue);
48
5.12k
  return key.Encode().ToString();
49
5.12k
}
50
51
5.12k
static std::string MakeValue(int i) {
52
5.12k
  char buf[100];
53
5.12k
  snprintf(buf, sizeof(buf), "v_%04d", i);
54
5.12k
  InternalKey key(std::string(buf), 0, ValueType::kTypeValue);
55
5.12k
  return key.Encode().ToString();
56
5.12k
}
57
58
void createSST(const std::string& base_file_name,
59
5
               const BlockBasedTableOptions& table_options) {
60
5
  std::shared_ptr<rocksdb::TableFactory> tf;
61
5
  tf.reset(new rocksdb::BlockBasedTableFactory(table_options));
62
63
5
  unique_ptr<WritableFile> base_file;
64
5
  Env* env = Env::Default();
65
5
  EnvOptions env_options;
66
5
  ReadOptions read_options;
67
5
  Options opts;
68
5
  const ImmutableCFOptions imoptions(opts);
69
5
  auto ikc = std::make_shared<rocksdb::InternalKeyComparator>(opts.comparator);
70
5
  unique_ptr<TableBuilder> tb;
71
72
5
  ASSERT_OK(env->NewWritableFile(base_file_name, &base_file, env_options));
73
5
  opts.table_factory = tf;
74
5
  std::vector<std::unique_ptr<IntTblPropCollectorFactory> >
75
5
      int_tbl_prop_collector_factories;
76
5
  unique_ptr<WritableFileWriter> base_file_writer(
77
5
      new WritableFileWriter(std::move(base_file), EnvOptions()));
78
5
  unique_ptr<WritableFileWriter> data_file_writer;
79
5
  if (opts.table_factory->IsSplitSstForWriteSupported()) {
80
5
    unique_ptr<WritableFile> data_file;
81
5
    ASSERT_OK(env->NewWritableFile(
82
5
        TableBaseToDataFileName(base_file_name), &data_file, env_options));
83
5
    data_file_writer.reset(new WritableFileWriter(std::move(data_file), EnvOptions()));
84
5
  }
85
5
  tb.reset(opts.table_factory->NewTableBuilder(
86
5
      TableBuilderOptions(imoptions,
87
5
                          ikc,
88
5
                          int_tbl_prop_collector_factories,
89
5
                          CompressionType::kNoCompression,
90
5
                          CompressionOptions(),
91
5
                          /* skip_filters */ false),
92
5
      TablePropertiesCollectorFactory::Context::kUnknownColumnFamily,
93
5
      base_file_writer.get(), data_file_writer.get()));
94
95
  // Populate slightly more than 1K keys
96
5
  uint32_t num_keys = 1024;
97
5.12k
  for (uint32_t i = 0; i < num_keys; i++) {
98
5.12k
    tb->Add(MakeKey(i), MakeValue(i));
99
5.12k
  }
100
5
  ASSERT_OK(tb->Finish());
101
5
  ASSERT_OK(base_file_writer->Close());
102
5
}
103
104
5
void cleanup(const std::string& file_name) {
105
5
  Env* env = Env::Default();
106
5
  ASSERT_OK(env->DeleteFile(file_name));
107
5
  std::string outfile_name = file_name.substr(0, file_name.length() - 4);
108
5
  outfile_name.append("_dump.txt");
109
5
  env->CleanupFile(outfile_name);
110
5
}
111
112
}  // namespace
113
114
// Test for sst dump tool "raw" mode
115
class SSTDumpToolTest : public RocksDBTest {
116
 public:
117
  BlockBasedTableOptions table_options_;
118
119
5
  SSTDumpToolTest() {}
120
121
5
  ~SSTDumpToolTest() {}
122
};
123
124
1
TEST_F(SSTDumpToolTest, EmptyFilter) {
125
1
  std::string file_name = "rocksdb_sst_test.sst";
126
1
  createSST(file_name, table_options_);
127
128
1
  char* usage[3];
129
4
  for (int i = 0; i < 3; i++) {
130
3
    usage[i] = new char[optLength];
131
3
  }
132
1
  snprintf(usage[0], optLength, "./sst_dump");
133
1
  snprintf(usage[1], optLength, "--command=raw");
134
1
  snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
135
136
1
  rocksdb::SSTDumpTool tool(nullptr);
137
1
  ASSERT_TRUE(!tool.Run(3, usage));
138
139
1
  cleanup(file_name);
140
4
  for (int i = 0; i < 3; i++) {
141
3
    delete[] usage[i];
142
3
  }
143
1
}
144
145
1
TEST_F(SSTDumpToolTest, FilterBlock) {
146
1
  table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, true));
147
1
  std::string file_name = "rocksdb_sst_test.sst";
148
1
  createSST(file_name, table_options_);
149
150
1
  char* usage[3];
151
4
  for (int i = 0; i < 3; i++) {
152
3
    usage[i] = new char[optLength];
153
3
  }
154
1
  snprintf(usage[0], optLength, "./sst_dump");
155
1
  snprintf(usage[1], optLength, "--command=raw");
156
1
  snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
157
158
1
  rocksdb::SSTDumpTool tool;
159
1
  ASSERT_TRUE(!tool.Run(3, usage));
160
161
1
  cleanup(file_name);
162
4
  for (int i = 0; i < 3; i++) {
163
3
    delete[] usage[i];
164
3
  }
165
1
}
166
167
1
TEST_F(SSTDumpToolTest, FullFilterBlock) {
168
1
  table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
169
1
  std::string file_name = "rocksdb_sst_test.sst";
170
1
  createSST(file_name, table_options_);
171
172
1
  char* usage[3];
173
4
  for (int i = 0; i < 3; i++) {
174
3
    usage[i] = new char[optLength];
175
3
  }
176
1
  snprintf(usage[0], optLength, "./sst_dump");
177
1
  snprintf(usage[1], optLength, "--command=raw");
178
1
  snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
179
180
1
  rocksdb::SSTDumpTool tool;
181
1
  ASSERT_TRUE(!tool.Run(3, usage));
182
183
1
  cleanup(file_name);
184
4
  for (int i = 0; i < 3; i++) {
185
3
    delete[] usage[i];
186
3
  }
187
1
}
188
189
1
TEST_F(SSTDumpToolTest, GetProperties) {
190
1
  table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
191
1
  std::string file_name = "rocksdb_sst_test.sst";
192
1
  createSST(file_name, table_options_);
193
194
1
  char* usage[3];
195
4
  for (int i = 0; i < 3; i++) {
196
3
    usage[i] = new char[optLength];
197
3
  }
198
1
  snprintf(usage[0], optLength, "./sst_dump");
199
1
  snprintf(usage[1], optLength, "--show_properties");
200
1
  snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
201
202
1
  rocksdb::SSTDumpTool tool;
203
1
  ASSERT_TRUE(!tool.Run(3, usage));
204
205
1
  cleanup(file_name);
206
4
  for (int i = 0; i < 3; i++) {
207
3
    delete[] usage[i];
208
3
  }
209
1
}
210
211
1
TEST_F(SSTDumpToolTest, CompressedSizes) {
212
1
  table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
213
1
  std::string file_name = "rocksdb_sst_test.sst";
214
1
  createSST(file_name, table_options_);
215
216
1
  char* usage[3];
217
4
  for (int i = 0; i < 3; i++) {
218
3
    usage[i] = new char[optLength];
219
3
  }
220
221
1
  snprintf(usage[0], optLength, "./sst_dump");
222
1
  snprintf(usage[1], optLength, "--show_compression_sizes");
223
1
  snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
224
1
  rocksdb::SSTDumpTool tool;
225
1
  ASSERT_TRUE(!tool.Run(3, usage));
226
227
1
  cleanup(file_name);
228
4
  for (int i = 0; i < 3; i++) {
229
3
    delete[] usage[i];
230
3
  }
231
1
}
232
}  // namespace rocksdb
233
234
13.2k
int main(int argc, char** argv) {
235
13.2k
  ::testing::InitGoogleTest(&argc, argv);
236
13.2k
  return RUN_ALL_TESTS();
237
13.2k
}
238
239
#else
240
241
int main(int argc, char** argv) {
242
  fprintf(stderr, "SKIPPED as SSTDumpTool is not supported in ROCKSDB_LITE\n");
243
  return 0;
244
}
245
246
#endif  // !ROCKSDB_LITE  return RUN_ALL_TESTS();