ldb_tool.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #include "rocksdb/ldb_tool.h"
  7. #include "rocksdb/utilities/ldb_cmd.h"
  8. #include "tools/ldb_cmd_impl.h"
  9. namespace ROCKSDB_NAMESPACE {
  10. LDBOptions::LDBOptions() = default;
  11. void LDBCommandRunner::PrintHelp(const LDBOptions& ldb_options,
  12. const char* /*exec_name*/, bool to_stderr) {
  13. std::string ret;
  14. ret.append(ldb_options.print_help_header);
  15. ret.append("\n\n");
  16. ret.append("commands MUST specify --" + LDBCommand::ARG_DB +
  17. "=<full_path_to_db_directory> when necessary\n");
  18. ret.append("\n");
  19. ret.append("commands can optionally specify\n");
  20. ret.append(" --" + LDBCommand::ARG_ENV_URI + "=<uri_of_environment> or --" +
  21. LDBCommand::ARG_FS_URI + "=<uri_of_filesystem> if necessary");
  22. ret.append("\n");
  23. ret.append(" --" + LDBCommand::ARG_SECONDARY_PATH +
  24. "=<secondary_path> to open DB as secondary instance. Operations "
  25. "not supported in secondary instance will fail.\n\n");
  26. ret.append(" --" + LDBCommand::ARG_LEADER_PATH +
  27. "=<leader_path> to open DB as a follower instance. Operations "
  28. "not supported in follower instance will fail.\n\n");
  29. ret.append(
  30. "The following optional parameters control if keys/values are "
  31. "input/output as hex or as plain strings:\n");
  32. ret.append(" --" + LDBCommand::ARG_KEY_HEX +
  33. " : Keys are input/output as hex\n");
  34. ret.append(" --" + LDBCommand::ARG_VALUE_HEX +
  35. " : Values are input/output as hex\n");
  36. ret.append(" --" + LDBCommand::ARG_HEX +
  37. " : Both keys and values are input/output as hex\n");
  38. ret.append("\n");
  39. ret.append(
  40. "The following optional parameters control the database "
  41. "internals:\n");
  42. ret.append(
  43. " --" + LDBCommand::ARG_CF_NAME +
  44. "=<string> : name of the column family to operate on. default: default "
  45. "column family\n");
  46. ret.append(" --" + LDBCommand::ARG_TTL +
  47. " with 'put','get','scan','dump','query','batchput'"
  48. " : DB supports ttl and value is internally timestamp-suffixed\n");
  49. ret.append(" --" + LDBCommand::ARG_TRY_LOAD_OPTIONS +
  50. " : Try to load option file from DB. Default to true if " +
  51. LDBCommand::ARG_DB +
  52. " is specified and not creating a new DB and not open as TTL DB. "
  53. "Can be set to false explicitly.\n");
  54. ret.append(" --" + LDBCommand::ARG_DISABLE_CONSISTENCY_CHECKS +
  55. " : Set options.force_consistency_checks = false.\n");
  56. ret.append(" --" + LDBCommand::ARG_IGNORE_UNKNOWN_OPTIONS +
  57. " : Ignore unknown options when loading option file.\n");
  58. ret.append(" --" + LDBCommand::ARG_BLOOM_BITS + "=<int,e.g.:14>\n");
  59. ret.append(" --" + LDBCommand::ARG_FIX_PREFIX_LEN + "=<int,e.g.:14>\n");
  60. ret.append(" --" + LDBCommand::ARG_COMPRESSION_TYPE +
  61. "=<no|snappy|zlib|bzip2|lz4|lz4hc|xpress|zstd>\n");
  62. ret.append(" --" + LDBCommand::ARG_COMPRESSION_MAX_DICT_BYTES +
  63. "=<int,e.g.:16384>\n");
  64. ret.append(" --" + LDBCommand::ARG_BLOCK_SIZE + "=<block_size_in_bytes>\n");
  65. ret.append(" --" + LDBCommand::ARG_AUTO_COMPACTION + "=<true|false>\n");
  66. ret.append(" --" + LDBCommand::ARG_DB_WRITE_BUFFER_SIZE +
  67. "=<int,e.g.:16777216>\n");
  68. ret.append(" --" + LDBCommand::ARG_WRITE_BUFFER_SIZE +
  69. "=<int,e.g.:4194304>\n");
  70. ret.append(" --" + LDBCommand::ARG_FILE_SIZE + "=<int,e.g.:2097152>\n");
  71. ret.append(" --" + LDBCommand::ARG_ENABLE_BLOB_FILES +
  72. " : Enable key-value separation using BlobDB\n");
  73. ret.append(" --" + LDBCommand::ARG_MIN_BLOB_SIZE + "=<int,e.g.:2097152>\n");
  74. ret.append(" --" + LDBCommand::ARG_BLOB_FILE_SIZE + "=<int,e.g.:2097152>\n");
  75. ret.append(" --" + LDBCommand::ARG_BLOB_COMPRESSION_TYPE +
  76. "=<no|snappy|zlib|bzip2|lz4|lz4hc|xpress|zstd>\n");
  77. ret.append(" --" + LDBCommand::ARG_ENABLE_BLOB_GARBAGE_COLLECTION +
  78. " : Enable blob garbage collection\n");
  79. ret.append(" --" + LDBCommand::ARG_BLOB_GARBAGE_COLLECTION_AGE_CUTOFF +
  80. "=<double,e.g.:0.25>\n");
  81. ret.append(" --" + LDBCommand::ARG_BLOB_GARBAGE_COLLECTION_FORCE_THRESHOLD +
  82. "=<double,e.g.:0.25>\n");
  83. ret.append(" --" + LDBCommand::ARG_BLOB_COMPACTION_READAHEAD_SIZE +
  84. "=<int,e.g.:2097152>\n");
  85. ret.append(" --" + LDBCommand::ARG_READ_TIMESTAMP +
  86. "=<uint64_ts, e.g.:323> : read timestamp, required if column "
  87. "family enables timestamp, otherwise invalid if provided.");
  88. ret.append("\n\n");
  89. ret.append("Data Access Commands:\n");
  90. PutCommand::Help(ret);
  91. PutEntityCommand::Help(ret);
  92. GetCommand::Help(ret);
  93. GetEntityCommand::Help(ret);
  94. MultiGetCommand::Help(ret);
  95. MultiGetEntityCommand::Help(ret);
  96. BatchPutCommand::Help(ret);
  97. ScanCommand::Help(ret);
  98. DeleteCommand::Help(ret);
  99. SingleDeleteCommand::Help(ret);
  100. DeleteRangeCommand::Help(ret);
  101. DBQuerierCommand::Help(ret);
  102. ApproxSizeCommand::Help(ret);
  103. CheckConsistencyCommand::Help(ret);
  104. ListFileRangeDeletesCommand::Help(ret);
  105. ret.append("\n\n");
  106. ret.append("Admin Commands:\n");
  107. WALDumperCommand::Help(ret);
  108. CompactorCommand::Help(ret);
  109. ReduceDBLevelsCommand::Help(ret);
  110. ChangeCompactionStyleCommand::Help(ret);
  111. DBDumperCommand::Help(ret);
  112. DBLoaderCommand::Help(ret);
  113. ManifestDumpCommand::Help(ret);
  114. CompactionProgressDumpCommand::Help(ret);
  115. UpdateManifestCommand::Help(ret);
  116. FileChecksumDumpCommand::Help(ret);
  117. GetPropertyCommand::Help(ret);
  118. ListColumnFamiliesCommand::Help(ret);
  119. CreateColumnFamilyCommand::Help(ret);
  120. DropColumnFamilyCommand::Help(ret);
  121. DBFileDumperCommand::Help(ret);
  122. InternalDumpCommand::Help(ret);
  123. DBLiveFilesMetadataDumperCommand::Help(ret);
  124. RepairCommand::Help(ret);
  125. BackupCommand::Help(ret);
  126. RestoreCommand::Help(ret);
  127. CheckPointCommand::Help(ret);
  128. WriteExternalSstFilesCommand::Help(ret);
  129. IngestExternalSstFilesCommand::Help(ret);
  130. UnsafeRemoveSstFileCommand::Help(ret);
  131. fprintf(to_stderr ? stderr : stdout, "%s\n", ret.c_str());
  132. }
  133. int LDBCommandRunner::RunCommand(
  134. int argc, char const* const* argv, const Options& options,
  135. const LDBOptions& ldb_options,
  136. const std::vector<ColumnFamilyDescriptor>* column_families) {
  137. if (argc <= 2) {
  138. if (argc <= 1) {
  139. PrintHelp(ldb_options, argv[0], /*to_stderr*/ true);
  140. return 1;
  141. } else if (std::string(argv[1]) == "--version") {
  142. printf("ldb from RocksDB %d.%d.%d\n", ROCKSDB_MAJOR, ROCKSDB_MINOR,
  143. ROCKSDB_PATCH);
  144. return 0;
  145. } else if (std::string(argv[1]) == "--help") {
  146. PrintHelp(ldb_options, argv[0], /*to_stderr*/ false);
  147. return 0;
  148. } else {
  149. PrintHelp(ldb_options, argv[0], /*to_stderr*/ true);
  150. return 1;
  151. }
  152. }
  153. LDBCommand* cmdObj = LDBCommand::InitFromCmdLineArgs(
  154. argc, argv, options, ldb_options, column_families);
  155. if (cmdObj == nullptr) {
  156. fprintf(stderr, "Unknown command\n");
  157. PrintHelp(ldb_options, argv[0], /*to_stderr*/ true);
  158. return 1;
  159. }
  160. if (!cmdObj->ValidateCmdLineOptions()) {
  161. return 1;
  162. }
  163. cmdObj->Run();
  164. LDBCommandExecuteResult ret = cmdObj->GetExecuteState();
  165. if (!ret.ToString().empty()) {
  166. fprintf(stderr, "%s\n", ret.ToString().c_str());
  167. }
  168. delete cmdObj;
  169. return ret.IsFailed() ? 1 : 0;
  170. }
  171. void LDBTool::Run(int argc, char** argv, Options options,
  172. const LDBOptions& ldb_options,
  173. const std::vector<ColumnFamilyDescriptor>* column_families) {
  174. exit(RunAndReturn(argc, argv, options, ldb_options, column_families));
  175. }
  176. int LDBTool::RunAndReturn(
  177. int argc, char** argv, const Options& options,
  178. const LDBOptions& ldb_options,
  179. const std::vector<ColumnFamilyDescriptor>* column_families) {
  180. return LDBCommandRunner::RunCommand(argc, argv, options, ldb_options,
  181. column_families);
  182. }
  183. } // namespace ROCKSDB_NAMESPACE