1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
6 #include "rocksdb/table_properties.h"
8 #include "rocksdb/env.h"
9 #include "rocksdb/iterator.h"
10 #include "table/block.h"
11 #include "table/internal_iterator.h"
12 #include "table/table_properties_internal.h"
13 #include "util/string_util.h"
17 const uint32_t TablePropertiesCollectorFactory::Context::kUnknownColumnFamily
=
23 const std::string
& key
,
24 const std::string
& value
,
25 const std::string
& prop_delim
,
26 const std::string
& kv_delim
) {
28 props
.append(kv_delim
);
30 props
.append(prop_delim
);
33 template <class TValue
>
36 const std::string
& key
,
38 const std::string
& prop_delim
,
39 const std::string
& kv_delim
) {
41 props
, key
, ToString(value
), prop_delim
, kv_delim
45 // Seek to the specified meta block.
46 // Return true if it successfully seeks to that block.
47 Status
SeekToMetaBlock(InternalIterator
* meta_iter
,
48 const std::string
& block_name
, bool* is_found
,
49 BlockHandle
* block_handle
= nullptr) {
50 if (block_handle
!= nullptr) {
51 *block_handle
= BlockHandle::NullBlockHandle();
54 meta_iter
->Seek(block_name
);
55 if (meta_iter
->status().ok()) {
56 if (meta_iter
->Valid() && meta_iter
->key() == block_name
) {
59 Slice v
= meta_iter
->value();
60 return block_handle
->DecodeFrom(&v
);
67 return meta_iter
->status();
71 std::string
TableProperties::ToString(
72 const std::string
& prop_delim
,
73 const std::string
& kv_delim
) const {
78 AppendProperty(result
, "# data blocks", num_data_blocks
, prop_delim
,
80 AppendProperty(result
, "# entries", num_entries
, prop_delim
, kv_delim
);
82 AppendProperty(result
, "raw key size", raw_key_size
, prop_delim
, kv_delim
);
83 AppendProperty(result
, "raw average key size",
84 num_entries
!= 0 ? 1.0 * raw_key_size
/ num_entries
: 0.0,
85 prop_delim
, kv_delim
);
86 AppendProperty(result
, "raw value size", raw_value_size
, prop_delim
,
88 AppendProperty(result
, "raw average value size",
89 num_entries
!= 0 ? 1.0 * raw_value_size
/ num_entries
: 0.0,
90 prop_delim
, kv_delim
);
92 AppendProperty(result
, "data block size", data_size
, prop_delim
, kv_delim
);
93 AppendProperty(result
, "index block size", index_size
, prop_delim
, kv_delim
);
94 AppendProperty(result
, "filter block size", filter_size
, prop_delim
,
96 AppendProperty(result
, "(estimated) table size",
97 data_size
+ index_size
+ filter_size
, prop_delim
, kv_delim
);
100 result
, "filter policy name",
101 filter_policy_name
.empty() ? std::string("N/A") : filter_policy_name
,
102 prop_delim
, kv_delim
);
104 AppendProperty(result
, "column family ID",
105 column_family_id
== rocksdb::TablePropertiesCollectorFactory::
106 Context::kUnknownColumnFamily
108 : rocksdb::ToString(column_family_id
),
109 prop_delim
, kv_delim
);
111 result
, "column family name",
112 column_family_name
.empty() ? std::string("N/A") : column_family_name
,
113 prop_delim
, kv_delim
);
115 AppendProperty(result
, "comparator name",
116 comparator_name
.empty() ? std::string("N/A") : comparator_name
,
117 prop_delim
, kv_delim
);
120 result
, "merge operator name",
121 merge_operator_name
.empty() ? std::string("N/A") : merge_operator_name
,
122 prop_delim
, kv_delim
);
124 AppendProperty(result
, "property collectors names",
125 property_collectors_names
.empty() ? std::string("N/A")
126 : property_collectors_names
,
127 prop_delim
, kv_delim
);
130 result
, "SST file compression algo",
131 compression_name
.empty() ? std::string("N/A") : compression_name
,
132 prop_delim
, kv_delim
);
137 void TableProperties::Add(const TableProperties
& tp
) {
138 data_size
+= tp
.data_size
;
139 index_size
+= tp
.index_size
;
140 filter_size
+= tp
.filter_size
;
141 raw_key_size
+= tp
.raw_key_size
;
142 raw_value_size
+= tp
.raw_value_size
;
143 num_data_blocks
+= tp
.num_data_blocks
;
144 num_entries
+= tp
.num_entries
;
147 const std::string
TablePropertiesNames::kDataSize
=
149 const std::string
TablePropertiesNames::kIndexSize
=
150 "rocksdb.index.size";
151 const std::string
TablePropertiesNames::kFilterSize
=
152 "rocksdb.filter.size";
153 const std::string
TablePropertiesNames::kRawKeySize
=
154 "rocksdb.raw.key.size";
155 const std::string
TablePropertiesNames::kRawValueSize
=
156 "rocksdb.raw.value.size";
157 const std::string
TablePropertiesNames::kNumDataBlocks
=
158 "rocksdb.num.data.blocks";
159 const std::string
TablePropertiesNames::kNumEntries
=
160 "rocksdb.num.entries";
161 const std::string
TablePropertiesNames::kFilterPolicy
=
162 "rocksdb.filter.policy";
163 const std::string
TablePropertiesNames::kFormatVersion
=
164 "rocksdb.format.version";
165 const std::string
TablePropertiesNames::kFixedKeyLen
=
166 "rocksdb.fixed.key.length";
167 const std::string
TablePropertiesNames::kColumnFamilyId
=
168 "rocksdb.column.family.id";
169 const std::string
TablePropertiesNames::kColumnFamilyName
=
170 "rocksdb.column.family.name";
171 const std::string
TablePropertiesNames::kComparator
= "rocksdb.comparator";
172 const std::string
TablePropertiesNames::kMergeOperator
=
173 "rocksdb.merge.operator";
174 const std::string
TablePropertiesNames::kPrefixExtractorName
=
175 "rocksdb.prefix.extractor.name";
176 const std::string
TablePropertiesNames::kPropertyCollectors
=
177 "rocksdb.property.collectors";
178 const std::string
TablePropertiesNames::kCompression
= "rocksdb.compression";
180 extern const std::string kPropertiesBlock
= "rocksdb.properties";
181 // Old property block name for backward compatibility
182 extern const std::string kPropertiesBlockOldName
= "rocksdb.stats";
183 extern const std::string kCompressionDictBlock
= "rocksdb.compression_dict";
184 extern const std::string kRangeDelBlock
= "rocksdb.range_del";
186 // Seek to the properties block.
187 // Return true if it successfully seeks to the properties block.
188 Status
SeekToPropertiesBlock(InternalIterator
* meta_iter
, bool* is_found
) {
189 Status status
= SeekToMetaBlock(meta_iter
, kPropertiesBlock
, is_found
);
190 if (!*is_found
&& status
.ok()) {
191 status
= SeekToMetaBlock(meta_iter
, kPropertiesBlockOldName
, is_found
);
196 // Seek to the compression dictionary block.
197 // Return true if it successfully seeks to that block.
198 Status
SeekToCompressionDictBlock(InternalIterator
* meta_iter
, bool* is_found
) {
199 return SeekToMetaBlock(meta_iter
, kCompressionDictBlock
, is_found
);
202 Status
SeekToRangeDelBlock(InternalIterator
* meta_iter
, bool* is_found
,
203 BlockHandle
* block_handle
= nullptr) {
204 return SeekToMetaBlock(meta_iter
, kRangeDelBlock
, is_found
, block_handle
);
207 } // namespace rocksdb