Allow command line tool sst-dump to display table properties.

Summary:
Add option '--show_properties' to sst_dump tool to allow displaying
property block of the specified files.

Test Plan:
Run sst_dump with the following arguments, which covers cases affected by
this diff:

  1. with only --file
  2. with both --file and --show_properties
  3. with --file, --show_properties, and --from

Reviewers: kailiu, xjin

Differential Revision: https://reviews.facebook.net/D15453
This commit is contained in:
Yueh-Hsuan Chiang 2014-01-27 12:45:08 -08:00
parent 3170abd297
commit a46ac92138

View File

@ -15,6 +15,7 @@
#include "rocksdb/env.h"
#include "rocksdb/iterator.h"
#include "rocksdb/table.h"
#include "rocksdb/table_properties.h"
#include "table/block.h"
#include "table/block_builder.h"
#include "table/format.h"
@ -38,22 +39,51 @@ class SstFileReader {
bool has_to,
const std::string& to_key);
Status ReadTableProperties(TableProperties* table_properties);
uint64_t GetReadNumber() { return read_num_; }
private:
Status NewTableReader(const std::string& file_path);
std::string file_name_;
uint64_t read_num_;
bool verify_checksum_;
bool output_hex_;
EnvOptions soptions_;
Status init_result_;
unique_ptr<TableReader> table_reader_;
unique_ptr<RandomAccessFile> file_;
// table_options_ and internal_comparator_ will also be used in
// ReadSequential internally (specifically, seek-related operations)
Options table_options_;
InternalKeyComparator internal_comparator_;
};
SstFileReader::SstFileReader(const std::string& file_path,
bool verify_checksum,
bool output_hex)
:file_name_(file_path), read_num_(0), verify_checksum_(verify_checksum),
output_hex_(output_hex) {
std::cout << "Process " << file_path << "\n";
output_hex_(output_hex), internal_comparator_(BytewiseComparator()) {
fprintf(stdout, "Process %s\n", file_path.c_str());
init_result_ = NewTableReader(file_name_);
}
Status SstFileReader::NewTableReader(const std::string& file_path) {
table_options_.comparator = &internal_comparator_;
Status s = table_options_.env->NewRandomAccessFile(file_path, &file_,
soptions_);
if (!s.ok()) {
return s;
}
uint64_t file_size;
table_options_.env->GetFileSize(file_path, &file_size);
unique_ptr<TableFactory> table_factory;
s = table_options_.table_factory->GetTableReader(table_options_, soptions_,
std::move(file_), file_size,
&table_reader_);
return s;
}
Status SstFileReader::ReadSequential(bool print_kv,
@ -61,29 +91,12 @@ Status SstFileReader::ReadSequential(bool print_kv,
bool has_from,
const std::string& from_key,
bool has_to,
const std::string& to_key)
{
unique_ptr<TableReader> table_reader;
InternalKeyComparator internal_comparator_(BytewiseComparator());
Options table_options;
table_options.comparator = &internal_comparator_;
unique_ptr<RandomAccessFile> file;
Status s = table_options.env->NewRandomAccessFile(file_name_, &file,
soptions_);
if(!s.ok()) {
return s;
}
uint64_t file_size;
table_options.env->GetFileSize(file_name_, &file_size);
unique_ptr<TableFactory> table_factory;
s = table_options.table_factory->GetTableReader(table_options, soptions_,
std::move(file), file_size,
&table_reader);
if(!s.ok()) {
return s;
const std::string& to_key) {
if (!table_reader_) {
return init_result_;
}
Iterator* iter = table_reader->NewIterator(ReadOptions(verify_checksum_,
Iterator* iter = table_reader_->NewIterator(ReadOptions(verify_checksum_,
false));
uint64_t i = 0;
if (has_from) {
@ -113,11 +126,10 @@ Status SstFileReader::ReadSequential(bool print_kv,
}
if (print_kv) {
std::cout << ikey.DebugString(output_hex_)
<< " => "
<< value.ToString(output_hex_) << "\n";
fprintf(stdout, "%s => %s\n",
ikey.DebugString(output_hex_).c_str(),
value.ToString(output_hex_).c_str());
}
}
read_num_ += i;
@ -127,6 +139,15 @@ Status SstFileReader::ReadSequential(bool print_kv,
return ret;
}
Status SstFileReader::ReadTableProperties(TableProperties* table_properties) {
if (!table_reader_) {
return init_result_;
}
*table_properties = table_reader_->GetTableProperties();
return init_result_;
}
} // namespace rocksdb
static void print_help() {
@ -137,7 +158,8 @@ static void print_help() {
" [--input_key_hex]"
" [--from=<user_key>]"
" [--to=<user_key>]"
" [--read_num=NUM]\n");
" [--read_num=NUM]"
" [--show_properties]\n");
}
string HexToString(const string& str) {
@ -158,7 +180,6 @@ string HexToString(const string& str) {
}
int main(int argc, char** argv) {
const char* dir_or_file = nullptr;
uint64_t read_num = -1;
std::string command;
@ -170,10 +191,10 @@ int main(int argc, char** argv) {
bool input_key_hex = false;
bool has_from = false;
bool has_to = false;
bool show_properties = false;
std::string from_key;
std::string to_key;
for (int i = 1; i < argc; i++)
{
for (int i = 1; i < argc; i++) {
if (strncmp(argv[i], "--file=", 7) == 0) {
dir_or_file = argv[i] + 7;
} else if (strcmp(argv[i], "--output_hex") == 0) {
@ -194,6 +215,8 @@ int main(int argc, char** argv) {
} else if (strncmp(argv[i], "--to=", 5) == 0) {
to_key = argv[i] + 5;
has_to = true;
} else if (strcmp(argv[i], "--show_properties") == 0) {
show_properties = true;
} else {
print_help();
exit(1);
@ -225,8 +248,9 @@ int main(int argc, char** argv) {
dir = false;
}
std::cout << "from [" << rocksdb::Slice(from_key).ToString(true)
<< "] to [" << rocksdb::Slice(to_key).ToString(true) << "]\n";
fprintf(stdout, "from [%s] to [%s]\n",
rocksdb::Slice(from_key).ToString(true).c_str(),
rocksdb::Slice(to_key).ToString(true).c_str());
uint64_t total_read = 0;
for (size_t i = 0; i < filenames.size(); i++) {
@ -257,5 +281,20 @@ int main(int argc, char** argv) {
break;
}
}
if (show_properties) {
rocksdb::TableProperties table_properties;
st = reader.ReadTableProperties(&table_properties);
if (!st.ok()) {
fprintf(stderr, "%s: %s\n", filename.c_str(), st.ToString().c_str());
} else {
fprintf(stdout,
"Table Properties:\n"
"------------------------------\n"
" %s", table_properties.ToString("\n ", ": ").c_str());
fprintf(stdout, "# deleted keys: %lu\n",
rocksdb::GetDeletedKeys(
table_properties.user_collected_properties));
}
}
}
}