extern CAMLprim
value kc_cursor_next(value caml_cursor)
{
  CAMLparam1(caml_cursor);
  CAMLlocal2(val,pair);
  
  KCCUR* cur = get_cursor(caml_cursor);
  if (kccuraccept(cur, get_pair, &pair, 0, 1)) {
    val = caml_alloc(1,0); // Some(pair);
    Store_field(val, 0, pair); 
  }
  else {
     if (kccurecode(cur) == KCENOREC) {
       val = Val_int(0); // None
     }
     else {
       RAISE(kccuremsg(cur));
     }
  }
  
  CAMLreturn(val);
}
Exemple #2
0
/* perform order command */
static int32_t procorder(const char* path, int64_t rnum, int32_t rnd, int32_t etc,
                         int32_t tran, int32_t oflags) {
    KCDB* db;
    KCCUR* cur, *paracur;
    int32_t err;
    char kbuf[RECBUFSIZ], *vbuf, wbuf[RECBUFSIZ], *corepath, *copypath, *snappath;
    size_t ksiz, vsiz, psiz;
    int32_t wsiz;
    int64_t i, cnt;
    double stime, etime;
    VISARG visarg;
    oprintf("<In-order Test>\n  path=%s  rnum=%ld  rnd=%d  etc=%d  tran=%d  oflags=%d\n\n",
            path, (long)rnum, rnd, etc, tran, oflags);
    err = FALSE;
    db = kcdbnew();
    oprintf("opening the database:\n");
    stime = kctime();
    if (!kcdbopen(db, path, KCOWRITER | KCOCREATE | KCOTRUNCATE | oflags)) {
        dberrprint(db, __LINE__, "kcdbopen");
        err = TRUE;
    }
    etime = kctime();
    dbmetaprint(db, FALSE);
    oprintf("time: %.3f\n", etime - stime);
    oprintf("setting records:\n");
    stime = kctime();
    for (i = 1; !err && i <= rnum; i++) {
        if (tran && !kcdbbegintran(db, FALSE)) {
            dberrprint(db, __LINE__, "kcdbbegintran");
            err = TRUE;
        }
        ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i));
        if (!kcdbset(db, kbuf, ksiz, kbuf, ksiz)) {
            dberrprint(db, __LINE__, "kcdbset");
            err = TRUE;
        }
        if (tran && !kcdbendtran(db, TRUE)) {
            dberrprint(db, __LINE__, "kcdbendtran");
            err = TRUE;
        }
        if (rnum > 250 && i % (rnum / 250) == 0) {
            oputchar('.');
            if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i);
        }
    }
    etime = kctime();
    dbmetaprint(db, FALSE);
    oprintf("time: %.3f\n", etime - stime);
    if (etc) {
        oprintf("adding records:\n");
        stime = kctime();
        for (i = 1; !err && i <= rnum; i++) {
            if (tran && !kcdbbegintran(db, FALSE)) {
                dberrprint(db, __LINE__, "kcdbbegintran");
                err = TRUE;
            }
            ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i));
            if (!kcdbadd(db, kbuf, ksiz, kbuf, ksiz) && kcdbecode(db) != KCEDUPREC) {
                dberrprint(db, __LINE__, "kcdbadd");
                err = TRUE;
            }
            if (tran && !kcdbendtran(db, TRUE)) {
                dberrprint(db, __LINE__, "kcdbendtran");
                err = TRUE;
            }
            if (rnum > 250 && i % (rnum / 250) == 0) {
                oputchar('.');
                if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i);
            }
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
    }
    if (etc) {
        oprintf("appending records:\n");
        stime = kctime();
        for (i = 1; !err && i <= rnum; i++) {
            if (tran && !kcdbbegintran(db, FALSE)) {
                dberrprint(db, __LINE__, "kcdbbegintran");
                err = TRUE;
            }
            ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i));
            if (!kcdbappend(db, kbuf, ksiz, kbuf, ksiz)) {
                dberrprint(db, __LINE__, "kcdbadd");
                err = TRUE;
            }
            if (tran && !kcdbendtran(db, TRUE)) {
                dberrprint(db, __LINE__, "kcdbendtran");
                err = TRUE;
            }
            if (rnum > 250 && i % (rnum / 250) == 0) {
                oputchar('.');
                if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i);
            }
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
    }
    oprintf("getting records:\n");
    stime = kctime();
    for (i = 1; !err && i <= rnum; i++) {
        if (tran && !kcdbbegintran(db, FALSE)) {
            dberrprint(db, __LINE__, "kcdbbegintran");
            err = TRUE;
        }
        ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i));
        vbuf = kcdbget(db, kbuf, ksiz, &vsiz);
        if (vbuf) {
            if (vsiz < ksiz || memcmp(vbuf, kbuf, ksiz)) {
                dberrprint(db, __LINE__, "kcdbget");
                err = TRUE;
            }
            kcfree(vbuf);
        } else if (!rnd || kcdbecode(db) != KCENOREC) {
            dberrprint(db, __LINE__, "kcdbget");
            err = TRUE;
        }
        if (tran && !kcdbendtran(db, TRUE)) {
            dberrprint(db, __LINE__, "kcdbendtran");
            err = TRUE;
        }
        if (rnum > 250 && i % (rnum / 250) == 0) {
            oputchar('.');
            if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i);
        }
    }
    etime = kctime();
    dbmetaprint(db, FALSE);
    oprintf("time: %.3f\n", etime - stime);
    if (etc) {
        oprintf("getting records with a buffer:\n");
        stime = kctime();
        for (i = 1; !err && i <= rnum; i++) {
            if (tran && !kcdbbegintran(db, FALSE)) {
                dberrprint(db, __LINE__, "kcdbbegintran");
                err = TRUE;
            }
            ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i));
            wsiz = kcdbgetbuf(db, kbuf, ksiz, wbuf, sizeof(wbuf));
            if (wsiz >= 0) {
                if (wsiz < (int32_t)ksiz || memcmp(wbuf, kbuf, ksiz)) {
                    dberrprint(db, __LINE__, "kcdbgetbuf");
                    err = TRUE;
                }
            } else if (!rnd || kcdbecode(db) != KCENOREC) {
                dberrprint(db, __LINE__, "kcdbgetbuf");
                err = TRUE;
            }
            if (tran && !kcdbendtran(db, TRUE)) {
                dberrprint(db, __LINE__, "kcdbendtran");
                err = TRUE;
            }
            if (rnum > 250 && i % (rnum / 250) == 0) {
                oputchar('.');
                if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i);
            }
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
    }
    if (etc) {
        oprintf("traversing the database by the inner iterator:\n");
        stime = kctime();
        cnt = kcdbcount(db);
        visarg.rnum = rnum;
        visarg.rnd = rnd;
        visarg.cnt = 0;
        memset(visarg.rbuf, '+', sizeof(visarg.rbuf));
        if (tran && !kcdbbegintran(db, FALSE)) {
            dberrprint(db, __LINE__, "kcdbbegintran");
            err = TRUE;
        }
        if (!kcdbiterate(db, visitfull, &visarg, TRUE)) {
            dberrprint(db, __LINE__, "kcdbiterate");
            err = TRUE;
        }
        if (rnd) oprintf(" (end)\n");
        if (tran && !kcdbendtran(db, TRUE)) {
            dberrprint(db, __LINE__, "kcdbendtran");
            err = TRUE;
        }
        if (visarg.cnt != cnt) {
            dberrprint(db, __LINE__, "kcdbiterate");
            err = TRUE;
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
    }
    if (etc) {
        oprintf("traversing the database by the outer cursor:\n");
        stime = kctime();
        cnt = kcdbcount(db);
        visarg.rnum = rnum;
        visarg.rnd = rnd;
        visarg.cnt = 0;
        if (tran && !kcdbbegintran(db, FALSE)) {
            dberrprint(db, __LINE__, "kcdbbegintran");
            err = TRUE;
        }
        cur = kcdbcursor(db);
        if (!kccurjump(cur) && kccurecode(cur) != KCENOREC) {
            dberrprint(db, __LINE__, "kccurjump");
            err = TRUE;
        }
        paracur = kcdbcursor(db);
        while (!err && kccuraccept(cur, &visitfull, &visarg, TRUE, !rnd)) {
            if (rnd) {
                ksiz = sprintf(kbuf, "%08ld", (long)myrand(rnum));
                switch (myrand(3)) {
                case 0: {
                    if (!kcdbremove(db, kbuf, ksiz) && kcdbecode(db) != KCENOREC) {
                        dberrprint(db, __LINE__, "kcdbremove");
                        err = TRUE;
                    }
                    break;
                }
                case 1: {
                    if (!kccurjumpkey(paracur, kbuf, ksiz) && kccurecode(paracur) != KCENOREC) {
                        dberrprint(db, __LINE__, "kccurjump");
                        err = TRUE;
                    }
                    break;
                }
                default: {
                    if (!kccurstep(cur) && kccurecode(cur) != KCENOREC) {
                        dberrprint(db, __LINE__, "kccurstep");
                        err = TRUE;
                    }
                    break;
                }
                }
            }
        }
        oprintf(" (end)\n");
        kccurdel(paracur);
        kccurdel(cur);
        if (tran && !kcdbendtran(db, TRUE)) {
            dberrprint(db, __LINE__, "kcdbendtran");
            err = TRUE;
        }
        if (!rnd && visarg.cnt != cnt) {
            dberrprint(db, __LINE__, "kccuraccept");
            err = TRUE;
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
    }
    if (etc) {
        oprintf("synchronizing the database:\n");
        stime = kctime();
        if (!kcdbsync(db, FALSE, NULL, NULL)) {
            dberrprint(db, __LINE__, "kcdbsync");
            err = TRUE;
        }
        if (!kcdboccupy(db, FALSE, NULL, NULL)) {
            dberrprint(db, __LINE__, "kcdboccupy");
            err = TRUE;
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
    }
    if (etc) {
        corepath = kcdbpath(db);
        psiz = strlen(corepath);
        if (strstr(corepath, ".kch") || strstr(corepath, ".kct")) {
            copypath = kcmalloc(psiz + 256);
            sprintf(copypath, "%s.tmp", corepath);
            snappath = kcmalloc(psiz + 256);
            sprintf(snappath, "%s.kcss", corepath);
        } else {
            copypath = kcmalloc(256);
            sprintf(copypath, "kclangctest.tmp");
            snappath = kcmalloc(256);
            sprintf(snappath, "kclangctest.kcss");
        }
        oprintf("copying the database file:\n");
        stime = kctime();
        if (!kcdbcopy(db, copypath)) {
            dberrprint(db, __LINE__, "kcdbcopy");
            err = TRUE;
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
        remove(copypath);
        oprintf("dumping records into snapshot:\n");
        stime = kctime();
        if (!kcdbdumpsnap(db, snappath)) {
            dberrprint(db, __LINE__, "kcdbdumpsnap");
            err = TRUE;
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
        oprintf("loading records into snapshot:\n");
        stime = kctime();
        cnt = kcdbcount(db);
        if (rnd && myrand(2) == 0 && !kcdbclear(db)) {
            dberrprint(db, __LINE__, "kcdbclear");
            err = TRUE;
        }
        if (!kcdbloadsnap(db, snappath) || kcdbcount(db) != cnt) {
            dberrprint(db, __LINE__, "kcdbloadsnap");
            err = TRUE;
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
        remove(snappath);
        kcfree(copypath);
        kcfree(snappath);
        kcfree(corepath);
    }
    oprintf("removing records:\n");
    stime = kctime();
    for (i = 1; !err && i <= rnum; i++) {
        if (tran && !kcdbbegintran(db, FALSE)) {
            dberrprint(db, __LINE__, "kcdbbegintran");
            err = TRUE;
        }
        ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i));
        if (!kcdbremove(db, kbuf, ksiz) &&
                ((!rnd && !etc) || kcdbecode(db) != KCENOREC)) {
            dberrprint(db, __LINE__, "kcdbremove");
            err = TRUE;
        }
        if (tran && !kcdbendtran(db, TRUE)) {
            dberrprint(db, __LINE__, "kcdbendtran");
            err = TRUE;
        }
        if (rnum > 250 && i % (rnum / 250) == 0) {
            oputchar('.');
            if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i);
        }
    }
    etime = kctime();
    dbmetaprint(db, TRUE);
    oprintf("time: %.3f\n", etime - stime);
    oprintf("closing the database:\n");
    stime = kctime();
    if (!kcdbclose(db)) {
        dberrprint(db, __LINE__, "kcdbclose");
        err = TRUE;
    }
    etime = kctime();
    oprintf("time: %.3f\n", etime - stime);
    kcdbdel(db);
    oprintf("%s\n\n", err ? "error" : "ok");
    return err ? 1 : 0;
}