convenience.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/convenience.h"
  7. #include "db/convenience_impl.h"
  8. #include "db/db_impl/db_impl.h"
  9. #include "util/cast_util.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. void CancelAllBackgroundWork(DB* db, bool wait) {
  12. (static_cast_with_check<DBImpl>(db->GetRootDB()))
  13. ->CancelAllBackgroundWork(wait);
  14. }
  15. Status DeleteFilesInRange(DB* db, ColumnFamilyHandle* column_family,
  16. const Slice* begin, const Slice* end,
  17. bool include_end) {
  18. RangePtr range(begin, end);
  19. return DeleteFilesInRanges(db, column_family, &range, 1, include_end);
  20. }
  21. Status DeleteFilesInRanges(DB* db, ColumnFamilyHandle* column_family,
  22. const RangePtr* ranges, size_t n, bool include_end) {
  23. std::vector<RangeOpt> range_opts(n);
  24. for (size_t i = 0; i < n; ++i) {
  25. range_opts[i] = {OptSlice::CopyFromPtr(ranges[i].start),
  26. OptSlice::CopyFromPtr(ranges[i].limit)};
  27. }
  28. return DeleteFilesInRanges(db, column_family, range_opts.data(), n,
  29. include_end);
  30. }
  31. Status DeleteFilesInRanges(DB* db, ColumnFamilyHandle* column_family,
  32. const RangeOpt* ranges, size_t n, bool include_end) {
  33. return (static_cast_with_check<DBImpl>(db->GetRootDB()))
  34. ->DeleteFilesInRanges(column_family, ranges, n, include_end);
  35. }
  36. Status VerifySstFileChecksum(const Options& options,
  37. const EnvOptions& env_options,
  38. const std::string& file_path) {
  39. // TODO: plumb Env::IOActivity, Env::IOPriority
  40. const ReadOptions read_options;
  41. return VerifySstFileChecksum(options, env_options, read_options, file_path);
  42. }
  43. Status VerifySstFileChecksum(const Options& options,
  44. const EnvOptions& env_options,
  45. const ReadOptions& _read_options,
  46. const std::string& file_path,
  47. const SequenceNumber& largest_seqno) {
  48. if (_read_options.io_activity != Env::IOActivity::kUnknown) {
  49. return Status::InvalidArgument(
  50. "Can only call VerifySstFileChecksum with `ReadOptions::io_activity` "
  51. "is "
  52. "`Env::IOActivity::kUnknown`");
  53. }
  54. ReadOptions read_options(_read_options);
  55. return VerifySstFileChecksumInternal(options, env_options, read_options,
  56. file_path, largest_seqno);
  57. }
  58. Status VerifySstFileChecksumInternal(const Options& options,
  59. const EnvOptions& env_options,
  60. const ReadOptions& read_options,
  61. const std::string& file_path,
  62. const SequenceNumber& largest_seqno) {
  63. std::unique_ptr<FSRandomAccessFile> file;
  64. uint64_t file_size;
  65. InternalKeyComparator internal_comparator(options.comparator);
  66. ImmutableOptions ioptions(options);
  67. Status s = ioptions.fs->NewRandomAccessFile(
  68. file_path, FileOptions(env_options), &file, nullptr);
  69. if (s.ok()) {
  70. s = ioptions.fs->GetFileSize(file_path, IOOptions(), &file_size, nullptr);
  71. } else {
  72. return s;
  73. }
  74. if (!s.ok()) {
  75. return s;
  76. }
  77. std::unique_ptr<TableReader> table_reader;
  78. std::unique_ptr<RandomAccessFileReader> file_reader(
  79. new RandomAccessFileReader(
  80. std::move(file), file_path, ioptions.clock, nullptr /* io_tracer */,
  81. ioptions.stats /* stats */,
  82. Histograms::SST_READ_MICROS /* hist_type */,
  83. nullptr /* file_read_hist */, ioptions.rate_limiter.get()));
  84. const bool kImmortal = true;
  85. auto reader_options = TableReaderOptions(
  86. ioptions, options.prefix_extractor, options.compression_manager.get(),
  87. env_options, internal_comparator, options.block_protection_bytes_per_key,
  88. false /* skip_filters */, !kImmortal, false /* force_direct_prefetch */,
  89. -1 /* level */);
  90. reader_options.largest_seqno = largest_seqno;
  91. s = options.table_factory->NewTableReader(
  92. read_options, reader_options, std::move(file_reader), file_size,
  93. &table_reader, false /* prefetch_index_and_filter_in_cache */);
  94. if (!s.ok()) {
  95. return s;
  96. }
  97. s = table_reader->VerifyChecksum(read_options,
  98. TableReaderCaller::kUserVerifyChecksum);
  99. return s;
  100. }
  101. } // namespace ROCKSDB_NAMESPACE