~tokyocabinet/tokyocabinet/jmansion-1.3.22-win32port

« back to all changes in this revision

Viewing changes to tcatest.c

  • Committer: Baptiste Lepilleur
  • Date: 2009-07-30 19:18:20 UTC
  • Revision ID: blep@users.sourceforge.net-20090730191820-39v7rhqpb68x5bpc
Tags: tokyocabinet-1.3.20
releaseĀ 1.3.20

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
static int runrcat(int argc, char **argv);
39
39
static int runmisc(int argc, char **argv);
40
40
static int runwicked(int argc, char **argv);
 
41
static int runcompare(int argc, char **argv);
41
42
static int procwrite(const char *name, int rnum);
42
43
static int procread(const char *name);
43
44
static int procremove(const char *name);
44
45
static int procrcat(const char *name, int rnum);
45
46
static int procmisc(const char *name, int rnum);
46
47
static int procwicked(const char *name, int rnum);
 
48
static int proccompare(const char *name, int tnum, int rnum);
47
49
 
48
50
 
49
51
/* main routine */
64
66
    rv = runmisc(argc, argv);
65
67
  } else if(!strcmp(argv[1], "wicked")){
66
68
    rv = runwicked(argc, argv);
 
69
  } else if(!strcmp(argv[1], "compare")){
 
70
    rv = runcompare(argc, argv);
67
71
  } else {
68
72
    usage();
69
73
  }
82
86
  fprintf(stderr, "  %s rcat name rnum\n", g_progname);
83
87
  fprintf(stderr, "  %s misc name rnum\n", g_progname);
84
88
  fprintf(stderr, "  %s wicked name rnum\n", g_progname);
 
89
  fprintf(stderr, "  %s compare name tnum rnum\n", g_progname);
85
90
  fprintf(stderr, "\n");
86
91
  exit(1);
87
92
}
244
249
}
245
250
 
246
251
 
 
252
/* parse arguments of compare command */
 
253
static int runcompare(int argc, char **argv){
 
254
  char *name = NULL;
 
255
  char *tstr = NULL;
 
256
  char *rstr = NULL;
 
257
  for(int i = 2; i < argc; i++){
 
258
    if(!name && argv[i][0] == '-'){
 
259
      usage();
 
260
    } else if(!name){
 
261
      name = argv[i];
 
262
    } else if(!tstr){
 
263
      tstr = argv[i];
 
264
    } else if(!rstr){
 
265
      rstr = argv[i];
 
266
    } else {
 
267
      usage();
 
268
    }
 
269
  }
 
270
  if(!name || !tstr || !rstr) usage();
 
271
  int tnum = tcatoi(tstr);
 
272
  int rnum = tcatoi(rstr);
 
273
  if(tnum < 1 || rnum < 1) usage();
 
274
  int rv = proccompare(name, tnum, rnum);
 
275
  return rv;
 
276
}
 
277
 
 
278
 
247
279
/* perform write command */
248
280
static int procwrite(const char *name, int rnum){
249
281
  iprintf("<Writing Test>\n  name=%s  rnum=%d\n\n", name, rnum);
790
822
}
791
823
 
792
824
 
 
825
/* perform compare command */
 
