-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcmdcommon.h
383 lines (339 loc) · 11.9 KB
/
cmdcommon.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*************************************************************************************************
* Common symbols for command line utilities
* Copyright (C) 2009-2012 FAL Labs
* This file is part of Kyoto Tycoon.
* This program is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation, either version
* 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*************************************************************************************************/
#ifndef _CMDCOMMON_H // duplication check
#define _CMDCOMMON_H
#include <ktcommon.h>
#include <ktutil.h>
#include <ktsocket.h>
#include <ktthserv.h>
#include <kthttp.h>
#include <ktrpc.h>
#include <ktulog.h>
#include <ktshlib.h>
#include <kttimeddb.h>
#include <ktdbext.h>
#include <ktremotedb.h>
#include <ktplugserv.h>
#include <ktplugdb.h>
#include "myscript.h"
namespace kc = kyotocabinet;
namespace kt = kyototycoon;
// constants
const int32_t OPENDBMAX = 200; // maximum number of open databases
const int32_t THREADMAX = 128; // maximum number of threads
const size_t RECBUFSIZ = 64; // buffer size for a record
const size_t RECBUFSIZL = 1024; // buffer size for a long record
const size_t LINEBUFSIZ = 8192; // buffer size for a line
const double DEFTOUT = 30; // default networking timeout
const int32_t DEFTHNUM = 16; // default number of threads
const double DEFRIV = 0.04; // default interval of replication
const int64_t DEFULIM = 256LL << 20; // default limit size of update log file
const double DEFBGSI = 180; // default interval of background saver
const char* const BGSPATHEXT = "ktss"; // extension of a snapshot file
// global variables
uint64_t g_rnd_x = 123456789;
uint64_t g_rnd_y = 362436069;
uint64_t g_rnd_z = 521288629;
uint64_t g_rnd_w = 88675123;
// function prototypes
void mysrand(int64_t seed);
int64_t myrand(int64_t range);
int64_t memusage();
void oprintf(const char* format, ...);
void oputchar(char c);
void eprintf(const char* format, ...);
void printversion();
void printdata(const char* buf, int32_t size, bool px);
bool mygetline(std::istream* is, std::string* str);
std::string unitnumstr(int64_t num);
std::string unitnumstrbyte(int64_t num);
kt::RPCServer::Logger* stdlogger(const char* prefix, std::ostream* strm);
kc::BasicDB::Logger* stddblogger(const char* prefix, std::ostream* strm);
void printdb(kc::BasicDB* db, bool px = false);
// checker to show progress by printing dots
class DotChecker : public kc::BasicDB::ProgressChecker {
public:
explicit DotChecker(std::ostream* strm, int64_t freq) : freq_(freq), cnt_(0) {}
int64_t count() {
return cnt_;
}
private:
bool check(const char* name, const char* message, int64_t curcnt, int64_t allcnt) {
if (std::strcmp(message, "processing") || freq_ == 0) return true;
if (freq_ < 0) {
cnt_++;
if (cnt_ % -freq_ == 0) {
oputchar('.');
if (cnt_ % (-freq_ * 50) == 0) oprintf(" (%lld)\n", (long long)cnt_);
}
} else {
if (curcnt > cnt_) {
cnt_ = curcnt;
if (cnt_ % freq_ == 0) {
oputchar('.');
if (cnt_ % (freq_ * 50) == 0) oprintf(" (%lld)\n", (long long)cnt_);
}
}
}
return true;
}
int64_t freq_;
int64_t cnt_;
};
// update logger for timed database.
class DBUpdateLogger : public kt::TimedDB::UpdateTrigger {
public:
explicit DBUpdateLogger() :
ulog_(NULL), sid_(0), dbid_(0), rsid_(), tran_(false), trlock_(), trcache_() {}
void initialize(kt::UpdateLogger* ulog, uint16_t sid, uint16_t dbid) {
ulog_ = ulog;
sid_ = sid;
dbid_ = dbid;
}
void trigger(const char* mbuf, size_t msiz) {
int32_t rsid = (int32_t)(intptr_t)rsid_.get();
uint16_t sid = rsid == 0 ? sid_ : (uint16_t)(rsid - 1);
size_t nsiz = sizeof(sid) + sizeof(dbid_) + msiz;
char* nbuf = new char[nsiz];
char* wp = nbuf;
kc::writefixnum(wp, sid, sizeof(sid));
wp += sizeof(sid);
kc::writefixnum(wp, dbid_, sizeof(dbid_));
wp += sizeof(dbid_);
std::memcpy(wp, mbuf, msiz);
if (tran_) {
trlock_.lock();
trcache_.push_back(std::string(nbuf, nsiz));
delete[] nbuf;
trlock_.unlock();
} else {
ulog_->write_volatile(nbuf, nsiz);
}
}
void begin_transaction() {
tran_ = true;
}
void end_transaction(bool commit) {
if (commit && !trcache_.empty()) ulog_->write_bulk(trcache_);
trcache_.clear();
tran_ = false;
}
void set_rsid(uint16_t sid) {
rsid_.set((void*)(intptr_t)(sid + 1));
}
void clear_rsid() {
rsid_.set(0);
}
static const char* parse(const char* mbuf, size_t msiz,
size_t* sp, uint16_t* sidp, uint16_t* dbidp) {
if (msiz < sizeof(uint16_t) + sizeof(uint16_t)) return NULL;
const char* rp = mbuf;
*sidp = kc::readfixnum(rp, sizeof(uint16_t));
rp += sizeof(uint16_t);
*dbidp = kc::readfixnum(rp, sizeof(uint16_t));
rp += sizeof(uint16_t);
*sp = msiz - sizeof(uint16_t) - sizeof(uint16_t);
return rp;
}
private:
kt::UpdateLogger* ulog_;
uint16_t sid_;
uint16_t dbid_;
kc::TSDKey rsid_;
bool tran_;
kc::SpinLock trlock_;
std::vector<std::string> trcache_;
};
// get the random seed
inline void mysrand(int64_t seed) {
g_rnd_x = seed;
for (int32_t i = 0; i < 16; i++) {
myrand(1);
}
}
// get a random number
inline int64_t myrand(int64_t range) {
uint64_t t = g_rnd_x ^ (g_rnd_x << 11);
g_rnd_x = g_rnd_y;
g_rnd_y = g_rnd_z;
g_rnd_z = g_rnd_w;
g_rnd_w = (g_rnd_w ^ (g_rnd_w >> 19)) ^ (t ^ (t >> 8));
return (g_rnd_w & kc::INT64MAX) % range;
}
// get the current memory usage
inline int64_t memusage() {
std::map<std::string, std::string> info;
kc::getsysinfo(&info);
return kc::atoi(info["mem_rss"].c_str());
}
// print formatted information string and flush the buffer
inline void oprintf(const char* format, ...) {
std::string msg;
va_list ap;
va_start(ap, format);
kc::vstrprintf(&msg, format, ap);
va_end(ap);
std::cout << msg;
std::cout.flush();
}
// print a character and flush the buffer
inline void oputchar(char c) {
std::cout << c;
std::cout.flush();
}
// print formatted error string and flush the buffer
inline void eprintf(const char* format, ...) {
std::string msg;
va_list ap;
va_start(ap, format);
kc::vstrprintf(&msg, format, ap);
va_end(ap);
std::cerr << msg;
std::cerr.flush();
}
// print the versin information
inline void printversion() {
oprintf("Kyoto Tycoon %s (%d.%d) on %s (Kyoto Cabinet %s)\n",
kt::VERSION, kt::LIBVER, kt::LIBREV, kc::OSNAME, kc::VERSION);
}
// print record data
inline void printdata(const char* buf, int32_t size, bool px) {
size_t cnt = 0;
char numbuf[kc::NUMBUFSIZ];
while (size-- > 0) {
if (px) {
if (cnt++ > 0) putchar(' ');
std::sprintf(numbuf, "%02X", *(unsigned char*)buf);
std::cout << numbuf;
} else {
std::cout << *buf;
}
buf++;
}
}
// read a line from a file descriptor
inline bool mygetline(std::istream* is, std::string* str) {
str->clear();
bool hit = false;
char c;
while (is->get(c)) {
hit = true;
if (c == '\0' || c == '\r') continue;
if (c == '\n') break;
str->append(1, c);
}
return hit;
}
// convert a number into the string with the decimal unit
inline std::string unitnumstr(int64_t num) {
if (num >= std::pow(1000.0, 6)) {
return kc::strprintf("%.3Lf quintillion", (long double)num / std::pow(1000.0, 6));
} else if (num >= std::pow(1000.0, 5)) {
return kc::strprintf("%.3Lf quadrillion", (long double)num / std::pow(1000.0, 5));
} else if (num >= std::pow(1000.0, 4)) {
return kc::strprintf("%.3Lf trillion", (long double)num / std::pow(1000.0, 4));
} else if (num >= std::pow(1000.0, 3)) {
return kc::strprintf("%.3Lf billion", (long double)num / std::pow(1000.0, 3));
} else if (num >= std::pow(1000.0, 2)) {
return kc::strprintf("%.3Lf million", (long double)num / std::pow(1000.0, 2));
} else if (num >= std::pow(1000.0, 1)) {
return kc::strprintf("%.3Lf thousand", (long double)num / std::pow(1000.0, 1));
}
return kc::strprintf("%lld", (long long)num);
}
// convert a number into the string with the byte unit
inline std::string unitnumstrbyte(int64_t num) {
if ((unsigned long long)num >= 1ULL << 60) {
return kc::strprintf("%.3Lf EiB", (long double)num / (1ULL << 60));
} else if ((unsigned long long)num >= 1ULL << 50) {
return kc::strprintf("%.3Lf PiB", (long double)num / (1ULL << 50));
} else if ((unsigned long long)num >= 1ULL << 40) {
return kc::strprintf("%.3Lf TiB", (long double)num / (1ULL << 40));
} else if ((unsigned long long)num >= 1ULL << 30) {
return kc::strprintf("%.3Lf GiB", (long double)num / (1ULL << 30));
} else if ((unsigned long long)num >= 1ULL << 20) {
return kc::strprintf("%.3Lf MiB", (long double)num / (1ULL << 20));
} else if ((unsigned long long)num >= 1ULL << 10) {
return kc::strprintf("%.3Lf KiB", (long double)num / (1ULL << 10));
}
return kc::strprintf("%lld B", (long long)num);
}
// get the logger into the standard stream
inline kt::RPCServer::Logger* stdlogger(const char* prefix, std::ostream* strm) {
class LoggerImpl : public kt::RPCServer::Logger {
public:
explicit LoggerImpl(std::ostream* strm, const char* prefix) :
strm_(strm), prefix_(prefix) {}
void log(Kind kind, const char* message) {
const char* kstr = "MISC";
switch (kind) {
case kt::RPCServer::Logger::DEBUG: kstr = "DEBUG"; break;
case kt::RPCServer::Logger::INFO: kstr = "INFO"; break;
case kt::RPCServer::Logger::SYSTEM: kstr = "SYSTEM"; break;
case kt::RPCServer::Logger::ERROR: kstr = "ERROR"; break;
}
*strm_ << prefix_ << ": [" << kstr << "]: " << message << std::endl;
}
private:
std::ostream* strm_;
const char* prefix_;
};
static LoggerImpl logger(strm, prefix);
return &logger;
}
// get the database logger into the standard stream
inline kc::BasicDB::Logger* stddblogger(const char* prefix, std::ostream* strm) {
class LoggerImpl : public kc::BasicDB::Logger {
public:
explicit LoggerImpl(std::ostream* strm, const char* prefix) :
strm_(strm), prefix_(prefix) {}
void log(const char* file, int32_t line, const char* func, Kind kind,
const char* message) {
const char* kstr = "MISC";
switch (kind) {
case kc::BasicDB::Logger::DEBUG: kstr = "DEBUG"; break;
case kc::BasicDB::Logger::INFO: kstr = "INFO"; break;
case kc::BasicDB::Logger::WARN: kstr = "WARN"; break;
case kc::BasicDB::Logger::ERROR: kstr = "ERROR"; break;
}
*strm_ << prefix_ << ": [" << kstr << "]: " <<
file << ": " << line << ": " << func << ": " << message << std::endl;
}
private:
std::ostream* strm_;
const char* prefix_;
};
static LoggerImpl logger(strm, prefix);
return &logger;
}
// print all record of a database
inline void printdb(kc::BasicDB* db, bool px) {
class Printer : public kc::DB::Visitor {
public:
explicit Printer(bool px) : px_(px) {}
private:
const char* visit_full(const char* kbuf, size_t ksiz,
const char* vbuf, size_t vsiz, size_t* sp) {
printdata(kbuf, ksiz, px_);
oputchar('\t');
printdata(vbuf, vsiz, px_);
oputchar('\n');
return NOP;
}
bool px_;
} printer(px);
db->iterate(&printer, false);
}
#endif // duplication check
// END OF FILE