block_based_table_factory.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #pragma once
  10. #include <stdint.h>
  11. #include <memory>
  12. #include <string>
  13. #include "cache/cache_reservation_manager.h"
  14. #include "port/port.h"
  15. #include "rocksdb/flush_block_policy.h"
  16. #include "rocksdb/table.h"
  17. namespace ROCKSDB_NAMESPACE {
  18. struct ColumnFamilyOptions;
  19. struct ConfigOptions;
  20. struct DBOptions;
  21. struct EnvOptions;
  22. class BlockBasedTableBuilder;
  23. class RandomAccessFileReader;
  24. class WritableFileWriter;
  25. // TODO: deprecate this class as it can be replaced with
  26. // `FileMetaData::tail_size`
  27. //
  28. // A class used to track actual bytes written from the tail in the recent SST
  29. // file opens, and provide a suggestion for following open.
  30. class TailPrefetchStats {
  31. public:
  32. void RecordEffectiveSize(size_t len);
  33. // 0 indicates no information to determine.
  34. size_t GetSuggestedPrefetchSize();
  35. private:
  36. const static size_t kNumTracked = 32;
  37. size_t records_[kNumTracked];
  38. port::Mutex mutex_;
  39. size_t next_ = 0;
  40. size_t num_records_ = 0;
  41. };
  42. class BlockBasedTableFactory : public TableFactory {
  43. public:
  44. explicit BlockBasedTableFactory(
  45. const BlockBasedTableOptions& table_options = BlockBasedTableOptions());
  46. ~BlockBasedTableFactory() {}
  47. // Method to allow CheckedCast to work for this class
  48. static const char* kClassName() { return kBlockBasedTableName(); }
  49. const char* Name() const override { return kBlockBasedTableName(); }
  50. using TableFactory::NewTableReader;
  51. Status NewTableReader(
  52. const ReadOptions& ro, const TableReaderOptions& table_reader_options,
  53. std::unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size,
  54. std::unique_ptr<TableReader>* table_reader,
  55. bool prefetch_index_and_filter_in_cache = true) const override;
  56. TableBuilder* NewTableBuilder(
  57. const TableBuilderOptions& table_builder_options,
  58. WritableFileWriter* file) const override;
  59. // Valdates the specified DB Options.
  60. Status ValidateOptions(const DBOptions& db_opts,
  61. const ColumnFamilyOptions& cf_opts) const override;
  62. Status PrepareOptions(const ConfigOptions& opts) override;
  63. std::string GetPrintableOptions() const override;
  64. bool IsDeleteRangeSupported() const override { return true; }
  65. std::unique_ptr<TableFactory> Clone() const override {
  66. return std::make_unique<BlockBasedTableFactory>(*this);
  67. }
  68. TailPrefetchStats* tail_prefetch_stats() {
  69. return &shared_state_->tail_prefetch_stats;
  70. }
  71. static constexpr int kMinSupportedFormatVersion = 2;
  72. protected:
  73. const void* GetOptionsPtr(const std::string& name) const override;
  74. Status ParseOption(const ConfigOptions& config_options,
  75. const OptionTypeInfo& opt_info,
  76. const std::string& opt_name, const std::string& opt_value,
  77. void* opt_ptr) override;
  78. void InitializeOptions();
  79. private:
  80. BlockBasedTableOptions table_options_;
  81. // Share some state among cloned instances
  82. struct SharedState {
  83. std::shared_ptr<CacheReservationManager> table_reader_cache_res_mgr;
  84. TailPrefetchStats tail_prefetch_stats;
  85. };
  86. std::shared_ptr<SharedState> shared_state_;
  87. };
  88. extern const std::string kHashIndexPrefixesBlock;
  89. extern const std::string kHashIndexPrefixesMetadataBlock;
  90. extern const std::string kPropTrue;
  91. extern const std::string kPropFalse;
  92. } // namespace ROCKSDB_NAMESPACE