blob_dump.cc 3.1 KB

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