826
static int proccompare(const char *name, int tnum, int rnum){
 
827
  iprintf("<Comparison Test>\n  name=%s  tnum=%d  rnum=%d\n\n", name, tnum, rnum);
 
828
  bool err = false;
 
829
  double stime = tctime();
 
830
  char path[PATH_MAX];
 
831
  TCMDB *mdb = tcmdbnew2(rnum / 2);
 
832
  TCNDB *ndb = tcndbnew();
 
833
  TCHDB *hdb = tchdbnew();
 
834
  int hopts = 0;
 
835
  if(myrand(2) == 1) hopts |= HDBTLARGE;
 
836
  if(myrand(2) == 1) hopts |= HDBTBZIP;
 
837
  if(!tchdbtune(hdb, rnum / 2, -1, -1, hopts)){
 
838
    eprint(NULL, "tchdbtune");
 
839
    err = true;
 
840
  }
 
841
  sprintf(path, "%s.tch", name);
 
842
  int homode = HDBOWRITER | HDBOCREAT | HDBOTRUNC;
 
843
  if(myrand(2) == 1) homode |= HDBOTSYNC;
 
844
  if(!tchdbopen(hdb, path, homode)){
 
845
    eprint(NULL, "tchdbopen");
 
846
    err = true;
 
847
  }
 
848
  TCBDB *bdb = tcbdbnew();
 
849
  int bopts = 0;
 
850
  if(myrand(2) == 1) bopts |= BDBTLARGE;
 
851
  if(myrand(2) == 1) bopts |= BDBTBZIP;
 
852
  if(!tcbdbtune(bdb, 5, 5, rnum / 10, -1, -1, bopts)){
 
853
    eprint(NULL, "tcbdbtune");
 
854
    err = true;
 
855
  }
 
856
  sprintf(path, "%s.tcb", name);
 
857
  int bomode = BDBOWRITER | BDBOCREAT | BDBOTRUNC;
 
858
  if(myrand(2) == 1) bomode |= BDBOTSYNC;
 
859
  if(!tcbdbopen(bdb, path, bomode)){
 
860
    eprint(NULL, "tcbdbopen");
 
861
    err = true;
 
862
  }
 
863
  for(int t = 1; !err && t <= tnum; t++){
 
864
    bool commit = myrand(2) == 0;
 
865
    iprintf("transaction %d (%s):\n", t, commit ? "commit" : "abort");
 
866
    if(!tchdbtranbegin(hdb)){
 
867
      eprint(NULL, "tchdbtranbegin");
 
868
      err = true;
 
869
    }
 
870
    if(!tcbdbtranbegin(bdb)){
 
871
      eprint(NULL, "tcbdbtranbegin");
 
872
      err = true;
 
873
    }
 
874
    int act = myrand(7);
 
875
    for(int i = 1; !err && i <= rnum; i++){
 
876
      if(myrand(10) == 0) act = myrand(7);
 
877
      char kbuf[RECBUFSIZ];
 
878
      int ksiz = sprintf(kbuf, "%d", myrand(i) + 1);
 
879
      char vbuf[RECBUFSIZ];
 
880
      int vsiz = sprintf(vbuf, "%d:%d:%d", t, i, myrand(i));
 
881
      switch(act){
 
882
      case 0:
 
883
        if(!tchdbput(hdb, kbuf, ksiz, vbuf, vsiz)){
 
884
          eprint(NULL, "tchdbput");
 
885
          err = true;
 
886
        }
 
887
        if(!tcbdbput(bdb, kbuf, ksiz, vbuf, vsiz)){
 
888
          eprint(NULL, "tcbdbput");
 
889
          err = true;
 
890
        }
 
891
        if(commit){
 
892
          tcmdbput(mdb, kbuf, ksiz, vbuf, vsiz);
 
893
          tcndbput(ndb, kbuf, ksiz, vbuf, vsiz);
 
894
        }
 
895
        break;
 
896
      case 1:
 
897
        if(!tchdbputkeep(hdb, kbuf, ksiz, vbuf, vsiz) && tchdbecode(hdb) != TCEKEEP){
 
898
          eprint(NULL, "tchdbputkeep");
 
899
          err = true;
 
900
        }
 
901
        if(!tcbdbputkeep(bdb, kbuf, ksiz, vbuf, vsiz) && tcbdbecode(bdb) != TCEKEEP){
 
902
          eprint(NULL, "tcbdbputkeep");
 
903
          err = true;
 
904
        }
 
905
        if(commit){
 
906
          tcmdbputkeep(mdb, kbuf, ksiz, vbuf, vsiz);
 
907
          tcndbputkeep(ndb, kbuf, ksiz, vbuf, vsiz);
 
908
        }
 
909
        break;
 
910
      case 2:
 
911
        if(!tchdbputcat(hdb, kbuf, ksiz, vbuf, vsiz)){
 
912
          eprint(NULL, "tchdbputcat");
 
913
          err = true;
 
914
        }
 
915
        if(!tcbdbputcat(bdb, kbuf, ksiz, vbuf, vsiz)){
 
916
          eprint(NULL, "tcbdbputcat");
 
917
          err = true;
 
918
        }
 
919
        if(commit){
 
920
          tcmdbputcat(mdb, kbuf, ksiz, vbuf, vsiz);
 
921
          tcndbputcat(ndb, kbuf, ksiz, vbuf, vsiz);
 
922
        }
 
923
        break;
 
924
      case 3:
 
925
        if(tchdbaddint(hdb, kbuf, ksiz, 1) == INT_MIN && tchdbecode(hdb) != TCEKEEP){
 
926
          eprint(NULL, "tchdbaddint");
 
927
          err = true;
 
928
        }
 
929
        if(tcbdbaddint(bdb, kbuf, ksiz, 1) == INT_MIN && tcbdbecode(bdb) != TCEKEEP){
 
930
          eprint(NULL, "tchdbaddint");
 
931
          err = true;
 
932
        }
 
933
        if(commit){
 
934
          tcmdbaddint(mdb, kbuf, ksiz, 1);
 
935
          tcndbaddint(ndb, kbuf, ksiz, 1);
 
936
        }
 
937
        break;
 
938
      case 4:
 
939
        if(isnan(tchdbadddouble(hdb, kbuf, ksiz, 1.0)) && tchdbecode(hdb) != TCEKEEP){
 
940
          eprint(NULL, "tchdbadddouble");
 
941
          err = true;
 
942
        }
 
943
        if(isnan(tcbdbadddouble(bdb, kbuf, ksiz, 1.0)) && tcbdbecode(bdb) != TCEKEEP){
 
944
          eprint(NULL, "tchdbadddouble");
 
945
          err = true;
 
946
        }
 
947
        if(commit){
 
948
          tcmdbadddouble(mdb, kbuf, ksiz, 1.0);
 
949
          tcndbadddouble(ndb, kbuf, ksiz, 1.0);
 
950
        }
 
951
        break;
 
952
      default:
 
953
        if(!tchdbout(hdb, kbuf, ksiz) && tchdbecode(hdb) != TCENOREC){
 
954
          eprint(NULL, "tchdbout");
 
955
          err = true;
 
956
        }
 
957
        if(!tcbdbout(bdb, kbuf, ksiz) && tcbdbecode(bdb) != TCENOREC){
 
958
          eprint(NULL, "tcbdbout");
 
959
          err = true;
 
960
        }
 
961
        if(commit){
 
962
          tcmdbout(mdb, kbuf, ksiz);
 
963
          tcndbout(ndb, kbuf, ksiz);
 
964
        }
 
965
        break;
 
966
      }
 
967
      if(rnum > 250 && i % (rnum / 250) == 0){
 
968
        iputchar('.');
 
969
        if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
 
970
      }
 
971
    }
 
972
    if(commit){
 
973
      if(!tchdbtrancommit(hdb)){
 
974
        eprint(NULL, "tchdbcommit");
 
975
        err = true;
 
976
      }
 
977
      if(!tcbdbtrancommit(bdb)){
 
978
        eprint(NULL, "tcbdbcommit");
 
979
        err = true;
 
980
      }
 
981
    } else {
 
982
      if(myrand(10) == 0){
 
983
        if(!tchdbclose(hdb)){
 
984
          eprint(NULL, "tchdbclose");
 
985
          err = true;
 
986
        }
 
987
        sprintf(path, "%s.tch", name);
 
988
        if(!tchdbopen(hdb, path, HDBOWRITER)){
 
989
          eprint(NULL, "tchdbopen");
 
990
          err = true;
 
991
        }
 
992
        if(!tcbdbclose(bdb)){
 
993
          eprint(NULL, "tcbdbclose");
 
994
          err = true;
 
995
        }
 
996
        sprintf(path, "%s.tcb", name);
 
997
        if(!tcbdbopen(bdb, path, BDBOWRITER)){
 
998
          eprint(NULL, "tcbdbopen");
 
999
          err = true;
 
1000
        }
 
1001
      } else {
 
1002
        if(!tchdbtranabort(hdb)){
 
1003
          eprint(NULL, "tchdbtranabort");
 
1004
          err = true;
 
1005
        }
 
1006
        if(!tcbdbtranabort(bdb)){
 
1007
          eprint(NULL, "tcbdbtranabort");
 
1008
          err = true;
 
1009
        }
 
1010
      }
 
1011
    }
 
1012
  }
 
1013
  iprintf("checking consistency of all:\n");
 
1014
  if(tchdbrnum(hdb) != tcbdbrnum(bdb)){
 
1015
    eprint(NULL, "(validation)");
 
1016
    err = true;
 
1017
  }
 
1018
  int inum = 0;
 
1019
  tcmdbiterinit(mdb);
 
1020
  char *kbuf;
 
1021
  int ksiz;
 
1022
  for(int i = 1; (kbuf = tcmdbiternext(mdb, &ksiz)) != NULL; i++, inum++){
 
1023
    int vsiz;
 
1024
    char *vbuf = tcmdbget(mdb, kbuf, ksiz, &vsiz);
 
1025
    int rsiz;
 
1026
    char *rbuf = tcndbget(ndb, kbuf, ksiz, &rsiz);
 
1027
    if(!rbuf || rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
 
1028
      eprint(NULL, "tcndbget");
 
1029
      err = true;
 
1030
    }
 
1031
    tcfree(rbuf);
 
1032
    rbuf = tchdbget(hdb, kbuf, ksiz, &rsiz);
 
1033
    if(!rbuf || rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
 
1034
      eprint(NULL, "tchdbget");
 
1035
      err = true;
 
1036
    }
 
1037
    tcfree(rbuf);
 
1038
    rbuf = tcbdbget(bdb, kbuf, ksiz, &rsiz);
 
1039
    if(!rbuf || rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
 
1040
      eprint(NULL, "tcbdbget");
 
1041
      err = true;
 
1042
    }
 
1043
    tcfree(rbuf);
 
1044
    tcfree(vbuf);
 
1045
    tcfree(kbuf);
 
1046
    if(rnum > 250 && i % (rnum / 250) == 0){
 
1047
      iputchar('.');
 
1048
      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
 
1049
    }
 
1050
  }
 
1051
  if(rnum > 250) iprintf(" (%08d)\n", inum);
 
1052
  iprintf("checking consistency of hash:\n");
 
1053
  inum = 0;
 
1054
  if(!tchdbiterinit(hdb)){
 
1055
    eprint(NULL, "tchdbiterinit");
 
1056
    err = true;
 
1057
  }
 
1058
  for(int i = 1; (kbuf = tchdbiternext(hdb, &ksiz)) != NULL; i++, inum++){
 
1059
    int vsiz;
 
1060
    char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz);
 
1061
    int rsiz;
 
1062
    char *rbuf = tcmdbget(mdb, kbuf, ksiz, &rsiz);
 
1063
    if(!rbuf || rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
 
1064
      eprint(NULL, "(validation)");
 
1065
      err = true;
 
1066
    }
 
1067
    tcfree(rbuf);
 
1068
    tcfree(vbuf);
 
1069
    tcfree(kbuf);
 
1070
    if(rnum > 250 && i % (rnum / 250) == 0){
 
1071
      iputchar('.');
 
1072
      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
 
1073
    }
 
1074
  }
 
