-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathttulmgr.c
266 lines (237 loc) · 6.52 KB
/
ttulmgr.c
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
/*************************************************************************************************
* The test cases of the update log API
* Copyright (C) 2006-2010 FAL Labs
* This file is part of Tokyo Tyrant.
* Tokyo Tyrant is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
* version 2.1 of the License or any later version. Tokyo Tyrant 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 Lesser General Public
* License for more details.
* You should have received a copy of the GNU Lesser General Public License along with Tokyo
* Tyrant; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA.
*************************************************************************************************/
#include <tculog.h>
#include "myconf.h"
#define RECBUFSIZ 32 // buffer for records
typedef struct { // type of structure for read thread
TCULRD *ulrd;
int id;
int rnum;
} TARGREAD;
/* global variables */
const char *g_progname; // program name
/* function prototypes */
int main(int argc, char **argv);
static void usage(void);
static void printerr(const char *msg);
static int printhex(const char *ptr, int size);
static char *mygetline(FILE *ifp);
static int runexport(int argc, char **argv);
static int runimport(int argc, char **argv);
static int procexport(const char *upath, uint64_t ts, uint32_t sid);
static int procimport(const char *upath, uint64_t lim);
/* main routine */
int main(int argc, char **argv){
g_progname = argv[0];
if(argc < 2) usage();
int rv = 0;
if(!strcmp(argv[1], "export")){
rv = runexport(argc, argv);
} else if(!strcmp(argv[1], "import")){
rv = runimport(argc, argv);
} else {
usage();
}
return rv;
}
/* print the usage and exit */
static void usage(void){
fprintf(stderr, "%s: test cases of the remote database API of Tokyo Tyrant\n", g_progname);
fprintf(stderr, "\n");
fprintf(stderr, "usage:\n");
fprintf(stderr, " %s export [-ts num] [-sid num] upath\n", g_progname);
fprintf(stderr, " %s import upath\n", g_progname);
fprintf(stderr, "\n");
exit(1);
}
/* print error information */
static void printerr(const char *msg){
fprintf(stderr, "%s: error: %s\n", g_progname, msg);
}
/* print record data */
static int printhex(const char *ptr, int size){
int len = 0;
while(size-- > 0){
if(len > 0) putchar(' ');
len += printf("%02X", *(unsigned char *)ptr);
ptr++;
}
return len;
}
/* read a line from a file descriptor */
static char *mygetline(FILE *ifp){
int len = 0;
int blen = 1024;
char *buf = tcmalloc(blen);
bool end = true;
int c;
while((c = fgetc(ifp)) != EOF){
end = false;
if(c == '\0') continue;
if(blen <= len){
blen *= 2;
buf = tcrealloc(buf, blen + 1);
}
if(c == '\n' || c == '\r') c = '\0';
buf[len++] = c;
if(c == '\0') break;
}
if(end){
tcfree(buf);
return NULL;
}
buf[len] = '\0';
return buf;
}
/* parse arguments of export command */
static int runexport(int argc, char **argv){
char *upath = NULL;
uint64_t ts = 0;
uint32_t sid = UINT32_MAX;
for(int i = 2; i < argc; i++){
if(!upath && argv[i][0] == '-'){
if(!strcmp(argv[i], "-ts")){
if(++i >= argc) usage();
ts = ttstrtots(argv[i]);
} else if(!strcmp(argv[i], "-sid")){
if(++i >= argc) usage();
sid = tcatoi(argv[i]);
} else {
usage();
}
} else if(!upath){
upath = argv[i];
} else {
usage();
}
}
if(!upath) usage();
int rv = procexport(upath, ts, sid);
return rv;
}
/* parse arguments of import command */
static int runimport(int argc, char **argv){
char *upath = NULL;
uint64_t lim = 0;
for(int i = 2; i < argc; i++){
if(!upath && argv[i][0] == '-'){
if(!strcmp(argv[i], "-lim")){
if(++i >= argc) usage();
lim = tcatoix(argv[i]);
} else {
usage();
}
} else if(!upath){
upath = argv[i];
} else {
usage();
}
}
if(!upath) usage();
int rv = procimport(upath, lim);
return rv;
}
/* perform export command */
static int procexport(const char *upath, uint64_t ts, uint32_t sid){
TCULOG *ulog = tculognew();
if(!tculogopen(ulog, upath, 0)){
printerr("tculogopen");
return 1;
}
bool err = false;
TCULRD *ulrd = tculrdnew(ulog, ts);
if(ulrd){
const char *rbuf;
int rsiz;
uint64_t rts;
uint32_t rsid, rmid;
while(!err && (rbuf = tculrdread(ulrd, &rsiz, &rts, &rsid, &rmid)) != NULL){
if(rsid == sid || rmid == sid) continue;
printf("%llu\t%u:%u\t", (unsigned long long)rts, (unsigned int)rsid, (unsigned int)rmid);
if(rsiz >= 2){
printf("%s\t", ttcmdidtostr(((unsigned char *)rbuf)[1]));
printhex(rbuf, rsiz);
putchar('\n');
} else {
printf("[broken entry]\n");
}
}
tculrddel(ulrd);
} else {
printerr("tculrdnew");
err = true;
}
if(!tculogclose(ulog)){
printerr("tculogclose");
err = true;
}
tculogdel(ulog);
return err ? 1 : 0;
}
/* perform import command */
static int procimport(const char *upath, uint64_t lim){
TCULOG *ulog = tculognew();
if(!tculogopen(ulog, upath, lim)){
printerr("tculogopen");
return 1;
}
bool err = false;
char *line;
while(!err && (line = mygetline(stdin)) != NULL){
uint64_t ts = ttstrtots(line);
char *pv = strchr(line, '\t');
if(!pv || ts < 1){
tcfree(line);
continue;
}
pv++;
uint32_t sid = tcatoi(pv);
char *mp = strchr(pv, ':');
pv = strchr(pv, '\t');
if(!pv){
tcfree(line);
continue;
}
pv++;
uint32_t mid = 0;
if(mp && mp < pv) mid = tcatoi(mp + 1);
pv = strchr(pv, '\t');
if(!pv){
tcfree(line);
continue;
}
pv++;
int osiz;
unsigned char *obj = (unsigned char *)tchexdecode(pv, &osiz);
if(!obj || osiz < 3 || *obj != TTMAGICNUM){
tcfree(obj);
tcfree(line);
continue;
}
if(!tculogwrite(ulog, ts, sid, mid, obj, osiz)){
printerr("tculogwrite");
err = true;
}
tcfree(obj);
tcfree(line);
}
if(!tculogclose(ulog)){
printerr("tculogclose");
err = true;
}
tculogdel(ulog);
return err ? 1 : 0;
}
// END OF FILE