]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/include/rocksdb/utilities/ldb_cmd.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / rocksdb / include / rocksdb / utilities / ldb_cmd.h
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).
5 //
6 #pragma once
7
8 #ifndef ROCKSDB_LITE
9
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include <algorithm>
14 #include <functional>
15 #include <map>
16 #include <sstream>
17 #include <string>
18 #include <vector>
19
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"
28
29 namespace ROCKSDB_NAMESPACE {
30
31 class LDBCommand {
32 public:
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;
63
64 struct ParsedParams {
65 std::string cmd;
66 std::vector<std::string> cmd_params;
67 std::map<std::string, std::string> option_map;
68 std::vector<std::string> flags;
69 };
70
71 static LDBCommand* SelectCommand(const ParsedParams& parsed_parms);
72
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 =
78 SelectCommand);
79
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);
84
85 bool ValidateCmdLineOptions();
86
87 virtual void PrepareOptions();
88
89 virtual void OverrideBaseOptions();
90
91 virtual void SetDBOptions(Options options) { options_ = options; }
92
93 virtual void SetColumnFamilies(
94 const std::vector<ColumnFamilyDescriptor>* column_families) {
95 if (column_families != nullptr) {
96 column_families_ = *column_families;
97 } else {
98 column_families_.clear();
99 }
100 }
101
102 void SetLDBOptions(const LDBOptions& ldb_options) {
103 ldb_options_ = ldb_options;
104 }
105
106 const std::map<std::string, std::string>& TEST_GetOptionMap() {
107 return option_map_;
108 }
109
110 const std::vector<std::string>& TEST_GetFlags() { return flags_; }
111
112 virtual bool NoDBOpen() { return false; }
113
114 virtual ~LDBCommand() { CloseDB(); }
115
116 /* Run the command, and return the execute result. */
117 void Run();
118
119 virtual void DoCommand() = 0;
120
121 LDBCommandExecuteResult GetExecuteState() { return exec_state_; }
122
123 void ClearPreviousRunState() { exec_state_.Reset(); }
124
125 // Consider using Slice::DecodeHex directly instead if you don't need the
126 // 0x prefix
127 static std::string HexToString(const std::string& str);
128
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);
132
133 static const char* DELIM;
134
135 protected:
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_;
144 DB* db_;
145 DBWithTTL* db_ttl_;
146 std::map<std::string, ColumnFamilyHandle*> cf_handles_;
147
148 /**
149 * true implies that this command can work if the db is opened in read-only
150 * mode.
151 */
152 bool is_read_only_;
153
154 /** If true, the key is input/output as hex in get/put/scan/delete etc. */
155 bool is_key_hex_;
156
157 /** If true, the value is input/output as hex in get/put/scan/delete etc. */
158 bool is_value_hex_;
159
160 /** If true, the value is treated as timestamp suffixed */
161 bool is_db_ttl_;
162
163 // If true, the kvs are output with their insert/modify timestamp in a ttl db
164 bool timestamp_;
165
166 // If true, try to construct options from DB's option files.
167 bool try_load_options_;
168
169 // The value passed to options.force_consistency_checks.
170 bool force_consistency_checks_;
171
172 bool create_if_missing_;
173
174 /**
175 * Map of options passed on the command-line.
176 */
177 const std::map<std::string, std::string> option_map_;
178
179 /**
180 * Flags passed on the command-line.
181 */
182 const std::vector<std::string> flags_;
183
184 /** List of command-line options valid for this command */
185 const std::vector<std::string> valid_cmd_line_options_;
186
187 /** Shared pointer to underlying environment if applicable **/
188 std::shared_ptr<Env> env_guard_;
189
190 bool ParseKeyValue(const std::string& line, std::string* key,
191 std::string* value, bool is_key_hex, bool is_value_hex);
192
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);
196
197 void OpenDB();
198
199 void CloseDB();
200
201 ColumnFamilyHandle* GetCfHandle();
202
203 static std::string PrintKeyValue(const std::string& key,
204 const std::string& value, bool is_key_hex,
205 bool is_value_hex);
206
207 static std::string PrintKeyValue(const std::string& key,
208 const std::string& value, bool is_hex);
209
210 /**
211 * Return true if the specified flag is present in the specified flags vector
212 */
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());
216 }
217
218 static std::string HelpRangeCmdArgs();
219
220 /**
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
223 * passed in.
224 */
225 static std::vector<std::string> BuildCmdLineOptions(
226 std::vector<std::string> options);
227
228 bool ParseIntOption(const std::map<std::string, std::string>& options,
229 const std::string& option, int& value,
230 LDBCommandExecuteResult& exec_state);
231
232 bool ParseStringOption(const std::map<std::string, std::string>& options,
233 const std::string& option, std::string* value);
234
235 /**
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).
240 */
241 bool ParseBooleanOption(const std::map<std::string, std::string>& options,
242 const std::string& option, bool default_val);
243
244 Options options_;
245 std::vector<ColumnFamilyDescriptor> column_families_;
246 ConfigOptions config_options_;
247 LDBOptions ldb_options_;
248
249 private:
250 /**
251 * Interpret command line options and flags to determine if the key
252 * should be input/output in hex.
253 */
254 bool IsKeyHex(const std::map<std::string, std::string>& options,
255 const std::vector<std::string>& flags);
256
257 /**
258 * Interpret command line options and flags to determine if the value
259 * should be input/output in hex.
260 */
261 bool IsValueHex(const std::map<std::string, std::string>& options,
262 const std::vector<std::string>& flags);
263
264 /**
265 * Converts val to a boolean.
266 * val must be either true or false (case insensitive).
267 * Otherwise an exception is thrown.
268 */
269 bool StringToBool(std::string val);
270 };
271
272 class LDBCommandRunner {
273 public:
274 static void PrintHelp(const LDBOptions& ldb_options, const char* exec_name,
275 bool to_stderr = true);
276
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);
282 };
283
284 } // namespace ROCKSDB_NAMESPACE
285
286 #endif // ROCKSDB_LITE