2013-04-23 00:20:20 +02:00
|
|
|
#include "table/table.h"
|
2012-08-24 20:28:59 +02:00
|
|
|
|
|
|
|
#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:
|
2013-03-21 18:45:57 +01:00
|
|
|
explicit SstFileReader(std::string file_name,
|
2012-12-21 08:49:32 +01:00
|
|
|
bool verify_checksum = false,
|
|
|
|
bool output_hex = false);
|
2012-08-24 20:28:59 +02:00
|
|
|
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_;
|
2012-08-25 01:28:06 +02:00
|
|
|
bool verify_checksum_;
|
2012-12-21 08:49:32 +01:00
|
|
|
bool output_hex_;
|
2013-06-08 00:35:17 +02:00
|
|
|
EnvOptions soptions_;
|
2012-08-24 20:28:59 +02:00
|
|
|
};
|
|
|
|
|
2012-12-21 08:49:32 +01:00
|
|
|
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) {
|
2012-08-24 20:28:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Status SstFileReader::ReadSequential(bool print_kv, uint64_t read_num)
|
|
|
|
{
|
2013-01-20 11:07:13 +01:00
|
|
|
unique_ptr<Table> table;
|
2012-08-24 20:28:59 +02:00
|
|
|
Options table_options;
|
2013-01-20 11:07:13 +01:00
|
|
|
unique_ptr<RandomAccessFile> file;
|
2013-03-15 01:00:04 +01:00
|
|
|
Status s = table_options.env->NewRandomAccessFile(file_name_, &file,
|
|
|
|
soptions_);
|
2012-08-24 20:28:59 +02:00
|
|
|
if(!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
uint64_t file_size;
|
|
|
|
table_options.env->GetFileSize(file_name_, &file_size);
|
2013-03-15 01:00:04 +01:00
|
|
|
s = Table::Open(table_options, soptions_, std::move(file), file_size, &table);
|
2012-08-24 20:28:59 +02:00
|
|
|
if(!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2012-08-25 01:28:06 +02:00
|
|
|
Iterator* iter = table->NewIterator(ReadOptions(verify_checksum_, false));
|
2012-08-29 21:29:43 +02:00
|
|
|
uint64_t i = 0;
|
2012-08-24 20:28:59 +02:00
|
|
|
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
|
|
|
|
Slice key = iter->key();
|
|
|
|
Slice value = iter->value();
|
|
|
|
++i;
|
2012-08-25 01:28:06 +02:00
|
|
|
if (read_num > 0 && i > read_num)
|
2012-08-24 20:28:59 +02:00
|
|
|
break;
|
|
|
|
if (print_kv) {
|
2012-12-21 08:49:32 +01:00
|
|
|
fprintf(stdout, "%s ==> %s\n",
|
|
|
|
key.ToString(output_hex_).c_str(),
|
|
|
|
value.ToString(output_hex_).c_str());
|
2012-08-24 20:28:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
read_num_ += i;
|
|
|
|
|
|
|
|
Status ret = iter->status();
|
|
|
|
delete iter;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace leveldb
|
|
|
|
|
|
|
|
static void print_help() {
|
|
|
|
fprintf(stderr,
|
2012-08-25 01:28:06 +02:00
|
|
|
"sst_dump [--command=check|scan] [--verify_checksum] "
|
|
|
|
"--file=data_dir_OR_sst_file"
|
2012-12-21 08:49:32 +01:00
|
|
|
" [--output_hex]"
|
2012-08-24 20:28:59 +02:00
|
|
|
" [--read_num=NUM]\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
|
2013-03-21 18:45:57 +01:00
|
|
|
const char* dir_or_file = nullptr;
|
2012-08-24 20:28:59 +02:00
|
|
|
uint64_t read_num = -1;
|
|
|
|
std::string command;
|
|
|
|
|
|
|
|
char junk;
|
|
|
|
uint64_t n;
|
2012-08-25 01:28:06 +02:00
|
|
|
bool verify_checksum = false;
|
2012-12-21 08:49:32 +01:00
|
|
|
bool output_hex = false;
|
2012-08-24 20:28:59 +02:00
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (strncmp(argv[i], "--file=", 7) == 0) {
|
|
|
|
dir_or_file = argv[i] + 7;
|
2012-12-26 02:33:15 +01:00
|
|
|
} else if (strcmp(argv[i], "--output_hex") == 0) {
|
2012-12-21 08:49:32 +01:00
|
|
|
output_hex = true;
|
2012-08-24 20:28:59 +02:00
|
|
|
} else if (sscanf(argv[i], "--read_num=%ld%c", &n, &junk) == 1) {
|
|
|
|
read_num = n;
|
2012-12-26 02:33:15 +01:00
|
|
|
} else if (strcmp(argv[i], "--verify_checksum") == 0) {
|
2012-08-25 01:28:06 +02:00
|
|
|
verify_checksum = true;
|
2012-08-24 20:28:59 +02:00
|
|
|
} else if (strncmp(argv[i], "--command=", 10) == 0) {
|
|
|
|
command = argv[i] + 10;
|
|
|
|
} else {
|
|
|
|
print_help();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 18:45:57 +01:00
|
|
|
if(dir_or_file == nullptr) {
|
2012-08-24 20:28:59 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-29 21:29:43 +02:00
|
|
|
uint64_t total_read = 0;
|
|
|
|
for (size_t i = 0; i < filenames.size(); i++) {
|
2012-08-24 20:28:59 +02:00
|
|
|
std::string filename = filenames.at(i);
|
|
|
|
if (filename.length() <= 4 ||
|
|
|
|
filename.rfind(".sst") != filename.length() - 4) {
|
|
|
|
//ignore
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(dir) {
|
2012-12-21 08:49:32 +01:00
|
|
|
filename = std::string(dir_or_file) + "//" + filename;
|
2012-08-24 20:28:59 +02:00
|
|
|
}
|
2012-12-21 08:49:32 +01:00
|
|
|
leveldb::SstFileReader reader(filename, verify_checksum,
|
|
|
|
output_hex);
|
2012-08-24 20:28:59 +02:00
|
|
|
leveldb::Status st;
|
|
|
|
// scan all files in give file path.
|
|
|
|
if (command == "" || command == "scan" || command == "check") {
|
2012-08-25 01:28:06 +02:00
|
|
|
st = reader.ReadSequential(command != "check",
|
|
|
|
read_num > 0 ? (read_num - total_read) : read_num);
|
2012-08-24 20:28:59 +02:00
|
|
|
if (!st.ok()) {
|
|
|
|
fprintf(stderr, "%s: %s\n", filename.c_str(),
|
|
|
|
st.ToString().c_str());
|
|
|
|
}
|
|
|
|
total_read += reader.GetReadNumber();
|
2012-08-25 01:28:06 +02:00
|
|
|
if (read_num > 0 && total_read > read_num) {
|
2012-08-24 20:28:59 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|