plain_table_factory.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  2. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style license that can be
  4. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  5. #ifndef ROCKSDB_LITE
  6. #include "table/plain/plain_table_factory.h"
  7. #include <stdint.h>
  8. #include <memory>
  9. #include "db/dbformat.h"
  10. #include "options/options_helper.h"
  11. #include "port/port.h"
  12. #include "rocksdb/convenience.h"
  13. #include "table/plain/plain_table_builder.h"
  14. #include "table/plain/plain_table_reader.h"
  15. #include "util/string_util.h"
  16. namespace ROCKSDB_NAMESPACE {
  17. Status PlainTableFactory::NewTableReader(
  18. const TableReaderOptions& table_reader_options,
  19. std::unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size,
  20. std::unique_ptr<TableReader>* table,
  21. bool /*prefetch_index_and_filter_in_cache*/) const {
  22. return PlainTableReader::Open(
  23. table_reader_options.ioptions, table_reader_options.env_options,
  24. table_reader_options.internal_comparator, std::move(file), file_size,
  25. table, table_options_.bloom_bits_per_key, table_options_.hash_table_ratio,
  26. table_options_.index_sparseness, table_options_.huge_page_tlb_size,
  27. table_options_.full_scan_mode, table_reader_options.immortal,
  28. table_reader_options.prefix_extractor);
  29. }
  30. TableBuilder* PlainTableFactory::NewTableBuilder(
  31. const TableBuilderOptions& table_builder_options, uint32_t column_family_id,
  32. WritableFileWriter* file) const {
  33. // Ignore the skip_filters flag. PlainTable format is optimized for small
  34. // in-memory dbs. The skip_filters optimization is not useful for plain
  35. // tables
  36. //
  37. return new PlainTableBuilder(
  38. table_builder_options.ioptions, table_builder_options.moptions,
  39. table_builder_options.int_tbl_prop_collector_factories, column_family_id,
  40. file, table_options_.user_key_len, table_options_.encoding_type,
  41. table_options_.index_sparseness, table_options_.bloom_bits_per_key,
  42. table_builder_options.column_family_name, 6,
  43. table_options_.huge_page_tlb_size, table_options_.hash_table_ratio,
  44. table_options_.store_index_in_file);
  45. }
  46. std::string PlainTableFactory::GetPrintableTableOptions() const {
  47. std::string ret;
  48. ret.reserve(20000);
  49. const int kBufferSize = 200;
  50. char buffer[kBufferSize];
  51. snprintf(buffer, kBufferSize, " user_key_len: %u\n",
  52. table_options_.user_key_len);
  53. ret.append(buffer);
  54. snprintf(buffer, kBufferSize, " bloom_bits_per_key: %d\n",
  55. table_options_.bloom_bits_per_key);
  56. ret.append(buffer);
  57. snprintf(buffer, kBufferSize, " hash_table_ratio: %lf\n",
  58. table_options_.hash_table_ratio);
  59. ret.append(buffer);
  60. snprintf(buffer, kBufferSize, " index_sparseness: %" ROCKSDB_PRIszt "\n",
  61. table_options_.index_sparseness);
  62. ret.append(buffer);
  63. snprintf(buffer, kBufferSize, " huge_page_tlb_size: %" ROCKSDB_PRIszt "\n",
  64. table_options_.huge_page_tlb_size);
  65. ret.append(buffer);
  66. snprintf(buffer, kBufferSize, " encoding_type: %d\n",
  67. table_options_.encoding_type);
  68. ret.append(buffer);
  69. snprintf(buffer, kBufferSize, " full_scan_mode: %d\n",
  70. table_options_.full_scan_mode);
  71. ret.append(buffer);
  72. snprintf(buffer, kBufferSize, " store_index_in_file: %d\n",
  73. table_options_.store_index_in_file);
  74. ret.append(buffer);
  75. return ret;
  76. }
  77. const PlainTableOptions& PlainTableFactory::table_options() const {
  78. return table_options_;
  79. }
  80. Status GetPlainTableOptionsFromString(const PlainTableOptions& table_options,
  81. const std::string& opts_str,
  82. PlainTableOptions* new_table_options) {
  83. std::unordered_map<std::string, std::string> opts_map;
  84. Status s = StringToMap(opts_str, &opts_map);
  85. if (!s.ok()) {
  86. return s;
  87. }
  88. return GetPlainTableOptionsFromMap(table_options, opts_map,
  89. new_table_options);
  90. }
  91. Status GetMemTableRepFactoryFromString(
  92. const std::string& opts_str,
  93. std::unique_ptr<MemTableRepFactory>* new_mem_factory) {
  94. std::vector<std::string> opts_list = StringSplit(opts_str, ':');
  95. size_t len = opts_list.size();
  96. if (opts_list.empty() || opts_list.size() > 2) {
  97. return Status::InvalidArgument("Can't parse memtable_factory option ",
  98. opts_str);
  99. }
  100. MemTableRepFactory* mem_factory = nullptr;
  101. if (opts_list[0] == "skip_list") {
  102. // Expecting format
  103. // skip_list:<lookahead>
  104. if (2 == len) {
  105. size_t lookahead = ParseSizeT(opts_list[1]);
  106. mem_factory = new SkipListFactory(lookahead);
  107. } else if (1 == len) {
  108. mem_factory = new SkipListFactory();
  109. }
  110. } else if (opts_list[0] == "prefix_hash") {
  111. // Expecting format
  112. // prfix_hash:<hash_bucket_count>
  113. if (2 == len) {
  114. size_t hash_bucket_count = ParseSizeT(opts_list[1]);
  115. mem_factory = NewHashSkipListRepFactory(hash_bucket_count);
  116. } else if (1 == len) {
  117. mem_factory = NewHashSkipListRepFactory();
  118. }
  119. } else if (opts_list[0] == "hash_linkedlist") {
  120. // Expecting format
  121. // hash_linkedlist:<hash_bucket_count>
  122. if (2 == len) {
  123. size_t hash_bucket_count = ParseSizeT(opts_list[1]);
  124. mem_factory = NewHashLinkListRepFactory(hash_bucket_count);
  125. } else if (1 == len) {
  126. mem_factory = NewHashLinkListRepFactory();
  127. }
  128. } else if (opts_list[0] == "vector") {
  129. // Expecting format
  130. // vector:<count>
  131. if (2 == len) {
  132. size_t count = ParseSizeT(opts_list[1]);
  133. mem_factory = new VectorRepFactory(count);
  134. } else if (1 == len) {
  135. mem_factory = new VectorRepFactory();
  136. }
  137. } else if (opts_list[0] == "cuckoo") {
  138. return Status::NotSupported(
  139. "cuckoo hash memtable is not supported anymore.");
  140. } else {
  141. return Status::InvalidArgument("Unrecognized memtable_factory option ",
  142. opts_str);
  143. }
  144. if (mem_factory != nullptr) {
  145. new_mem_factory->reset(mem_factory);
  146. }
  147. return Status::OK();
  148. }
  149. std::string ParsePlainTableOptions(const std::string& name,
  150. const std::string& org_value,
  151. PlainTableOptions* new_options,
  152. bool input_strings_escaped = false,
  153. bool ignore_unknown_options = false) {
  154. const std::string& value =
  155. input_strings_escaped ? UnescapeOptionString(org_value) : org_value;
  156. const auto iter = plain_table_type_info.find(name);
  157. if (iter == plain_table_type_info.end()) {
  158. if (ignore_unknown_options) {
  159. return "";
  160. } else {
  161. return "Unrecognized option";
  162. }
  163. }
  164. const auto& opt_info = iter->second;
  165. if (opt_info.verification != OptionVerificationType::kDeprecated &&
  166. !ParseOptionHelper(reinterpret_cast<char*>(new_options) + opt_info.offset,
  167. opt_info.type, value)) {
  168. return "Invalid value";
  169. }
  170. return "";
  171. }
  172. Status GetPlainTableOptionsFromMap(
  173. const PlainTableOptions& table_options,
  174. const std::unordered_map<std::string, std::string>& opts_map,
  175. PlainTableOptions* new_table_options, bool input_strings_escaped,
  176. bool /*ignore_unknown_options*/) {
  177. assert(new_table_options);
  178. *new_table_options = table_options;
  179. for (const auto& o : opts_map) {
  180. auto error_message = ParsePlainTableOptions(
  181. o.first, o.second, new_table_options, input_strings_escaped);
  182. if (error_message != "") {
  183. const auto iter = plain_table_type_info.find(o.first);
  184. if (iter == plain_table_type_info.end() ||
  185. !input_strings_escaped || // !input_strings_escaped indicates
  186. // the old API, where everything is
  187. // parsable.
  188. (iter->second.verification != OptionVerificationType::kByName &&
  189. iter->second.verification !=
  190. OptionVerificationType::kByNameAllowNull &&
  191. iter->second.verification !=
  192. OptionVerificationType::kByNameAllowFromNull &&
  193. iter->second.verification != OptionVerificationType::kDeprecated)) {
  194. // Restore "new_options" to the default "base_options".
  195. *new_table_options = table_options;
  196. return Status::InvalidArgument("Can't parse PlainTableOptions:",
  197. o.first + " " + error_message);
  198. }
  199. }
  200. }
  201. return Status::OK();
  202. }
  203. extern TableFactory* NewPlainTableFactory(const PlainTableOptions& options) {
  204. return new PlainTableFactory(options);
  205. }
  206. const std::string PlainTablePropertyNames::kEncodingType =
  207. "rocksdb.plain.table.encoding.type";
  208. const std::string PlainTablePropertyNames::kBloomVersion =
  209. "rocksdb.plain.table.bloom.version";
  210. const std::string PlainTablePropertyNames::kNumBloomBlocks =
  211. "rocksdb.plain.table.bloom.numblocks";
  212. } // namespace ROCKSDB_NAMESPACE
  213. #endif // ROCKSDB_LITE