Example #1
0
/* print members of fixed-length database */
static void mprint(TCFDB *fdb){
  if(fdb->cnt_writerec < 0) return;
  my_my_my_iprintf("minimum ID number: %llu\n", (unsigned long long)tcfdbmin(fdb));
  my_my_my_iprintf("maximum ID number: %llu\n", (unsigned long long)tcfdbmax(fdb));
  my_my_my_iprintf("width of the value: %u\n", (unsigned int)tcfdbwidth(fdb));
  my_my_my_iprintf("limit file size: %llu\n", (unsigned long long)tcfdblimsiz(fdb));
  my_my_my_iprintf("limit ID number: %llu\n", (unsigned long long)tcfdblimid(fdb));
  my_my_my_iprintf("cnt_writerec: %lld\n", (long long)fdb->cnt_writerec);
  my_my_my_iprintf("cnt_readrec: %lld\n", (long long)fdb->cnt_readrec);
  my_my_my_iprintf("cnt_truncfile: %lld\n", (long long)fdb->cnt_truncfile);
}
Example #2
0
/* perform inform command */
static int procinform(const char *path, int omode) {
    TCFDB *fdb = tcfdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd);
    if (!tcfdbopen(fdb, path, FDBOREADER | omode)) {
        printerr(fdb);
        tcfdbdel(fdb);
        return 1;
    }
    bool err = false;
    const char *npath = tcfdbpath(fdb);
    if (!npath) npath = "(unknown)";
    printf("path: %s\n", npath);
    const char *type = "(unknown)";
    switch (tcfdbtype(fdb)) {
        case TCDBTHASH: type = "hash";
            break;
        case TCDBTBTREE: type = "btree";
            break;
        case TCDBTFIXED: type = "fixed";
            break;
        case TCDBTTABLE: type = "table";
            break;
    }
    printf("database type: %s\n", type);
    uint8_t flags = tcfdbflags(fdb);
    printf("additional flags:");
    if (flags & FDBFOPEN) printf(" open");
    if (flags & FDBFFATAL) printf(" fatal");
    printf("\n");
    printf("minimum ID number: %" PRIu64 "\n", (uint64_t) tcfdbmin(fdb));
    printf("maximum ID number: %" PRIu64 "\n", (uint64_t) tcfdbmax(fdb));
    printf("width of the value: %u\n", (unsigned int) tcfdbwidth(fdb));
    printf("limit file size: %" PRIu64 "\n", (uint64_t) tcfdblimsiz(fdb));
    printf("limit ID number: %" PRIu64 "\n", (uint64_t) tcfdblimid(fdb));
    printf("inode number: %" PRId64 "\n", (int64_t) tcfdbinode(fdb));
    char date[48];
    tcdatestrwww(tcfdbmtime(fdb), INT_MAX, date);
    printf("modified time: %s\n", date);
    printf("record number: %" PRIu64 "\n", (uint64_t) tcfdbrnum(fdb));
    printf("file size: %" PRIu64 "\n", (uint64_t) tcfdbfsiz(fdb));
    if (!tcfdbclose(fdb)) {
        if (!err) printerr(fdb);
        err = true;
    }
    tcfdbdel(fdb);
    return err ? 1 : 0;
}
Example #3
0
/* perform wicked command */
static int procwicked(const char *path, int tnum, int rnum, int omode, bool nc){
  iprintf("<Writing Test>\n  seed=%u  path=%s  tnum=%d  rnum=%d  omode=%d  nc=%d\n\n",
          g_randseed, path, tnum, rnum, omode, nc);
  bool err = false;
  double stime = tctime();
  TCFDB *fdb = tcfdbnew();
  if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd);
  if(!tcfdbsetmutex(fdb)){
    eprint(fdb, __LINE__, "tcfdbsetmutex");
    err = true;
  }
  if(!tcfdbtune(fdb, RECBUFSIZ * 2, EXHEADSIZ + (RECBUFSIZ * 2 + sizeof(int)) * rnum * tnum)){
    eprint(fdb, __LINE__, "tcfdbtune");
    err = true;
  }
  if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)){
    eprint(fdb, __LINE__, "tcfdbopen");
    err = true;
  }
  if(!tcfdbiterinit(fdb)){
    eprint(fdb, __LINE__, "tcfdbiterinit");
    err = true;
  }
  TARGWICKED targs[tnum];
  pthread_t threads[tnum];
  TCMAP *map = tcmapnew();
  if(tnum == 1){
    targs[0].fdb = fdb;
    targs[0].rnum = rnum;
    targs[0].nc = nc;
    targs[0].id = 0;
    targs[0].map = map;
    if(threadwicked(targs) != NULL) err = true;
  } else {
    for(int i = 0; i < tnum; i++){
      targs[i].fdb = fdb;
      targs[i].rnum = rnum;
      targs[i].nc = nc;
      targs[i].id = i;
      targs[i].map = map;
      if(pthread_create(threads + i, NULL, threadwicked, targs + i) != 0){
        eprint(fdb, __LINE__, "pthread_create");
        targs[i].id = -1;
        err = true;
      }
    }
    for(int i = 0; i < tnum; i++){
      if(targs[i].id == -1) continue;
      void *rv;
      if(pthread_join(threads[i], &rv) != 0){
        eprint(fdb, __LINE__, "pthread_join");
        err = true;
      } else if(rv){
        err = true;
      }
    }
  }
  if(!nc){
    if(!tcfdbsync(fdb)){
      eprint(fdb, __LINE__, "tcfdbsync");
      err = true;
    }
    if(tcfdbrnum(fdb) != tcmaprnum(map)){
      eprint(fdb, __LINE__, "(validation)");
      err = true;
    }
    int end = rnum * tnum;
    for(int i = 1; i <= end && !err; i++){
      char kbuf[RECBUFSIZ];
      int ksiz = sprintf(kbuf, "%d", i);
      int vsiz;
      const char *vbuf = tcmapget(map, kbuf, ksiz, &vsiz);
      int rsiz;
      char *rbuf = tcfdbget2(fdb, kbuf, ksiz, &rsiz);
      if(vbuf){
        iputchar('.');
        if(vsiz > tcfdbwidth(fdb)) vsiz = tcfdbwidth(fdb);
        if(!rbuf){
          eprint(fdb, __LINE__, "tcfdbget");
          err = true;
        } else if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
          eprint(fdb, __LINE__, "(validation)");
          err = true;
        }
      } else {
        iputchar('*');
        if(rbuf || tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "(validation)");
          err = true;
        }
      }
      tcfree(rbuf);
      if(i % 50 == 0) iprintf(" (%08d)\n", i);
    }
    if(rnum % 50 > 0) iprintf(" (%08d)\n", rnum);
  }
  tcmapdel(map);
  iprintf("record number: %llu\n", (unsigned long long)tcfdbrnum(fdb));
  iprintf("size: %llu\n", (unsigned long long)tcfdbfsiz(fdb));
  mprint(fdb);
  sysprint();
  if(!tcfdbclose(fdb)){
    eprint(fdb, __LINE__, "tcfdbclose");
    err = true;
  }
  tcfdbdel(fdb);
  iprintf("time: %.3f\n", tctime() - stime);
  iprintf("%s\n\n", err ? "error" : "ok");
  return err ? 1 : 0;
}
Example #4
0
/* thread the typical function */
static void *threadtypical(void *targ){
  TCFDB *fdb = ((TARGTYPICAL *)targ)->fdb;
  int rnum = ((TARGTYPICAL *)targ)->rnum;
  bool nc = ((TARGTYPICAL *)targ)->nc;
  int rratio = ((TARGTYPICAL *)targ)->rratio;
  int id = ((TARGTYPICAL *)targ)->id;
  bool err = false;
  TCMAP *map = (!nc && id == 0) ? tcmapnew2(rnum + 1) : NULL;
  int base = id * rnum;
  int mrange = tclmax(50 + rratio, 100);
  int width = tcfdbwidth(fdb);
  for(int i = 1; !err && i <= rnum; i++){
    char buf[RECBUFSIZ];
    int len = sprintf(buf, "%08d", base + myrandnd(i) + 1);
    int rnd = myrand(mrange);
    if(rnd < 10){
      if(!tcfdbput2(fdb, buf, len, buf, len)){
        eprint(fdb, __LINE__, "tcfdbput2");
        err = true;
      }
      if(map) tcmapput(map, buf, len, buf, len);
    } else if(rnd < 15){
      if(!tcfdbputkeep2(fdb, buf, len, buf, len) && tcfdbecode(fdb) != TCEKEEP){
        eprint(fdb, __LINE__, "tcfdbputkeep2");
        err = true;
      }
      if(map) tcmapputkeep(map, buf, len, buf, len);
    } else if(rnd < 20){
      if(!tcfdbputcat2(fdb, buf, len, buf, len)){
        eprint(fdb, __LINE__, "tcfdbputcat2");
        err = true;
      }
      if(map) tcmapputcat(map, buf, len, buf, len);
    } else if(rnd < 25){
      if(!tcfdbout2(fdb, buf, len) && tcfdbecode(fdb) && tcfdbecode(fdb) != TCENOREC){
        eprint(fdb, __LINE__, "tcfdbout");
        err = true;
      }
      if(map) tcmapout(map, buf, len);
    } else if(rnd < 26){
      if(myrand(10) == 0 && !tcfdbiterinit(fdb) && tcfdbecode(fdb) != TCENOREC){
        eprint(fdb, __LINE__, "tcfdbiterinit");
        err = true;
      }
      for(int j = 0; !err && j < 10; j++){
        if(tcfdbiternext(fdb) < 1 &&
           tcfdbecode(fdb) != TCEINVALID && tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "tcfdbiternext");
          err = true;
        }
      }
    } else {
      int vsiz;
      char *vbuf = tcfdbget2(fdb, buf, len, &vsiz);
      if(vbuf){
        if(map){
          int msiz = 0;
          const char *mbuf = tcmapget(map, buf, len, &msiz);
          if(msiz > width) msiz = width;
          if(!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){
            eprint(fdb, __LINE__, "(validation)");
            err = true;
          }
        }
        tcfree(vbuf);
      } else {
        if(tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "tcfdbget");
          err = true;
        }
        if(map && tcmapget(map, buf, len, &vsiz)){
          eprint(fdb, __LINE__, "(validation)");
          err = true;
        }
      }
    }
    if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
    }
  }
  if(map){
    tcmapiterinit(map);
    int ksiz;
    const char *kbuf;
    while(!err && (kbuf = tcmapiternext(map, &ksiz)) != NULL){
      int vsiz;
      char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz);
      if(vbuf){
        int msiz = 0;
        const char *mbuf = tcmapget(map, kbuf, ksiz, &msiz);
        if(msiz > width) msiz = width;
        if(!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){
          eprint(fdb, __LINE__, "(validation)");
          err = true;
        }
        tcfree(vbuf);
      } else {
        eprint(fdb, __LINE__, "(validation)");
        err = true;
      }
    }
    tcmapdel(map);
  }
  return err ? "error" : NULL;
}
Example #5
-1
/* perform wicked command */
static int procwicked(const char *path, int rnum, bool mt, int omode){
  my_my_my_iprintf("<Wicked Writing Test>\n  seed=%u  path=%s  rnum=%d  mt=%d  omode=%d\n\n",
          g_randseed, path, rnum, mt, omode);
  bool err = false;
  double stime = tctime();
  TCFDB *fdb = tcfdbnew();
  if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd);
  if(mt && !tcfdbsetmutex(fdb)){
    eprint(fdb, __LINE__, "tcfdbsetmutex");
    err = true;
  }
  if(!tcfdbtune(fdb, RECBUFSIZ * 2, EXHEADSIZ + (RECBUFSIZ * 2 + sizeof(int)) * rnum)){
    eprint(fdb, __LINE__, "tcfdbtune");
    err = true;
  }
  if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)){
    eprint(fdb, __LINE__, "tcfdbopen");
    err = true;
  }
  if(!tcfdbiterinit(fdb)){
    eprint(fdb, __LINE__, "tcfdbiterinit");
    err = true;
  }
  TCMAP *map = tcmapnew2(rnum / 5);
  for(int i = 1; i <= rnum && !err; i++){
    uint64_t id = myrand(rnum) + 1;
    char kbuf[RECBUFSIZ];
    int ksiz = sprintf(kbuf, "%llu", (unsigned long long)id);
    char vbuf[RECBUFSIZ];
    int vsiz = myrand(RECBUFSIZ);
    memset(vbuf, '*', vsiz);
    vbuf[vsiz] = '\0';
    char *rbuf;
    switch(myrand(16)){
      case 0:
        iputchar('0');
        if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){
          eprint(fdb, __LINE__, "tcfdbput2");
          err = true;
        }
        tcmapput(map, kbuf, ksiz, vbuf, vsiz);
        break;
      case 1:
        iputchar('1');
        if(!tcfdbput3(fdb, kbuf, vbuf)){
          eprint(fdb, __LINE__, "tcfdbput3");
          err = true;
        }
        tcmapput2(map, kbuf, vbuf);
        break;
      case 2:
        iputchar('2');
        if(!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP){
          eprint(fdb, __LINE__, "tcfdbputkeep2");
          err = true;
        }
        tcmapputkeep(map, kbuf, ksiz, vbuf, vsiz);
        break;
      case 3:
        iputchar('3');
        if(!tcfdbputkeep3(fdb, kbuf, vbuf) && tcfdbecode(fdb) != TCEKEEP){
          eprint(fdb, __LINE__, "tcfdbputkeep3");
          err = true;
        }
        tcmapputkeep2(map, kbuf, vbuf);
        break;
      case 4:
        iputchar('4');
        if(!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz)){
          eprint(fdb, __LINE__, "tcfdbputcat2");
          err = true;
        }
        tcmapputcat(map, kbuf, ksiz, vbuf, vsiz);
        break;
      case 5:
        iputchar('5');
        if(!tcfdbputcat3(fdb, kbuf, vbuf)){
          eprint(fdb, __LINE__, "tcfdbputcat3");
          err = true;
        }
        tcmapputcat2(map, kbuf, vbuf);
        break;
      case 6:
        iputchar('6');
        if(myrand(10) == 0){
          if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){
            eprint(fdb, __LINE__, "tcfdbout2");
            err = true;
          }
          tcmapout(map, kbuf, ksiz);
        }
        break;
      case 7:
        iputchar('7');
        if(myrand(10) == 0){
          if(!tcfdbout3(fdb, kbuf) && tcfdbecode(fdb) != TCENOREC){
            eprint(fdb, __LINE__, "tcfdbout3");
            err = true;
          }
          tcmapout2(map, kbuf);
        }
        break;
      case 8:
        iputchar('8');
        if(!(rbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz))){
          if(tcfdbecode(fdb) != TCENOREC){
            eprint(fdb, __LINE__, "tcfdbget2");
            err = true;
          }
          rbuf = tcsprintf("[%d]", myrand(i + 1));
          vsiz = strlen(rbuf);
        }
        vsiz += myrand(vsiz);
        rbuf = tcrealloc(rbuf, vsiz + 1);
        for(int j = 0; j < vsiz; j++){
          rbuf[j] = myrand(0x100);
        }
        if(!tcfdbput2(fdb, kbuf, ksiz, rbuf, vsiz)){
          eprint(fdb, __LINE__, "tcfdbput2");
          err = true;
        }
        tcmapput(map, kbuf, ksiz, rbuf, vsiz);
        tcfree(rbuf);
        break;
      case 9:
        iputchar('9');
        if(!(rbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz)) && tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "tcfdbget2");
          err = true;
        }
        tcfree(rbuf);
        break;
      case 10:
        iputchar('A');
        if(!(rbuf = tcfdbget3(fdb, kbuf)) && tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "tcfdbget3");
          err = true;
        }
        tcfree(rbuf);
        break;
      case 11:
        iputchar('B');
        if(myrand(1) == 0) vsiz = 1;
        if((vsiz = tcfdbget4(fdb, id, vbuf, vsiz)) < 0 && tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "tcfdbget4");
          err = true;
        }
        break;
      case 12:
        iputchar('C');
        if(myrand(rnum / 128) == 0){
          if(myrand(2) == 0){
            if(!tcfdbiterinit(fdb)){
              eprint(fdb, __LINE__, "tcfdbiterinit");
              err = true;
            }
          } else {
            if(!tcfdbiterinit2(fdb, myrand(rnum) + 1) && tcfdbecode(fdb) != TCENOREC){
              eprint(fdb, __LINE__, "tcfdbiterinit2");
              err = true;
            }
          }
        }
        for(int j = myrand(rnum) / 1000 + 1; j >= 0; j--){
          if(tcfdbiternext(fdb) < 0){
            int ecode = tcfdbecode(fdb);
            if(ecode != TCEINVALID && ecode != TCENOREC){
              eprint(fdb, __LINE__, "tcfdbiternext");
              err = true;
            }
          }
        }
        break;
      default:
        iputchar('@');
        if(myrand(10000) == 0) srand((unsigned int)(tctime() * 1000) % UINT_MAX);
        if(myrand(rnum / 16 + 1) == 0){
          int cnt = myrand(30);
          for(int j = 0; j < rnum && !err; j++){
            ksiz = sprintf(kbuf, "%d", i + j);
            if(tcfdbout2(fdb, kbuf, ksiz)){
              cnt--;
            } else if(tcfdbecode(fdb) != TCENOREC){
              eprint(fdb, __LINE__, "tcfdbout2");
              err = true;
            }
            tcmapout(map, kbuf, ksiz);
            if(cnt < 0) break;
          }
        }
        break;
    }
    if(i % 50 == 0) my_my_my_iprintf(" (%08d)\n", i);
    if(i == rnum / 2){
      if(!tcfdbclose(fdb)){
        eprint(fdb, __LINE__, "tcfdbclose");
        err = true;
      }
      if(!tcfdbopen(fdb, path, FDBOWRITER | omode)){
        eprint(fdb, __LINE__, "tcfdbopen");
        err = true;
      }
    } else if(i == rnum / 4){
      char *npath = tcsprintf("%s-tmp", path);
      if(!tcfdbcopy(fdb, npath)){
        eprint(fdb, __LINE__, "tcfdbcopy");
        err = true;
      }
      TCFDB *nfdb = tcfdbnew();
      if(!tcfdbopen(nfdb, npath, FDBOREADER | omode)){
        eprint(nfdb, __LINE__, "tcfdbopen");
        err = true;
      }
      tcfdbdel(nfdb);
      unlink(npath);
      tcfree(npath);
      if(!tcfdboptimize(fdb, RECBUFSIZ, -1)){
        eprint(fdb, __LINE__, "tcfdboptimize");
        err = true;
      }
      if(!tcfdbiterinit(fdb)){
        eprint(fdb, __LINE__, "tcfdbiterinit");
        err = true;
      }
    } else if(i == rnum / 8){
      if(!tcfdbtranbegin(fdb)){
        eprint(fdb, __LINE__, "tcfdbtranbegin");
        err = true;
      }
    } else if(i == rnum / 8 + rnum / 16){
      if(!tcfdbtrancommit(fdb)){
        eprint(fdb, __LINE__, "tcfdbtrancommit");
        err = true;
      }
    }
  }
  if(rnum % 50 > 0) my_my_my_iprintf(" (%08d)\n", rnum);
  if(!tcfdbsync(fdb)){
    eprint(fdb, __LINE__, "tcfdbsync");
    err = true;
  }
  if(tcfdbrnum(fdb) != tcmaprnum(map)){
    eprint(fdb, __LINE__, "(validation)");
    err = true;
  }
  for(int i = 1; i <= rnum && !err; i++){
    char kbuf[RECBUFSIZ];
    int ksiz = sprintf(kbuf, "%d", i);
    int vsiz;
    const char *vbuf = tcmapget(map, kbuf, ksiz, &vsiz);
    int rsiz;
    char *rbuf = tcfdbget2(fdb, kbuf, ksiz, &rsiz);
    if(vbuf){
      iputchar('.');
      if(vsiz > RECBUFSIZ) vsiz = RECBUFSIZ;
      if(!rbuf){
        eprint(fdb, __LINE__, "tcfdbget2");
        err = true;
      } else if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
        eprint(fdb, __LINE__, "(validation)");
        err = true;
      }
    } else {
      iputchar('*');
      if(rbuf || tcfdbecode(fdb) != TCENOREC){
        eprint(fdb, __LINE__, "(validation)");
        err = true;
      }
    }
    tcfree(rbuf);
    if(i % 50 == 0) my_my_my_iprintf(" (%08d)\n", i);
  }
  if(rnum % 50 > 0) my_my_my_iprintf(" (%08d)\n", rnum);
  tcmapiterinit(map);
  int ksiz;
  const char *kbuf;
  for(int i = 1; (kbuf = tcmapiternext(map, &ksiz)) != NULL; i++){
    iputchar('+');
    int vsiz;
    const char *vbuf = tcmapiterval(kbuf, &vsiz);
    if(vsiz > tcfdbwidth(fdb)) vsiz = tcfdbwidth(fdb);
    int rsiz;
    char *rbuf = tcfdbget2(fdb, kbuf, ksiz, &rsiz);
    if(!rbuf){
      eprint(fdb, __LINE__, "tcfdbget2");
      err = true;
    } else if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
      eprint(fdb, __LINE__, "(validation)");
      err = true;
    }
    tcfree(rbuf);
    if(!tcfdbout2(fdb, kbuf, ksiz)){
      eprint(fdb, __LINE__, "tcfdbout2");
      err = true;
    }
    if(i % 50 == 0) my_my_my_iprintf(" (%08d)\n", i);
  }
  int mrnum = tcmaprnum(map);
  if(mrnum % 50 > 0) my_my_my_iprintf(" (%08d)\n", mrnum);
  if(tcfdbrnum(fdb) != 0){
    eprint(fdb, __LINE__, "(validation)");
    err = true;
  }
  my_my_my_iprintf("record number: %llu\n", (unsigned long long)tcfdbrnum(fdb));
  my_my_my_iprintf("size: %llu\n", (unsigned long long)tcfdbfsiz(fdb));
  mprint(fdb);
  sysprint();
  tcmapdel(map);
  if(!tcfdbclose(fdb)){
    eprint(fdb, __LINE__, "tcfdbclose");
    err = true;
  }
  tcfdbdel(fdb);
  my_my_my_iprintf("time: %.3f\n", tctime() - stime);
  my_my_my_iprintf("%s\n\n", err ? "error" : "ok");
  return err ? 1 : 0;
}