fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
220 lines
6.2 KiB
220 lines
6.2 KiB
10 years ago
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
||
|
// This source code is licensed under the BSD-style license found in the
|
||
|
// LICENSE file in the root directory of this source tree. An additional grant
|
||
|
// of patent rights can be found in the PATENTS file in the same directory.
|
||
|
//
|
||
|
// Copyright (c) 2012 The LevelDB Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style license that can be
|
||
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
||
|
|
||
10 years ago
|
#ifndef ROCKSDB_LITE
|
||
|
|
||
10 years ago
|
#include <stdint.h>
|
||
|
#include "rocksdb/sst_dump_tool.h"
|
||
|
|
||
|
#include "rocksdb/filter_policy.h"
|
||
|
#include "table/block_based_table_factory.h"
|
||
|
#include "table/table_builder.h"
|
||
10 years ago
|
#include "util/file_reader_writer.h"
|
||
10 years ago
|
#include "util/testharness.h"
|
||
|
#include "util/testutil.h"
|
||
|
|
||
|
namespace rocksdb {
|
||
|
|
||
|
const uint32_t optLength = 100;
|
||
|
|
||
|
namespace {
|
||
|
static std::string MakeKey(int i) {
|
||
|
char buf[100];
|
||
|
snprintf(buf, sizeof(buf), "k_%04d", i);
|
||
|
InternalKey key(std::string(buf), 0, ValueType::kTypeValue);
|
||
|
return key.Encode().ToString();
|
||
|
}
|
||
|
|
||
|
static std::string MakeValue(int i) {
|
||
|
char buf[100];
|
||
|
snprintf(buf, sizeof(buf), "v_%04d", i);
|
||
|
InternalKey key(std::string(buf), 0, ValueType::kTypeValue);
|
||
|
return key.Encode().ToString();
|
||
|
}
|
||
|
|
||
|
void createSST(const std::string& file_name,
|
||
|
const BlockBasedTableOptions& table_options) {
|
||
|
std::shared_ptr<rocksdb::TableFactory> tf;
|
||
|
tf.reset(new rocksdb::BlockBasedTableFactory(table_options));
|
||
|
|
||
|
unique_ptr<WritableFile> file;
|
||
|
Env* env = Env::Default();
|
||
|
EnvOptions env_options;
|
||
|
ReadOptions read_options;
|
||
|
Options opts;
|
||
|
const ImmutableCFOptions imoptions(opts);
|
||
|
rocksdb::InternalKeyComparator ikc(opts.comparator);
|
||
10 years ago
|
unique_ptr<TableBuilder> tb;
|
||
10 years ago
|
|
||
|
env->NewWritableFile(file_name, &file, env_options);
|
||
|
opts.table_factory = tf;
|
||
10 years ago
|
std::vector<std::unique_ptr<IntTblPropCollectorFactory> >
|
||
|
int_tbl_prop_collector_factories;
|
||
10 years ago
|
unique_ptr<WritableFileWriter> file_writer(
|
||
|
new WritableFileWriter(std::move(file), EnvOptions()));
|
||
10 years ago
|
tb.reset(opts.table_factory->NewTableBuilder(
|
||
|
TableBuilderOptions(imoptions, ikc, &int_tbl_prop_collector_factories,
|
||
|
CompressionType::kNoCompression, CompressionOptions(),
|
||
|
false),
|
||
9 years ago
|
TablePropertiesCollectorFactory::Context::kUnknownColumnFamily,
|
||
10 years ago
|
file_writer.get()));
|
||
10 years ago
|
|
||
|
// Populate slightly more than 1K keys
|
||
|
uint32_t num_keys = 1024;
|
||
|
for (uint32_t i = 0; i < num_keys; i++) {
|
||
|
tb->Add(MakeKey(i), MakeValue(i));
|
||
|
}
|
||
|
tb->Finish();
|
||
10 years ago
|
file_writer->Close();
|
||
10 years ago
|
}
|
||
|
|
||
|
void cleanup(const std::string& file_name) {
|
||
|
Env* env = Env::Default();
|
||
|
env->DeleteFile(file_name);
|
||
|
std::string outfile_name = file_name.substr(0, file_name.length() - 4);
|
||
|
outfile_name.append("_dump.txt");
|
||
|
env->DeleteFile(outfile_name);
|
||
|
}
|
||
|
} // namespace
|
||
|
|
||
|
// Test for sst dump tool "raw" mode
|
||
10 years ago
|
class SSTDumpToolTest : public testing::Test {
|
||
10 years ago
|
public:
|
||
|
BlockBasedTableOptions table_options_;
|
||
|
|
||
|
SSTDumpToolTest() {}
|
||
|
|
||
|
~SSTDumpToolTest() {}
|
||
|
};
|
||
|
|
||
10 years ago
|
TEST_F(SSTDumpToolTest, EmptyFilter) {
|
||
10 years ago
|
std::string file_name = "rocksdb_sst_test.sst";
|
||
|
createSST(file_name, table_options_);
|
||
|
|
||
|
char* usage[3];
|
||
|
for (int i = 0; i < 3; i++) {
|
||
|
usage[i] = new char[optLength];
|
||
|
}
|
||
|
snprintf(usage[0], optLength, "./sst_dump");
|
||
|
snprintf(usage[1], optLength, "--command=raw");
|
||
|
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
|
||
|
|
||
|
rocksdb::SSTDumpTool tool;
|
||
|
ASSERT_TRUE(!tool.Run(3, usage));
|
||
|
|
||
|
cleanup(file_name);
|
||
|
for (int i = 0; i < 3; i++) {
|
||
|
delete[] usage[i];
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
TEST_F(SSTDumpToolTest, FilterBlock) {
|
||
10 years ago
|
table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, true));
|
||
|
std::string file_name = "rocksdb_sst_test.sst";
|
||
|
createSST(file_name, table_options_);
|
||
|
|
||
|
char* usage[3];
|
||
|
for (int i = 0; i < 3; i++) {
|
||
|
usage[i] = new char[optLength];
|
||
|
}
|
||
|
snprintf(usage[0], optLength, "./sst_dump");
|
||
|
snprintf(usage[1], optLength, "--command=raw");
|
||
|
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
|
||
|
|
||
|
rocksdb::SSTDumpTool tool;
|
||
|
ASSERT_TRUE(!tool.Run(3, usage));
|
||
|
|
||
|
cleanup(file_name);
|
||
|
for (int i = 0; i < 3; i++) {
|
||
|
delete[] usage[i];
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
TEST_F(SSTDumpToolTest, FullFilterBlock) {
|
||
10 years ago
|
table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
|
||
|
std::string file_name = "rocksdb_sst_test.sst";
|
||
|
createSST(file_name, table_options_);
|
||
|
|
||
|
char* usage[3];
|
||
|
for (int i = 0; i < 3; i++) {
|
||
|
usage[i] = new char[optLength];
|
||
|
}
|
||
|
snprintf(usage[0], optLength, "./sst_dump");
|
||
|
snprintf(usage[1], optLength, "--command=raw");
|
||
|
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
|
||
|
|
||
|
rocksdb::SSTDumpTool tool;
|
||
|
ASSERT_TRUE(!tool.Run(3, usage));
|
||
|
|
||
|
cleanup(file_name);
|
||
|
for (int i = 0; i < 3; i++) {
|
||
|
delete[] usage[i];
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
TEST_F(SSTDumpToolTest, GetProperties) {
|
||
10 years ago
|
table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
|
||
|
std::string file_name = "rocksdb_sst_test.sst";
|
||
|
createSST(file_name, table_options_);
|
||
|
|
||
|
char* usage[3];
|
||
|
for (int i = 0; i < 3; i++) {
|
||
|
usage[i] = new char[optLength];
|
||
|
}
|
||
|
snprintf(usage[0], optLength, "./sst_dump");
|
||
|
snprintf(usage[1], optLength, "--show_properties");
|
||
|
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
|
||
|
|
||
|
rocksdb::SSTDumpTool tool;
|
||
|
ASSERT_TRUE(!tool.Run(3, usage));
|
||
|
|
||
|
cleanup(file_name);
|
||
|
for (int i = 0; i < 3; i++) {
|
||
|
delete[] usage[i];
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
|
TEST_F(SSTDumpToolTest, CompressedSizes) {
|
||
|
table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
|
||
|
std::string file_name = "rocksdb_sst_test.sst";
|
||
|
createSST(file_name, table_options_);
|
||
|
|
||
|
char* usage[3];
|
||
|
for (int i = 0; i < 3; i++) {
|
||
|
usage[i] = new char[optLength];
|
||
|
}
|
||
|
|
||
|
snprintf(usage[0], optLength, "./sst_dump");
|
||
|
snprintf(usage[1], optLength, "--show_compression_sizes");
|
||
|
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
|
||
|
rocksdb::SSTDumpTool tool;
|
||
|
ASSERT_TRUE(!tool.Run(3, usage));
|
||
|
|
||
|
cleanup(file_name);
|
||
|
for (int i = 0; i < 3; i++) {
|
||
|
delete[] usage[i];
|
||
|
}
|
||
|
}
|
||
10 years ago
|
} // namespace rocksdb
|
||
|
|
||
10 years ago
|
int main(int argc, char** argv) {
|
||
|
::testing::InitGoogleTest(&argc, argv);
|
||
|
return RUN_ALL_TESTS();
|
||
|
}
|
||
10 years ago
|
|
||
|
#else
|
||
|
#include <stdio.h>
|
||
|
|
||
|
int main(int argc, char** argv) {
|
||
|
fprintf(stderr, "SKIPPED as SSTDumpTool is not supported in ROCKSDB_LITE\n");
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
#endif // !ROCKSDB_LITE return RUN_ALL_TESTS();
|