version_builder.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. //
  10. #pragma once
  11. #include <memory>
  12. #include "db/version_edit.h"
  13. #include "rocksdb/file_system.h"
  14. #include "rocksdb/metadata.h"
  15. #include "rocksdb/slice_transform.h"
  16. namespace ROCKSDB_NAMESPACE {
  17. struct ImmutableCFOptions;
  18. class TableCache;
  19. class VersionStorageInfo;
  20. class VersionEdit;
  21. struct FileMetaData;
  22. class InternalStats;
  23. class Version;
  24. class VersionSet;
  25. class VersionEditHandler;
  26. class ColumnFamilyData;
  27. class CacheReservationManager;
  28. // A helper class so we can efficiently apply a whole sequence
  29. // of edits to a particular state without creating intermediate
  30. // Versions that contain full copies of the intermediate state.
  31. class VersionBuilder {
  32. public:
  33. VersionBuilder(const FileOptions& file_options,
  34. const ImmutableCFOptions* ioptions, TableCache* table_cache,
  35. VersionStorageInfo* base_vstorage, VersionSet* version_set,
  36. std::shared_ptr<CacheReservationManager>
  37. file_metadata_cache_res_mgr = nullptr,
  38. ColumnFamilyData* cfd = nullptr,
  39. VersionEditHandler* version_edit_handler = nullptr,
  40. bool track_found_and_missing_files = false,
  41. bool allow_incomplete_valid_version = false);
  42. ~VersionBuilder();
  43. bool CheckConsistencyForNumLevels();
  44. Status Apply(const VersionEdit* edit);
  45. // Save the current Version to the provided `vstorage`.
  46. Status SaveTo(VersionStorageInfo* vstorage) const;
  47. // Load all the table handlers for the current Version in the builder.
  48. Status LoadTableHandlers(InternalStats* internal_stats, int max_threads,
  49. bool prefetch_index_and_filter_in_cache,
  50. bool is_initial_load,
  51. const MutableCFOptions& mutable_cf_options,
  52. size_t max_file_size_for_l0_meta_pin,
  53. const ReadOptions& read_options);
  54. //============APIs only used by VersionEditHandlerPointInTime ============//
  55. // Creates a save point for the Version that has been built so far. Subsequent
  56. // VersionEdits applied to the builder will not affect the Version in this
  57. // save point. VersionBuilder currently only supports creating one save point,
  58. // so when `CreateOrReplaceSavePoint` is called again, the previous save point
  59. // is cleared. `ClearSavePoint` can be called explicitly to clear
  60. // the save point too.
  61. void CreateOrReplaceSavePoint();
  62. // The builder can find all the files to build a `Version`. Or if
  63. // `allow_incomplete_valid_version_` is true and the version history is never
  64. // edited in an atomic group, and only a suffix of L0 SST files and their
  65. // associated blob files are missing.
  66. // From the users' perspective, missing a suffix of L0 files means missing the
  67. // user's most recently written data. So the remaining available files still
  68. // presents a valid point in time view, although for some previous time.
  69. // This validity check result will be cached and reused if the Version is not
  70. // updated between two validity checks.
  71. bool ValidVersionAvailable();
  72. bool HasMissingFiles() const;
  73. // When applying a sequence of VersionEdit, intermediate files are the ones
  74. // that are added and then deleted. The caller should clear this intermediate
  75. // files tracking after calling this API. So that the tracking for subsequent
  76. // VersionEdits can start over with a clean state.
  77. std::vector<std::string>& GetAndClearIntermediateFiles();
  78. // Clearing all the found files in this Version.
  79. void ClearFoundFiles();
  80. // Save the Version in the save point to the provided `vstorage`.
  81. // Non-OK status will be returned if there is not a valid save point.
  82. Status SaveSavePointTo(VersionStorageInfo* vstorage) const;
  83. // Load all the table handlers for the Version in the save point.
  84. // Non-OK status will be returned if there is not a valid save point.
  85. Status LoadSavePointTableHandlers(InternalStats* internal_stats,
  86. int max_threads,
  87. bool prefetch_index_and_filter_in_cache,
  88. bool is_initial_load,
  89. const MutableCFOptions& mutable_cf_options,
  90. size_t max_file_size_for_l0_meta_pin,
  91. const ReadOptions& read_options);
  92. void ClearSavePoint();
  93. //======= End of APIs only used by VersionEditPointInTime==========//
  94. private:
  95. class Rep;
  96. std::unique_ptr<Rep> savepoint_;
  97. std::unique_ptr<Rep> rep_;
  98. };
  99. // A wrapper of version builder which references the current version in
  100. // constructor and unref it in the destructor.
  101. // Both of the constructor and destructor need to be called inside DB Mutex.
  102. class BaseReferencedVersionBuilder {
  103. public:
  104. explicit BaseReferencedVersionBuilder(
  105. ColumnFamilyData* cfd, VersionEditHandler* version_edit_handler = nullptr,
  106. bool track_found_and_missing_files = false,
  107. bool allow_incomplete_valid_version = false);
  108. BaseReferencedVersionBuilder(
  109. ColumnFamilyData* cfd, Version* v,
  110. VersionEditHandler* version_edit_handler = nullptr,
  111. bool track_found_and_missing_files = false,
  112. bool allow_incomplete_valid_version = false);
  113. ~BaseReferencedVersionBuilder();
  114. VersionBuilder* version_builder() const { return version_builder_.get(); }
  115. private:
  116. std::unique_ptr<VersionBuilder> version_builder_;
  117. Version* version_;
  118. };
  119. } // namespace ROCKSDB_NAMESPACE