rocksdb/tools/sst_dump.cc
Zheng Shao 01d57a33fe sst_dump: Add --output_hex option and output the same format as ldb dump
Summary: Now sst_dump has the same option --output_hex as "ldb dump" and also share the same output format.  So we can do "sst_dump ... | ldb load ..." for an experiment.

Test Plan:
[zshao@dev485 ~/git/rocksdb] ./sst_dump --file=/data/users/zshao/test_leveldb/000005.sst  --output_hex | head -n 2
0000027F4FBE00000101000000000000 ==> D901000000000000000057596F7520726563656976656420746F6461792773207370656369616C20676966742120436C69636B2041636365707420746F207669657720796F75722047696674206265666F72652069742064697361707065617273210000000000000000
000007F9C2D400000102000000000000 ==> D1010000000000000000544974277320676F6F6420746F206265204B696E67E280A6206F7220517565656E212048657265277320796F75722054756573646179204D79737465727920476966742066726F6D20436173746C6556696C6C65219B7B227A636F6465223A22633566306531633039663764222C227470223A22613275222C227A6B223A22663638663061343262666264303966383435666239626235366365396536643024246234506C345139382E734C4D33522169482D4F31315A64794C4B7A4F4653766D7863746534625F2A3968684E3433786521776C427636504A414355795F70222C227473223A313334343936313737357D0000000000000000

Reviewers: dhruba, sheki, emayanke

Reviewed By: dhruba

CC: leveldb

Differential Revision: https://reviews.facebook.net/D7587
2012-12-21 10:44:19 -08:00

167 lines
4.2 KiB
C++

#include "leveldb/table.h"
#include <map>
#include <string>
#include <vector>
#include "db/dbformat.h"
#include "db/memtable.h"
#include "db/write_batch_internal.h"
#include "leveldb/db.h"
#include "leveldb/env.h"
#include "leveldb/iterator.h"
#include "leveldb/table_builder.h"
#include "table/block.h"
#include "table/block_builder.h"
#include "table/format.h"
#include "util/random.h"
#include "util/testharness.h"
#include "util/testutil.h"
namespace leveldb {
class SstFileReader {
public:
SstFileReader(std::string file_name,
bool verify_checksum = false,
bool output_hex = false);
Status ReadSequential(bool print_kv, uint64_t read_num = -1);
uint64_t GetReadNumber() { return read_num_; }
private:
std::string file_name_;
uint64_t read_num_;
bool verify_checksum_;
bool output_hex_;
};
SstFileReader::SstFileReader(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) {
}
Status SstFileReader::ReadSequential(bool print_kv, uint64_t read_num)
{
Table* table;
Options table_options;
RandomAccessFile* file = NULL;
Status s = table_options.env->NewRandomAccessFile(file_name_, &file);
if(!s.ok()) {
return s;
}
uint64_t file_size;
table_options.env->GetFileSize(file_name_, &file_size);
s = Table::Open(table_options, file, file_size, &table);
if(!s.ok()) {
return s;
}
Iterator* iter = table->NewIterator(ReadOptions(verify_checksum_, false));
uint64_t i = 0;
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
Slice key = iter->key();
Slice value = iter->value();
++i;
if (read_num > 0 && i > read_num)
break;
if (print_kv) {
fprintf(stdout, "%s ==> %s\n",
key.ToString(output_hex_).c_str(),
value.ToString(output_hex_).c_str());
}
}
read_num_ += i;
Status ret = iter->status();
delete iter;
return ret;
}
} // namespace leveldb
static void print_help() {
fprintf(stderr,
"sst_dump [--command=check|scan] [--verify_checksum] "
"--file=data_dir_OR_sst_file"
" [--output_hex]"
" [--read_num=NUM]\n");
}
int main(int argc, char** argv) {
const char* dir_or_file = NULL;
uint64_t read_num = -1;
std::string command;
char junk;
uint64_t n;
bool verify_checksum = false;
bool output_hex = false;
for (int i = 1; i < argc; i++)
{
if (strncmp(argv[i], "--file=", 7) == 0) {
dir_or_file = argv[i] + 7;
} else if (strncmp(argv[i], "--output_hex", 12) == 0) {
output_hex = true;
} else if (sscanf(argv[i], "--read_num=%ld%c", &n, &junk) == 1) {
read_num = n;
} else if (strncmp(argv[i], "--verify_checksum",
strlen("--verify_checksum")) == 0) {
verify_checksum = true;
} else if (strncmp(argv[i], "--command=", 10) == 0) {
command = argv[i] + 10;
} else {
print_help();
exit(1);
}
}
if(dir_or_file == NULL) {
print_help();
exit(1);
}
std::vector<std::string> filenames;
leveldb::Env* env = leveldb::Env::Default();
leveldb::Status st = env->GetChildren(dir_or_file, &filenames);
bool dir = true;
if (!st.ok()) {
filenames.clear();
filenames.push_back(dir_or_file);
dir = false;
}
uint64_t total_read = 0;
for (size_t i = 0; i < filenames.size(); i++) {
std::string filename = filenames.at(i);
if (filename.length() <= 4 ||
filename.rfind(".sst") != filename.length() - 4) {
//ignore
continue;
}
if(dir) {
filename = std::string(dir_or_file) + "//" + filename;
}
leveldb::SstFileReader reader(filename, verify_checksum,
output_hex);
leveldb::Status st;
// scan all files in give file path.
if (command == "" || command == "scan" || command == "check") {
st = reader.ReadSequential(command != "check",
read_num > 0 ? (read_num - total_read) : read_num);
if (!st.ok()) {
fprintf(stderr, "%s: %s\n", filename.c_str(),
st.ToString().c_str());
}
total_read += reader.GetReadNumber();
if (read_num > 0 && total_read > read_num) {
break;
}
}
}
}