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