meta_blocks.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #pragma once
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "db/builder.h"
  11. #include "db/table_properties_collector.h"
  12. #include "rocksdb/comparator.h"
  13. #include "rocksdb/memory_allocator.h"
  14. #include "rocksdb/options.h"
  15. #include "rocksdb/slice.h"
  16. #include "table/block_based/block_builder.h"
  17. #include "table/block_based/block_type.h"
  18. #include "table/format.h"
  19. #include "util/kv_map.h"
  20. namespace ROCKSDB_NAMESPACE {
  21. class BlockBuilder;
  22. class BlockHandle;
  23. class Env;
  24. class Footer;
  25. class Logger;
  26. class RandomAccessFile;
  27. struct TableProperties;
  28. class MetaIndexBuilder {
  29. public:
  30. MetaIndexBuilder(const MetaIndexBuilder&) = delete;
  31. MetaIndexBuilder& operator=(const MetaIndexBuilder&) = delete;
  32. MetaIndexBuilder();
  33. void Add(const std::string& key, const BlockHandle& handle);
  34. // Write all the added key/value pairs to the block and return the contents
  35. // of the block.
  36. Slice Finish();
  37. private:
  38. // store the sorted key/handle of the metablocks.
  39. stl_wrappers::KVMap meta_block_handles_;
  40. std::unique_ptr<BlockBuilder> meta_index_block_;
  41. };
  42. class PropertyBlockBuilder {
  43. public:
  44. PropertyBlockBuilder(const PropertyBlockBuilder&) = delete;
  45. PropertyBlockBuilder& operator=(const PropertyBlockBuilder&) = delete;
  46. PropertyBlockBuilder();
  47. void AddTableProperty(const TableProperties& props);
  48. void Add(const std::string& key, uint64_t value);
  49. void Add(const std::string& key, const std::string& value);
  50. void Add(const UserCollectedProperties& user_collected_properties);
  51. // Write all the added entries to the block and return the block contents
  52. Slice Finish();
  53. private:
  54. std::unique_ptr<BlockBuilder> properties_block_;
  55. stl_wrappers::KVMap props_;
  56. };
  57. // Were we encounter any error occurs during user-defined statistics collection,
  58. // we'll write the warning message to info log.
  59. void LogPropertiesCollectionError(
  60. Logger* info_log, const std::string& method, const std::string& name);
  61. // Utility functions help table builder to trigger batch events for user
  62. // defined property collectors.
  63. // Return value indicates if there is any error occurred; if error occurred,
  64. // the warning message will be logged.
  65. // NotifyCollectTableCollectorsOnAdd() triggers the `Add` event for all
  66. // property collectors.
  67. bool NotifyCollectTableCollectorsOnAdd(
  68. const Slice& key, const Slice& value, uint64_t file_size,
  69. const std::vector<std::unique_ptr<IntTblPropCollector>>& collectors,
  70. Logger* info_log);
  71. void NotifyCollectTableCollectorsOnBlockAdd(
  72. const std::vector<std::unique_ptr<IntTblPropCollector>>& collectors,
  73. uint64_t blockRawBytes, uint64_t blockCompressedBytesFast,
  74. uint64_t blockCompressedBytesSlow);
  75. // NotifyCollectTableCollectorsOnFinish() triggers the `Finish` event for all
  76. // property collectors. The collected properties will be added to `builder`.
  77. bool NotifyCollectTableCollectorsOnFinish(
  78. const std::vector<std::unique_ptr<IntTblPropCollector>>& collectors,
  79. Logger* info_log, PropertyBlockBuilder* builder);
  80. // Read the properties from the table.
  81. // @returns a status to indicate if the operation succeeded. On success,
  82. // *table_properties will point to a heap-allocated TableProperties
  83. // object, otherwise value of `table_properties` will not be modified.
  84. Status ReadProperties(const Slice& handle_value, RandomAccessFileReader* file,
  85. FilePrefetchBuffer* prefetch_buffer, const Footer& footer,
  86. const ImmutableCFOptions& ioptions,
  87. TableProperties** table_properties, bool verify_checksum,
  88. BlockHandle* block_handle,
  89. CacheAllocationPtr* verification_buf,
  90. bool compression_type_missing = false,
  91. MemoryAllocator* memory_allocator = nullptr);
  92. // Directly read the properties from the properties block of a plain table.
  93. // @returns a status to indicate if the operation succeeded. On success,
  94. // *table_properties will point to a heap-allocated TableProperties
  95. // object, otherwise value of `table_properties` will not be modified.
  96. // certain tables do not have compression_type byte setup properly for
  97. // uncompressed blocks, caller can request to reset compression type by
  98. // passing compression_type_missing = true, the same applies to
  99. // `ReadProperties`, `FindMetaBlock`, and `ReadMetaBlock`
  100. Status ReadTableProperties(RandomAccessFileReader* file, uint64_t file_size,
  101. uint64_t table_magic_number,
  102. const ImmutableCFOptions& ioptions,
  103. TableProperties** properties,
  104. bool compression_type_missing = false,
  105. MemoryAllocator* memory_allocator = nullptr);
  106. // Find the meta block from the meta index block.
  107. Status FindMetaBlock(InternalIterator* meta_index_iter,
  108. const std::string& meta_block_name,
  109. BlockHandle* block_handle);
  110. // Find the meta block
  111. Status FindMetaBlock(RandomAccessFileReader* file, uint64_t file_size,
  112. uint64_t table_magic_number,
  113. const ImmutableCFOptions& ioptions,
  114. const std::string& meta_block_name,
  115. BlockHandle* block_handle,
  116. bool compression_type_missing = false,
  117. MemoryAllocator* memory_allocator = nullptr);
  118. // Read the specified meta block with name meta_block_name
  119. // from `file` and initialize `contents` with contents of this block.
  120. // Return Status::OK in case of success.
  121. Status ReadMetaBlock(RandomAccessFileReader* file,
  122. FilePrefetchBuffer* prefetch_buffer, uint64_t file_size,
  123. uint64_t table_magic_number,
  124. const ImmutableCFOptions& ioptions,
  125. const std::string& meta_block_name, BlockType block_type,
  126. BlockContents* contents,
  127. bool compression_type_missing = false,
  128. MemoryAllocator* memory_allocator = nullptr);
  129. } // namespace ROCKSDB_NAMESPACE