/* Duplicate a region on memory. */ void *tcmemdup(const void *ptr, size_t size){ assert(ptr && size >= 0); char *p = tcmalloc(size + 1); memcpy(p, ptr, size); p[size] = '\0'; return p; }
/* duplication callback function */ static void *pdprocfunc(const void *vbuf, int vsiz, int *sp, void *op){ if(op){ char *buf = NULL; int len = 0; switch((int)(intptr_t)op){ case 1: len = vsiz + 1; buf = tcmalloc(len + 1); memset(buf, '*', len); break; case 2: buf = (void *)-1; break; } *sp = len; return buf; } if(myrand(4) == 0) return (void *)-1; if(myrand(2) == 0) return NULL; int len = myrand(RECBUFSIZ); char buf[RECBUFSIZ]; memset(buf, '*', len); *sp = len; return tcmemdup(buf, len); }
/* Search a q-gram database. `wdb' specifies the q-gram database object. `word' specifies the string of the word to be matched to. `np' specifies the pointer to the variable into which the number of elements of the return value is assigned. If successful, the return value is the pointer to an array of ID numbers of the corresponding records. */ static uint64_t *tcwdbsearchimpl(TCWDB *wdb, const char *word, int *np){ assert(wdb && word && np); int wlen = strlen(word); if(wlen > WDBMAXWORDLEN){ tcbdbsetecode(wdb->idx, TCEINVALID, __FILE__, __LINE__, __func__); return NULL; } int vsiz; const char *vbuf = tcbdbget3(wdb->idx, word, wlen, &vsiz); if(!vbuf){ vbuf = ""; vsiz = 0; } uint64_t *res = tcmalloc(WDBRESUNIT * sizeof(*res)); int rnum = 0; int ranum = WDBRESUNIT; while(vsiz > 0){ int step; uint64_t id; TDREADVNUMBUF64(vbuf, id, step); vbuf += step; vsiz -= step; if(rnum >= ranum){ ranum *= 2; res = tcrealloc(res, ranum * sizeof(*res)); } res[rnum++] = id; } *np = rnum; return res; }
/* Duplicate a string on memory. */ char *tcstrdup(const void *str){ assert(str); int size = strlen(str); char *p = tcmalloc(size + 1); memcpy(p, str, size); p[size] = '\0'; return p; }
/* Create a word database object. */ TCWDB *tcwdbnew(void){ TCWDB *wdb = tcmalloc(sizeof(*wdb)); wdb->mmtx = tcmalloc(sizeof(pthread_rwlock_t)); if(pthread_rwlock_init(wdb->mmtx, NULL) != 0) tcmyfatal("pthread_rwlock_init failed"); wdb->idx = tcbdbnew(); if(!tcbdbsetmutex(wdb->idx)) tcmyfatal("tcbdbsetmutex failed"); wdb->open = false; wdb->cc = NULL; wdb->icsiz = WDBCCDEFICSIZ; wdb->lcnum = 0; wdb->dtokens = NULL; wdb->dids = NULL; wdb->etnum = WDBDEFETNUM; wdb->opts = 0; wdb->fwmmax = WDBDEFFWMMAX; wdb->synccb = NULL; wdb->syncopq = NULL; wdb->addcb = NULL; wdb->addopq = NULL; return wdb; }
/* create a binary object from a hexadecimal string */ static char *hextoobj(const char *str, int *sp){ int len = strlen(str); char *buf = tcmalloc(len + 1); int j = 0; for(int i = 0; i < len; i += 2){ while(strchr(" \n\r\t\f\v", str[i])){ i++; } char mbuf[3]; if((mbuf[0] = str[i]) == '\0') break; if((mbuf[1] = str[i+1]) == '\0') break; mbuf[2] = '\0'; buf[j++] = (char)strtol(mbuf, NULL, 16); } buf[j] = '\0'; *sp = j; return buf; }
/* setcomparator */ JNIEXPORT jboolean JNICALL Java_tokyocabinet_BDB_setcomparator (JNIEnv *env, jobject self, jobject cmp){ if(!cmp){ throwillarg(env); return false; } TCBDB *bdb = (TCBDB *)(intptr_t)(*env)->GetLongField(env, self, bdb_fid_ptr); TCCMPOP *cmpop = tcbdbcmpop(bdb); if(cmpop){ (*env)->DeleteGlobalRef(env, cmpop->obj); tcfree(cmpop); } JavaVM *vm; (*env)->GetJavaVM(env, &vm); jclass clscmp = (*env)->GetObjectClass(env, cmp); cmpop = tcmalloc(sizeof(*cmpop)); cmpop->vm = vm; cmpop->obj = (*env)->NewGlobalRef(env, cmp); cmpop->mid = (*env)->GetMethodID(env, clscmp, "compare", "([B[B)I"); return tcbdbsetcmpfunc(bdb, (TCCMP)tccmpobj, cmpop); }
/* read a line from a file descriptor */ static char *mygetline(FILE *ifp){ int len = 0; int blen = 1024; char *buf = tcmalloc(blen + 1); 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; }
/* perform misc command */ static int procmisc(const char *path, int rnum, bool mt, int omode){ my_my_my_iprintf("<Miscellaneous 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, EXHEADSIZ + (RECBUFSIZ + sizeof(int)) * rnum)){ eprint(fdb, __LINE__, "tcfdbtune"); err = true; } if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)){ eprint(fdb, __LINE__, "tcfdbopen"); err = true; } if(TCUSEPTHREAD){ TCFDB *fdbdup = tcfdbnew(); if(tcfdbopen(fdbdup, path, FDBOREADER)){ eprint(fdb, __LINE__, "(validation)"); err = true; } else if(tcfdbecode(fdbdup) != TCETHREAD){ eprint(fdb, __LINE__, "(validation)"); err = true; } tcfdbdel(fdbdup); } my_my_my_iprintf("writing:\n"); for(int i = 1; i <= rnum; i++){ char buf[RECBUFSIZ]; int len = sprintf(buf, "%08d", i); if(!tcfdbputkeep2(fdb, buf, len, buf, len)){ eprint(fdb, __LINE__, "tcfdbputkeep"); err = true; break; } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } my_my_my_iprintf("reading:\n"); for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "%08d", i); int vsiz; char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz); if(!vbuf){ eprint(fdb, __LINE__, "tcfdbget"); err = true; break; } else if(vsiz != ksiz || memcmp(vbuf, kbuf, vsiz)){ eprint(fdb, __LINE__, "(validation)"); err = true; tcfree(vbuf); break; } tcfree(vbuf); if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } if(tcfdbrnum(fdb) != rnum){ eprint(fdb, __LINE__, "(validation)"); err = true; } my_my_my_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(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){ eprint(fdb, __LINE__, "tcfdbput"); err = true; break; } int rsiz; char *rbuf = tcfdbget2(fdb, kbuf, ksiz, &rsiz); if(!rbuf){ eprint(fdb, __LINE__, "tcfdbget"); err = true; break; } if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){ eprint(fdb, __LINE__, "(validation)"); err = true; tcfree(rbuf); break; } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } tcfree(rbuf); } my_my_my_iprintf("random erasing:\n"); for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "%d", myrand(rnum) + 1); if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){ eprint(fdb, __LINE__, "tcfdbout"); err = true; break; } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } my_my_my_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(!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP){ eprint(fdb, __LINE__, "tcfdbputkeep"); err = true; break; } if(vsiz < 1){ char tbuf[PATH_MAX]; for(int j = 0; j < PATH_MAX; j++){ tbuf[j] = myrand(0x100); } if(!tcfdbput2(fdb, kbuf, ksiz, tbuf, PATH_MAX)){ eprint(fdb, __LINE__, "tcfdbput"); err = true; break; } } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } my_my_my_iprintf("erasing:\n"); for(int i = 1; i <= rnum; i++){ if(i % 2 == 1){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "[%d]", i); if(!tcfdbout2(fdb, kbuf, ksiz)){ eprint(fdb, __LINE__, "tcfdbout"); err = true; break; } if(tcfdbout2(fdb, kbuf, ksiz) || tcfdbecode(fdb) != TCENOREC){ eprint(fdb, __LINE__, "tcfdbout"); err = true; break; } } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } my_my_my_iprintf("random writing and reopening:\n"); for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "%d", myrand(rnum) + 1); int vsiz = myrand(RECBUFSIZ); char *vbuf = tcmalloc(vsiz + 1); memset(vbuf, '*', vsiz); switch(myrand(3)){ case 0: if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){ eprint(fdb, __LINE__, "tcfdbput"); err = true; } break; case 1: if(!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz)){ eprint(fdb, __LINE__, "tcfdbputcat"); err = true; } break; case 2: if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){ eprint(fdb, __LINE__, "tcfdbout"); err = true; } break; } tcfree(vbuf); if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } if(!tcfdbclose(fdb)){ eprint(fdb, __LINE__, "tcfdbclose"); err = true; } if(!tcfdbopen(fdb, path, FDBOWRITER | omode)){ eprint(fdb, __LINE__, "tcfdbopen"); err = true; } my_my_my_iprintf("checking:\n"); for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "[%d]", i); int vsiz; char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz); if(vbuf){ tcfree(vbuf); } else if(tcfdbecode(fdb) != TCENOREC){ eprint(fdb, __LINE__, "tcfdbget"); err = true; break; } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } my_my_my_iprintf("writing:\n"); for(int i = 1; i <= rnum; i++){ char buf[RECBUFSIZ]; int len = sprintf(buf, "%08d", i); if(!tcfdbput2(fdb, buf, len, buf, len)){ eprint(fdb, __LINE__, "tcfdbput"); err = true; break; } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } my_my_my_iprintf("reading:\n"); for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "%08d", i); int vsiz; char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz); if(!vbuf){ eprint(fdb, __LINE__, "tcfdbget"); err = true; break; } else if(vsiz != ksiz || memcmp(vbuf, kbuf, vsiz)){ eprint(fdb, __LINE__, "(validation)"); err = true; tcfree(vbuf); break; } tcfree(vbuf); if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } my_my_my_iprintf("checking iterator:\n"); if(!tcfdbiterinit(fdb)){ eprint(fdb, __LINE__, "tcfdbiterinit"); err = true; } char *kbuf; int ksiz; int inum = 0; for(int i = 1; (kbuf = tcfdbiternext2(fdb, &ksiz)) != NULL; i++, inum++){ int vsiz; char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz); if(!vbuf){ eprint(fdb, __LINE__, "tcfdbget2"); err = true; tcfree(kbuf); break; } tcfree(vbuf); tcfree(kbuf); if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } if(tcfdbecode(fdb) != TCENOREC || inum != tcfdbrnum(fdb)){ eprint(fdb, __LINE__, "(validation)"); err = true; } my_my_my_iprintf("iteration updating:\n"); if(!tcfdbiterinit(fdb)){ eprint(fdb, __LINE__, "tcfdbiterinit"); err = true; } inum = 0; for(int i = 1; (kbuf = tcfdbiternext2(fdb, &ksiz)) != NULL; i++, inum++){ if(myrand(2) == 0){ if(!tcfdbputcat2(fdb, kbuf, ksiz, "0123456789", 10)){ eprint(fdb, __LINE__, "tcfdbputcat2"); err = true; tcfree(kbuf); break; } } else { if(!tcfdbout2(fdb, kbuf, ksiz)){ eprint(fdb, __LINE__, "tcfdbout"); err = true; tcfree(kbuf); break; } } tcfree(kbuf); if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } if(tcfdbecode(fdb) != TCENOREC || inum < tcfdbrnum(fdb)){ eprint(fdb, __LINE__, "(validation)"); err = true; } if(myrand(10) == 0 && !tcfdbsync(fdb)){ eprint(fdb, __LINE__, "tcfdbsync"); err = true; } if(!tcfdbvanish(fdb)){ eprint(fdb, __LINE__, "tcfdbvanish"); err = true; } TCMAP *map = tcmapnew(); my_my_my_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 = sprintf(vbuf, "%d", myrand(rnum) + 1); switch(myrand(7)){ case 0: if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){ eprint(fdb, __LINE__, "tcfdbput2"); err = true; } tcmapput(map, kbuf, ksiz, vbuf, vsiz); break; case 1: if(!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP){ eprint(fdb, __LINE__, "tcfdbputkeep2"); err = true; } tcmapputkeep(map, kbuf, ksiz, vbuf, vsiz); break; case 2: if(!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP){ eprint(fdb, __LINE__, "tcfdbputcat2"); err = true; } tcmapputcat(map, kbuf, ksiz, vbuf, vsiz); break; case 3: if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){ eprint(fdb, __LINE__, "tcfdbout2"); err = true; } tcmapout(map, kbuf, ksiz); break; } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } my_my_my_iprintf("checking transaction commit:\n"); if(!tcfdbtranbegin(fdb)){ eprint(fdb, __LINE__, "tcfdbtranbegin"); err = true; } for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "%d", myrand(rnum) + 1); char vbuf[RECBUFSIZ]; int vsiz = sprintf(vbuf, "[%d]", myrand(rnum) + 1); switch(myrand(7)){ case 0: if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){ eprint(fdb, __LINE__, "tcfdbput2"); err = true; } tcmapput(map, kbuf, ksiz, vbuf, vsiz); break; case 1: if(!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP){ eprint(fdb, __LINE__, "tcfdbputkeep2"); err = true; } tcmapputkeep(map, kbuf, ksiz, vbuf, vsiz); break; case 2: if(!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz)){ eprint(fdb, __LINE__, "tcfdbputcat2"); err = true; } tcmapputcat(map, kbuf, ksiz, vbuf, vsiz); break; case 3: if(tcfdbaddint(fdb, tcfdbkeytoid(kbuf, ksiz), 1) == INT_MIN && tcfdbecode(fdb) != TCEKEEP){ eprint(fdb, __LINE__, "tcfdbaddint"); err = true; } tcmapaddint(map, kbuf, ksiz, 1); break; case 4: if(isnan(tcfdbadddouble(fdb, tcfdbkeytoid(kbuf, ksiz), 1.0)) && tcfdbecode(fdb) != TCEKEEP){ eprint(fdb, __LINE__, "tcfdbadddouble"); err = true; } tcmapadddouble(map, kbuf, ksiz, 1.0); break; case 5: if(myrand(2) == 0){ void *op = (void *)(intptr_t)(myrand(3) + 1); if(!tcfdbputproc(fdb, tcfdbkeytoid(kbuf, ksiz), vbuf, vsiz, pdprocfunc, op) && tcfdbecode(fdb) != TCEKEEP){ eprint(fdb, __LINE__, "tcfdbputproc"); err = true; } tcmapputproc(map, kbuf, ksiz, vbuf, vsiz, pdprocfunc, op); } else { vsiz = myrand(10); void *op = (void *)(intptr_t)(myrand(3) + 1); if(!tcfdbputproc(fdb, tcfdbkeytoid(kbuf, ksiz), NULL, vsiz, pdprocfunc, op) && tcfdbecode(fdb) != TCEKEEP && tcfdbecode(fdb) != TCENOREC){ eprint(fdb, __LINE__, "tcfdbputproc"); err = true; } tcmapputproc(map, kbuf, ksiz, NULL, vsiz, pdprocfunc, op); } break; case 6: if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){ eprint(fdb, __LINE__, "tcfdbout2"); err = true; } tcmapout(map, kbuf, ksiz); break; } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } if(!tcfdbtrancommit(fdb)){ eprint(fdb, __LINE__, "tcfdbtrancommit"); err = true; } my_my_my_iprintf("checking transaction abort:\n"); uint64_t ornum = tcfdbrnum(fdb); uint64_t ofsiz = tcfdbfsiz(fdb); if(!tcfdbtranbegin(fdb)){ eprint(fdb, __LINE__, "tcfdbtranbegin"); err = true; } for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "%d", myrand(rnum) + 1); char vbuf[RECBUFSIZ]; int vsiz = sprintf(vbuf, "[%d]", myrand(rnum) + 1); switch(myrand(7)){ case 0: if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){ eprint(fdb, __LINE__, "tcfdbput2"); err = true; } break; case 1: if(!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP){ eprint(fdb, __LINE__, "tcfdbputkeep2"); err = true; } break; case 2: if(!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz)){ eprint(fdb, __LINE__, "tcfdbputcat2"); err = true; } break; case 3: if(tcfdbaddint(fdb, tcfdbkeytoid(kbuf, ksiz), 1) == INT_MIN && tcfdbecode(fdb) != TCEKEEP){ eprint(fdb, __LINE__, "tcfdbaddint"); err = true; } break; case 4: if(isnan(tcfdbadddouble(fdb, tcfdbkeytoid(kbuf, ksiz), 1.0)) && tcfdbecode(fdb) != TCEKEEP){ eprint(fdb, __LINE__, "tcfdbadddouble"); err = true; } break; case 5: if(myrand(2) == 0){ void *op = (void *)(intptr_t)(myrand(3) + 1); if(!tcfdbputproc(fdb, tcfdbkeytoid(kbuf, ksiz), vbuf, vsiz, pdprocfunc, op) && tcfdbecode(fdb) != TCEKEEP){ eprint(fdb, __LINE__, "tcfdbputproc"); err = true; } } else { vsiz = myrand(10); void *op = (void *)(intptr_t)(myrand(3) + 1); if(!tcfdbputproc(fdb, tcfdbkeytoid(kbuf, ksiz), NULL, vsiz, pdprocfunc, op) && tcfdbecode(fdb) != TCEKEEP && tcfdbecode(fdb) != TCENOREC){ eprint(fdb, __LINE__, "tcfdbputproc"); err = true; } } break; case 6: if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){ eprint(fdb, __LINE__, "tcfdbout2"); err = true; } break; } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } if(!tcfdbtranabort(fdb)){ eprint(fdb, __LINE__, "tcfdbtranabort"); err = true; } my_my_my_iprintf("checking consistency:\n"); if(tcfdbrnum(fdb) != ornum || tcfdbfsiz(fdb) != ofsiz || tcfdbrnum(fdb) != tcmaprnum(map)){ eprint(fdb, __LINE__, "(validation)"); err = true; } inum = 0; tcmapiterinit(map); const char *tkbuf; int tksiz; for(int i = 1; (tkbuf = tcmapiternext(map, &tksiz)) != NULL; i++, inum++){ int tvsiz; const char *tvbuf = tcmapiterval(tkbuf, &tvsiz); if(tvsiz > RECBUFSIZ) tvsiz = RECBUFSIZ; int rsiz; char *rbuf = tcfdbget2(fdb, tkbuf, tksiz, &rsiz); if(!rbuf || rsiz != tvsiz || memcmp(rbuf, tvbuf, rsiz)){ eprint(fdb, __LINE__, "(validation)"); err = true; tcfree(rbuf); break; } tcfree(rbuf); if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } if(rnum > 250) my_my_my_iprintf(" (%08d)\n", inum); inum = 0; if(!tcfdbiterinit(fdb)){ eprint(fdb, __LINE__, "tcfdbiterinit"); err = true; } for(int i = 1; (kbuf = tcfdbiternext2(fdb, &ksiz)) != NULL; i++, inum++){ int vsiz; char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz); int rsiz; const char *rbuf = tcmapget(map, kbuf, ksiz, &rsiz); if(rsiz > RECBUFSIZ) rsiz = RECBUFSIZ; if(!rbuf || rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){ eprint(fdb, __LINE__, "(validation)"); err = true; tcfree(vbuf); tcfree(kbuf); break; } tcfree(vbuf); tcfree(kbuf); if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i); } } if(rnum > 250) my_my_my_iprintf(" (%08d)\n", inum); tcmapdel(map); if(!tcfdbvanish(fdb)){ eprint(fdb, __LINE__, "tcfdbvanish"); err = true; } if(rnum >= 100){ if(!tcfdbtranbegin(fdb)){ eprint(fdb, __LINE__, "tcfdbtranbegin"); err = true; } if(!tcfdbput3(fdb, "99", "hirabayashi")){ eprint(fdb, __LINE__, "tcfdbput3"); err = true; } for(int i = 0; i < 10; i++){ char buf[RECBUFSIZ]; int size = sprintf(buf, "%d", myrand(rnum) + 1); if(!tcfdbput2(fdb, buf, size, buf, size)){ eprint(fdb, __LINE__, "tcfdbput2"); err = true; } } for(int i = myrand(3) + 1; i < PATH_MAX; i = i * 2 + myrand(3)){ char vbuf[i]; memset(vbuf, '@', i - 1); vbuf[i-1] = '\0'; if(!tcfdbput3(fdb, "99", vbuf)){ eprint(fdb, __LINE__, "tcfdbput3"); err = true; } } if(!tcfdbforeach(fdb, iterfunc, NULL)){ eprint(fdb, __LINE__, "tcfdbforeach"); 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(); 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; }
static TCDDB *tcddbnew(void){ TCDDB *ddb = tcmalloc(sizeof(*ddb)); ddb->name = NULL; return ddb; }
/* Synchronize updating contents on memory of a word database object. */ bool tcwdbmemsync(TCWDB *wdb, int level){ assert(wdb); if(!wdb->open || !wdb->cc){ tcbdbsetecode(wdb->idx, TCEINVALID, __FILE__, __LINE__, __func__); return false; } bool err = false; bool (*synccb)(int, int, const char *, void *) = wdb->synccb; void *syncopq = wdb->syncopq; bool (*addcb)(const char *, void *) = wdb->addcb; void *addopq = wdb->addopq; TCBDB *idx = wdb->idx; TCMAP *cc = wdb->cc; if(synccb && !synccb(0, 0, "started", syncopq)){ tcbdbsetecode(wdb->idx, TCEMISC, __FILE__, __LINE__, __func__); return false; } if(tcmaprnum(cc) > 0){ if(synccb && !synccb(0, 0, "getting tokens", syncopq)){ tcbdbsetecode(wdb->idx, TCEMISC, __FILE__, __LINE__, __func__); return false; } int kn; const char **keys = tcmapkeys2(cc, &kn); if(synccb && !synccb(kn, 0, "sorting tokens", syncopq)){ tcbdbsetecode(wdb->idx, TCEMISC, __FILE__, __LINE__, __func__); tcfree(keys); return false; } qsort(keys, kn, sizeof(*keys), (int(*)(const void *, const void *))tccmpwords); for(int i = 0; i < kn; i++){ if(synccb && !synccb(kn, i + 1, "storing tokens", syncopq)){ tcbdbsetecode(wdb->idx, TCEMISC, __FILE__, __LINE__, __func__); tcfree(keys); return false; } const char *kbuf = keys[i]; int ksiz = strlen(kbuf); int vsiz; const char *vbuf = tcmapget(cc, kbuf, ksiz, &vsiz); if(!tcbdbputcat(idx, kbuf, ksiz, vbuf, vsiz)) err = true; } if(addcb){ if(synccb && !synccb(0, 0, "storing keyword list", syncopq)){ tcbdbsetecode(wdb->idx, TCEMISC, __FILE__, __LINE__, __func__); tcfree(keys); return false; } for(int i = 0; i < kn; i++){ if(!addcb(keys[i], addopq)){ tcfree(keys); return false; } } } tcfree(keys); tcmapclear(cc); } TCMAP *dtokens = wdb->dtokens; TCIDSET *dids = wdb->dids; if(tcmaprnum(dtokens) > 0){ if(synccb && !synccb(0, 0, "getting deleted tokens", syncopq)){ tcbdbsetecode(wdb->idx, TCEMISC, __FILE__, __LINE__, __func__); return false; } int kn; const char **keys = tcmapkeys2(dtokens, &kn); if(synccb && !synccb(kn, 0, "sorting deleted tokens", syncopq)){ tcbdbsetecode(wdb->idx, TCEMISC, __FILE__, __LINE__, __func__); tcfree(keys); return false; } qsort(keys, kn, sizeof(*keys), (int(*)(const void *, const void *))tccmpwords); for(int i = 0; i < kn; i++){ if(synccb && !synccb(kn, i + 1, "storing deleted tokens", syncopq)){ tcbdbsetecode(wdb->idx, TCEMISC, __FILE__, __LINE__, __func__); tcfree(keys); return false; } const char *kbuf = keys[i]; int ksiz = strlen(kbuf); int vsiz; const char *vbuf = tcbdbget3(idx, kbuf, ksiz, &vsiz); if(!vbuf) continue; char *nbuf = tcmalloc(vsiz + 1); char *wp = nbuf; const char *pv; while(vsiz > 0){ pv = vbuf; int step; uint64_t id; TDREADVNUMBUF64(vbuf, id, step); vbuf += step; vsiz -= step; if(!tcidsetcheck(dids, id)){ int len = vbuf - pv; memcpy(wp, pv, len); wp += len; } } int nsiz = wp - nbuf; if(nsiz > 0){ if(!tcbdbput(idx, kbuf, ksiz, nbuf, nsiz)) err = true; } else { if(!tcbdbout(idx, kbuf, ksiz)) err = true; } tcfree(nbuf); } tcfree(keys); tcmapclear(dtokens); tcidsetclear(dids); } if(level > 0){ if(synccb && !synccb(0, 0, "synchronizing database", syncopq)){ tcbdbsetecode(wdb->idx, TCEMISC, __FILE__, __LINE__, __func__); return false; } if(!tcbdbmemsync(idx, level > 1)) err = true; } if(synccb && !synccb(0, 0, "finished", syncopq)){ tcbdbsetecode(wdb->idx, TCEMISC, __FILE__, __LINE__, __func__); return false; } return !err; }