Ejemplo n.º 1
0
Archivo: linux.c Proyecto: takar/iowow
iwrc iwp_flock(HANDLE fd, iwp_lockmode lmode) {
    assert(!INVALIDHANDLE(fd));
    if (lmode == IWP_NOLOCK) {
        return 0;
    }
    struct flock lock = {
        .l_type = (lmode & IWP_WLOCK) ? F_WRLCK : F_RDLCK,
        .l_whence = SEEK_SET
    };
    while (fcntl(fd, (lmode & IWP_NBLOCK) ? F_SETLK : F_SETLKW, &lock) == -1) {
        if (errno != EINTR) {
            return iwrc_set_errno(IW_ERROR_IO_ERRNO, errno);
        }
    }
    return 0;
}

iwrc iwp_unlock(HANDLE fd) {
    assert(!INVALIDHANDLE(fd));
    struct flock lock = {
        .l_type = F_UNLCK,
        .l_whence = SEEK_SET
    };
    while (fcntl(fd, F_SETLKW, &lock) == -1) {
        if (errno != EINTR) {
            return iwrc_set_errno(IW_ERROR_IO_ERRNO, errno);
        }
    }
    return 0;
}
Ejemplo n.º 2
0
/* perform optimize command */
static int procoptimize(const char *path, int bnum, int apow, int fpow, int opts, int omode,
        bool df) {
    TCHDB *hdb = tchdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tchdbsetdbgfd(hdb, g_dbgfd);
    if (!tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(hdb);
    if (!tchdbopen(hdb, path, HDBOWRITER | omode)) {
        printerr(hdb);
        tchdbdel(hdb);
        return 1;
    }
    bool err = false;
    if (df) {
        if (!tchdbdefrag(hdb, INT64_MAX)) {
            printerr(hdb);
            err = true;
        }
    } else {
        if (!tchdboptimize(hdb, bnum, apow, fpow, opts)) {
            printerr(hdb);
            err = true;
        }
    }
    if (!tchdbclose(hdb)) {
        if (!err) printerr(hdb);
        err = true;
    }
    tchdbdel(hdb);
    return err ? 1 : 0;
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
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;
}
Ejemplo n.º 5
0
/* perform get command */
static int procget(const char *path, const char *kbuf, int ksiz, int omode, bool px, bool pz) {
    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;
    int vsiz;
    char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz);
    if (vbuf) {
        printdata(vbuf, vsiz, px);
        if (!pz) putchar('\n');
        tcfree(vbuf);
    } else {
        printerr(fdb);
        err = true;
    }
    if (!tcfdbclose(fdb)) {
        if (!err) printerr(fdb);
        err = true;
    }
    tcfdbdel(fdb);
    return err ? 1 : 0;
}
Ejemplo n.º 6
0
/* perform put command */
static int procput(const char *path, const char *kbuf, int ksiz, const char *vbuf, int vsiz,
        int omode, int dmode) {
    TCHDB *hdb = tchdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tchdbsetdbgfd(hdb, g_dbgfd);
    if (!tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(hdb);
    if (!tchdbopen(hdb, path, HDBOWRITER | omode)) {
        printerr(hdb);
        tchdbdel(hdb);
        return 1;
    }
    bool err = false;
    int inum;
    double dnum;
    switch (dmode) {
        case -1:
            if (!tchdbputkeep(hdb, kbuf, ksiz, vbuf, vsiz)) {
                printerr(hdb);
                err = true;
            }
            break;
        case 1:
            if (!tchdbputcat(hdb, kbuf, ksiz, vbuf, vsiz)) {
                printerr(hdb);
                err = true;
            }
            break;
        case 10:
            inum = tchdbaddint(hdb, kbuf, ksiz, tcatoi(vbuf));
            if (inum == INT_MIN) {
                printerr(hdb);
                err = true;
            } else {
                printf("%d\n", inum);
            }
            break;
        case 11:
            dnum = tchdbadddouble(hdb, kbuf, ksiz, tcatof(vbuf));
            if (isnan(dnum)) {
                printerr(hdb);
                err = true;
            } else {
                printf("%.6f\n", dnum);
            }
            break;
        default:
            if (!tchdbput(hdb, kbuf, ksiz, vbuf, vsiz)) {
                printerr(hdb);
                err = true;
            }
            break;
    }
    if (!tchdbclose(hdb)) {
        if (!err) printerr(hdb);
        err = true;
    }
    tchdbdel(hdb);
    return err ? 1 : 0;
}
Ejemplo n.º 7
0
/* perform inform command */
static int procinform(const char *path, int omode) {
    TCHDB *hdb = tchdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tchdbsetdbgfd(hdb, g_dbgfd);
    tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL);
    if (!tchdbopen(hdb, path, HDBOREADER | omode)) {
        printerr(hdb);
        tchdbdel(hdb);
        return 1;
    }
    bool err = false;
    const char *npath = tchdbpath(hdb);
    if (!npath) npath = "(unknown)";
    printf("path: %s\n", npath);
    const char *type = "(unknown)";
    switch (tchdbtype(hdb)) {
        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 = tchdbflags(hdb);
    printf("additional flags:");
    if (flags & HDBFOPEN) printf(" open");
    if (flags & HDBFFATAL) printf(" fatal");
    printf("\n");
    printf("bucket number: %" PRIu64 "\n", (uint64_t) tchdbbnum(hdb));
#ifndef NDEBUG
    if (hdb->cnt_writerec >= 0)
        printf("used bucket number: %" PRId64 "\n", (int64_t) tchdbbnumused(hdb));
#endif
    printf("alignment: %u\n", tchdbalign(hdb));
    printf("free block pool: %u\n", tchdbfbpmax(hdb));
    printf("inode number: %" PRId64 "\n", (int64_t) tchdbinode(hdb));
    char date[48];
    tcdatestrwww(tchdbmtime(hdb), INT_MAX, date);
    printf("modified time: %s\n", date);
    uint8_t opts = tchdbopts(hdb);
    printf("options:");
    if (opts & HDBTLARGE) printf(" large");
    if (opts & HDBTDEFLATE) printf(" deflate");
    if (opts & HDBTBZIP) printf(" bzip");
    if (opts & HDBTTCBS) printf(" tcbs");
    if (opts & HDBTEXCODEC) printf(" excodec");
    printf("\n");
    printf("record number: %" PRIu64 "\n", (uint64_t) tchdbrnum(hdb));
    printf("file size: %" PRIu64 "\n", (uint64_t) tchdbfsiz(hdb));
    if (!tchdbclose(hdb)) {
        if (!err) printerr(hdb);
        err = true;
    }
    tchdbdel(hdb);
    return err ? 1 : 0;
}
Ejemplo n.º 8
0
Archivo: linux.c Proyecto: takar/iowow
iwrc iwp_closefh(HANDLE fh) {
    if (INVALIDHANDLE(fh)) {
        return 0;
    }
    if (close(fh) == -1) {
        return iwrc_set_errno(IW_ERROR_IO_ERRNO, errno);
    }
    return 0;
}
Ejemplo n.º 9
0
/* perform list command */
static int proclist(const char *path, int omode, int max, bool pv, bool px,
        const char *rlstr, const char *rustr, const char *ristr) {
    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;
    if (rlstr || ristr) {
        TCLIST *keys = ristr ? tcfdbrange5(fdb, ristr, max) : tcfdbrange3(fdb, rlstr, rustr, max);
        for (int i = 0; i < tclistnum(keys); i++) {
            int ksiz;
            const char *kbuf = tclistval(keys, i, &ksiz);
            printf("%s", kbuf);
            if (pv) {
                int vsiz;
                char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz);
                if (vbuf) {
                    putchar('\t');
                    printdata(vbuf, vsiz, px);
                    tcfree(vbuf);
                }
            }
            putchar('\n');
        }
        tclistdel(keys);
    } else {
        if (!tcfdbiterinit(fdb)) {
            printerr(fdb);
            err = true;
        }
        int cnt = 0;
        uint64_t id;
        while ((id = tcfdbiternext(fdb)) > 0) {
            printf("%" PRIu64 "", (uint64_t) id);
            if (pv) {
                int vsiz;
                char *vbuf = tcfdbget(fdb, id, &vsiz);
                if (vbuf) {
                    putchar('\t');
                    printdata(vbuf, vsiz, px);
                    tcfree(vbuf);
                }
            }
            putchar('\n');
            if (max >= 0 && ++cnt >= max) break;
        }
    }
    if (!tcfdbclose(fdb)) {
        if (!err) printerr(fdb);
        err = true;
    }
    tcfdbdel(fdb);
    return err ? 1 : 0;
}
Ejemplo n.º 10
0
/* perform list command */
static int proclist(const char *path, int omode, int max, bool pv, bool px, const char *fmstr) {
    TCHDB *hdb = tchdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tchdbsetdbgfd(hdb, g_dbgfd);
    if (!tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(hdb);
    if (!tchdbopen(hdb, path, HDBOREADER | omode)) {
        printerr(hdb);
        tchdbdel(hdb);
        return 1;
    }
    bool err = false;
    if (fmstr) {
        TCLIST *keys = tchdbfwmkeys2(hdb, fmstr, max);
        for (int i = 0; i < tclistnum(keys); i++) {
            int ksiz;
            const char *kbuf = tclistval(keys, i, &ksiz);
            printdata(kbuf, ksiz, px);
            if (pv) {
                int vsiz;
                char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz);
                if (vbuf) {
                    putchar('\t');
                    printdata(vbuf, vsiz, px);
                    tcfree(vbuf);
                }
            }
            putchar('\n');
        }
        tclistdel(keys);
    } else {
        if (!tchdbiterinit(hdb)) {
            printerr(hdb);
            err = true;
        }
        TCXSTR *key = tcxstrnew();
        TCXSTR *val = tcxstrnew();
        int cnt = 0;
        while (tchdbiternext3(hdb, key, val)) {
            printdata(tcxstrptr(key), tcxstrsize(key), px);
            if (pv) {
                putchar('\t');
                printdata(tcxstrptr(val), tcxstrsize(val), px);
            }
            putchar('\n');
            if (max >= 0 && ++cnt >= max) break;
        }
        tcxstrdel(val);
        tcxstrdel(key);
    }
    if (!tchdbclose(hdb)) {
        if (!err) printerr(hdb);
        err = true;
    }
    tchdbdel(hdb);
    return err ? 1 : 0;
}
Ejemplo n.º 11
0
/* perform importtsv command */
static int procimporttsv(const char *path, const char *file, int omode, bool sc) {
    FILE *ifp = file ? fopen(file, "rb") : stdin;
    if (!ifp) {
        fprintf(stderr, "%s: could not open\n", file ? file : "(stdin)");
        return 1;
    }
    TCBDB *bdb = tcbdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tcbdbsetdbgfd(bdb, g_dbgfd);
    if (!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(bdb);
    if (!tcbdbopen(bdb, path, BDBOWRITER | BDBOCREAT | omode)) {
        printerr(bdb);
        tcbdbdel(bdb);
        if (ifp != stdin) fclose(ifp);
        return 1;
    }
    bool err = false;
    char *line;
    int cnt = 0;
    while (!err && (line = mygetline(ifp)) != NULL) {
        char *pv = strchr(line, '\t');
        if (!pv) {
            tcfree(line);
            continue;
        }
        *pv = '\0';
        if (sc) tcstrutfnorm(line, TCUNSPACE | TCUNLOWER | TCUNNOACC | TCUNWIDTH);
        if (!tcbdbputdup2(bdb, line, pv + 1)) {
            printerr(bdb);
            err = true;
        }
        tcfree(line);
        if (cnt > 0 && cnt % 100 == 0) {
            putchar('.');
            fflush(stdout);
            if (cnt % 5000 == 0) printf(" (%08d)\n", cnt);
        }
        cnt++;
    }
    printf(" (%08d)\n", cnt);
    if (!tcbdbclose(bdb)) {
        if (!err) printerr(bdb);
        err = true;
    }
    tcbdbdel(bdb);
    if (ifp != stdin) fclose(ifp);
    return err ? 1 : 0;
}
Ejemplo n.º 12
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;
}
Ejemplo n.º 13
0
/* perform out command */
static int procout(const char *path, const char *kbuf, int ksiz, int omode) {
    TCFDB *fdb = tcfdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd);
    if (!tcfdbopen(fdb, path, FDBOWRITER | omode)) {
        printerr(fdb);
        tcfdbdel(fdb);
        return 1;
    }
    bool err = false;
    if (!tcfdbout2(fdb, kbuf, ksiz)) {
        printerr(fdb);
        err = true;
    }
    if (!tcfdbclose(fdb)) {
        if (!err) printerr(fdb);
        err = true;
    }
    tcfdbdel(fdb);
    return err ? 1 : 0;
}
Ejemplo n.º 14
0
/* perform optimize command */
static int procoptimize(const char *path, int width, int64_t limsiz, int omode) {
    TCFDB *fdb = tcfdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd);
    if (!tcfdbopen(fdb, path, FDBOWRITER | omode)) {
        printerr(fdb);
        tcfdbdel(fdb);
        return 1;
    }
    bool err = false;
    if (!tcfdboptimize(fdb, width, limsiz)) {
        printerr(fdb);
        err = true;
    }
    if (!tcfdbclose(fdb)) {
        if (!err) printerr(fdb);
        err = true;
    }
    tcfdbdel(fdb);
    return err ? 1 : 0;
}
Ejemplo n.º 15
0
/* perform create command */
static int proccreate(const char *path, int width, int64_t limsiz) {
    TCFDB *fdb = tcfdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd);
    if (!tcfdbtune(fdb, width, limsiz)) {
        printerr(fdb);
        tcfdbdel(fdb);
        return 1;
    }
    if (!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC)) {
        printerr(fdb);
        tcfdbdel(fdb);
        return 1;
    }
    bool err = false;
    if (!tcfdbclose(fdb)) {
        printerr(fdb);
        err = true;
    }
    tcfdbdel(fdb);
    return err ? 1 : 0;
}
Ejemplo n.º 16
0
/* perform out command */
static int procout(const char *path, const char *kbuf, int ksiz, int omode) {
    TCHDB *hdb = tchdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tchdbsetdbgfd(hdb, g_dbgfd);
    if (!tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(hdb);
    if (!tchdbopen(hdb, path, HDBOWRITER | omode)) {
        printerr(hdb);
        tchdbdel(hdb);
        return 1;
    }
    bool err = false;
    if (!tchdbout(hdb, kbuf, ksiz)) {
        printerr(hdb);
        err = true;
    }
    if (!tchdbclose(hdb)) {
        if (!err) printerr(hdb);
        err = true;
    }
    tchdbdel(hdb);
    return err ? 1 : 0;
}
Ejemplo n.º 17
0
/* perform create command */
static int proccreate(const char *path, int bnum, int apow, int fpow, int opts) {
    TCHDB *hdb = tchdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tchdbsetdbgfd(hdb, g_dbgfd);
    if (!tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(hdb);
    if (!tchdbtune(hdb, bnum, apow, fpow, opts)) {
        printerr(hdb);
        tchdbdel(hdb);
        return 1;
    }
    if (!tchdbopen(hdb, path, HDBOWRITER | HDBOCREAT | HDBOTRUNC)) {
        printerr(hdb);
        tchdbdel(hdb);
        return 1;
    }
    bool err = false;
    if (!tchdbclose(hdb)) {
        printerr(hdb);
        err = true;
    }
    tchdbdel(hdb);
    return err ? 1 : 0;
}
Ejemplo n.º 18
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;
}
Ejemplo n.º 19
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;
}
Ejemplo n.º 20
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;
}