1075
  if(rnum > 250) iprintf(" (%08d)\n", inum);
 
1076
  iprintf("checking consistency of tree:\n");
 
1077
  BDBCUR *cur = tcbdbcurnew(bdb);
 
1078
  if(!tcbdbcurfirst(cur) && tcbdbecode(bdb) != TCENOREC){
 
1079
    eprint(NULL, "tcbdbcurfirst");
 
1080
    err = true;
 
1081
  }
 
1082
  for(int i = 1; (kbuf = tcbdbcurkey(cur, &ksiz)) != NULL; i++, inum++){
 
1083
    int vsiz;
 
1084
    char *vbuf = tcbdbget(bdb, kbuf, ksiz, &vsiz);
 
1085
    int rsiz;
 
1086
    char *rbuf = tcndbget(ndb, kbuf, ksiz, &rsiz);
 
1087
    if(!rbuf || rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
 
1088
      eprint(NULL, "(validation)");
 
1089
      err = true;
 
1090
    }
 
1091
    tcfree(rbuf);
 
1092
    tcfree(vbuf);
 
1093
    tcfree(kbuf);
 
1094
    if(rnum > 250 && i % (rnum / 250) == 0){
 
1095
      iputchar('.');
 
1096
      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
 
1097
    }
 
1098
    tcbdbcurnext(cur);
 
1099
  }
 
1100
  if(rnum > 250) iprintf(" (%08d)\n", inum);
 
1101
  tcbdbcurdel(cur);
 
1102
  if(!tchdbclose(hdb)){
 
1103
    eprint(NULL, "tcbdbclose");
 
1104
    err = true;
 
1105
  }
 
1106
  if(!tcbdbclose(bdb)){
 
1107
    eprint(NULL, "tcbdbclose");
 
1108
    err = true;
 
1109
  }
 
1110
  tcmdbdel(mdb);
 
1111
  tcndbdel(ndb);
 
1112
  tchdbdel(hdb);
 
1113
  tcbdbdel(bdb);
 
1114
  iprintf("time: %.3f\n", tctime() - stime);
 
1115
  iprintf("%s\n\n", err ? "error" : "ok");
 
1116
  return err ? 1 : 0;
 
1117
}
 
1118
 
 
1119
 
793
1120
 
794
1121
// END OF FILE