示例#1
0
/* perform get command */
static int procget(const char *path, const char *kbuf, int ksiz, TCCMP cmp, int omode,
        bool px, bool pz) {
    TCBDB *bdb = tcbdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tcbdbsetdbgfd(bdb, g_dbgfd);
    if (cmp && !tcbdbsetcmpfunc(bdb, cmp, NULL)) printerr(bdb);
    if (!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(bdb);
    if (!tcbdbopen(bdb, path, BDBOREADER | omode)) {
        printerr(bdb);
        tcbdbdel(bdb);
        return 1;
    }
    bool err = false;
    int vsiz;
    char *vbuf = tcbdbget(bdb, kbuf, ksiz, &vsiz);
    if (vbuf) {
        printdata(vbuf, vsiz, px);
        if (!pz) putchar('\n');
        tcfree(vbuf);
    } else {
        printerr(bdb);
        err = true;
    }
    if (!tcbdbclose(bdb)) {
        if (!err) printerr(bdb);
        err = true;
    }
    tcbdbdel(bdb);
    return err ? 1 : 0;
}
示例#2
0
/* perform optimize command */
static int procoptimize(const char *path, int lmemb, int nmemb,
        int bnum, int apow, int fpow, TCCMP cmp, int opts, int omode, bool df) {
    TCBDB *bdb = tcbdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tcbdbsetdbgfd(bdb, g_dbgfd);
    if (cmp && !tcbdbsetcmpfunc(bdb, cmp, NULL)) printerr(bdb);
    if (!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(bdb);
    if (!tcbdbopen(bdb, path, BDBOWRITER | omode)) {
        printerr(bdb);
        tcbdbdel(bdb);
        return 1;
    }
    bool err = false;
    if (df) {
        if (!tcbdbdefrag(bdb, INT64_MAX)) {
            printerr(bdb);
            err = true;
        }
    } else {
        if (!tcbdboptimize(bdb, lmemb, nmemb, bnum, apow, fpow, opts)) {
            printerr(bdb);
            err = true;
        }
    }
    if (!tcbdbclose(bdb)) {
        if (!err) printerr(bdb);
        err = true;
    }
    tcbdbdel(bdb);
    return err ? 1 : 0;
}
示例#3
0
/* setcmpfunc */
JNIEXPORT jboolean JNICALL Java_tokyocabinet_BDB_setcmpfunc
(JNIEnv *env, jobject self, jint cmp){
  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);
  }
  switch(cmp){
  case tokyocabinet_BDB_CMPLEXICAL: return tcbdbsetcmpfunc(bdb, tccmplexical, NULL);
  case tokyocabinet_BDB_CMPDECIMAL: return tcbdbsetcmpfunc(bdb, tccmpdecimal, NULL);
  case tokyocabinet_BDB_CMPINT32: return tcbdbsetcmpfunc(bdb, tccmpint32, NULL);
  case tokyocabinet_BDB_CMPINT64: return tcbdbsetcmpfunc(bdb, tccmpint64, NULL);
  default: break;
  }
  tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
  return false;
}
示例#4
0
/* 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);
}
示例#5
0
/* perform out command */
static int procout(const char *path, const char *kbuf, int ksiz, TCCMP cmp, int omode) {
    TCBDB *bdb = tcbdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tcbdbsetdbgfd(bdb, g_dbgfd);
    if (cmp && !tcbdbsetcmpfunc(bdb, cmp, NULL)) printerr(bdb);
    if (!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(bdb);
    if (!tcbdbopen(bdb, path, BDBOWRITER | omode)) {
        printerr(bdb);
        tcbdbdel(bdb);
        return 1;
    }
    bool err = false;
    if (!tcbdbout(bdb, kbuf, ksiz)) {
        printerr(bdb);
        err = true;
    }
    if (!tcbdbclose(bdb)) {
        if (!err) printerr(bdb);
        err = true;
    }
    tcbdbdel(bdb);
    return err ? 1 : 0;
}
示例#6
0
/* perform create command */
static int proccreate(const char *path, int lmemb, int nmemb,
        int bnum, int apow, int fpow, TCCMP cmp, int opts) {
    TCBDB *bdb = tcbdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tcbdbsetdbgfd(bdb, g_dbgfd);
    if (cmp && !tcbdbsetcmpfunc(bdb, cmp, NULL)) printerr(bdb);
    if (!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(bdb);
    if (!tcbdbtune(bdb, lmemb, nmemb, bnum, apow, fpow, opts)) {
        printerr(bdb);
        tcbdbdel(bdb);
        return 1;
    }
    if (!tcbdbopen(bdb, path, BDBOWRITER | BDBOCREAT | BDBOTRUNC)) {
        printerr(bdb);
        tcbdbdel(bdb);
        return 1;
    }
    bool err = false;
    if (!tcbdbclose(bdb)) {
        printerr(bdb);
        err = true;
    }
    tcbdbdel(bdb);
    return err ? 1 : 0;
}
示例#7
0
/* perform list command */
static int proclist(const char *path, TCCMP cmp, int omode, int max, bool pv, bool px, bool bk,
        const char *jstr, const char *bstr, const char *estr, const char *fmstr) {
    TCBDB *bdb = tcbdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tcbdbsetdbgfd(bdb, g_dbgfd);
    if (cmp && !tcbdbsetcmpfunc(bdb, cmp, NULL)) printerr(bdb);
    if (!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(bdb);
    if (!tcbdbopen(bdb, path, BDBOREADER | omode)) {
        printerr(bdb);
        tcbdbdel(bdb);
        return 1;
    }
    bool err = false;
    if (bstr || fmstr) {
        TCLIST *keys = fmstr ? tcbdbfwmkeys2(bdb, fmstr, max) :
                tcbdbrange(bdb, bstr, strlen(bstr), true, estr, strlen(estr), true, max);
        int cnt = 0;
        for (int i = 0; i < tclistnum(keys); i++) {
            int ksiz;
            const char *kbuf = tclistval(keys, i, &ksiz);
            if (pv) {
                TCLIST *vals = tcbdbget4(bdb, kbuf, ksiz);
                if (vals) {
                    for (int j = 0; j < tclistnum(vals); j++) {
                        int vsiz;
                        const char *vbuf = tclistval(vals, j, &vsiz);
                        printdata(kbuf, ksiz, px);
                        putchar('\t');
                        printdata(vbuf, vsiz, px);
                        putchar('\n');
                        if (max >= 0 && ++cnt >= max) break;
                    }
                    tclistdel(vals);
                }
            } else {
                int num = tcbdbvnum(bdb, kbuf, ksiz);
                for (int j = 0; j < num; j++) {
                    printdata(kbuf, ksiz, px);
                    putchar('\n');
                    if (max >= 0 && ++cnt >= max) break;
                }
            }
            if (max >= 0 && cnt >= max) break;
        }
        tclistdel(keys);
    } else {
        BDBCUR *cur = tcbdbcurnew(bdb);
        if (bk) {
            if (jstr) {
                if (!tcbdbcurjumpback(cur, jstr, strlen(jstr)) && tcbdbecode(bdb) != TCENOREC) {
                    printerr(bdb);
                    err = true;
                }
            } else {
                if (!tcbdbcurlast(cur) && tcbdbecode(bdb) != TCENOREC) {
                    printerr(bdb);
                    err = true;
                }
            }
        } else {
            if (jstr) {
                if (!tcbdbcurjump(cur, jstr, strlen(jstr)) && tcbdbecode(bdb) != TCENOREC) {
                    printerr(bdb);
                    err = true;
                }
            } else {
                if (!tcbdbcurfirst(cur) && tcbdbecode(bdb) != TCENOREC) {
                    printerr(bdb);
                    err = true;
                }
            }
        }
        TCXSTR *key = tcxstrnew();
        TCXSTR *val = tcxstrnew();
        int cnt = 0;
        while (tcbdbcurrec(cur, key, val)) {
            printdata(tcxstrptr(key), tcxstrsize(key), px);
            if (pv) {
                putchar('\t');
                printdata(tcxstrptr(val), tcxstrsize(val), px);
            }
            putchar('\n');
            if (bk) {
                if (!tcbdbcurprev(cur) && tcbdbecode(bdb) != TCENOREC) {
                    printerr(bdb);
                    err = true;
                }
            } else {
                if (!tcbdbcurnext(cur) && tcbdbecode(bdb) != TCENOREC) {
                    printerr(bdb);
                    err = true;
                }
            }
            if (max >= 0 && ++cnt >= max) break;
        }
        tcxstrdel(val);
        tcxstrdel(key);
        tcbdbcurdel(cur);
    }
    if (!tcbdbclose(bdb)) {
        if (!err) printerr(bdb);
        err = true;
    }
    tcbdbdel(bdb);
    return err ? 1 : 0;
}
示例#8
0
/* perform put command */
static int procput(const char *path, const char *kbuf, int ksiz, const char *vbuf, int vsiz,
        TCCMP cmp, int omode, int dmode) {
    TCBDB *bdb = tcbdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tcbdbsetdbgfd(bdb, g_dbgfd);
    if (cmp && !tcbdbsetcmpfunc(bdb, cmp, NULL)) printerr(bdb);
    if (!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(bdb);
    if (!tcbdbopen(bdb, path, BDBOWRITER | omode)) {
        printerr(bdb);
        tcbdbdel(bdb);
        return 1;
    }
    bool err = false;
    int inum;
    double dnum;
    switch (dmode) {
        case -1:
            if (!tcbdbputkeep(bdb, kbuf, ksiz, vbuf, vsiz)) {
                printerr(bdb);
                err = true;
            }
            break;
        case 1:
            if (!tcbdbputcat(bdb, kbuf, ksiz, vbuf, vsiz)) {
                printerr(bdb);
                err = true;
            }
            break;
        case 2:
            if (!tcbdbputdup(bdb, kbuf, ksiz, vbuf, vsiz)) {
                printerr(bdb);
                err = true;
            }
            break;
        case 3:
            if (!tcbdbputdupback(bdb, kbuf, ksiz, vbuf, vsiz)) {
                printerr(bdb);
                err = true;
            }
            break;
        case 10:
            inum = tcbdbaddint(bdb, kbuf, ksiz, tcatoi(vbuf));
            if (inum == INT_MIN) {
                printerr(bdb);
                err = true;
            } else {
                printf("%d\n", inum);
            }
            break;
        case 11:
            dnum = tcbdbadddouble(bdb, kbuf, ksiz, tcatof(vbuf));
            if (isnan(dnum)) {
                printerr(bdb);
                err = true;
            } else {
                printf("%.6f\n", dnum);
            }
            break;
        default:
            if (!tcbdbput(bdb, kbuf, ksiz, vbuf, vsiz)) {
                printerr(bdb);
                err = true;
            }
            break;
    }
    if (!tcbdbclose(bdb)) {
        if (!err) printerr(bdb);
        err = true;
    }
    tcbdbdel(bdb);
    return err ? 1 : 0;
}
示例#9
0
/* perform inform command */
static int procinform(const char *path, int omode) {
    TCBDB *bdb = tcbdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tcbdbsetdbgfd(bdb, g_dbgfd);
    tcbdbsetcmpfunc(bdb, mycmpfunc, NULL);
    tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL);
    if (!tcbdbopen(bdb, path, BDBOREADER | omode)) {
        printerr(bdb);
        tcbdbdel(bdb);
        return 1;
    }
    bool err = false;
    const char *npath = tcbdbpath(bdb);
    if (!npath) npath = "(unknown)";
    printf("path: %s\n", npath);
    printf("database type: btree\n");
    uint8_t flags = tcbdbflags(bdb);
    printf("additional flags:");
    if (flags & BDBFOPEN) printf(" open");
    if (flags & BDBFFATAL) printf(" fatal");
    printf("\n");
    TCCMP cmp = tcbdbcmpfunc(bdb);
    printf("comparison function: ");
    if (cmp == tccmplexical) {
        printf("lexical");
    } else if (cmp == tccmpdecimal) {
        printf("decimal");
    } else if (cmp == tccmpint32) {
        printf("int32");
    } else if (cmp == tccmpint64) {
        printf("int64");
    } else {
        printf("custom");
    }
    printf("\n");
    printf("max leaf member: %d\n", tcbdblmemb(bdb));
    printf("max node member: %d\n", tcbdbnmemb(bdb));
    printf("leaf number: %" PRIuMAX "\n", (uint64_t) tcbdblnum(bdb));
    printf("node number: %" PRIuMAX "\n", (uint64_t) tcbdbnnum(bdb));
    printf("bucket number: %" PRIuMAX "\n", (uint64_t) tcbdbbnum(bdb));

#ifndef NDEBUG
    if (bdb->hdb->cnt_writerec >= 0)
        printf("used bucket number: %" PRIdMAX "\n", (int64_t) tcbdbbnumused(bdb));
#endif

    printf("alignment: %u\n", tcbdbalign(bdb));
    printf("free block pool: %u\n", tcbdbfbpmax(bdb));
    printf("inode number: %" PRIdMAX "\n", (int64_t) tcbdbinode(bdb));
    char date[48];
    tcdatestrwww(tcbdbmtime(bdb), INT_MAX, date);
    printf("modified time: %s\n", date);
    uint8_t opts = tcbdbopts(bdb);
    printf("options:");
    if (opts & BDBTLARGE) printf(" large");
    if (opts & BDBTDEFLATE) printf(" deflate");
    if (opts & BDBTBZIP) printf(" bzip");
    if (opts & BDBTTCBS) printf(" tcbs");
    if (opts & BDBTEXCODEC) printf(" excodec");
    printf("\n");
    printf("record number: %" PRIuMAX "\n", (uint64_t) tcbdbrnum(bdb));
    printf("file size: %" PRIuMAX "\n", (uint64_t) tcbdbfsiz(bdb));
    if (!tcbdbclose(bdb)) {
        if (!err) printerr(bdb);
        err = true;
    }
    tcbdbdel(bdb);
    return err ? 1 : 0;
}