table_properties.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include "rocksdb/table_properties.h"
  6. #include "port/port.h"
  7. #include "rocksdb/env.h"
  8. #include "rocksdb/iterator.h"
  9. #include "table/block_based/block.h"
  10. #include "table/internal_iterator.h"
  11. #include "table/table_properties_internal.h"
  12. #include "util/string_util.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. const uint32_t TablePropertiesCollectorFactory::Context::kUnknownColumnFamily =
  15. port::kMaxInt32;
  16. namespace {
  17. void AppendProperty(
  18. std::string& props,
  19. const std::string& key,
  20. const std::string& value,
  21. const std::string& prop_delim,
  22. const std::string& kv_delim) {
  23. props.append(key);
  24. props.append(kv_delim);
  25. props.append(value);
  26. props.append(prop_delim);
  27. }
  28. template <class TValue>
  29. void AppendProperty(
  30. std::string& props,
  31. const std::string& key,
  32. const TValue& value,
  33. const std::string& prop_delim,
  34. const std::string& kv_delim) {
  35. AppendProperty(
  36. props, key, ToString(value), prop_delim, kv_delim
  37. );
  38. }
  39. // Seek to the specified meta block.
  40. // Return true if it successfully seeks to that block.
  41. Status SeekToMetaBlock(InternalIterator* meta_iter,
  42. const std::string& block_name, bool* is_found,
  43. BlockHandle* block_handle = nullptr) {
  44. if (block_handle != nullptr) {
  45. *block_handle = BlockHandle::NullBlockHandle();
  46. }
  47. *is_found = true;
  48. meta_iter->Seek(block_name);
  49. if (meta_iter->status().ok()) {
  50. if (meta_iter->Valid() && meta_iter->key() == block_name) {
  51. *is_found = true;
  52. if (block_handle) {
  53. Slice v = meta_iter->value();
  54. return block_handle->DecodeFrom(&v);
  55. }
  56. } else {
  57. *is_found = false;
  58. return Status::OK();
  59. }
  60. }
  61. return meta_iter->status();
  62. }
  63. }
  64. std::string TableProperties::ToString(
  65. const std::string& prop_delim,
  66. const std::string& kv_delim) const {
  67. std::string result;
  68. result.reserve(1024);
  69. // Basic Info
  70. AppendProperty(result, "# data blocks", num_data_blocks, prop_delim,
  71. kv_delim);
  72. AppendProperty(result, "# entries", num_entries, prop_delim, kv_delim);
  73. AppendProperty(result, "# deletions", num_deletions, prop_delim, kv_delim);
  74. AppendProperty(result, "# merge operands", num_merge_operands, prop_delim,
  75. kv_delim);
  76. AppendProperty(result, "# range deletions", num_range_deletions, prop_delim,
  77. kv_delim);
  78. AppendProperty(result, "raw key size", raw_key_size, prop_delim, kv_delim);
  79. AppendProperty(result, "raw average key size",
  80. num_entries != 0 ? 1.0 * raw_key_size / num_entries : 0.0,
  81. prop_delim, kv_delim);
  82. AppendProperty(result, "raw value size", raw_value_size, prop_delim,
  83. kv_delim);
  84. AppendProperty(result, "raw average value size",
  85. num_entries != 0 ? 1.0 * raw_value_size / num_entries : 0.0,
  86. prop_delim, kv_delim);
  87. AppendProperty(result, "data block size", data_size, prop_delim, kv_delim);
  88. char index_block_size_str[80];
  89. snprintf(index_block_size_str, sizeof(index_block_size_str),
  90. "index block size (user-key? %d, delta-value? %d)",
  91. static_cast<int>(index_key_is_user_key),
  92. static_cast<int>(index_value_is_delta_encoded));
  93. AppendProperty(result, index_block_size_str, index_size, prop_delim,
  94. kv_delim);
  95. if (index_partitions != 0) {
  96. AppendProperty(result, "# index partitions", index_partitions, prop_delim,
  97. kv_delim);
  98. AppendProperty(result, "top-level index size", top_level_index_size, prop_delim,
  99. kv_delim);
  100. }
  101. AppendProperty(result, "filter block size", filter_size, prop_delim,
  102. kv_delim);
  103. AppendProperty(result, "(estimated) table size",
  104. data_size + index_size + filter_size, prop_delim, kv_delim);
  105. AppendProperty(
  106. result, "filter policy name",
  107. filter_policy_name.empty() ? std::string("N/A") : filter_policy_name,
  108. prop_delim, kv_delim);
  109. AppendProperty(result, "prefix extractor name",
  110. prefix_extractor_name.empty() ? std::string("N/A")
  111. : prefix_extractor_name,
  112. prop_delim, kv_delim);
  113. AppendProperty(result, "column family ID",
  114. column_family_id ==
  115. ROCKSDB_NAMESPACE::TablePropertiesCollectorFactory::
  116. Context::kUnknownColumnFamily
  117. ? std::string("N/A")
  118. : ROCKSDB_NAMESPACE::ToString(column_family_id),
  119. prop_delim, kv_delim);
  120. AppendProperty(
  121. result, "column family name",
  122. column_family_name.empty() ? std::string("N/A") : column_family_name,
  123. prop_delim, kv_delim);
  124. AppendProperty(result, "comparator name",
  125. comparator_name.empty() ? std::string("N/A") : comparator_name,
  126. prop_delim, kv_delim);
  127. AppendProperty(
  128. result, "merge operator name",
  129. merge_operator_name.empty() ? std::string("N/A") : merge_operator_name,
  130. prop_delim, kv_delim);
  131. AppendProperty(result, "property collectors names",
  132. property_collectors_names.empty() ? std::string("N/A")
  133. : property_collectors_names,
  134. prop_delim, kv_delim);
  135. AppendProperty(
  136. result, "SST file compression algo",
  137. compression_name.empty() ? std::string("N/A") : compression_name,
  138. prop_delim, kv_delim);
  139. AppendProperty(
  140. result, "SST file compression options",
  141. compression_options.empty() ? std::string("N/A") : compression_options,
  142. prop_delim, kv_delim);
  143. AppendProperty(result, "creation time", creation_time, prop_delim, kv_delim);
  144. AppendProperty(result, "time stamp of earliest key", oldest_key_time,
  145. prop_delim, kv_delim);
  146. AppendProperty(result, "file creation time", file_creation_time, prop_delim,
  147. kv_delim);
  148. return result;
  149. }
  150. void TableProperties::Add(const TableProperties& tp) {
  151. data_size += tp.data_size;
  152. index_size += tp.index_size;
  153. index_partitions += tp.index_partitions;
  154. top_level_index_size += tp.top_level_index_size;
  155. index_key_is_user_key += tp.index_key_is_user_key;
  156. index_value_is_delta_encoded += tp.index_value_is_delta_encoded;
  157. filter_size += tp.filter_size;
  158. raw_key_size += tp.raw_key_size;
  159. raw_value_size += tp.raw_value_size;
  160. num_data_blocks += tp.num_data_blocks;
  161. num_entries += tp.num_entries;
  162. num_deletions += tp.num_deletions;
  163. num_merge_operands += tp.num_merge_operands;
  164. num_range_deletions += tp.num_range_deletions;
  165. }
  166. const std::string TablePropertiesNames::kDataSize =
  167. "rocksdb.data.size";
  168. const std::string TablePropertiesNames::kIndexSize =
  169. "rocksdb.index.size";
  170. const std::string TablePropertiesNames::kIndexPartitions =
  171. "rocksdb.index.partitions";
  172. const std::string TablePropertiesNames::kTopLevelIndexSize =
  173. "rocksdb.top-level.index.size";
  174. const std::string TablePropertiesNames::kIndexKeyIsUserKey =
  175. "rocksdb.index.key.is.user.key";
  176. const std::string TablePropertiesNames::kIndexValueIsDeltaEncoded =
  177. "rocksdb.index.value.is.delta.encoded";
  178. const std::string TablePropertiesNames::kFilterSize =
  179. "rocksdb.filter.size";
  180. const std::string TablePropertiesNames::kRawKeySize =
  181. "rocksdb.raw.key.size";
  182. const std::string TablePropertiesNames::kRawValueSize =
  183. "rocksdb.raw.value.size";
  184. const std::string TablePropertiesNames::kNumDataBlocks =
  185. "rocksdb.num.data.blocks";
  186. const std::string TablePropertiesNames::kNumEntries =
  187. "rocksdb.num.entries";
  188. const std::string TablePropertiesNames::kDeletedKeys = "rocksdb.deleted.keys";
  189. const std::string TablePropertiesNames::kMergeOperands =
  190. "rocksdb.merge.operands";
  191. const std::string TablePropertiesNames::kNumRangeDeletions =
  192. "rocksdb.num.range-deletions";
  193. const std::string TablePropertiesNames::kFilterPolicy =
  194. "rocksdb.filter.policy";
  195. const std::string TablePropertiesNames::kFormatVersion =
  196. "rocksdb.format.version";
  197. const std::string TablePropertiesNames::kFixedKeyLen =
  198. "rocksdb.fixed.key.length";
  199. const std::string TablePropertiesNames::kColumnFamilyId =
  200. "rocksdb.column.family.id";
  201. const std::string TablePropertiesNames::kColumnFamilyName =
  202. "rocksdb.column.family.name";
  203. const std::string TablePropertiesNames::kComparator = "rocksdb.comparator";
  204. const std::string TablePropertiesNames::kMergeOperator =
  205. "rocksdb.merge.operator";
  206. const std::string TablePropertiesNames::kPrefixExtractorName =
  207. "rocksdb.prefix.extractor.name";
  208. const std::string TablePropertiesNames::kPropertyCollectors =
  209. "rocksdb.property.collectors";
  210. const std::string TablePropertiesNames::kCompression = "rocksdb.compression";
  211. const std::string TablePropertiesNames::kCompressionOptions =
  212. "rocksdb.compression_options";
  213. const std::string TablePropertiesNames::kCreationTime = "rocksdb.creation.time";
  214. const std::string TablePropertiesNames::kOldestKeyTime =
  215. "rocksdb.oldest.key.time";
  216. const std::string TablePropertiesNames::kFileCreationTime =
  217. "rocksdb.file.creation.time";
  218. extern const std::string kPropertiesBlock = "rocksdb.properties";
  219. // Old property block name for backward compatibility
  220. extern const std::string kPropertiesBlockOldName = "rocksdb.stats";
  221. extern const std::string kCompressionDictBlock = "rocksdb.compression_dict";
  222. extern const std::string kRangeDelBlock = "rocksdb.range_del";
  223. // Seek to the properties block.
  224. // Return true if it successfully seeks to the properties block.
  225. Status SeekToPropertiesBlock(InternalIterator* meta_iter, bool* is_found) {
  226. Status status = SeekToMetaBlock(meta_iter, kPropertiesBlock, is_found);
  227. if (!*is_found && status.ok()) {
  228. status = SeekToMetaBlock(meta_iter, kPropertiesBlockOldName, is_found);
  229. }
  230. return status;
  231. }
  232. // Seek to the compression dictionary block.
  233. // Return true if it successfully seeks to that block.
  234. Status SeekToCompressionDictBlock(InternalIterator* meta_iter, bool* is_found,
  235. BlockHandle* block_handle) {
  236. return SeekToMetaBlock(meta_iter, kCompressionDictBlock, is_found, block_handle);
  237. }
  238. Status SeekToRangeDelBlock(InternalIterator* meta_iter, bool* is_found,
  239. BlockHandle* block_handle = nullptr) {
  240. return SeekToMetaBlock(meta_iter, kRangeDelBlock, is_found, block_handle);
  241. }
  242. } // namespace ROCKSDB_NAMESPACE