]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/table/plain/plain_table_factory.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / rocksdb / table / plain / plain_table_factory.cc
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
11 #include <memory>
12
13 #include "db/dbformat.h"
14 #include "options/configurable_helper.h"
15 #include "port/port.h"
16 #include "rocksdb/convenience.h"
17 #include "rocksdb/utilities/options_type.h"
18 #include "table/plain/plain_table_builder.h"
19 #include "table/plain/plain_table_reader.h"
20 #include "util/string_util.h"
21
22 namespace ROCKSDB_NAMESPACE {
23 static std::unordered_map<std::string, OptionTypeInfo> plain_table_type_info = {
24 {"user_key_len",
25 {offsetof(struct PlainTableOptions, user_key_len), OptionType::kUInt32T,
26 OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
27 {"bloom_bits_per_key",
28 {offsetof(struct PlainTableOptions, bloom_bits_per_key), OptionType::kInt,
29 OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
30 {"hash_table_ratio",
31 {offsetof(struct PlainTableOptions, hash_table_ratio), OptionType::kDouble,
32 OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
33 {"index_sparseness",
34 {offsetof(struct PlainTableOptions, index_sparseness), OptionType::kSizeT,
35 OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
36 {"huge_page_tlb_size",
37 {offsetof(struct PlainTableOptions, huge_page_tlb_size),
38 OptionType::kSizeT, OptionVerificationType::kNormal,
39 OptionTypeFlags::kNone}},
40 {"encoding_type",
41 {offsetof(struct PlainTableOptions, encoding_type),
42 OptionType::kEncodingType, OptionVerificationType::kNormal,
43 OptionTypeFlags::kNone}},
44 {"full_scan_mode",
45 {offsetof(struct PlainTableOptions, full_scan_mode), OptionType::kBoolean,
46 OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
47 {"store_index_in_file",
48 {offsetof(struct PlainTableOptions, store_index_in_file),
49 OptionType::kBoolean, OptionVerificationType::kNormal,
50 OptionTypeFlags::kNone}},
51 };
52
53 PlainTableFactory::PlainTableFactory(const PlainTableOptions& options)
54 : table_options_(options) {
55 ConfigurableHelper::RegisterOptions(*this, &table_options_,
56 &plain_table_type_info);
57 }
58
59 Status PlainTableFactory::NewTableReader(
60 const ReadOptions& /*ro*/, const TableReaderOptions& table_reader_options,
61 std::unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size,
62 std::unique_ptr<TableReader>* table,
63 bool /*prefetch_index_and_filter_in_cache*/) const {
64 return PlainTableReader::Open(
65 table_reader_options.ioptions, table_reader_options.env_options,
66 table_reader_options.internal_comparator, std::move(file), file_size,
67 table, table_options_.bloom_bits_per_key, table_options_.hash_table_ratio,
68 table_options_.index_sparseness, table_options_.huge_page_tlb_size,
69 table_options_.full_scan_mode, table_reader_options.immortal,
70 table_reader_options.prefix_extractor);
71 }
72
73 TableBuilder* PlainTableFactory::NewTableBuilder(
74 const TableBuilderOptions& table_builder_options, uint32_t column_family_id,
75 WritableFileWriter* file) const {
76 // Ignore the skip_filters flag. PlainTable format is optimized for small
77 // in-memory dbs. The skip_filters optimization is not useful for plain
78 // tables
79 //
80 return new PlainTableBuilder(
81 table_builder_options.ioptions, table_builder_options.moptions,
82 table_builder_options.int_tbl_prop_collector_factories, column_family_id,
83 file, table_options_.user_key_len, table_options_.encoding_type,
84 table_options_.index_sparseness, table_options_.bloom_bits_per_key,
85 table_builder_options.column_family_name, 6,
86 table_options_.huge_page_tlb_size, table_options_.hash_table_ratio,
87 table_options_.store_index_in_file, table_builder_options.db_id,
88 table_builder_options.db_session_id);
89 }
90
91 std::string PlainTableFactory::GetPrintableOptions() const {
92 std::string ret;
93 ret.reserve(20000);
94 const int kBufferSize = 200;
95 char buffer[kBufferSize];
96
97 snprintf(buffer, kBufferSize, " user_key_len: %u\n",
98 table_options_.user_key_len);
99 ret.append(buffer);
100 snprintf(buffer, kBufferSize, " bloom_bits_per_key: %d\n",
101 table_options_.bloom_bits_per_key);
102 ret.append(buffer);
103 snprintf(buffer, kBufferSize, " hash_table_ratio: %lf\n",
104 table_options_.hash_table_ratio);
105 ret.append(buffer);
106 snprintf(buffer, kBufferSize, " index_sparseness: %" ROCKSDB_PRIszt "\n",
107 table_options_.index_sparseness);
108 ret.append(buffer);
109 snprintf(buffer, kBufferSize, " huge_page_tlb_size: %" ROCKSDB_PRIszt "\n",
110 table_options_.huge_page_tlb_size);
111 ret.append(buffer);
112 snprintf(buffer, kBufferSize, " encoding_type: %d\n",
113 table_options_.encoding_type);
114 ret.append(buffer);
115 snprintf(buffer, kBufferSize, " full_scan_mode: %d\n",
116 table_options_.full_scan_mode);
117 ret.append(buffer);
118 snprintf(buffer, kBufferSize, " store_index_in_file: %d\n",
119 table_options_.store_index_in_file);
120 ret.append(buffer);
121 return ret;
122 }
123
124 Status GetPlainTableOptionsFromString(const PlainTableOptions& table_options,
125 const std::string& opts_str,
126 PlainTableOptions* new_table_options) {
127 ConfigOptions config_options;
128 config_options.input_strings_escaped = false;
129 config_options.ignore_unknown_options = false;
130 config_options.invoke_prepare_options = false;
131 return GetPlainTableOptionsFromString(config_options, table_options, opts_str,
132 new_table_options);
133 }
134
135 Status GetPlainTableOptionsFromString(const ConfigOptions& config_options,
136 const PlainTableOptions& table_options,
137 const std::string& opts_str,
138 PlainTableOptions* new_table_options) {
139 std::unordered_map<std::string, std::string> opts_map;
140 Status s = StringToMap(opts_str, &opts_map);
141 if (!s.ok()) {
142 return s;
143 }
144
145 s = GetPlainTableOptionsFromMap(config_options, table_options, opts_map,
146 new_table_options);
147 // Translate any errors (NotFound, NotSupported, to InvalidArgument
148 if (s.ok() || s.IsInvalidArgument()) {
149 return s;
150 } else {
151 return Status::InvalidArgument(s.getState());
152 }
153 }
154
155 Status GetMemTableRepFactoryFromString(
156 const std::string& opts_str,
157 std::unique_ptr<MemTableRepFactory>* new_mem_factory) {
158 std::vector<std::string> opts_list = StringSplit(opts_str, ':');
159 size_t len = opts_list.size();
160
161 if (opts_list.empty() || opts_list.size() > 2) {
162 return Status::InvalidArgument("Can't parse memtable_factory option ",
163 opts_str);
164 }
165
166 MemTableRepFactory* mem_factory = nullptr;
167
168 if (opts_list[0] == "skip_list" || opts_list[0] == "SkipListFactory") {
169 // Expecting format
170 // skip_list:<lookahead>
171 if (2 == len) {
172 size_t lookahead = ParseSizeT(opts_list[1]);
173 mem_factory = new SkipListFactory(lookahead);
174 } else if (1 == len) {
175 mem_factory = new SkipListFactory();
176 }
177 } else if (opts_list[0] == "prefix_hash" ||
178 opts_list[0] == "HashSkipListRepFactory") {
179 // Expecting format
180 // prfix_hash:<hash_bucket_count>
181 if (2 == len) {
182 size_t hash_bucket_count = ParseSizeT(opts_list[1]);
183 mem_factory = NewHashSkipListRepFactory(hash_bucket_count);
184 } else if (1 == len) {
185 mem_factory = NewHashSkipListRepFactory();
186 }
187 } else if (opts_list[0] == "hash_linkedlist" ||
188 opts_list[0] == "HashLinkListRepFactory") {
189 // Expecting format
190 // hash_linkedlist:<hash_bucket_count>
191 if (2 == len) {
192 size_t hash_bucket_count = ParseSizeT(opts_list[1]);
193 mem_factory = NewHashLinkListRepFactory(hash_bucket_count);
194 } else if (1 == len) {
195 mem_factory = NewHashLinkListRepFactory();
196 }
197 } else if (opts_list[0] == "vector" || opts_list[0] == "VectorRepFactory") {
198 // Expecting format
199 // vector:<count>
200 if (2 == len) {
201 size_t count = ParseSizeT(opts_list[1]);
202 mem_factory = new VectorRepFactory(count);
203 } else if (1 == len) {
204 mem_factory = new VectorRepFactory();
205 }
206 } else if (opts_list[0] == "cuckoo") {
207 return Status::NotSupported(
208 "cuckoo hash memtable is not supported anymore.");
209 } else {
210 return Status::InvalidArgument("Unrecognized memtable_factory option ",
211 opts_str);
212 }
213
214 if (mem_factory != nullptr) {
215 new_mem_factory->reset(mem_factory);
216 }
217
218 return Status::OK();
219 }
220
221 Status GetPlainTableOptionsFromMap(
222 const PlainTableOptions& table_options,
223 const std::unordered_map<std::string, std::string>& opts_map,
224 PlainTableOptions* new_table_options, bool input_strings_escaped,
225 bool ignore_unknown_options) {
226 ConfigOptions config_options;
227 config_options.input_strings_escaped = input_strings_escaped;
228 config_options.ignore_unknown_options = ignore_unknown_options;
229 return GetPlainTableOptionsFromMap(config_options, table_options, opts_map,
230 new_table_options);
231 }
232
233 Status GetPlainTableOptionsFromMap(
234 const ConfigOptions& config_options, const PlainTableOptions& table_options,
235 const std::unordered_map<std::string, std::string>& opts_map,
236 PlainTableOptions* new_table_options) {
237 assert(new_table_options);
238 PlainTableFactory ptf(table_options);
239 Status s = ptf.ConfigureFromMap(config_options, opts_map);
240 if (s.ok()) {
241 *new_table_options = *(ptf.GetOptions<PlainTableOptions>());
242 } else {
243 // Restore "new_options" to the default "base_options".
244 *new_table_options = table_options;
245 }
246 return s;
247 }
248
249 extern TableFactory* NewPlainTableFactory(const PlainTableOptions& options) {
250 return new PlainTableFactory(options);
251 }
252
253 const std::string PlainTablePropertyNames::kEncodingType =
254 "rocksdb.plain.table.encoding.type";
255
256 const std::string PlainTablePropertyNames::kBloomVersion =
257 "rocksdb.plain.table.bloom.version";
258
259 const std::string PlainTablePropertyNames::kNumBloomBlocks =
260 "rocksdb.plain.table.bloom.numblocks";
261
262 } // namespace ROCKSDB_NAMESPACE
263 #endif // ROCKSDB_LITE