You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.8KB

  1. /** zstreedb2 maint.cxx
  2. (C) 2018 Erik Zscheile
  3. License: GPLv2
  4. **/
  5. #include <stdio.h>
  6. #include <rocksdb/db.h>
  7. #include <memory>
  8. #include <thread>
  9. using namespace std;
  10. static void pdberrmsg(const rocksdb::Status &st, const char *fn) {
  11. auto str = st.ToString();
  12. fprintf(stderr, "zstreedb2maint: ERROR: db %s failed: %s", fn, str.c_str());
  13. }
  14. int main(const int argc, char *argv[]) {
  15. rocksdb::Status st;
  16. rocksdb::Options opts;
  17. opts.IncreaseParallelism(std::max(thread::hardware_concurrency(), 1u));
  18. opts.allow_mmap_reads = true;
  19. opts.allow_mmap_writes = true;
  20. if(argc > 1)
  21. switch(argv[1][0]) {
  22. case 'j':
  23. opts.compression = rocksdb::kBZip2Compression;
  24. break;
  25. case 'J':
  26. opts.compression = rocksdb::kLZ4Compression;
  27. break;
  28. case 's':
  29. opts.compression = rocksdb::kSnappyCompression;
  30. break;
  31. case 'z':
  32. opts.compression = rocksdb::kZlibCompression;
  33. break;
  34. case 'Z':
  35. opts.compression = rocksdb::kZSTD;
  36. break;
  37. case 'h':
  38. puts("USAGE: zstreedb2maint [COMPR]");
  39. puts(" COMPR := one of: j J s z Z");
  40. return 0;
  41. }
  42. opts.bottommost_compression = opts.compression;
  43. std::unique_ptr<rocksdb::DB> db;
  44. {
  45. rocksdb::DB *dbptr;
  46. st = rocksdb::DB::Open(opts, ".", &dbptr);
  47. if(!st.ok()) {
  48. pdberrmsg(st, "open");
  49. st = rocksdb::RepairDB(".", opts);
  50. if(!st.ok())
  51. pdberrmsg(st, "repair");
  52. return 1;
  53. }
  54. db.reset(dbptr);
  55. }
  56. rocksdb::CompactRangeOptions copts;
  57. copts.change_level = true;
  58. copts.bottommost_level_compaction
  59. = rocksdb::BottommostLevelCompaction::kForce;
  60. st = db->CompactRange(copts, nullptr, nullptr);
  61. if(!st.ok()) {
  62. pdberrmsg(st, "compact");
  63. return 1;
  64. }
  65. return 0;
  66. }