plain_table_factory.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #include "table/plain/plain_table_factory.h"
  6. #include <cstdint>
  7. #include <memory>
  8. #include "db/dbformat.h"
  9. #include "port/port.h"
  10. #include "rocksdb/convenience.h"
  11. #include "rocksdb/utilities/customizable_util.h"
  12. #include "rocksdb/utilities/object_registry.h"
  13. #include "rocksdb/utilities/options_type.h"
  14. #include "table/plain/plain_table_builder.h"
  15. #include "table/plain/plain_table_reader.h"
  16. #include "util/string_util.h"
  17. namespace ROCKSDB_NAMESPACE {
  18. static std::unordered_map<std::string, OptionTypeInfo> plain_table_type_info = {
  19. {"user_key_len",
  20. {offsetof(struct PlainTableOptions, user_key_len), OptionType::kUInt32T,
  21. OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
  22. {"bloom_bits_per_key",
  23. {offsetof(struct PlainTableOptions, bloom_bits_per_key), OptionType::kInt,
  24. OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
  25. {"hash_table_ratio",
  26. {offsetof(struct PlainTableOptions, hash_table_ratio), OptionType::kDouble,
  27. OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
  28. {"index_sparseness",
  29. {offsetof(struct PlainTableOptions, index_sparseness), OptionType::kSizeT,
  30. OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
  31. {"huge_page_tlb_size",
  32. {offsetof(struct PlainTableOptions, huge_page_tlb_size),
  33. OptionType::kSizeT, OptionVerificationType::kNormal,
  34. OptionTypeFlags::kNone}},
  35. {"encoding_type",
  36. {offsetof(struct PlainTableOptions, encoding_type),
  37. OptionType::kEncodingType, OptionVerificationType::kNormal,
  38. OptionTypeFlags::kNone}},
  39. {"full_scan_mode",
  40. {offsetof(struct PlainTableOptions, full_scan_mode), OptionType::kBoolean,
  41. OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
  42. {"store_index_in_file",
  43. {offsetof(struct PlainTableOptions, store_index_in_file),
  44. OptionType::kBoolean, OptionVerificationType::kNormal,
  45. OptionTypeFlags::kNone}},
  46. };
  47. PlainTableFactory::PlainTableFactory(const PlainTableOptions& options)
  48. : table_options_(options) {
  49. RegisterOptions(&table_options_, &plain_table_type_info);
  50. }
  51. Status PlainTableFactory::NewTableReader(
  52. const ReadOptions& /*ro*/, const TableReaderOptions& table_reader_options,
  53. std::unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size,
  54. std::unique_ptr<TableReader>* table,
  55. bool /*prefetch_index_and_filter_in_cache*/) const {
  56. return PlainTableReader::Open(
  57. table_reader_options.ioptions, table_reader_options.env_options,
  58. table_reader_options.internal_comparator, std::move(file), file_size,
  59. table, table_options_.bloom_bits_per_key, table_options_.hash_table_ratio,
  60. table_options_.index_sparseness, table_options_.huge_page_tlb_size,
  61. table_options_.full_scan_mode, table_reader_options.immortal,
  62. table_reader_options.prefix_extractor.get());
  63. }
  64. TableBuilder* PlainTableFactory::NewTableBuilder(
  65. const TableBuilderOptions& table_builder_options,
  66. WritableFileWriter* file) const {
  67. // Ignore the skip_filters flag. PlainTable format is optimized for small
  68. // in-memory dbs. The skip_filters optimization is not useful for plain
  69. // tables
  70. //
  71. return new PlainTableBuilder(
  72. table_builder_options.ioptions, table_builder_options.moptions,
  73. table_builder_options.internal_tbl_prop_coll_factories,
  74. table_builder_options.column_family_id,
  75. table_builder_options.level_at_creation, file,
  76. table_options_.user_key_len, table_options_.encoding_type,
  77. table_options_.index_sparseness, table_options_.bloom_bits_per_key,
  78. table_builder_options.column_family_name, 6,
  79. table_options_.huge_page_tlb_size, table_options_.hash_table_ratio,
  80. table_options_.store_index_in_file, table_builder_options.db_id,
  81. table_builder_options.db_session_id, table_builder_options.cur_file_num);
  82. }
  83. std::string PlainTableFactory::GetPrintableOptions() const {
  84. std::string ret;
  85. ret.reserve(20000);
  86. const int kBufferSize = 200;
  87. char buffer[kBufferSize];
  88. snprintf(buffer, kBufferSize, " user_key_len: %u\n",
  89. table_options_.user_key_len);
  90. ret.append(buffer);
  91. snprintf(buffer, kBufferSize, " bloom_bits_per_key: %d\n",
  92. table_options_.bloom_bits_per_key);
  93. ret.append(buffer);
  94. snprintf(buffer, kBufferSize, " hash_table_ratio: %lf\n",
  95. table_options_.hash_table_ratio);
  96. ret.append(buffer);
  97. snprintf(buffer, kBufferSize, " index_sparseness: %" ROCKSDB_PRIszt "\n",
  98. table_options_.index_sparseness);
  99. ret.append(buffer);
  100. snprintf(buffer, kBufferSize, " huge_page_tlb_size: %" ROCKSDB_PRIszt "\n",
  101. table_options_.huge_page_tlb_size);
  102. ret.append(buffer);
  103. snprintf(buffer, kBufferSize, " encoding_type: %d\n",
  104. table_options_.encoding_type);
  105. ret.append(buffer);
  106. snprintf(buffer, kBufferSize, " full_scan_mode: %d\n",
  107. table_options_.full_scan_mode);
  108. ret.append(buffer);
  109. snprintf(buffer, kBufferSize, " store_index_in_file: %d\n",
  110. table_options_.store_index_in_file);
  111. ret.append(buffer);
  112. return ret;
  113. }
  114. Status GetPlainTableOptionsFromString(const ConfigOptions& config_options,
  115. const PlainTableOptions& table_options,
  116. const std::string& opts_str,
  117. PlainTableOptions* new_table_options) {
  118. std::unordered_map<std::string, std::string> opts_map;
  119. Status s = StringToMap(opts_str, &opts_map);
  120. if (!s.ok()) {
  121. return s;
  122. }
  123. s = GetPlainTableOptionsFromMap(config_options, table_options, opts_map,
  124. new_table_options);
  125. // Translate any errors (NotFound, NotSupported, to InvalidArgument
  126. if (s.ok() || s.IsInvalidArgument()) {
  127. return s;
  128. } else {
  129. return Status::InvalidArgument(s.getState());
  130. }
  131. }
  132. static int RegisterBuiltinMemTableRepFactory(ObjectLibrary& library,
  133. const std::string& /*arg*/) {
  134. // The MemTableRepFactory built-in classes will be either a class
  135. // (VectorRepFactory) or a nickname (vector), followed optionally by ":#",
  136. // where # is the "size" of the factory.
  137. auto AsPattern = [](const std::string& name, const std::string& alt) {
  138. auto pattern = ObjectLibrary::PatternEntry(name, true);
  139. pattern.AnotherName(alt);
  140. pattern.AddNumber(":");
  141. return pattern;
  142. };
  143. library.AddFactory<MemTableRepFactory>(
  144. AsPattern(VectorRepFactory::kClassName(), VectorRepFactory::kNickName()),
  145. [](const std::string& uri, std::unique_ptr<MemTableRepFactory>* guard,
  146. std::string* /*errmsg*/) {
  147. auto colon = uri.find(':');
  148. if (colon != std::string::npos) {
  149. size_t count = ParseSizeT(uri.substr(colon + 1));
  150. guard->reset(new VectorRepFactory(count));
  151. } else {
  152. guard->reset(new VectorRepFactory());
  153. }
  154. return guard->get();
  155. });
  156. library.AddFactory<MemTableRepFactory>(
  157. AsPattern(SkipListFactory::kClassName(), SkipListFactory::kNickName()),
  158. [](const std::string& uri, std::unique_ptr<MemTableRepFactory>* guard,
  159. std::string* /*errmsg*/) {
  160. auto colon = uri.find(':');
  161. if (colon != std::string::npos) {
  162. size_t lookahead = ParseSizeT(uri.substr(colon + 1));
  163. guard->reset(new SkipListFactory(lookahead));
  164. } else {
  165. guard->reset(new SkipListFactory());
  166. }
  167. return guard->get();
  168. });
  169. library.AddFactory<MemTableRepFactory>(
  170. AsPattern("HashLinkListRepFactory", "hash_linkedlist"),
  171. [](const std::string& uri, std::unique_ptr<MemTableRepFactory>* guard,
  172. std::string* /*errmsg*/) {
  173. // Expecting format: hash_linkedlist:<hash_bucket_count>
  174. auto colon = uri.find(':');
  175. if (colon != std::string::npos) {
  176. size_t hash_bucket_count = ParseSizeT(uri.substr(colon + 1));
  177. guard->reset(NewHashLinkListRepFactory(hash_bucket_count));
  178. } else {
  179. guard->reset(NewHashLinkListRepFactory());
  180. }
  181. return guard->get();
  182. });
  183. library.AddFactory<MemTableRepFactory>(
  184. AsPattern("HashSkipListRepFactory", "prefix_hash"),
  185. [](const std::string& uri, std::unique_ptr<MemTableRepFactory>* guard,
  186. std::string* /*errmsg*/) {
  187. // Expecting format: prefix_hash:<hash_bucket_count>
  188. auto colon = uri.find(':');
  189. if (colon != std::string::npos) {
  190. size_t hash_bucket_count = ParseSizeT(uri.substr(colon + 1));
  191. guard->reset(NewHashSkipListRepFactory(hash_bucket_count));
  192. } else {
  193. guard->reset(NewHashSkipListRepFactory());
  194. }
  195. return guard->get();
  196. });
  197. library.AddFactory<MemTableRepFactory>(
  198. "cuckoo",
  199. [](const std::string& /*uri*/,
  200. std::unique_ptr<MemTableRepFactory>* /*guard*/, std::string* errmsg) {
  201. *errmsg = "cuckoo hash memtable is not supported anymore.";
  202. return nullptr;
  203. });
  204. size_t num_types;
  205. return static_cast<int>(library.GetFactoryCount(&num_types));
  206. }
  207. Status GetMemTableRepFactoryFromString(
  208. const std::string& opts_str, std::unique_ptr<MemTableRepFactory>* result) {
  209. ConfigOptions config_options;
  210. config_options.ignore_unsupported_options = false;
  211. config_options.ignore_unknown_options = false;
  212. return MemTableRepFactory::CreateFromString(config_options, opts_str, result);
  213. }
  214. Status MemTableRepFactory::CreateFromString(
  215. const ConfigOptions& config_options, const std::string& value,
  216. std::unique_ptr<MemTableRepFactory>* result) {
  217. static std::once_flag once;
  218. std::call_once(once, [&]() {
  219. RegisterBuiltinMemTableRepFactory(*(ObjectLibrary::Default().get()), "");
  220. });
  221. std::string id;
  222. std::unordered_map<std::string, std::string> opt_map;
  223. Status status = Customizable::GetOptionsMap(config_options, result->get(),
  224. value, &id, &opt_map);
  225. if (!status.ok()) { // GetOptionsMap failed
  226. return status;
  227. } else if (value.empty()) {
  228. // No Id and no options. Clear the object
  229. result->reset();
  230. return Status::OK();
  231. } else if (id.empty()) { // We have no Id but have options. Not good
  232. return Status::NotSupported("Cannot reset object ", id);
  233. } else {
  234. status = NewUniqueObject<MemTableRepFactory>(config_options, id, opt_map,
  235. result);
  236. }
  237. return status;
  238. }
  239. Status MemTableRepFactory::CreateFromString(
  240. const ConfigOptions& config_options, const std::string& value,
  241. std::shared_ptr<MemTableRepFactory>* result) {
  242. std::unique_ptr<MemTableRepFactory> factory;
  243. Status s = CreateFromString(config_options, value, &factory);
  244. if (factory && s.ok()) {
  245. result->reset(factory.release());
  246. }
  247. return s;
  248. }
  249. Status GetPlainTableOptionsFromMap(
  250. const ConfigOptions& config_options, const PlainTableOptions& table_options,
  251. const std::unordered_map<std::string, std::string>& opts_map,
  252. PlainTableOptions* new_table_options) {
  253. assert(new_table_options);
  254. PlainTableFactory ptf(table_options);
  255. Status s = ptf.ConfigureFromMap(config_options, opts_map);
  256. if (s.ok()) {
  257. *new_table_options = *(ptf.GetOptions<PlainTableOptions>());
  258. } else {
  259. // Restore "new_options" to the default "base_options".
  260. *new_table_options = table_options;
  261. }
  262. return s;
  263. }
  264. TableFactory* NewPlainTableFactory(const PlainTableOptions& options) {
  265. return new PlainTableFactory(options);
  266. }
  267. const std::string PlainTablePropertyNames::kEncodingType =
  268. "rocksdb.plain.table.encoding.type";
  269. const std::string PlainTablePropertyNames::kBloomVersion =
  270. "rocksdb.plain.table.bloom.version";
  271. const std::string PlainTablePropertyNames::kNumBloomBlocks =
  272. "rocksdb.plain.table.bloom.numblocks";
  273. } // namespace ROCKSDB_NAMESPACE