forked from estraier/tkrzw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkrzw_cmd_util.h
300 lines (267 loc) · 9.17 KB
/
tkrzw_cmd_util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*************************************************************************************************
* Command-line utilities
*
* Copyright 2020 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*************************************************************************************************/
#ifndef _TKRZW_CMD_UTIL_H
#define _TKRZW_CMD_UTIL_H
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <random>
#include <set>
#include <string>
#include <string_view>
#include <thread>
#include <vector>
#include <cinttypes>
#include <cstdarg>
#include "tkrzw_containers.h"
#include "tkrzw_compress.h"
#include "tkrzw_dbm.h"
#include "tkrzw_dbm_async.h"
#include "tkrzw_dbm_baby.h"
#include "tkrzw_dbm_cache.h"
#include "tkrzw_dbm_common_impl.h"
#include "tkrzw_dbm_hash.h"
#include "tkrzw_dbm_hash_impl.h"
#include "tkrzw_dbm_poly.h"
#include "tkrzw_dbm_shard.h"
#include "tkrzw_dbm_skip.h"
#include "tkrzw_dbm_skip_impl.h"
#include "tkrzw_dbm_std.h"
#include "tkrzw_dbm_tiny.h"
#include "tkrzw_dbm_tree.h"
#include "tkrzw_dbm_tree_impl.h"
#include "tkrzw_file.h"
#include "tkrzw_file_mmap.h"
#include "tkrzw_file_pos.h"
#include "tkrzw_file_std.h"
#include "tkrzw_file_util.h"
#include "tkrzw_hash_util.h"
#include "tkrzw_index.h"
#include "tkrzw_key_comparators.h"
#include "tkrzw_lib_common.h"
#include "tkrzw_str_util.h"
#include "tkrzw_thread_util.h"
#include "tkrzw_time_util.h"
namespace tkrzw {
/**
* Prints an empty string to the stdout and flush the buffer.
*/
inline void Print() {
std::cout.flush();
}
/**
* Prints strings to the stdout and flush the buffer.
* @param first The first string.
* @param rest The rest strings.
*/
template <typename FIRST, typename... REST>
inline void Print(const FIRST& first, const REST&... rest) {
std::cout << ToString(first);
Print(rest...);
}
/**
* Prints an empty string and a line feed to the stdout and flush the buffer.
*/
inline void PrintL() {
std::cout << std::endl;
std::cout.flush();
}
/**
* Prints strings and a line feed to the stdout and flush the buffer.
* @param first The first string.
* @param rest The rest strings.
*/
template <typename FIRST, typename... REST>
inline void PrintL(const FIRST& first, const REST&... rest) {
std::cout << ToString(first);
PrintL(rest...);
}
/**
* Prints a formatted string to the stdout and flush the buffer.
* @param format The format string.
* @param ... The other arguments.
*/
void PrintF(const char* format, ...);
/**
* Prints a character to the stdout and flush the buffer.
* @param c The character to print.
*/
void PutChar(char c);
/**
* Prints an empty string to the stderr and flush the buffer.
*/
inline void EPrint() {
std::cerr.flush();
}
/**
* Prints strings to the stderr and flush the buffer.
* @param first The first string.
* @param rest The rest strings.
*/
template <typename FIRST, typename... REST>
inline void EPrint(const FIRST& first, const REST&... rest) {
std::cerr << ToString(first);
EPrint(rest...);
}
/**
* Prints an empty string and a line feed to the stderr and flush the buffer.
*/
inline void EPrintL() {
std::cerr << std::endl;
std::cerr.flush();
}
/**
* Prints strings and a line feed to the stderr and flush the buffer.
* @param first The first string.
* @param rest The rest strings.
*/
template <typename FIRST, typename... REST>
inline void EPrintL(const FIRST& first, const REST&... rest) {
std::cerr << ToString(first);
EPrintL(rest...);
}
/**
* Prints a formatted string to the stderr and flush the buffer
* @param format The format string.
* @param ... The other arguments.
*/
void EPrintF(const char* format, ...);
/**
* Prints a character to the stderr and flush the buffer.
* @param c The character to print.
*/
void EPutChar(char c);
/**
* Parses command line arguments.
* @param argc The number of input arguments.
* @param argv The input arguments.
* @param configs A map of option names and numbers of the required arguments. If an empty
* string represents positional arguments.
* @param result The pointer to a map object to contain option names and their arguments.
* @param error_message The pointer to a string object to contain the error message.
* @return True on success or false on failure.
*/
bool ParseCommandArguments(
int32_t argc, const char** argv,
const std::map<std::string, int32_t>& configs,
std::map<std::string, std::vector<std::string>>* result,
std::string* error_message);
/**
* Gets a string argument of parsed command arguments.
* @param args The parsed command arguments.
* @param name The name of the argument.
* @param index The index of the value.
* @param default_value The value to be returned on failure.
* @return The value of the matching argument on success, or the default value on failure.
*/
std::string GetStringArgument(
const std::map<std::string, std::vector<std::string>>& args,
const std::string& name, int32_t index, const std::string& default_value);
/**
* Gets an integer argument of parsed command arguments.
* @param args The parsed command arguments.
* @param name The name of the argument.
* @param index The index of the value.
* @param default_value The value to be returned on failure.
* @return The value of the matching argument on success, or the default value on failure.
*/
int64_t GetIntegerArgument(
const std::map<std::string, std::vector<std::string>>& args,
const std::string& name, int32_t index, int64_t default_value);
/**
* Gets a real number argument of parsed command arguments.
* @param args The parsed command arguments.
* @param name The name of the argument.
* @param index The index of the value.
* @param default_value The value to be returned on failure.
* @return The value of the matching argument on success, or the default value on failure.
*/
double GetDoubleArgument(
const std::map<std::string, std::vector<std::string>>& args,
const std::string& name, int32_t index, double default_value);
/**
* Throws an exception of StatusException to terminates the process with a message.
* @param message The message to print.
*/
inline void Die(const std::string& message) {
throw StatusException(Status(Status::APPLICATION_ERROR, message));
}
/**
* Throws an exception of StatusException to terminates the process with a message.
* @param first The first parameter.
* @param rest The rest parameters.
*/
template <typename FIRST, typename... REST>
inline void Die(const FIRST& first, const REST&... rest) {
Die(StrCat(first, rest...));
}
/**
* Makes a file object or die.
* @param impl_name The name of a File implementation: "mmap-para" for MemoryMapParallelFile,
* "mmap-atom" for MemoryMapAtomicFile, "pos-para" for PositionalParallelFile. "pos-atom" fo
* PositionalAtomicFile,
* @param alloc_init_size An initial size of allocation.
* @param alloc_inc_factor A factor to increase the size of allocation.
* @return The created object.
*/
std::unique_ptr<File> MakeFileOrDie(
const std::string& impl_name, int64_t alloc_init_size, double alloc_inc_factor);
/**
* Sets access strategy of the positional access file.
* @param file The file object.
* @param block_size The block size to which all records should be aligned.
* @param is_direct_io If true, the direct I/O access option is set.
* @param is_sync_io If true, the synchronous I/O access option is set.
* @param is_padding If true, the padding access option is set.
* @param is_pagecache If true, the mini page cache option is set.
*/
void SetAccessStrategyOrDie(File* file, int64_t block_size,
bool is_direct_io, bool is_sync_io, bool is_padding,
bool is_pagecache);
/**
* Locks the memory of the beginning region or die.
* @param file The file object.
* @param size The size of the beginning region to lock.
* @details If the operation is not supported, this does nothing.
*/
void LockMemoryOfFileOrDie(File* file, size_t size);
/**
* Sets the head buffer of the positional access file.
* @param file The file object.
* @param size The size of the head buffer.
*/
void SetHeadBufferOfFileOrDie(File* file, int64_t size);
/**
* Prints all records of a DBM in TSV format.
* @param dbm The DBM object.
*/
void PrintDBMRecordsInTSV(DBM* dbm);
/**
* Makes a text whose characters appear in a cyclic pattern.
* @param size The size of the output text.
* @param seed The random seed.
* @return The result text.
*/
std::string MakeCyclishText(size_t size, int32_t seed);
/**
* Makes a text whose character distribution is sililar to natural Englsh.
* @param size The size of the output text.
* @param seed The random seed.
* @return The result text.
*/
std::string MakeNaturalishText(size_t size, int32_t seed);
} // namespace tkrzw
#endif // _TKRZW_CMD_UTIL_H
// END OF FILE