-
Notifications
You must be signed in to change notification settings - Fork 6
/
tcrtest.c
1542 lines (1490 loc) · 45 KB
/
tcrtest.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************************************
* The test cases of the remote database 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 <tcrdb.h>
#include "myconf.h"
#define RECBUFSIZ 32 // buffer for records
/* global variables */
const char *g_progname; // program name
/* function prototypes */
int main(int argc, char **argv);
static void usage(void);
static void iprintf(const char *format, ...);
static void iputchar(int c);
static void eprint(TCRDB *rdb, int line, const char *func);
static int myrand(int range);
static bool myopen(TCRDB *rdb, const char *host, int port);
static int runwrite(int argc, char **argv);
static int runread(int argc, char **argv);
static int runremove(int argc, char **argv);
static int runrcat(int argc, char **argv);
static int runmisc(int argc, char **argv);
static int runwicked(int argc, char **argv);
static int runtable(int argc, char **argv);
static int procwrite(const char *host, int port, int cnum, int tout,
int rnum, bool nr, bool rnd);
static int procread(const char *host, int port, int cnum, int tout, int mul, bool rnd);
static int procremove(const char *host, int port, int cnum, int tout, bool rnd);
static int procrcat(const char *host, int port, int cnum, int tout, int rnum,
int shl, bool dai, bool dad, const char *ext, int xopts);
static int procmisc(const char *host, int port, int cnum, int tout, int rnum);
static int procwicked(const char *host, int port, int cnum, int tout, int rnum);
static int proctable(const char *host, int port, int cnum, int tout, int rnum, int exp);
/* main routine */
int main(int argc, char **argv){
g_progname = argv[0];
srand((unsigned int)(tctime() * 1000) % UINT_MAX);
signal(SIGPIPE, SIG_IGN);
if(argc < 2) usage();
int rv = 0;
if(!strcmp(argv[1], "write")){
rv = runwrite(argc, argv);
} else if(!strcmp(argv[1], "read")){
rv = runread(argc, argv);
} else if(!strcmp(argv[1], "remove")){
rv = runremove(argc, argv);
} else if(!strcmp(argv[1], "rcat")){
rv = runrcat(argc, argv);
} else if(!strcmp(argv[1], "misc")){
rv = runmisc(argc, argv);
} else if(!strcmp(argv[1], "wicked")){
rv = runwicked(argc, argv);
} else if(!strcmp(argv[1], "table")){
rv = runtable(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 write [-port num] [-cnum num] [-tout num] [-nr] [-rnd] host rnum\n",
g_progname);
fprintf(stderr, " %s read [-port num] [-cnum num] [-tout num] [-mul num] [-rnd] host\n",
g_progname);
fprintf(stderr, " %s remove [-port num] [-cnum num] [-tout num] [-rnd] host\n", g_progname);
fprintf(stderr, " %s rcat [-port num] [-cnum num] [-tout num] [-shl num] [-dai|-dad]"
" [-ext name] [-xlr|-xlg] host rnum\n", g_progname);
fprintf(stderr, " %s misc [-port num] [-cnum num] [-tout num] host rnum\n", g_progname);
fprintf(stderr, " %s wicked [-port num] [-cnum num] [-tout num] host rnum\n", g_progname);
fprintf(stderr, " %s table [-port num] [-cnum num] [-tout num] [-exp num] host rnum\n",
g_progname);
fprintf(stderr, "\n");
exit(1);
}
/* print formatted information string and flush the buffer */
static void iprintf(const char *format, ...){
va_list ap;
va_start(ap, format);
vprintf(format, ap);
fflush(stdout);
va_end(ap);
}
/* print a character and flush the buffer */
static void iputchar(int c){
putchar(c);
fflush(stdout);
}
/* print error message of abstract database */
static void eprint(TCRDB *rdb, int line, const char *func){
int ecode = tcrdbecode(rdb);
fprintf(stderr, "%s: %d: %s: error: %d: %s\n",
g_progname, line, func, ecode, tcrdberrmsg(ecode));
}
/* get a random number */
static int myrand(int range){
if(range < 2) return 0;
int high = (unsigned int)rand() >> 4;
int low = range * (rand() / (RAND_MAX + 1.0));
low &= (unsigned int)INT_MAX >> 4;
return (high + low) % range;
}
/* open the remote database */
static bool myopen(TCRDB *rdb, const char *host, int port){
bool err = false;
if(strchr(host, ':') || strchr(host, '#')){
if(!tcrdbopen2(rdb, host)) err = true;
} else {
if(!tcrdbopen(rdb, host, port)) err = true;
}
return !err;
}
/* parse arguments of write command */
static int runwrite(int argc, char **argv){
char *host = NULL;
char *rstr = NULL;
int port = TTDEFPORT;
int cnum = 1;
int tout = 0;
bool nr = false;
bool rnd = false;
for(int i = 2; i < argc; i++){
if(!host && argv[i][0] == '-'){
if(!strcmp(argv[i], "-port")){
if(++i >= argc) usage();
port = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-cnum")){
if(++i >= argc) usage();
cnum = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-tout")){
if(++i >= argc) usage();
tout = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-nr")){
nr = true;
} else if(!strcmp(argv[i], "-rnd")){
rnd = true;
} else {
usage();
}
} else if(!host){
host = argv[i];
} else if(!rstr){
rstr = argv[i];
} else {
usage();
}
}
if(!host || !rstr || cnum < 1) usage();
int rnum = tcatoi(rstr);
if(rnum < 1) usage();
int rv = procwrite(host, port, cnum, tout, rnum, nr, rnd);
return rv;
}
/* parse arguments of read command */
static int runread(int argc, char **argv){
char *host = NULL;
int port = TTDEFPORT;
int cnum = 1;
int tout = 0;
int mul = 0;
bool rnd = false;
for(int i = 2; i < argc; i++){
if(!host && argv[i][0] == '-'){
if(!strcmp(argv[i], "-port")){
if(++i >= argc) usage();
port = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-cnum")){
if(++i >= argc) usage();
cnum = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-tout")){
if(++i >= argc) usage();
tout = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-mul")){
if(++i >= argc) usage();
mul = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-rnd")){
rnd = true;
} else {
usage();
}
} else if(!host){
host = argv[i];
} else {
usage();
}
}
if(!host || cnum < 1) usage();
int rv = procread(host, port, cnum, tout, mul, rnd);
return rv;
}
/* parse arguments of remove command */
static int runremove(int argc, char **argv){
char *host = NULL;
int port = TTDEFPORT;
int cnum = 1;
int tout = 0;
bool rnd = false;
for(int i = 2; i < argc; i++){
if(!host && argv[i][0] == '-'){
if(!strcmp(argv[i], "-port")){
if(++i >= argc) usage();
port = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-cnum")){
if(++i >= argc) usage();
cnum = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-tout")){
if(++i >= argc) usage();
tout = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-rnd")){
rnd = true;
} else {
usage();
}
} else if(!host){
host = argv[i];
} else {
usage();
}
}
if(!host || cnum < 1) usage();
int rv = procremove(host, port, cnum, tout, rnd);
return rv;
}
/* parse arguments of rcat command */
static int runrcat(int argc, char **argv){
char *host = NULL;
char *rstr = NULL;
int port = TTDEFPORT;
int cnum = 1;
int tout = 0;
int shl = 0;
bool dai = false;
bool dad = false;
char *ext = NULL;
int xopts = 0;
for(int i = 2; i < argc; i++){
if(!host && argv[i][0] == '-'){
if(!strcmp(argv[i], "-port")){
if(++i >= argc) usage();
port = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-cnum")){
if(++i >= argc) usage();
cnum = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-tout")){
if(++i >= argc) usage();
tout = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-shl")){
if(++i >= argc) usage();
shl = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-dai")){
dai = true;
} else if(!strcmp(argv[i], "-dad")){
dad = true;
} else if(!strcmp(argv[i], "-ext")){
if(++i >= argc) usage();
ext = argv[i];
} else if(!strcmp(argv[i], "-xlr")){
xopts |= RDBXOLCKREC;
} else if(!strcmp(argv[i], "-xlg")){
xopts |= RDBXOLCKGLB;
} else {
usage();
}
} else if(!host){
host = argv[i];
} else if(!rstr){
rstr = argv[i];
} else {
usage();
}
}
if(!host || !rstr || cnum < 1) usage();
int rnum = tcatoi(rstr);
if(rnum < 1) usage();
int rv = procrcat(host, port, cnum, tout, rnum, shl, dai, dad, ext, xopts);
return rv;
}
/* parse arguments of misc command */
static int runmisc(int argc, char **argv){
char *host = NULL;
char *rstr = NULL;
int port = TTDEFPORT;
int cnum = 1;
int tout = 0;
for(int i = 2; i < argc; i++){
if(!host && argv[i][0] == '-'){
if(!strcmp(argv[i], "-port")){
if(++i >= argc) usage();
port = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-cnum")){
if(++i >= argc) usage();
cnum = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-tout")){
if(++i >= argc) usage();
tout = tcatoi(argv[i]);
} else {
usage();
}
} else if(!host){
host = argv[i];
} else if(!rstr){
rstr = argv[i];
} else {
usage();
}
}
if(!host || !rstr || cnum < 1) usage();
int rnum = tcatoi(rstr);
if(rnum < 1) usage();
int rv = procmisc(host, port, cnum, tout, rnum);
return rv;
}
/* parse arguments of wicked command */
static int runwicked(int argc, char **argv){
char *host = NULL;
char *rstr = NULL;
int port = TTDEFPORT;
int cnum = 1;
int tout = 0;
for(int i = 2; i < argc; i++){
if(!host && argv[i][0] == '-'){
if(!strcmp(argv[i], "-port")){
if(++i >= argc) usage();
port = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-cnum")){
if(++i >= argc) usage();
cnum = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-tout")){
if(++i >= argc) usage();
tout = tcatoi(argv[i]);
} else {
usage();
}
} else if(!host){
host = argv[i];
} else if(!rstr){
rstr = argv[i];
} else {
usage();
}
}
if(!host || !rstr || cnum < 1) usage();
int rnum = tcatoi(rstr);
if(rnum < 1) usage();
int rv = procwicked(host, port, cnum, tout, rnum);
return rv;
}
/* parse arguments of table command */
static int runtable(int argc, char **argv){
char *host = NULL;
char *rstr = NULL;
int port = TTDEFPORT;
int cnum = 1;
int tout = 0;
int exp = 0;
for(int i = 2; i < argc; i++){
if(!host && argv[i][0] == '-'){
if(!strcmp(argv[i], "-port")){
if(++i >= argc) usage();
port = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-cnum")){
if(++i >= argc) usage();
cnum = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-tout")){
if(++i >= argc) usage();
tout = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-exp")){
if(++i >= argc) usage();
exp = tcatoi(argv[i]);
} else {
usage();
}
} else if(!host){
host = argv[i];
} else if(!rstr){
rstr = argv[i];
} else {
usage();
}
}
if(!host || !rstr || cnum < 1) usage();
int rnum = tcatoi(rstr);
if(rnum < 1) usage();
int rv = proctable(host, port, cnum, tout, rnum, exp);
return rv;
}
/* perform write command */
static int procwrite(const char *host, int port, int cnum, int tout,
int rnum, bool nr, bool rnd){
iprintf("<Writing Test>\n host=%s port=%d cnum=%d tout=%d rnum=%d nr=%d rnd=%d\n\n",
host, port, cnum, tout, rnum, nr, rnd);
bool err = false;
double stime = tctime();
TCRDB *rdbs[cnum];
for(int i = 0; i < cnum; i++){
rdbs[i] = tcrdbnew();
if(tout > 0) tcrdbtune(rdbs[i], tout, RDBTRECON);
if(!myopen(rdbs[i], host, port)){
eprint(rdbs[i], __LINE__, "tcrdbopen");
err = true;
}
}
TCRDB *rdb = rdbs[0];
if(!rnd && !tcrdbvanish(rdb)){
eprint(rdb, __LINE__, "tcrdbvanish");
err = true;
}
for(int i = 1; i <= rnum; i++){
char buf[RECBUFSIZ];
int len = sprintf(buf, "%08d", rnd ? myrand(rnum) + 1 : i);
if(nr){
if(!tcrdbputnr(rdb, buf, len, buf, len)){
eprint(rdb, __LINE__, "tcrdbputnr");
err = true;
break;
}
} else {
if(!tcrdbput(rdb, buf, len, buf, len)){
eprint(rdb, __LINE__, "tcrdbput");
err = true;
break;
}
}
if(rnum > 250 && i % (rnum / 250) == 0){
iputchar('.');
if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
rdb = rdbs[myrand(rnum)%cnum];
}
}
iprintf("record number: %llu\n", (unsigned long long)tcrdbrnum(rdb));
iprintf("size: %llu\n", (unsigned long long)tcrdbsize(rdb));
for(int i = 0; i < cnum; i++){
if(!tcrdbclose(rdbs[i])){
eprint(rdbs[i], __LINE__, "tcrdbclose");
err = true;
}
tcrdbdel(rdbs[i]);
}
iprintf("time: %.3f\n", tctime() - stime);
iprintf("%s\n\n", err ? "error" : "ok");
return err ? 1 : 0;
}
/* perform read command */
static int procread(const char *host, int port, int cnum, int tout, int mul, bool rnd){
iprintf("<Reading Test>\n host=%s port=%d cnum=%d tout=%d mul=%d rnd=%d\n\n",
host, port, cnum, tout, mul, rnd);
bool err = false;
double stime = tctime();
TCRDB *rdbs[cnum];
for(int i = 0; i < cnum; i++){
rdbs[i] = tcrdbnew();
if(tout > 0) tcrdbtune(rdbs[i], tout, RDBTRECON);
if(!myopen(rdbs[i], host, port)){
eprint(rdbs[i], __LINE__, "tcrdbopen");
err = true;
}
}
TCRDB *rdb = rdbs[0];
TCMAP *recs = (mul > 1) ? tcmapnew() : NULL;
int rnum = tcrdbrnum(rdb);
for(int i = 1; i <= rnum; i++){
char kbuf[RECBUFSIZ];
int ksiz = sprintf(kbuf, "%08d", rnd ? myrand(rnum) + 1 : i);
if(mul > 1){
tcmapput(recs, kbuf, ksiz, kbuf, ksiz);
if(i % mul == 0){
if(!tcrdbget3(rdb, recs)){
eprint(rdb, __LINE__, "tcrdbget3");
err = true;
break;
}
tcmapclear(recs);
}
} else {
int vsiz;
char *vbuf = tcrdbget(rdb, kbuf, ksiz, &vsiz);
if(!vbuf && !rnd){
eprint(rdb, __LINE__, "tcrdbget");
err = true;
break;
}
tcfree(vbuf);
}
if(rnum > 250 && i % (rnum / 250) == 0){
iputchar('.');
if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
rdb = rdbs[myrand(rnum)%cnum];
}
}
if(recs) tcmapdel(recs);
iprintf("record number: %llu\n", (unsigned long long)tcrdbrnum(rdb));
iprintf("size: %llu\n", (unsigned long long)tcrdbsize(rdb));
for(int i = 0; i < cnum; i++){
if(!tcrdbclose(rdbs[i])){
eprint(rdbs[i], __LINE__, "tcrdbclose");
err = true;
}
tcrdbdel(rdbs[i]);
}
iprintf("time: %.3f\n", tctime() - stime);
iprintf("%s\n\n", err ? "error" : "ok");
return err ? 1 : 0;
}
/* perform remove command */
static int procremove(const char *host, int port, int cnum, int tout, bool rnd){
iprintf("<Removing Test>\n host=%s port=%d cnum=%d tout=%d rnd=%d\n\n",
host, port, cnum, tout, rnd);
bool err = false;
double stime = tctime();
TCRDB *rdbs[cnum];
for(int i = 0; i < cnum; i++){
rdbs[i] = tcrdbnew();
if(tout > 0) tcrdbtune(rdbs[i], tout, RDBTRECON);
if(!myopen(rdbs[i], host, port)){
eprint(rdbs[i], __LINE__, "tcrdbopen");
err = true;
}
}
TCRDB *rdb = rdbs[0];
int rnum = tcrdbrnum(rdb);
for(int i = 1; i <= rnum; i++){
char kbuf[RECBUFSIZ];
int ksiz = sprintf(kbuf, "%08d", rnd ? myrand(rnum) + 1 : i);
if(!tcrdbout(rdb, kbuf, ksiz) && !rnd){
eprint(rdb, __LINE__, "tcrdbout");
err = true;
break;
}
if(rnum > 250 && i % (rnum / 250) == 0){
iputchar('.');
if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
rdb = rdbs[myrand(rnum)%cnum];
}
}
iprintf("record number: %llu\n", (unsigned long long)tcrdbrnum(rdb));
iprintf("size: %llu\n", (unsigned long long)tcrdbsize(rdb));
for(int i = 0; i < cnum; i++){
if(!tcrdbclose(rdbs[i])){
eprint(rdbs[i], __LINE__, "tcrdbclose");
err = true;
}
tcrdbdel(rdbs[i]);
}
iprintf("time: %.3f\n", tctime() - stime);
iprintf("%s\n\n", err ? "error" : "ok");
return err ? 1 : 0;
}
/* perform rcat command */
static int procrcat(const char *host, int port, int cnum, int tout, int rnum,
int shl, bool dai, bool dad, const char *ext, int xopts){
iprintf("<Random Concatenating Test>\n host=%s port=%d cnum=%d tout=%d rnum=%d"
" shl=%d dai=%d dad=%d ext=%s xopts=%d\n\n",
host, port, cnum, tout, rnum, shl, dai, dad, ext ? ext : "", xopts);
int pnum = rnum / 5 + 1;
bool err = false;
double stime = tctime();
TCRDB *rdbs[cnum];
for(int i = 0; i < cnum; i++){
rdbs[i] = tcrdbnew();
if(tout > 0) tcrdbtune(rdbs[i], tout, RDBTRECON);
if(!myopen(rdbs[i], host, port)){
eprint(rdbs[i], __LINE__, "tcrdbopen");
err = true;
}
}
TCRDB *rdb = rdbs[0];
for(int i = 1; i <= rnum; i++){
char kbuf[RECBUFSIZ];
int ksiz = sprintf(kbuf, "%d", myrand(pnum) + 1);
if(shl > 0){
if(!tcrdbputshl(rdb, kbuf, ksiz, kbuf, ksiz, shl)){
eprint(rdb, __LINE__, "tcrdbputshl");
err = true;
break;
}
} else if(dai){
if(tcrdbaddint(rdb, kbuf, ksiz, 1) == INT_MIN){
eprint(rdb, __LINE__, "tcrdbaddint");
err = true;
break;
}
} else if(dad){
if(isnan(tcrdbadddouble(rdb, kbuf, ksiz, 1.0))){
eprint(rdb, __LINE__, "tcrdbadddouble");
err = true;
break;
}
} else if(ext){
int xsiz;
char *xbuf = tcrdbext(rdb, ext, xopts, kbuf, ksiz, kbuf, ksiz, &xsiz);
if(!xbuf && tcrdbecode(rdb) != TTEMISC){
eprint(rdb, __LINE__, "tcrdbext");
err = true;
break;
}
tcfree(xbuf);
} else {
if(!tcrdbputcat(rdb, kbuf, ksiz, kbuf, ksiz)){
eprint(rdb, __LINE__, "tcrdbputcat");
err = true;
break;
}
}
if(rnum > 250 && i % (rnum / 250) == 0){
iputchar('.');
if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
rdb = rdbs[myrand(rnum)%cnum];
}
}
iprintf("record number: %llu\n", (unsigned long long)tcrdbrnum(rdb));
iprintf("size: %llu\n", (unsigned long long)tcrdbsize(rdb));
for(int i = 0; i < cnum; i++){
if(!tcrdbclose(rdbs[i])){
eprint(rdbs[i], __LINE__, "tcrdbclose");
err = true;
}
tcrdbdel(rdbs[i]);
}
iprintf("time: %.3f\n", tctime() - stime);
iprintf("%s\n\n", err ? "error" : "ok");
return err ? 1 : 0;
}
/* perform misc command */
static int procmisc(const char *host, int port, int cnum, int tout, int rnum){
iprintf("<Miscellaneous Test>\n host=%s port=%d cnum=%d tout=%d rnum=%d\n\n",
host, port, cnum, tout, rnum);
bool err = false;
double stime = tctime();
TCRDB *rdbs[cnum];
for(int i = 0; i < cnum; i++){
rdbs[i] = tcrdbnew();
if(tout > 0) tcrdbtune(rdbs[i], tout, RDBTRECON);
if(!myopen(rdbs[i], host, port)){
eprint(rdbs[i], __LINE__, "tcrdbopen");
err = true;
}
}
TCRDB *rdb = rdbs[0];
if(!tcrdbvanish(rdb)){
eprint(rdb, __LINE__, "tcrdbvanish");
err = true;
}
iprintf("writing:\n");
for(int i = 1; i <= rnum; i++){
char buf[RECBUFSIZ];
int len = sprintf(buf, "%08d", i);
if(myrand(10) > 0){
if(!tcrdbputkeep(rdb, buf, len, buf, len)){
eprint(rdb, __LINE__, "tcrdbputkeep");
err = true;
break;
}
} else {
if(!tcrdbputnr(rdb, buf, len, buf, len)){
eprint(rdb, __LINE__, "tcrdbputnr");
err = true;
break;
}
}
if(rnum > 250 && i % (rnum / 250) == 0){
iputchar('.');
if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
rdb = rdbs[myrand(rnum)%cnum];
}
}
for(int i = 0; i < cnum; i++){
if(tcrdbrnum(rdbs[i]) < 1){
eprint(rdb, __LINE__, "tcrdbrnum");
err = true;
break;
}
}
iprintf("reading:\n");
for(int i = 1; i <= rnum; i++){
char kbuf[RECBUFSIZ];
int ksiz = sprintf(kbuf, "%08d", i);
int vsiz;
char *vbuf = tcrdbget(rdb, kbuf, ksiz, &vsiz);
if(!vbuf){
eprint(rdb, __LINE__, "tcrdbget");
err = true;
break;
} else if(vsiz != ksiz || memcmp(vbuf, kbuf, vsiz)){
eprint(rdb, __LINE__, "(validation)");
err = true;
tcfree(vbuf);
break;
}
tcfree(vbuf);
if(rnum > 250 && i % (rnum / 250) == 0){
iputchar('.');
if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
rdb = rdbs[myrand(rnum)%cnum];
}
}
if(tcrdbrnum(rdb) != rnum){
eprint(rdb, __LINE__, "(validation)");
err = true;
}
iprintf("random writing:\n");
for(int i = 1; i <= rnum; i++){
char kbuf[RECBUFSIZ];
int ksiz = sprintf(kbuf, "%d", myrand(rnum) + 1);
char vbuf[RECBUFSIZ];
int vsiz = myrand(RECBUFSIZ);
memset(vbuf, '*', vsiz);
if(!tcrdbput(rdb, kbuf, ksiz, vbuf, vsiz)){
eprint(rdb, __LINE__, "tcrdbput");
err = true;
break;
}
int rsiz;
char *rbuf = tcrdbget(rdb, kbuf, ksiz, &rsiz);
if(!rbuf){
eprint(rdb, __LINE__, "tcrdbget");
err = true;
break;
}
if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
eprint(rdb, __LINE__, "(validation)");
err = true;
tcfree(rbuf);
break;
}
if(rnum > 250 && i % (rnum / 250) == 0){
iputchar('.');
if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
rdb = rdbs[myrand(rnum)%cnum];
}
tcfree(rbuf);
}
iprintf("word writing:\n");
const char *words[] = {
"a", "A", "bb", "BB", "ccc", "CCC", "dddd", "DDDD", "eeeee", "EEEEEE",
"mikio", "hirabayashi", "tokyo", "cabinet", "hyper", "estraier", "19780211", "birth day",
"one", "first", "two", "second", "three", "third", "four", "fourth", "five", "fifth",
"_[1]_", "uno", "_[2]_", "dos", "_[3]_", "tres", "_[4]_", "cuatro", "_[5]_", "cinco",
"[\xe5\xb9\xb3\xe6\x9e\x97\xe5\xb9\xb9\xe9\x9b\x84]", "[\xe9\xa6\xac\xe9\xb9\xbf]", NULL
};
for(int i = 0; words[i] != NULL; i += 2){
const char *kbuf = words[i];
int ksiz = strlen(kbuf);
const char *vbuf = words[i+1];
int vsiz = strlen(vbuf);
if(!tcrdbputkeep(rdb, kbuf, ksiz, vbuf, vsiz)){
eprint(rdb, __LINE__, "tcrdbputkeep");
err = true;
break;
}
if(rnum > 250) putchar('.');
}
if(rnum > 250) iprintf(" (%08d)\n", (int)(sizeof(words) / sizeof(*words)));
iprintf("random erasing:\n");
for(int i = 1; i <= rnum; i++){
char kbuf[RECBUFSIZ];
int ksiz = sprintf(kbuf, "%d", myrand(rnum));
if(!tcrdbout(rdb, kbuf, ksiz) && tcrdbecode(rdb) != TTENOREC){
eprint(rdb, __LINE__, "tcrdbout");
err = true;
break;
}
if(rnum > 250 && i % (rnum / 250) == 0){
iputchar('.');
if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
rdb = rdbs[myrand(rnum)%cnum];
}
}
iprintf("writing:\n");
for(int i = 1; i <= rnum; i++){
char kbuf[RECBUFSIZ];
int ksiz = sprintf(kbuf, "[%d]", i);
char vbuf[RECBUFSIZ];
int vsiz = i % RECBUFSIZ;
memset(vbuf, '*', vsiz);
if(!tcrdbputkeep(rdb, kbuf, ksiz, vbuf, vsiz)){
eprint(rdb, __LINE__, "tcrdbputkeep");
err = true;
break;
}
if(vsiz < 1){
char tbuf[PATH_MAX];
for(int j = 0; j < PATH_MAX; j++){
tbuf[j] = myrand(0x100);
}
if(!tcrdbput(rdb, kbuf, ksiz, tbuf, PATH_MAX)){
eprint(rdb, __LINE__, "tcrdbput");
err = true;
break;
}
}
if(rnum > 250 && i % (rnum / 250) == 0){
iputchar('.');
if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
rdb = rdbs[myrand(rnum)%cnum];
}
}
iprintf("erasing:\n");
for(int i = 1; i <= rnum; i++){
if(i % 2 == 1){
char kbuf[RECBUFSIZ];
int ksiz = sprintf(kbuf, "[%d]", i);
if(!tcrdbout(rdb, kbuf, ksiz)){
eprint(rdb, __LINE__, "tcrdbout");
err = true;
break;
}
tcrdbout(rdb, kbuf, ksiz);
}
if(rnum > 250 && i % (rnum / 250) == 0){
iputchar('.');
if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
rdb = rdbs[myrand(rnum)%cnum];
}
}
iprintf("random multi reading:\n");
for(int i = 1; i <= rnum; i++){
if(i % 10 == 1){
TCMAP *recs = tcmapnew();
int num = myrand(10);
if(myrand(2) == 0){
char pbuf[RECBUFSIZ];
int psiz = sprintf(pbuf, "%d", myrand(100) + 1);
TCLIST *keys = tcrdbfwmkeys(rdb, pbuf, psiz, num);
for(int j = 0; j < tclistnum(keys); j++){
int ksiz;
const char *kbuf = tclistval(keys, j, &ksiz);
tcmapput(recs, kbuf, ksiz, kbuf, ksiz);
}
tclistdel(keys);
} else {
for(int j = 0; j < num; j++){
char kbuf[RECBUFSIZ];
int ksiz = sprintf(kbuf, "%d", myrand(rnum) + 1);
tcmapput(recs, kbuf, ksiz, kbuf, ksiz);
}
}
if(tcrdbget3(rdb, recs)){
tcmapiterinit(recs);
const char *kbuf;
int ksiz;
while((kbuf = tcmapiternext(recs, &ksiz))){
int vsiz;
const char *vbuf = tcmapiterval(kbuf, &vsiz);
int rsiz;
char *rbuf = tcrdbget(rdb, kbuf, ksiz, &rsiz);
if(rbuf){
if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
eprint(rdb, __LINE__, "(validation)");
err = true;
}
tcfree(rbuf);
} else {
eprint(rdb, __LINE__, "tcrdbget");
err = true;
}
}
} else {
eprint(rdb, __LINE__, "tcrdbget3");
err = true;
}
tcmapdel(recs);
}
if(rnum > 250 && i % (rnum / 250) == 0){
iputchar('.');
if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
rdb = rdbs[myrand(rnum)%cnum];
}
}
iprintf("script extension calling:\n");
for(int i = 1; i <= rnum; i++){
char kbuf[RECBUFSIZ];
int ksiz = sprintf(kbuf, "(%d)", i);
const char *name;
switch(myrand(7)){
default: name = "put"; break;
case 1: name = "putkeep"; break;
case 2: name = "putcat"; break;
case 3: name = "out"; break;
case 4: name = "get"; break;
case 5: name = "iterinit"; break;
case 6: name = "iternext"; break;
}
int vsiz;
char *vbuf = tcrdbext(rdb, name, 0, kbuf, ksiz, kbuf, ksiz, &vsiz);
if(!vbuf && tcrdbecode(rdb) != TTEMISC){
eprint(rdb, __LINE__, "tcrdbext");
err = true;
break;
}
tcfree(vbuf);
if(rnum > 250 && i % (rnum / 250) == 0){
iputchar('.');
if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
rdb = rdbs[myrand(rnum)%cnum];
}
}
iprintf("checking versatile functions:\n");
TCLIST *args = tclistnew();
for(int i = 1; i <= rnum; i++){
if(myrand(10) == 0){
const char *name;
switch(myrand(3)){
default: name = "putlist"; break;
case 1: name = "outlist"; break;
case 2: name = "getlist"; break;
}
TCLIST *res = tcrdbmisc(rdb, name, 0, args);
if(res){
tclistdel(res);
} else {
eprint(rdb, __LINE__, "tcrdbmisc");
err = true;
break;
}
tclistclear(args);
} else {
char kbuf[RECBUFSIZ];
int ksiz = sprintf(kbuf, "(%d)", myrand(rnum));
tclistpush(args, kbuf, ksiz);
}
if(rnum > 250 && i % (rnum / 250) == 0){
iputchar('.');
if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
}
}
tclistdel(args);
char *sbuf = tcrdbstat(rdb);
if(sbuf){
tcfree(sbuf);
} else {
eprint(rdb, __LINE__, "tcrdbstat");
err = true;
}
if(!tcrdbsync(rdb)){
eprint(rdb, __LINE__, "tcrdbsync");
err = true;
}
if(!tcrdboptimize(rdb, NULL)){
eprint(rdb, __LINE__, "tcrdbsync");
err = true;
}
if(!tcrdbvanish(rdb)){