meta_blocks.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 Block;
  22. class BlockBuilder;
  23. class BlockHandle;
  24. class Env;
  25. class Footer;
  26. class Logger;
  27. class RandomAccessFile;
  28. struct TableProperties;
  29. // Meta block names for metaindex
  30. extern const std::string kPropertiesBlockName;
  31. extern const std::string kIndexBlockName;
  32. extern const std::string kPropertiesBlockOldName;
  33. extern const std::string kCompressionDictBlockName;
  34. extern const std::string kRangeDelBlockName;
  35. class MetaIndexBuilder {
  36. public:
  37. MetaIndexBuilder(const MetaIndexBuilder&) = delete;
  38. MetaIndexBuilder& operator=(const MetaIndexBuilder&) = delete;
  39. MetaIndexBuilder();
  40. void Add(const std::string& key, const BlockHandle& handle);
  41. // Write all the added key/value pairs to the block and return the contents
  42. // of the block.
  43. Slice Finish();
  44. private:
  45. // store the sorted key/handle of the metablocks.
  46. stl_wrappers::KVMap meta_block_handles_;
  47. std::unique_ptr<BlockBuilder> meta_index_block_;
  48. };
  49. class PropertyBlockBuilder {
  50. public:
  51. PropertyBlockBuilder(const PropertyBlockBuilder&) = delete;
  52. PropertyBlockBuilder& operator=(const PropertyBlockBuilder&) = delete;
  53. PropertyBlockBuilder();
  54. void AddTableProperty(const TableProperties& props);
  55. void Add(const std::string& key, uint64_t value);
  56. void Add(const std::string& key, const std::string& value);
  57. void Add(const UserCollectedProperties& user_collected_properties);
  58. // Write all the added entries to the block and return the block contents
  59. Slice Finish();
  60. private:
  61. std::unique_ptr<BlockBuilder> properties_block_;
  62. stl_wrappers::KVMap props_;
  63. #ifndef NDEBUG
  64. const Comparator* comparator_ = BytewiseComparator();
  65. Slice last_prop_added_to_block_;
  66. #endif /* !NDEBUG */
  67. };
  68. // Were we encounter any error occurs during user-defined statistics collection,
  69. // we'll write the warning message to info log.
  70. void LogPropertiesCollectionError(Logger* info_log, const std::string& method,
  71. const std::string& name);
  72. // Utility functions help table builder to trigger batch events for user
  73. // defined property collectors.
  74. // Return value indicates if there is any error occurred; if error occurred,
  75. // the warning message will be logged.
  76. // NotifyCollectTableCollectorsOnAdd() triggers the `Add` event for all
  77. // property collectors.
  78. bool NotifyCollectTableCollectorsOnAdd(
  79. const Slice& key, const Slice& value, uint64_t file_size,
  80. const std::vector<std::unique_ptr<InternalTblPropColl>>& collectors,
  81. Logger* info_log);
  82. void NotifyCollectTableCollectorsOnBlockAdd(
  83. const std::vector<std::unique_ptr<InternalTblPropColl>>& collectors,
  84. uint64_t block_uncomp_bytes, uint64_t block_compressed_bytes_fast,
  85. uint64_t block_compressed_bytes_slow);
  86. // NotifyCollectTableCollectorsOnFinish() triggers the `Finish` event for all
  87. // property collectors. The collected properties will be added to `builder`.
  88. // It will also populate `user_collected_properties` and `readable_properties`
  89. // with the collected properties.
  90. bool NotifyCollectTableCollectorsOnFinish(
  91. const std::vector<std::unique_ptr<InternalTblPropColl>>& collectors,
  92. Logger* info_log, PropertyBlockBuilder* builder,
  93. UserCollectedProperties& user_collected_properties,
  94. UserCollectedProperties& readable_properties);
  95. Status ParsePropertiesBlock(
  96. const ImmutableOptions& ioptions, uint64_t offset, Block& block,
  97. std::unique_ptr<TableProperties>& new_table_properties);
  98. // Read table properties from a file using known BlockHandle.
  99. // @returns a status to indicate if the operation succeeded. On success,
  100. // *table_properties will point to a heap-allocated TableProperties
  101. // object, otherwise value of `table_properties` will not be modified.
  102. Status ReadTablePropertiesHelper(
  103. const ReadOptions& ro, const BlockHandle& handle,
  104. RandomAccessFileReader* file, FilePrefetchBuffer* prefetch_buffer,
  105. const Footer& footer, const ImmutableOptions& ioptions,
  106. std::unique_ptr<TableProperties>* table_properties,
  107. MemoryAllocator* memory_allocator = nullptr);
  108. // Read table properties from the properties block of a plain table.
  109. // @returns a status to indicate if the operation succeeded. On success,
  110. // *table_properties will point to a heap-allocated TableProperties
  111. // object, otherwise value of `table_properties` will not be modified.
  112. Status ReadTableProperties(RandomAccessFileReader* file, uint64_t file_size,
  113. uint64_t table_magic_number,
  114. const ImmutableOptions& ioptions,
  115. const ReadOptions& read_options,
  116. std::unique_ptr<TableProperties>* properties,
  117. MemoryAllocator* memory_allocator = nullptr,
  118. FilePrefetchBuffer* prefetch_buffer = nullptr);
  119. // Find the meta block from the meta index block. Returns OK and
  120. // block_handle->IsNull() if not found.
  121. Status FindOptionalMetaBlock(InternalIterator* meta_index_iter,
  122. const std::string& meta_block_name,
  123. BlockHandle* block_handle);
  124. // Find the meta block from the meta index block. Returns Corruption if not
  125. // found.
  126. Status FindMetaBlock(InternalIterator* meta_index_iter,
  127. const std::string& meta_block_name,
  128. BlockHandle* block_handle);
  129. // Find the meta block
  130. Status FindMetaBlockInFile(RandomAccessFileReader* file, uint64_t file_size,
  131. uint64_t table_magic_number,
  132. const ImmutableOptions& ioptions,
  133. const ReadOptions& read_options,
  134. const std::string& meta_block_name,
  135. BlockHandle* block_handle,
  136. MemoryAllocator* memory_allocator = nullptr,
  137. FilePrefetchBuffer* prefetch_buffer = nullptr,
  138. Footer* footer_out = nullptr);
  139. // Read meta block contents
  140. Status ReadMetaIndexBlockInFile(RandomAccessFileReader* file,
  141. uint64_t file_size, uint64_t table_magic_number,
  142. const ImmutableOptions& ioptions,
  143. const ReadOptions& read_options,
  144. BlockContents* block_contents,
  145. MemoryAllocator* memory_allocator = nullptr,
  146. FilePrefetchBuffer* prefetch_buffer = nullptr,
  147. Footer* footer_out = nullptr);
  148. // Read the specified meta block with name meta_block_name
  149. // from `file` and initialize `contents` with contents of this block.
  150. // Return Status::OK in case of success.
  151. Status ReadMetaBlock(RandomAccessFileReader* file,
  152. FilePrefetchBuffer* prefetch_buffer, uint64_t file_size,
  153. uint64_t table_magic_number,
  154. const ImmutableOptions& ioptions,
  155. const ReadOptions& read_options,
  156. const std::string& meta_block_name, BlockType block_type,
  157. BlockContents* contents,
  158. MemoryAllocator* memory_allocator = nullptr);
  159. } // namespace ROCKSDB_NAMESPACE