ldb_tool.cc 3.9 KB
Newer Older
1 2 3 4 5
//  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.
//
I
Igor Canadi 已提交
6
#ifndef ROCKSDB_LITE
7 8
#include "rocksdb/ldb_tool.h"
#include "util/ldb_cmd.h"
9

10
namespace rocksdb {
11

12 13 14 15 16 17 18 19 20 21 22
class DefaultSliceFormatter : public SliceFormatter {
 public:
  virtual std::string Format(const Slice& s) const override {
    return s.ToString();
  }
};

LDBOptions::LDBOptions()
    : key_formatter(new DefaultSliceFormatter()) {
}

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
class LDBCommandRunner {
public:

  static void PrintHelp(const char* exec_name) {
    string ret;

    ret.append("ldb - LevelDB Tool");
    ret.append("\n\n");
    ret.append("commands MUST specify --" + LDBCommand::ARG_DB +
        "=<full_path_to_db_directory> when necessary\n");
    ret.append("\n");
    ret.append("The following optional parameters control if keys/values are "
        "input/output as hex or as plain strings:\n");
    ret.append("  --" + LDBCommand::ARG_KEY_HEX +
        " : Keys are input/output as hex\n");
    ret.append("  --" + LDBCommand::ARG_VALUE_HEX +
        " : Values are input/output as hex\n");
    ret.append("  --" + LDBCommand::ARG_HEX +
        " : Both keys and values are input/output as hex\n");
    ret.append("\n");

    ret.append("The following optional parameters control the database "
        "internals:\n");
46 47 48
    ret.append("  --" + LDBCommand::ARG_TTL +
        " with 'put','get','scan','dump','query','batchput'"
        " : DB supports ttl and value is internally timestamp-suffixed\n");
49
    ret.append("  --" + LDBCommand::ARG_BLOOM_BITS + "=<int,e.g.:14>\n");
S
sdong 已提交
50
    ret.append("  --" + LDBCommand::ARG_FIX_PREFIX_LEN + "=<int,e.g.:14>\n");
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    ret.append("  --" + LDBCommand::ARG_COMPRESSION_TYPE +
        "=<no|snappy|zlib|bzip2>\n");
    ret.append("  --" + LDBCommand::ARG_BLOCK_SIZE +
        "=<block_size_in_bytes>\n");
    ret.append("  --" + LDBCommand::ARG_AUTO_COMPACTION + "=<true|false>\n");
    ret.append("  --" + LDBCommand::ARG_WRITE_BUFFER_SIZE +
        "=<int,e.g.:4194304>\n");
    ret.append("  --" + LDBCommand::ARG_FILE_SIZE + "=<int,e.g.:2097152>\n");

    ret.append("\n\n");
    ret.append("Data Access Commands:\n");
    PutCommand::Help(ret);
    GetCommand::Help(ret);
    BatchPutCommand::Help(ret);
    ScanCommand::Help(ret);
    DeleteCommand::Help(ret);
    DBQuerierCommand::Help(ret);
    ApproxSizeCommand::Help(ret);
Y
Yiting Li 已提交
69
    CheckConsistencyCommand::Help(ret);
70 71 72 73 74 75

    ret.append("\n\n");
    ret.append("Admin Commands:\n");
    WALDumperCommand::Help(ret);
    CompactorCommand::Help(ret);
    ReduceDBLevelsCommand::Help(ret);
76
    ChangeCompactionStyleCommand::Help(ret);
77 78 79
    DBDumperCommand::Help(ret);
    DBLoaderCommand::Help(ret);
    ManifestDumpCommand::Help(ret);
80
    ListColumnFamiliesCommand::Help(ret);
81
    InternalDumpCommand::Help(ret);
82 83 84 85

    fprintf(stderr, "%s\n", ret.c_str());
  }

86 87
  static void RunCommand(int argc, char** argv, Options options,
                         const LDBOptions& ldb_options) {
88 89 90 91 92
    if (argc <= 2) {
      PrintHelp(argv[0]);
      exit(1);
    }

93 94
    LDBCommand* cmdObj = LDBCommand::InitFromCmdLineArgs(argc, argv, options,
                                                         ldb_options);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    if (cmdObj == nullptr) {
      fprintf(stderr, "Unknown command\n");
      PrintHelp(argv[0]);
      exit(1);
    }

    if (!cmdObj->ValidateCmdLineOptions()) {
      exit(1);
    }

    cmdObj->Run();
    LDBCommandExecuteResult ret = cmdObj->GetExecuteState();
    fprintf(stderr, "%s\n", ret.ToString().c_str());
    delete cmdObj;

    exit(ret.IsFailed());
  }

};


116 117 118
void LDBTool::Run(int argc, char** argv, Options options,
                  const LDBOptions& ldb_options) {
  LDBCommandRunner::RunCommand(argc, argv, options, ldb_options);
119
}
120
} // namespace rocksdb
121

I
Igor Canadi 已提交
122
#endif  // ROCKSDB_LITE