1 //  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 //
6 #ifndef ROCKSDB_LITE
7 #include "rocksdb/ldb_tool.h"
8 #include "rocksdb/utilities/ldb_cmd.h"
9 #include "tools/ldb_cmd_impl.h"
10 
11 namespace ROCKSDB_NAMESPACE {
12 
LDBOptions()13 LDBOptions::LDBOptions() {}
14 
PrintHelp(const LDBOptions & ldb_options,const char *)15 void LDBCommandRunner::PrintHelp(const LDBOptions& ldb_options,
16                                  const char* /*exec_name*/) {
17   std::string ret;
18 
19   ret.append(ldb_options.print_help_header);
20   ret.append("\n\n");
21   ret.append("commands MUST specify --" + LDBCommand::ARG_DB +
22              "=<full_path_to_db_directory> when necessary\n");
23   ret.append("\n");
24   ret.append("commands can optionally specify --" + LDBCommand::ARG_ENV_URI +
25              "=<uri_of_environment> if necessary\n\n");
26   ret.append(
27       "The following optional parameters control if keys/values are "
28       "input/output as hex or as plain strings:\n");
29   ret.append("  --" + LDBCommand::ARG_KEY_HEX +
30              " : Keys are input/output as hex\n");
31   ret.append("  --" + LDBCommand::ARG_VALUE_HEX +
32              " : Values are input/output as hex\n");
33   ret.append("  --" + LDBCommand::ARG_HEX +
34              " : Both keys and values are input/output as hex\n");
35   ret.append("\n");
36 
37   ret.append(
38       "The following optional parameters control the database "
39       "internals:\n");
40   ret.append(
41       "  --" + LDBCommand::ARG_CF_NAME +
42       "=<string> : name of the column family to operate on. default: default "
43       "column family\n");
44   ret.append("  --" + LDBCommand::ARG_TTL +
45              " with 'put','get','scan','dump','query','batchput'"
46              " : DB supports ttl and value is internally timestamp-suffixed\n");
47   ret.append("  --" + LDBCommand::ARG_TRY_LOAD_OPTIONS +
48              " : Try to load option file from DB.\n");
49   ret.append("  --" + LDBCommand::ARG_IGNORE_UNKNOWN_OPTIONS +
50              " : Ignore unknown options when loading option file.\n");
51   ret.append("  --" + LDBCommand::ARG_BLOOM_BITS + "=<int,e.g.:14>\n");
52   ret.append("  --" + LDBCommand::ARG_FIX_PREFIX_LEN + "=<int,e.g.:14>\n");
53   ret.append("  --" + LDBCommand::ARG_COMPRESSION_TYPE +
54              "=<no|snappy|zlib|bzip2|lz4|lz4hc|xpress|zstd>\n");
55   ret.append("  --" + LDBCommand::ARG_COMPRESSION_MAX_DICT_BYTES +
56              "=<int,e.g.:16384>\n");
57   ret.append("  --" + LDBCommand::ARG_BLOCK_SIZE + "=<block_size_in_bytes>\n");
58   ret.append("  --" + LDBCommand::ARG_AUTO_COMPACTION + "=<true|false>\n");
59   ret.append("  --" + LDBCommand::ARG_DB_WRITE_BUFFER_SIZE +
60              "=<int,e.g.:16777216>\n");
61   ret.append("  --" + LDBCommand::ARG_WRITE_BUFFER_SIZE +
62              "=<int,e.g.:4194304>\n");
63   ret.append("  --" + LDBCommand::ARG_FILE_SIZE + "=<int,e.g.:2097152>\n");
64 
65   ret.append("\n\n");
66   ret.append("Data Access Commands:\n");
67   PutCommand::Help(ret);
68   GetCommand::Help(ret);
69   BatchPutCommand::Help(ret);
70   ScanCommand::Help(ret);
71   DeleteCommand::Help(ret);
72   DeleteRangeCommand::Help(ret);
73   DBQuerierCommand::Help(ret);
74   ApproxSizeCommand::Help(ret);
75   CheckConsistencyCommand::Help(ret);
76   ListFileRangeDeletesCommand::Help(ret);
77 
78   ret.append("\n\n");
79   ret.append("Admin Commands:\n");
80   WALDumperCommand::Help(ret);
81   CompactorCommand::Help(ret);
82   ReduceDBLevelsCommand::Help(ret);
83   ChangeCompactionStyleCommand::Help(ret);
84   DBDumperCommand::Help(ret);
85   DBLoaderCommand::Help(ret);
86   ManifestDumpCommand::Help(ret);
87   FileChecksumDumpCommand::Help(ret);
88   ListColumnFamiliesCommand::Help(ret);
89   CreateColumnFamilyCommand::Help(ret);
90   DropColumnFamilyCommand::Help(ret);
91   DBFileDumperCommand::Help(ret);
92   InternalDumpCommand::Help(ret);
93   RepairCommand::Help(ret);
94   BackupCommand::Help(ret);
95   RestoreCommand::Help(ret);
96   CheckPointCommand::Help(ret);
97   WriteExternalSstFilesCommand::Help(ret);
98   IngestExternalSstFilesCommand::Help(ret);
99 
100   fprintf(stderr, "%s\n", ret.c_str());
101 }
102 
RunCommand(int argc,char ** argv,Options options,const LDBOptions & ldb_options,const std::vector<ColumnFamilyDescriptor> * column_families)103 int LDBCommandRunner::RunCommand(
104     int argc, char** argv, Options options, const LDBOptions& ldb_options,
105     const std::vector<ColumnFamilyDescriptor>* column_families) {
106   if (argc <= 2) {
107     PrintHelp(ldb_options, argv[0]);
108     return 1;
109   }
110 
111   LDBCommand* cmdObj = LDBCommand::InitFromCmdLineArgs(
112       argc, argv, options, ldb_options, column_families);
113   if (cmdObj == nullptr) {
114     fprintf(stderr, "Unknown command\n");
115     PrintHelp(ldb_options, argv[0]);
116     return 1;
117   }
118 
119   if (!cmdObj->ValidateCmdLineOptions()) {
120     return 1;
121   }
122 
123   cmdObj->Run();
124   LDBCommandExecuteResult ret = cmdObj->GetExecuteState();
125   fprintf(stderr, "%s\n", ret.ToString().c_str());
126   delete cmdObj;
127 
128   return ret.IsFailed() ? 1 : 0;
129 }
130 
Run(int argc,char ** argv,Options options,const LDBOptions & ldb_options,const std::vector<ColumnFamilyDescriptor> * column_families)131 void LDBTool::Run(int argc, char** argv, Options options,
132                   const LDBOptions& ldb_options,
133                   const std::vector<ColumnFamilyDescriptor>* column_families) {
134   int error_code = LDBCommandRunner::RunCommand(argc, argv, options,
135                                                 ldb_options, column_families);
136   exit(error_code);
137 }
138 }  // namespace ROCKSDB_NAMESPACE
139 
140 #endif  // ROCKSDB_LITE
141