blob_dump.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #ifndef ROCKSDB_LITE
  6. #include <getopt.h>
  7. #include <cstdio>
  8. #include <string>
  9. #include <unordered_map>
  10. #include "utilities/blob_db/blob_dump_tool.h"
  11. using namespace ROCKSDB_NAMESPACE;
  12. using namespace ROCKSDB_NAMESPACE::blob_db;
  13. int main(int argc, char** argv) {
  14. using DisplayType = BlobDumpTool::DisplayType;
  15. const std::unordered_map<std::string, DisplayType> display_types = {
  16. {"none", DisplayType::kNone},
  17. {"raw", DisplayType::kRaw},
  18. {"hex", DisplayType::kHex},
  19. {"detail", DisplayType::kDetail},
  20. };
  21. const struct option options[] = {
  22. {"help", no_argument, nullptr, 'h'},
  23. {"file", required_argument, nullptr, 'f'},
  24. {"show_key", optional_argument, nullptr, 'k'},
  25. {"show_blob", optional_argument, nullptr, 'b'},
  26. {"show_uncompressed_blob", optional_argument, nullptr, 'r'},
  27. {"show_summary", optional_argument, nullptr, 's'},
  28. };
  29. DisplayType show_key = DisplayType::kRaw;
  30. DisplayType show_blob = DisplayType::kNone;
  31. DisplayType show_uncompressed_blob = DisplayType::kNone;
  32. bool show_summary = false;
  33. std::string file;
  34. while (true) {
  35. int c = getopt_long(argc, argv, "hk::b::f:", options, nullptr);
  36. if (c < 0) {
  37. break;
  38. }
  39. std::string arg_str(optarg ? optarg : "");
  40. switch (c) {
  41. case 'h':
  42. fprintf(stdout,
  43. "Usage: blob_dump --file=filename "
  44. "[--show_key[=none|raw|hex|detail]] "
  45. "[--show_blob[=none|raw|hex|detail]] "
  46. "[--show_uncompressed_blob[=none|raw|hex|detail]] "
  47. "[--show_summary]\n");
  48. return 0;
  49. case 'f':
  50. file = optarg;
  51. break;
  52. case 'k':
  53. if (optarg) {
  54. if (display_types.count(arg_str) == 0) {
  55. fprintf(stderr, "Unrecognized key display type.\n");
  56. return -1;
  57. }
  58. show_key = display_types.at(arg_str);
  59. }
  60. break;
  61. case 'b':
  62. if (optarg) {
  63. if (display_types.count(arg_str) == 0) {
  64. fprintf(stderr, "Unrecognized blob display type.\n");
  65. return -1;
  66. }
  67. show_blob = display_types.at(arg_str);
  68. } else {
  69. show_blob = DisplayType::kHex;
  70. }
  71. break;
  72. case 'r':
  73. if (optarg) {
  74. if (display_types.count(arg_str) == 0) {
  75. fprintf(stderr, "Unrecognized blob display type.\n");
  76. return -1;
  77. }
  78. show_uncompressed_blob = display_types.at(arg_str);
  79. } else {
  80. show_uncompressed_blob = DisplayType::kHex;
  81. }
  82. break;
  83. case 's':
  84. show_summary = true;
  85. break;
  86. default:
  87. fprintf(stderr, "Unrecognized option.\n");
  88. return -1;
  89. }
  90. }
  91. BlobDumpTool tool;
  92. Status s =
  93. tool.Run(file, show_key, show_blob, show_uncompressed_blob, show_summary);
  94. if (!s.ok()) {
  95. fprintf(stderr, "Failed: %s\n", s.ToString().c_str());
  96. return -1;
  97. }
  98. return 0;
  99. }
  100. #else
  101. #include <stdio.h>
  102. int main(int /*argc*/, char** /*argv*/) {
  103. fprintf(stderr, "Not supported in lite mode.\n");
  104. return -1;
  105. }
  106. #endif // ROCKSDB_LITE