version_util.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) Facebook, Inc. and its affiliates. 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. #pragma once
  6. #include "db/version_set.h"
  7. namespace ROCKSDB_NAMESPACE {
  8. // Instead of opening a `DB` to perform certain manifest updates, this
  9. // uses the underlying `VersionSet` API to read and modify the MANIFEST. This
  10. // allows us to use the user's real options, while not having to worry about
  11. // the DB persisting new SST files via flush/compaction or attempting to read/
  12. // compact files which may fail, particularly for the file we intend to remove
  13. // (the user may want to remove an already deleted file from MANIFEST).
  14. class OfflineManifestWriter {
  15. public:
  16. OfflineManifestWriter(const DBOptions& options, const std::string& db_path)
  17. : wc_(options.delayed_write_rate),
  18. wb_(options.db_write_buffer_size),
  19. immutable_db_options_(WithDbPath(options, db_path)),
  20. tc_(NewLRUCache(1 << 20 /* capacity */,
  21. options.table_cache_numshardbits)),
  22. versions_(db_path, &immutable_db_options_, sopt_, tc_.get(), &wb_, &wc_,
  23. /*block_cache_tracer=*/nullptr, /*io_tracer=*/nullptr,
  24. /*db_id=*/"", /*db_session_id=*/"",
  25. options.daily_offpeak_time_utc,
  26. /*error_handler=*/nullptr,
  27. /*read_only=*/false) {}
  28. Status Recover(const std::vector<ColumnFamilyDescriptor>& column_families) {
  29. return versions_.Recover(column_families, /*read_only*/ false,
  30. /*db_id*/ nullptr,
  31. /*no_error_if_files_missing*/ true);
  32. }
  33. Status LogAndApply(const ReadOptions& read_options,
  34. const WriteOptions& write_options, ColumnFamilyData* cfd,
  35. VersionEdit* edit,
  36. FSDirectory* dir_contains_current_file) {
  37. // Use `mutex` to imitate a locked DB mutex when calling `LogAndApply()`.
  38. InstrumentedMutex mutex;
  39. mutex.Lock();
  40. Status s = versions_.LogAndApply(cfd, read_options, write_options, edit,
  41. &mutex, dir_contains_current_file,
  42. false /* new_descriptor_log */);
  43. mutex.Unlock();
  44. return s;
  45. }
  46. VersionSet& Versions() { return versions_; }
  47. const ImmutableDBOptions& IOptions() { return immutable_db_options_; }
  48. private:
  49. WriteController wc_;
  50. WriteBufferManager wb_;
  51. ImmutableDBOptions immutable_db_options_;
  52. std::shared_ptr<Cache> tc_;
  53. EnvOptions sopt_;
  54. VersionSet versions_;
  55. static ImmutableDBOptions WithDbPath(const DBOptions& options,
  56. const std::string& db_path) {
  57. ImmutableDBOptions rv(options);
  58. if (rv.db_paths.empty()) {
  59. // `VersionSet` expects options that have been through
  60. // `SanitizeOptions()`, which would sanitize an empty `db_paths`.
  61. rv.db_paths.emplace_back(db_path, 0 /* target_size */);
  62. }
  63. return rv;
  64. }
  65. };
  66. } // namespace ROCKSDB_NAMESPACE