]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/include/rocksdb/convenience.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / include / rocksdb / convenience.h
CommitLineData
7c673cae 1// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
11fdf7f2
TL
2// This source code is licensed under both the GPLv2 (found in the
3// COPYING file in the root directory) and Apache 2.0 License
4// (found in the LICENSE.Apache file in the root directory).
7c673cae
FG
5
6#pragma once
7
8#include <string>
9#include <unordered_map>
10#include <vector>
11
20effc67 12#include "rocksdb/compression_type.h"
7c673cae 13#include "rocksdb/db.h"
20effc67 14#include "rocksdb/status.h"
7c673cae
FG
15#include "rocksdb/table.h"
16
f67539c2 17namespace ROCKSDB_NAMESPACE {
20effc67 18class Env;
1e59de90
TL
19class Logger;
20class ObjectRegistry;
21
20effc67
TL
22struct ColumnFamilyOptions;
23struct DBOptions;
24struct Options;
25
26// ConfigOptions containing the parameters/controls for
27// comparing objects and converting to/from strings.
28// These settings control how the methods
29// treat errors (e.g. ignore_unknown_objects), the format
30// of the serialization (e.g. delimiter), and how to compare
31// options (sanity_level).
32struct ConfigOptions {
1e59de90
TL
33 // Constructs a new ConfigOptions with a new object registry.
34 // This method should only be used when a DBOptions is not available,
35 // else registry settings may be lost
36 ConfigOptions();
37
38 // Constructs a new ConfigOptions using the settings from
39 // the input DBOptions. Currently constructs a new object registry.
40 explicit ConfigOptions(const DBOptions&);
41
20effc67
TL
42 // This enum defines the RocksDB options sanity level.
43 enum SanityLevel : unsigned char {
44 kSanityLevelNone = 0x01, // Performs no sanity check at all.
45 // Performs minimum check to ensure the RocksDB instance can be
46 // opened without corrupting / mis-interpreting the data.
47 kSanityLevelLooselyCompatible = 0x02,
48 // Perform exact match sanity check.
49 kSanityLevelExactMatch = 0xFF,
50 };
51
52 enum Depth {
53 kDepthDefault, // Traverse nested options that are not flagged as "shallow"
54 kDepthShallow, // Do not traverse into any nested options
55 kDepthDetailed, // Traverse nested options, overriding the options shallow
56 // setting
57 };
58
59 // When true, any unused options will be ignored and OK will be returned
60 bool ignore_unknown_options = false;
61
62 // When true, any unsupported options will be ignored and OK will be returned
63 bool ignore_unsupported_options = true;
64
65 // If the strings are escaped (old-style?)
66 bool input_strings_escaped = true;
67
68 // Whether or not to invoke PrepareOptions after configure is called.
69 bool invoke_prepare_options = true;
70
1e59de90
TL
71 // Options can be marked as Mutable (OptionTypeInfo::IsMutable()) or not.
72 // When "mutable_options_only=false", all options are evaluated.
73 // When "mutable_options_only="true", any option not marked as Mutable is
74 // either ignored (in the case of string/equals methods) or results in an
75 // error (in the case of Configure).
76 bool mutable_options_only = false;
77
20effc67
TL
78 // The separator between options when converting to a string
79 std::string delimiter = ";";
80
81 // Controls how to traverse options during print/match stages
82 Depth depth = Depth::kDepthDefault;
83
84 // Controls how options are serialized
85 // Controls how pedantic the comparison must be for equivalency
86 SanityLevel sanity_level = SanityLevel::kSanityLevelExactMatch;
87 // `file_readahead_size` is used for readahead for the option file.
88 size_t file_readahead_size = 512 * 1024;
89
90 // The environment to use for this option
91 Env* env = Env::Default();
92
1e59de90
TL
93#ifndef ROCKSDB_LITE
94 // The object registry to use for this options
95 std::shared_ptr<ObjectRegistry> registry;
96#endif
97
20effc67
TL
98 bool IsShallow() const { return depth == Depth::kDepthShallow; }
99 bool IsDetailed() const { return depth == Depth::kDepthDetailed; }
100
101 bool IsCheckDisabled() const {
102 return sanity_level == SanityLevel::kSanityLevelNone;
103 }
104
105 bool IsCheckEnabled(SanityLevel level) const {
106 return (level > SanityLevel::kSanityLevelNone && level <= sanity_level);
107 }
108};
7c673cae
FG
109
110#ifndef ROCKSDB_LITE
20effc67 111
7c673cae 112// The following set of functions provide a way to construct RocksDB Options
1e59de90 113// from a string or a string-to-string map. Here is the general rule of
7c673cae
FG
114// setting option values from strings by type. Some RocksDB types are also
115// supported in these APIs. Please refer to the comment of the function itself
116// to find more information about how to config those RocksDB types.
117//
118// * Strings:
119// Strings will be used as values directly without any truncating or
120// trimming.
121//
122// * Booleans:
123// - "true" or "1" => true
124// - "false" or "0" => false.
125// [Example]:
126// - {"optimize_filters_for_hits", "1"} in GetColumnFamilyOptionsFromMap, or
127// - "optimize_filters_for_hits=true" in GetColumnFamilyOptionsFromString.
128//
129// * Integers:
11fdf7f2 130// Integers are converted directly from string, in addition to the following
7c673cae
FG
131// units that we support:
132// - 'k' or 'K' => 2^10
133// - 'm' or 'M' => 2^20
134// - 'g' or 'G' => 2^30
135// - 't' or 'T' => 2^40 // only for unsigned int with sufficient bits.
136// [Example]:
137// - {"arena_block_size", "19G"} in GetColumnFamilyOptionsFromMap, or
138// - "arena_block_size=19G" in GetColumnFamilyOptionsFromString.
139//
140// * Doubles / Floating Points:
141// Doubles / Floating Points are converted directly from string. Note that
142// currently we do not support units.
143// [Example]:
1e59de90
TL
144// - {"memtable_prefix_bloom_size_ratio", "0.1"} in
145// GetColumnFamilyOptionsFromMap, or
146// - "memtable_prefix_bloom_size_ratio=0.1" in
147// GetColumnFamilyOptionsFromString.
7c673cae
FG
148// * Array / Vectors:
149// An array is specified by a list of values, where ':' is used as
150// the delimiter to separate each value.
151// [Example]:
152// - {"compression_per_level", "kNoCompression:kSnappyCompression"}
153// in GetColumnFamilyOptionsFromMap, or
154// - "compression_per_level=kNoCompression:kSnappyCompression" in
155// GetColumnFamilyOptionsFromMapString
156// * Enums:
157// The valid values of each enum are identical to the names of its constants.
158// [Example]:
159// - CompressionType: valid values are "kNoCompression",
160// "kSnappyCompression", "kZlibCompression", "kBZip2Compression", ...
161// - CompactionStyle: valid values are "kCompactionStyleLevel",
162// "kCompactionStyleUniversal", "kCompactionStyleFIFO", and
163// "kCompactionStyleNone".
164//
165
166// Take a default ColumnFamilyOptions "base_options" in addition to a
167// map "opts_map" of option name to option value to construct the new
168// ColumnFamilyOptions "new_options".
169//
170// Below are the instructions of how to config some non-primitive-typed
1e59de90 171// options in ColumnFamilyOptions:
7c673cae
FG
172//
173// * table_factory:
174// table_factory can be configured using our custom nested-option syntax.
175//
176// {option_a=value_a; option_b=value_b; option_c=value_c; ... }
177//
178// A nested option is enclosed by two curly braces, within which there are
179// multiple option assignments. Each assignment is of the form
180// "variable_name=value;".
181//
182// Currently we support the following types of TableFactory:
183// - BlockBasedTableFactory:
184// Use name "block_based_table_factory" to initialize table_factory with
185// BlockBasedTableFactory. Its BlockBasedTableFactoryOptions can be
186// configured using the nested-option syntax.
187// [Example]:
188// * {"block_based_table_factory", "{block_cache=1M;block_size=4k;}"}
189// is equivalent to assigning table_factory with a BlockBasedTableFactory
190// that has 1M LRU block-cache with block size equals to 4k:
191// ColumnFamilyOptions cf_opt;
192// BlockBasedTableOptions blk_opt;
193// blk_opt.block_cache = NewLRUCache(1 * 1024 * 1024);
194// blk_opt.block_size = 4 * 1024;
195// cf_opt.table_factory.reset(NewBlockBasedTableFactory(blk_opt));
196// - PlainTableFactory:
197// Use name "plain_table_factory" to initialize table_factory with
198// PlainTableFactory. Its PlainTableFactoryOptions can be configured using
199// the nested-option syntax.
200// [Example]:
201// * {"plain_table_factory", "{user_key_len=66;bloom_bits_per_key=20;}"}
202//
203// * memtable_factory:
204// Use "memtable" to config memtable_factory. Here are the supported
205// memtable factories:
206// - SkipList:
207// Pass "skip_list:<lookahead>" to config memtable to use SkipList,
208// or simply "skip_list" to use the default SkipList.
209// [Example]:
210// * {"memtable", "skip_list:5"} is equivalent to setting
211// memtable to SkipListFactory(5).
212// - PrefixHash:
1e59de90 213// Pass "prefix_hash:<hash_bucket_count>" to config memtable
7c673cae
FG
214// to use PrefixHash, or simply "prefix_hash" to use the default
215// PrefixHash.
216// [Example]:
217// * {"memtable", "prefix_hash:1000"} is equivalent to setting
218// memtable to NewHashSkipListRepFactory(hash_bucket_count).
219// - HashLinkedList:
220// Pass "hash_linkedlist:<hash_bucket_count>" to config memtable
221// to use HashLinkedList, or simply "hash_linkedlist" to use the default
222// HashLinkedList.
223// [Example]:
224// * {"memtable", "hash_linkedlist:1000"} is equivalent to
225// setting memtable to NewHashLinkListRepFactory(1000).
226// - VectorRepFactory:
227// Pass "vector:<count>" to config memtable to use VectorRepFactory,
228// or simply "vector" to use the default Vector memtable.
229// [Example]:
230// * {"memtable", "vector:1024"} is equivalent to setting memtable
231// to VectorRepFactory(1024).
7c673cae
FG
232//
233// * compression_opts:
234// Use "compression_opts" to config compression_opts. The value format
235// is of the form "<window_bits>:<level>:<strategy>:<max_dict_bytes>".
236// [Example]:
237// * {"compression_opts", "4:5:6:7"} is equivalent to setting:
238// ColumnFamilyOptions cf_opt;
239// cf_opt.compression_opts.window_bits = 4;
240// cf_opt.compression_opts.level = 5;
241// cf_opt.compression_opts.strategy = 6;
242// cf_opt.compression_opts.max_dict_bytes = 7;
243//
20effc67
TL
244// The GetColumnFamilyOptionsFromMap(ConfigOptions, ...) should be used; the
245// alternative signature may be deprecated in a future release. The equivalent
246// functionality can be achieved by setting the corresponding options in
247// the ConfigOptions parameter.
248//
249// @param config_options controls how the map is processed.
7c673cae
FG
250// @param base_options the default options of the output "new_options".
251// @param opts_map an option name to value map for specifying how "new_options"
252// should be set.
253// @param new_options the resulting options based on "base_options" with the
254// change specified in "opts_map".
255// @param input_strings_escaped when set to true, each escaped characters
256// prefixed by '\' in the values of the opts_map will be further converted
257// back to the raw string before assigning to the associated options.
11fdf7f2
TL
258// @param ignore_unknown_options when set to true, unknown options are ignored
259// instead of resulting in an unknown-option error.
7c673cae
FG
260// @return Status::OK() on success. Otherwise, a non-ok status indicating
261// error will be returned, and "new_options" will be set to "base_options".
20effc67
TL
262// @return Status::NotFound means the one (or more) of the option name in
263// the opts_map is not valid for this option
264// @return Status::NotSupported means we do not know how to parse one of the
265// value for this option
266// @return Status::InvalidArgument means the one of the option values is not
267// valid for this option.
268Status GetColumnFamilyOptionsFromMap(
269 const ConfigOptions& config_options,
270 const ColumnFamilyOptions& base_options,
271 const std::unordered_map<std::string, std::string>& opts_map,
272 ColumnFamilyOptions* new_options);
7c673cae
FG
273Status GetColumnFamilyOptionsFromMap(
274 const ColumnFamilyOptions& base_options,
275 const std::unordered_map<std::string, std::string>& opts_map,
11fdf7f2
TL
276 ColumnFamilyOptions* new_options, bool input_strings_escaped = false,
277 bool ignore_unknown_options = false);
7c673cae
FG
278
279// Take a default DBOptions "base_options" in addition to a
280// map "opts_map" of option name to option value to construct the new
281// DBOptions "new_options".
282//
283// Below are the instructions of how to config some non-primitive-typed
284// options in DBOptions:
285//
286// * rate_limiter_bytes_per_sec:
287// RateLimiter can be configured directly by specifying its bytes_per_sec.
288// [Example]:
289// - Passing {"rate_limiter_bytes_per_sec", "1024"} is equivalent to
290// passing NewGenericRateLimiter(1024) to rate_limiter_bytes_per_sec.
291//
20effc67
TL
292// The GetDBOptionsFromMap(ConfigOptions, ...) should be used; the
293// alternative signature may be deprecated in a future release. The equivalent
294// functionality can be achieved by setting the corresponding options in
295// the ConfigOptions parameter.
296//
297// @param config_options controls how the map is processed.
7c673cae
FG
298// @param base_options the default options of the output "new_options".
299// @param opts_map an option name to value map for specifying how "new_options"
300// should be set.
301// @param new_options the resulting options based on "base_options" with the
302// change specified in "opts_map".
303// @param input_strings_escaped when set to true, each escaped characters
304// prefixed by '\' in the values of the opts_map will be further converted
305// back to the raw string before assigning to the associated options.
11fdf7f2
TL
306// @param ignore_unknown_options when set to true, unknown options are ignored
307// instead of resulting in an unknown-option error.
7c673cae
FG
308// @return Status::OK() on success. Otherwise, a non-ok status indicating
309// error will be returned, and "new_options" will be set to "base_options".
20effc67
TL
310// @return Status::NotFound means the one (or more) of the option name in
311// the opts_map is not valid for this option
312// @return Status::NotSupported means we do not know how to parse one of the
313// value for this option
314// @return Status::InvalidArgument means the one of the option values is not
315// valid for this option.
316Status GetDBOptionsFromMap(
317 const ConfigOptions& cfg_options, const DBOptions& base_options,
318 const std::unordered_map<std::string, std::string>& opts_map,
319 DBOptions* new_options);
7c673cae
FG
320Status GetDBOptionsFromMap(
321 const DBOptions& base_options,
322 const std::unordered_map<std::string, std::string>& opts_map,
11fdf7f2
TL
323 DBOptions* new_options, bool input_strings_escaped = false,
324 bool ignore_unknown_options = false);
7c673cae
FG
325
326// Take a default BlockBasedTableOptions "table_options" in addition to a
327// map "opts_map" of option name to option value to construct the new
328// BlockBasedTableOptions "new_table_options".
329//
330// Below are the instructions of how to config some non-primitive-typed
331// options in BlockBasedTableOptions:
332//
333// * filter_policy:
334// We currently only support the following FilterPolicy in the convenience
335// functions:
336// - BloomFilter: use "bloomfilter:[bits_per_key]:[use_block_based_builder]"
337// to specify BloomFilter. The above string is equivalent to calling
338// NewBloomFilterPolicy(bits_per_key, use_block_based_builder).
339// [Example]:
340// - Pass {"filter_policy", "bloomfilter:4:true"} in
341// GetBlockBasedTableOptionsFromMap to use a BloomFilter with 4-bits
342// per key and use_block_based_builder enabled.
343//
344// * block_cache / block_cache_compressed:
345// We currently only support LRU cache in the GetOptions API. The LRU
346// cache can be set by directly specifying its size.
347// [Example]:
348// - Passing {"block_cache", "1M"} in GetBlockBasedTableOptionsFromMap is
349// equivalent to setting block_cache using NewLRUCache(1024 * 1024).
350//
20effc67
TL
351// The GetBlockBasedTableOptionsFromMap(ConfigOptions, ...) should be used;
352// the alternative signature may be deprecated in a future release. The
353// equivalent functionality can be achieved by setting the corresponding
354// options in the ConfigOptions parameter.
355//
356// @param config_options controls how the map is processed.
7c673cae
FG
357// @param table_options the default options of the output "new_table_options".
358// @param opts_map an option name to value map for specifying how
359// "new_table_options" should be set.
360// @param new_table_options the resulting options based on "table_options"
361// with the change specified in "opts_map".
362// @param input_strings_escaped when set to true, each escaped characters
363// prefixed by '\' in the values of the opts_map will be further converted
364// back to the raw string before assigning to the associated options.
11fdf7f2
TL
365// @param ignore_unknown_options when set to true, unknown options are ignored
366// instead of resulting in an unknown-option error.
7c673cae
FG
367// @return Status::OK() on success. Otherwise, a non-ok status indicating
368// error will be returned, and "new_table_options" will be set to
369// "table_options".
20effc67
TL
370Status GetBlockBasedTableOptionsFromMap(
371 const ConfigOptions& config_options,
372 const BlockBasedTableOptions& table_options,
373 const std::unordered_map<std::string, std::string>& opts_map,
374 BlockBasedTableOptions* new_table_options);
7c673cae
FG
375Status GetBlockBasedTableOptionsFromMap(
376 const BlockBasedTableOptions& table_options,
377 const std::unordered_map<std::string, std::string>& opts_map,
378 BlockBasedTableOptions* new_table_options,
11fdf7f2 379 bool input_strings_escaped = false, bool ignore_unknown_options = false);
7c673cae
FG
380
381// Take a default PlainTableOptions "table_options" in addition to a
382// map "opts_map" of option name to option value to construct the new
383// PlainTableOptions "new_table_options".
384//
20effc67
TL
385// The GetPlainTableOptionsFromMap(ConfigOptions, ...) should be used; the
386// alternative signature may be deprecated in a future release. The equivalent
387// functionality can be achieved by setting the corresponding options in
388// the ConfigOptions parameter.
389//
390// @param config_options controls how the map is processed.
7c673cae
FG
391// @param table_options the default options of the output "new_table_options".
392// @param opts_map an option name to value map for specifying how
393// "new_table_options" should be set.
394// @param new_table_options the resulting options based on "table_options"
395// with the change specified in "opts_map".
396// @param input_strings_escaped when set to true, each escaped characters
397// prefixed by '\' in the values of the opts_map will be further converted
398// back to the raw string before assigning to the associated options.
11fdf7f2
TL
399// @param ignore_unknown_options when set to true, unknown options are ignored
400// instead of resulting in an unknown-option error.
7c673cae
FG
401// @return Status::OK() on success. Otherwise, a non-ok status indicating
402// error will be returned, and "new_table_options" will be set to
403// "table_options".
20effc67
TL
404Status GetPlainTableOptionsFromMap(
405 const ConfigOptions& config_options, const PlainTableOptions& table_options,
406 const std::unordered_map<std::string, std::string>& opts_map,
407 PlainTableOptions* new_table_options);
7c673cae
FG
408Status GetPlainTableOptionsFromMap(
409 const PlainTableOptions& table_options,
410 const std::unordered_map<std::string, std::string>& opts_map,
11fdf7f2
TL
411 PlainTableOptions* new_table_options, bool input_strings_escaped = false,
412 bool ignore_unknown_options = false);
7c673cae 413
20effc67 414// Take a string representation of option names and values, apply them into the
7c673cae
FG
415// base_options, and return the new options as a result. The string has the
416// following format:
417// "write_buffer_size=1024;max_write_buffer_number=2"
418// Nested options config is also possible. For example, you can define
419// BlockBasedTableOptions as part of the string for block-based table factory:
420// "write_buffer_size=1024;block_based_table_factory={block_size=4k};"
421// "max_write_buffer_num=2"
20effc67
TL
422//
423//
424// The GetColumnFamilyOptionsFromString(ConfigOptions, ...) should be used; the
425// alternative signature may be deprecated in a future release. The equivalent
426// functionality can be achieved by setting the corresponding options in
427// the ConfigOptions parameter.
428Status GetColumnFamilyOptionsFromString(const ConfigOptions& config_options,
429 const ColumnFamilyOptions& base_options,
430 const std::string& opts_str,
431 ColumnFamilyOptions* new_options);
494da23a
TL
432Status GetColumnFamilyOptionsFromString(const ColumnFamilyOptions& base_options,
433 const std::string& opts_str,
434 ColumnFamilyOptions* new_options);
7c673cae 435
20effc67
TL
436Status GetDBOptionsFromString(const ConfigOptions& config_options,
437 const DBOptions& base_options,
438 const std::string& opts_str,
439 DBOptions* new_options);
440
494da23a
TL
441Status GetDBOptionsFromString(const DBOptions& base_options,
442 const std::string& opts_str,
443 DBOptions* new_options);
7c673cae 444
20effc67
TL
445Status GetStringFromDBOptions(const ConfigOptions& config_options,
446 const DBOptions& db_options,
447 std::string* opts_str);
448
7c673cae
FG
449Status GetStringFromDBOptions(std::string* opts_str,
450 const DBOptions& db_options,
451 const std::string& delimiter = "; ");
452
20effc67
TL
453Status GetStringFromColumnFamilyOptions(const ConfigOptions& config_options,
454 const ColumnFamilyOptions& cf_options,
455 std::string* opts_str);
7c673cae
FG
456Status GetStringFromColumnFamilyOptions(std::string* opts_str,
457 const ColumnFamilyOptions& cf_options,
458 const std::string& delimiter = "; ");
7c673cae
FG
459Status GetStringFromCompressionType(std::string* compression_str,
460 CompressionType compression_type);
461
462std::vector<CompressionType> GetSupportedCompressions();
463
464Status GetBlockBasedTableOptionsFromString(
494da23a 465 const BlockBasedTableOptions& table_options, const std::string& opts_str,
7c673cae 466 BlockBasedTableOptions* new_table_options);
20effc67
TL
467Status GetBlockBasedTableOptionsFromString(
468 const ConfigOptions& config_options,
469 const BlockBasedTableOptions& table_options, const std::string& opts_str,
470 BlockBasedTableOptions* new_table_options);
7c673cae 471
494da23a
TL
472Status GetPlainTableOptionsFromString(const PlainTableOptions& table_options,
473 const std::string& opts_str,
474 PlainTableOptions* new_table_options);
20effc67
TL
475Status GetPlainTableOptionsFromString(const ConfigOptions& config_options,
476 const PlainTableOptions& table_options,
477 const std::string& opts_str,
478 PlainTableOptions* new_table_options);
7c673cae
FG
479
480Status GetMemTableRepFactoryFromString(
481 const std::string& opts_str,
482 std::unique_ptr<MemTableRepFactory>* new_mem_factory);
483
484Status GetOptionsFromString(const Options& base_options,
485 const std::string& opts_str, Options* new_options);
20effc67
TL
486Status GetOptionsFromString(const ConfigOptions& config_options,
487 const Options& base_options,
488 const std::string& opts_str, Options* new_options);
7c673cae
FG
489
490Status StringToMap(const std::string& opts_str,
491 std::unordered_map<std::string, std::string>* opts_map);
492
493// Request stopping background work, if wait is true wait until it's done
494void CancelAllBackgroundWork(DB* db, bool wait = false);
495
496// Delete files which are entirely in the given range
497// Could leave some keys in the range which are in files which are not
11fdf7f2
TL
498// entirely in the range. Also leaves L0 files regardless of whether they're
499// in the range.
7c673cae
FG
500// Snapshots before the delete might not see the data in the given range.
501Status DeleteFilesInRange(DB* db, ColumnFamilyHandle* column_family,
11fdf7f2
TL
502 const Slice* begin, const Slice* end,
503 bool include_end = true);
504
505// Delete files in multiple ranges at once
506// Delete files in a lot of ranges one at a time can be slow, use this API for
507// better performance in that case.
508Status DeleteFilesInRanges(DB* db, ColumnFamilyHandle* column_family,
509 const RangePtr* ranges, size_t n,
510 bool include_end = true);
511
512// Verify the checksum of file
513Status VerifySstFileChecksum(const Options& options,
514 const EnvOptions& env_options,
515 const std::string& file_path);
f67539c2
TL
516
517// Verify the checksum of file
518Status VerifySstFileChecksum(const Options& options,
519 const EnvOptions& env_options,
520 const ReadOptions& read_options,
1e59de90
TL
521 const std::string& file_path,
522 const SequenceNumber& largest_seqno = 0);
7c673cae
FG
523#endif // ROCKSDB_LITE
524
f67539c2 525} // namespace ROCKSDB_NAMESPACE