|
| 1 | +// Copyright (c) 2013, Facebook, Inc. All rights reserved. |
| 2 | +// This source code is licensed under the BSD-style license found in the |
| 3 | +// LICENSE file in the root directory of this source tree. An additional grant |
| 4 | +// of patent rights can be found in the PATENTS file in the same directory. |
| 5 | +// |
| 6 | +// Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
| 7 | +// Use of this source code is governed by a BSD-style license that can be |
| 8 | +// found in the LICENSE file. See the AUTHORS file for names of contributors. |
| 9 | + |
| 10 | +#include <cassert> |
| 11 | +#include "rocksdb/options.h" |
| 12 | + |
| 13 | +namespace rocksdb { |
| 14 | + |
| 15 | +namespace { |
| 16 | +CompressionType ParseCompressionType(const std::string& type) { |
| 17 | + if (type == "kNoCompression") { |
| 18 | + return kNoCompression; |
| 19 | + } else if (type == "kSnappyCompression") { |
| 20 | + return kSnappyCompression; |
| 21 | + } else if (type == "kZlibCompression") { |
| 22 | + return kZlibCompression; |
| 23 | + } else if (type == "kBZip2Compression") { |
| 24 | + return kBZip2Compression; |
| 25 | + } else if (type == "kLZ4Compression") { |
| 26 | + return kLZ4Compression; |
| 27 | + } else if (type == "kLZ4HCCompression") { |
| 28 | + return kLZ4HCCompression; |
| 29 | + } else { |
| 30 | + throw "unknown compression type: " + type; |
| 31 | + } |
| 32 | + return kNoCompression; |
| 33 | +} |
| 34 | + |
| 35 | +bool ParseBoolean(const std::string& type, const std::string& value) { |
| 36 | + if (value == "true" || value == "1") { |
| 37 | + return true; |
| 38 | + } else if (value == "false" || value == "0") { |
| 39 | + return false; |
| 40 | + } else { |
| 41 | + throw type; |
| 42 | + } |
| 43 | +} |
| 44 | +uint32_t ParseInt(const std::string& value) { |
| 45 | + return std::stoi(value); |
| 46 | +} |
| 47 | + |
| 48 | +uint32_t ParseUint32(const std::string& value) { |
| 49 | + return std::stoul(value); |
| 50 | +} |
| 51 | + |
| 52 | +uint64_t ParseUint64(const std::string& value) { |
| 53 | + return std::stoull(value); |
| 54 | +} |
| 55 | + |
| 56 | +int64_t ParseInt64(const std::string& value) { |
| 57 | + return std::stol(value); |
| 58 | +} |
| 59 | + |
| 60 | +double ParseDouble(const std::string& value) { |
| 61 | + return std::stod(value); |
| 62 | +} |
| 63 | + |
| 64 | +CompactionStyle ParseCompactionStyle(const std::string& type) { |
| 65 | + if (type == "kCompactionStyleLevel") { |
| 66 | + return kCompactionStyleLevel; |
| 67 | + } else if (type == "kCompactionStyleUniversal") { |
| 68 | + return kCompactionStyleUniversal; |
| 69 | + } else if (type == "kCompactionStyleFIFO") { |
| 70 | + return kCompactionStyleFIFO; |
| 71 | + } else { |
| 72 | + throw "unknown compaction style: " + type; |
| 73 | + } |
| 74 | + return kCompactionStyleLevel; |
| 75 | +} |
| 76 | +} // anonymouse namespace |
| 77 | + |
| 78 | +bool GetOptionsFromStrings( |
| 79 | + const Options& base_options, |
| 80 | + const std::unordered_map<std::string, std::string>& options_map, |
| 81 | + Options* new_options) { |
| 82 | + assert(new_options); |
| 83 | + *new_options = base_options; |
| 84 | + for (const auto& o : options_map) { |
| 85 | + try { |
| 86 | + if (o.first == "write_buffer_size") { |
| 87 | + new_options->write_buffer_size = ParseInt64(o.second); |
| 88 | + } else if (o.first == "max_write_buffer_number") { |
| 89 | + new_options->max_write_buffer_number = ParseInt(o.second); |
| 90 | + } else if (o.first == "min_write_buffer_number_to_merge") { |
| 91 | + new_options->min_write_buffer_number_to_merge = ParseInt(o.second); |
| 92 | + } else if (o.first == "compression") { |
| 93 | + new_options->compression = ParseCompressionType(o.second); |
| 94 | + } else if (o.first == "compression_per_level") { |
| 95 | + new_options->compression_per_level.clear(); |
| 96 | + size_t start = 0; |
| 97 | + while (true) { |
| 98 | + size_t end = o.second.find_first_of(':', start); |
| 99 | + if (end == std::string::npos) { |
| 100 | + new_options->compression_per_level.push_back( |
| 101 | + ParseCompressionType(o.second.substr(start))); |
| 102 | + break; |
| 103 | + } else { |
| 104 | + new_options->compression_per_level.push_back( |
| 105 | + ParseCompressionType(o.second.substr(start, end - start))); |
| 106 | + start = end + 1; |
| 107 | + } |
| 108 | + } |
| 109 | + } else if (o.first == "compression_opts") { |
| 110 | + size_t start = 0; |
| 111 | + size_t end = o.second.find_first_of(':'); |
| 112 | + if (end == std::string::npos) { |
| 113 | + throw o.first; |
| 114 | + } |
| 115 | + new_options->compression_opts.window_bits = |
| 116 | + ParseInt(o.second.substr(start, end - start)); |
| 117 | + start = end + 1; |
| 118 | + end = o.second.find_first_of(':', start); |
| 119 | + if (end == std::string::npos) { |
| 120 | + throw o.first; |
| 121 | + } |
| 122 | + new_options->compression_opts.level = |
| 123 | + ParseInt(o.second.substr(start, end - start)); |
| 124 | + start = end + 1; |
| 125 | + if (start >= o.second.size()) { |
| 126 | + throw o.first; |
| 127 | + } |
| 128 | + new_options->compression_opts.strategy = |
| 129 | + ParseInt(o.second.substr(start, o.second.size() - start)); |
| 130 | + } else if (o.first == "num_levels") { |
| 131 | + new_options->num_levels = ParseInt(o.second); |
| 132 | + } else if (o.first == "level0_file_num_compaction_trigger") { |
| 133 | + new_options->level0_file_num_compaction_trigger = ParseInt(o.second); |
| 134 | + } else if (o.first == "level0_slowdown_writes_trigger") { |
| 135 | + new_options->level0_slowdown_writes_trigger = ParseInt(o.second); |
| 136 | + } else if (o.first == "level0_stop_writes_trigger") { |
| 137 | + new_options->level0_stop_writes_trigger = ParseInt(o.second); |
| 138 | + } else if (o.first == "max_mem_compaction_level") { |
| 139 | + new_options->max_mem_compaction_level = ParseInt(o.second); |
| 140 | + } else if (o.first == "target_file_size_base") { |
| 141 | + new_options->target_file_size_base = ParseInt(o.second); |
| 142 | + } else if (o.first == "target_file_size_multiplier") { |
| 143 | + new_options->target_file_size_multiplier = ParseInt(o.second); |
| 144 | + } else if (o.first == "max_bytes_for_level_base") { |
| 145 | + new_options->max_bytes_for_level_base = ParseUint64(o.second); |
| 146 | + } else if (o.first == "max_bytes_for_level_multiplier") { |
| 147 | + new_options->max_bytes_for_level_multiplier = ParseInt(o.second); |
| 148 | + } else if (o.first == "max_bytes_for_level_multiplier_additional") { |
| 149 | + new_options->max_bytes_for_level_multiplier_additional.clear(); |
| 150 | + size_t start = 0; |
| 151 | + while (true) { |
| 152 | + size_t end = o.second.find_first_of(':', start); |
| 153 | + if (end == std::string::npos) { |
| 154 | + new_options->max_bytes_for_level_multiplier_additional.push_back( |
| 155 | + ParseInt(o.second.substr(start))); |
| 156 | + break; |
| 157 | + } else { |
| 158 | + new_options->max_bytes_for_level_multiplier_additional.push_back( |
| 159 | + ParseInt(o.second.substr(start, end - start))); |
| 160 | + start = end + 1; |
| 161 | + } |
| 162 | + } |
| 163 | + } else if (o.first == "expanded_compaction_factor") { |
| 164 | + new_options->expanded_compaction_factor = ParseInt(o.second); |
| 165 | + } else if (o.first == "source_compaction_factor") { |
| 166 | + new_options->source_compaction_factor = ParseInt(o.second); |
| 167 | + } else if (o.first == "max_grandparent_overlap_factor") { |
| 168 | + new_options->max_grandparent_overlap_factor = ParseInt(o.second); |
| 169 | + } else if (o.first == "soft_rate_limit") { |
| 170 | + new_options->soft_rate_limit = ParseDouble(o.second); |
| 171 | + } else if (o.first == "hard_rate_limit") { |
| 172 | + new_options->hard_rate_limit = ParseDouble(o.second); |
| 173 | + } else if (o.first == "arena_block_size") { |
| 174 | + new_options->arena_block_size = ParseInt64(o.second); |
| 175 | + } else if (o.first == "disable_auto_compactions") { |
| 176 | + new_options->disable_auto_compactions = ParseBoolean(o.first, o.second); |
| 177 | + } else if (o.first == "purge_redundant_kvs_while_flush") { |
| 178 | + new_options->purge_redundant_kvs_while_flush = |
| 179 | + ParseBoolean(o.first, o.second); |
| 180 | + } else if (o.first == "compaction_style") { |
| 181 | + new_options->compaction_style = ParseCompactionStyle(o.second); |
| 182 | + } else if (o.first == "verify_checksums_in_compaction") { |
| 183 | + new_options->verify_checksums_in_compaction = |
| 184 | + ParseBoolean(o.first, o.second); |
| 185 | + } else if (o.first == "compaction_options_universal") { |
| 186 | + // TODO(ljin): add support |
| 187 | + throw o.first; |
| 188 | + } else if (o.first == "compaction_options_fifo") { |
| 189 | + new_options->compaction_options_fifo.max_table_files_size |
| 190 | + = ParseUint64(o.second); |
| 191 | + } else if (o.first == "filter_deletes") { |
| 192 | + new_options->filter_deletes = ParseBoolean(o.first, o.second); |
| 193 | + } else if (o.first == "max_sequential_skip_in_iterations") { |
| 194 | + new_options->max_sequential_skip_in_iterations = ParseUint64(o.second); |
| 195 | + } else if (o.first == "inplace_update_support") { |
| 196 | + new_options->inplace_update_support = ParseBoolean(o.first, o.second); |
| 197 | + } else if (o.first == "inplace_update_num_locks") { |
| 198 | + new_options->inplace_update_num_locks = ParseInt64(o.second); |
| 199 | + } else if (o.first == "memtable_prefix_bloom_bits") { |
| 200 | + new_options->memtable_prefix_bloom_bits = stoul(o.second); |
| 201 | + } else if (o.first == "memtable_prefix_bloom_probes") { |
| 202 | + new_options->memtable_prefix_bloom_probes = stoul(o.second); |
| 203 | + } else if (o.first == "memtable_prefix_bloom_huge_page_tlb_size") { |
| 204 | + new_options->memtable_prefix_bloom_huge_page_tlb_size = |
| 205 | + ParseInt64(o.second); |
| 206 | + } else if (o.first == "bloom_locality") { |
| 207 | + new_options->bloom_locality = ParseUint32(o.second); |
| 208 | + } else if (o.first == "max_successive_merges") { |
| 209 | + new_options->max_successive_merges = ParseInt64(o.second); |
| 210 | + } else if (o.first == "min_partial_merge_operands") { |
| 211 | + new_options->min_partial_merge_operands = ParseUint32(o.second); |
| 212 | + } else if (o.first == "create_if_missing") { |
| 213 | + new_options->create_if_missing = ParseBoolean(o.first, o.second); |
| 214 | + } else if (o.first == "create_missing_column_families") { |
| 215 | + new_options->create_missing_column_families = |
| 216 | + ParseBoolean(o.first, o.second); |
| 217 | + } else if (o.first == "error_if_exists") { |
| 218 | + new_options->error_if_exists = ParseBoolean(o.first, o.second); |
| 219 | + } else if (o.first == "paranoid_checks") { |
| 220 | + new_options->paranoid_checks = ParseBoolean(o.first, o.second); |
| 221 | + } else if (o.first == "max_open_files") { |
| 222 | + new_options->max_open_files = ParseInt(o.second); |
| 223 | + } else if (o.first == "max_total_wal_size") { |
| 224 | + new_options->max_total_wal_size = ParseUint64(o.second); |
| 225 | + } else if (o.first == "disable_data_sync") { |
| 226 | + new_options->disableDataSync = ParseBoolean(o.first, o.second); |
| 227 | + } else if (o.first == "use_fsync") { |
| 228 | + new_options->use_fsync = ParseBoolean(o.first, o.second); |
| 229 | + } else if (o.first == "db_paths") { |
| 230 | + // TODO(ljin): add support |
| 231 | + throw o.first; |
| 232 | + } else if (o.first == "db_log_dir") { |
| 233 | + new_options->db_log_dir = o.second; |
| 234 | + } else if (o.first == "wal_dir") { |
| 235 | + new_options->wal_dir = o.second; |
| 236 | + } else if (o.first == "delete_obsolete_files_period_micros") { |
| 237 | + new_options->delete_obsolete_files_period_micros = |
| 238 | + ParseUint64(o.second); |
| 239 | + } else if (o.first == "max_background_compactions") { |
| 240 | + new_options->max_background_compactions = ParseInt(o.second); |
| 241 | + } else if (o.first == "max_background_flushes") { |
| 242 | + new_options->max_background_flushes = ParseInt(o.second); |
| 243 | + } else if (o.first == "max_log_file_size") { |
| 244 | + new_options->max_log_file_size = ParseInt64(o.second); |
| 245 | + } else if (o.first == "log_file_time_to_roll") { |
| 246 | + new_options->log_file_time_to_roll = ParseInt64(o.second); |
| 247 | + } else if (o.first == "keep_log_file_num") { |
| 248 | + new_options->keep_log_file_num = ParseInt64(o.second); |
| 249 | + } else if (o.first == "max_manifest_file_size") { |
| 250 | + new_options->max_manifest_file_size = ParseUint64(o.second); |
| 251 | + } else if (o.first == "table_cache_numshardbits") { |
| 252 | + new_options->table_cache_numshardbits = ParseInt(o.second); |
| 253 | + } else if (o.first == "table_cache_remove_scan_count_limit") { |
| 254 | + new_options->table_cache_remove_scan_count_limit = ParseInt(o.second); |
| 255 | + } else if (o.first == "WAL_ttl_seconds") { |
| 256 | + new_options->WAL_ttl_seconds = ParseUint64(o.second); |
| 257 | + } else if (o.first == "WAL_size_limit_MB") { |
| 258 | + new_options->WAL_size_limit_MB = ParseUint64(o.second); |
| 259 | + } else if (o.first == "manifest_preallocation_size") { |
| 260 | + new_options->manifest_preallocation_size = ParseInt64(o.second); |
| 261 | + } else if (o.first == "allow_os_buffer") { |
| 262 | + new_options->allow_os_buffer = ParseBoolean(o.first, o.second); |
| 263 | + } else if (o.first == "allow_mmap_reads") { |
| 264 | + new_options->allow_mmap_reads = ParseBoolean(o.first, o.second); |
| 265 | + } else if (o.first == "allow_mmap_writes") { |
| 266 | + new_options->allow_mmap_writes = ParseBoolean(o.first, o.second); |
| 267 | + } else if (o.first == "is_fd_close_on_exec") { |
| 268 | + new_options->is_fd_close_on_exec = ParseBoolean(o.first, o.second); |
| 269 | + } else if (o.first == "skip_log_error_on_recovery") { |
| 270 | + new_options->skip_log_error_on_recovery = |
| 271 | + ParseBoolean(o.first, o.second); |
| 272 | + } else if (o.first == "stats_dump_period_sec") { |
| 273 | + new_options->stats_dump_period_sec = ParseUint32(o.second); |
| 274 | + } else if (o.first == "advise_random_on_open") { |
| 275 | + new_options->advise_random_on_open = ParseBoolean(o.first, o.second); |
| 276 | + } else if (o.first == "use_adaptive_mutex") { |
| 277 | + new_options->use_adaptive_mutex = ParseBoolean(o.first, o.second); |
| 278 | + } else if (o.first == "allow_thread_local") { |
| 279 | + new_options->allow_thread_local = ParseBoolean(o.first, o.second); |
| 280 | + } else if (o.first == "bytes_per_sync") { |
| 281 | + new_options->bytes_per_sync = ParseUint64(o.second); |
| 282 | + } else { |
| 283 | + return false; |
| 284 | + } |
| 285 | + } catch (std::exception) { |
| 286 | + return false; |
| 287 | + } |
| 288 | + } |
| 289 | + return true; |
| 290 | +} |
| 291 | + |
| 292 | +} // namespace rocksdb |
0 commit comments