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