1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
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).
20 #include "rocksdb/convenience.h"
21 #include "rocksdb/env.h"
22 #include "rocksdb/iterator.h"
23 #include "rocksdb/ldb_tool.h"
24 #include "rocksdb/options.h"
25 #include "rocksdb/slice.h"
26 #include "rocksdb/utilities/db_ttl.h"
27 #include "rocksdb/utilities/ldb_cmd_execute_result.h"
29 namespace ROCKSDB_NAMESPACE
{
33 // Command-line arguments
34 static const std::string ARG_ENV_URI
;
35 static const std::string ARG_DB
;
36 static const std::string ARG_PATH
;
37 static const std::string ARG_SECONDARY_PATH
;
38 static const std::string ARG_HEX
;
39 static const std::string ARG_KEY_HEX
;
40 static const std::string ARG_VALUE_HEX
;
41 static const std::string ARG_CF_NAME
;
42 static const std::string ARG_TTL
;
43 static const std::string ARG_TTL_START
;
44 static const std::string ARG_TTL_END
;
45 static const std::string ARG_TIMESTAMP
;
46 static const std::string ARG_TRY_LOAD_OPTIONS
;
47 static const std::string ARG_IGNORE_UNKNOWN_OPTIONS
;
48 static const std::string ARG_FROM
;
49 static const std::string ARG_TO
;
50 static const std::string ARG_MAX_KEYS
;
51 static const std::string ARG_BLOOM_BITS
;
52 static const std::string ARG_FIX_PREFIX_LEN
;
53 static const std::string ARG_COMPRESSION_TYPE
;
54 static const std::string ARG_COMPRESSION_MAX_DICT_BYTES
;
55 static const std::string ARG_BLOCK_SIZE
;
56 static const std::string ARG_AUTO_COMPACTION
;
57 static const std::string ARG_DB_WRITE_BUFFER_SIZE
;
58 static const std::string ARG_WRITE_BUFFER_SIZE
;
59 static const std::string ARG_FILE_SIZE
;
60 static const std::string ARG_CREATE_IF_MISSING
;
61 static const std::string ARG_NO_VALUE
;
62 static const std::string ARG_DISABLE_CONSISTENCY_CHECKS
;
66 std::vector
<std::string
> cmd_params
;
67 std::map
<std::string
, std::string
> option_map
;
68 std::vector
<std::string
> flags
;
71 static LDBCommand
* SelectCommand(const ParsedParams
& parsed_parms
);
73 static LDBCommand
* InitFromCmdLineArgs(
74 const std::vector
<std::string
>& args
, const Options
& options
,
75 const LDBOptions
& ldb_options
,
76 const std::vector
<ColumnFamilyDescriptor
>* column_families
,
77 const std::function
<LDBCommand
*(const ParsedParams
&)>& selector
=
80 static LDBCommand
* InitFromCmdLineArgs(
81 int argc
, char const* const* argv
, const Options
& options
,
82 const LDBOptions
& ldb_options
,
83 const std::vector
<ColumnFamilyDescriptor
>* column_families
);
85 bool ValidateCmdLineOptions();
87 virtual void PrepareOptions();
89 virtual void OverrideBaseOptions();
91 virtual void SetDBOptions(Options options
) { options_
= options
; }
93 virtual void SetColumnFamilies(
94 const std::vector
<ColumnFamilyDescriptor
>* column_families
) {
95 if (column_families
!= nullptr) {
96 column_families_
= *column_families
;
98 column_families_
.clear();
102 void SetLDBOptions(const LDBOptions
& ldb_options
) {
103 ldb_options_
= ldb_options
;
106 const std::map
<std::string
, std::string
>& TEST_GetOptionMap() {
110 const std::vector
<std::string
>& TEST_GetFlags() { return flags_
; }
112 virtual bool NoDBOpen() { return false; }
114 virtual ~LDBCommand() { CloseDB(); }
116 /* Run the command, and return the execute result. */
119 virtual void DoCommand() = 0;
121 LDBCommandExecuteResult
GetExecuteState() { return exec_state_
; }
123 void ClearPreviousRunState() { exec_state_
.Reset(); }
125 // Consider using Slice::DecodeHex directly instead if you don't need the
127 static std::string
HexToString(const std::string
& str
);
129 // Consider using Slice::ToString(true) directly instead if
130 // you don't need the 0x prefix
131 static std::string
StringToHex(const std::string
& str
);
133 static const char* DELIM
;
136 LDBCommandExecuteResult exec_state_
;
137 std::string env_uri_
;
138 std::string db_path_
;
139 // If empty, open DB as primary. If non-empty, open the DB as secondary
140 // with this secondary path. When running against a database opened by
141 // another process, ldb wll leave the source directory completely intact.
142 std::string secondary_path_
;
143 std::string column_family_name_
;
146 std::map
<std::string
, ColumnFamilyHandle
*> cf_handles_
;
149 * true implies that this command can work if the db is opened in read-only
154 /** If true, the key is input/output as hex in get/put/scan/delete etc. */
157 /** If true, the value is input/output as hex in get/put/scan/delete etc. */
160 /** If true, the value is treated as timestamp suffixed */
163 // If true, the kvs are output with their insert/modify timestamp in a ttl db
166 // If true, try to construct options from DB's option files.
167 bool try_load_options_
;
169 // The value passed to options.force_consistency_checks.
170 bool force_consistency_checks_
;
172 bool create_if_missing_
;
175 * Map of options passed on the command-line.
177 const std::map
<std::string
, std::string
> option_map_
;
180 * Flags passed on the command-line.
182 const std::vector
<std::string
> flags_
;
184 /** List of command-line options valid for this command */
185 const std::vector
<std::string
> valid_cmd_line_options_
;
187 /** Shared pointer to underlying environment if applicable **/
188 std::shared_ptr
<Env
> env_guard_
;
190 bool ParseKeyValue(const std::string
& line
, std::string
* key
,
191 std::string
* value
, bool is_key_hex
, bool is_value_hex
);
193 LDBCommand(const std::map
<std::string
, std::string
>& options
,
194 const std::vector
<std::string
>& flags
, bool is_read_only
,
195 const std::vector
<std::string
>& valid_cmd_line_options
);
201 ColumnFamilyHandle
* GetCfHandle();
203 static std::string
PrintKeyValue(const std::string
& key
,
204 const std::string
& value
, bool is_key_hex
,
207 static std::string
PrintKeyValue(const std::string
& key
,
208 const std::string
& value
, bool is_hex
);
211 * Return true if the specified flag is present in the specified flags vector
213 static bool IsFlagPresent(const std::vector
<std::string
>& flags
,
214 const std::string
& flag
) {
215 return (std::find(flags
.begin(), flags
.end(), flag
) != flags
.end());
218 static std::string
HelpRangeCmdArgs();
221 * A helper function that returns a list of command line options
222 * used by this command. It includes the common options and the ones
225 static std::vector
<std::string
> BuildCmdLineOptions(
226 std::vector
<std::string
> options
);
228 bool ParseIntOption(const std::map
<std::string
, std::string
>& options
,
229 const std::string
& option
, int& value
,
230 LDBCommandExecuteResult
& exec_state
);
232 bool ParseStringOption(const std::map
<std::string
, std::string
>& options
,
233 const std::string
& option
, std::string
* value
);
236 * Returns the value of the specified option as a boolean.
237 * default_val is used if the option is not found in options.
238 * Throws an exception if the value of the option is not
239 * "true" or "false" (case insensitive).
241 bool ParseBooleanOption(const std::map
<std::string
, std::string
>& options
,
242 const std::string
& option
, bool default_val
);
245 std::vector
<ColumnFamilyDescriptor
> column_families_
;
246 ConfigOptions config_options_
;
247 LDBOptions ldb_options_
;
251 * Interpret command line options and flags to determine if the key
252 * should be input/output in hex.
254 bool IsKeyHex(const std::map
<std::string
, std::string
>& options
,
255 const std::vector
<std::string
>& flags
);
258 * Interpret command line options and flags to determine if the value
259 * should be input/output in hex.
261 bool IsValueHex(const std::map
<std::string
, std::string
>& options
,
262 const std::vector
<std::string
>& flags
);
265 * Converts val to a boolean.
266 * val must be either true or false (case insensitive).
267 * Otherwise an exception is thrown.
269 bool StringToBool(std::string val
);
272 class LDBCommandRunner
{
274 static void PrintHelp(const LDBOptions
& ldb_options
, const char* exec_name
,
275 bool to_stderr
= true);
277 // Returns the status code to return. 0 is no error.
278 static int RunCommand(
279 int argc
, char const* const* argv
, Options options
,
280 const LDBOptions
& ldb_options
,
281 const std::vector
<ColumnFamilyDescriptor
>* column_families
);
284 } // namespace ROCKSDB_NAMESPACE
286 #endif // ROCKSDB_LITE