示例#1
0
static void _tc_release_conn(mapcache_context *ctx, mapcache_tile *tile, struct tc_conn conn)
{
  if(!conn.readonly)
    tcbdbsync(conn.bdb);

  if(!tcbdbclose(conn.bdb)) {
    int ecode = tcbdbecode(conn.bdb);
    ctx->set_error(ctx,500, "tokyocabinet close error: %s\n",tcbdberrmsg(ecode));
  }
  tcbdbdel(conn.bdb);
}
示例#2
0
void
mutt_hcache_close(header_cache_t *h)
{
  if (!h)
    return;

  if (!tcbdbclose(h->db))
      dprint (2, (debugfile, "tcbdbclose failed for %s: %d\n", h->folder, errno));
  tcbdbdel(h->db);
  FREE(&h->folder);
  FREE(&h);
}
示例#3
0
int main(int argc, char **argv){

  if (argc != 3) {
    std::cerr << "Usage: " << argv[0] << " dbname record_num" << std::endl; 
    exit(1);
  }

  double t1, t2;
  int rnum = atoi(argv[2]);
  int ecode;
  char *key, *value;

  t1 = gettimeofday_sec();
  TCBDB *bdb = tcbdbnew();
  tcbdbtune(bdb, 240, -1, -1, -1, -1, 0);
  //tcbdbtune(bdb, 480, -1, -1, -1, -1, BDBTDEFLATE);
  tcbdbsetcache(bdb, 20480, 10240);

  if (!tcbdbopen(bdb, argv[1], BDBOWRITER | BDBOCREAT)) {
    ecode = tcbdbecode(bdb);
    fprintf(stderr, "open error: %s\n", tcbdberrmsg(ecode));
  }

  for (int i = 0; i < rnum; ++i) {
    char key[9];
    memset(key, 0, 9);
    sprintf(key,"%08d", i);

    int size;
    void *data = tcbdbget(bdb, key, strlen(key), &size);
    if (data != NULL) {
      if (i != *(int32_t *) data) {
        std::cout << "[error] value incorrect." << std::endl;
      }
      free(data);
    } else {
      std::cout << "[error] entry not found." << std::endl;
    } 
  }

  if (!tcbdbclose(bdb)) {
    ecode = tcbdbecode(bdb);
    fprintf(stderr, "close error: %s\n", tcbdberrmsg(ecode));
  }
  tcbdbdel(bdb);
  t2 = gettimeofday_sec();
  std::cout << "get time: " << t2 - t1 << std::endl;

  return 0;
}
示例#4
0
文件: tctest.c 项目: Fleurer/nanodb
/* perform btwrite command */
int dobtwrite(char *name, int rnum, int rnd){
  TCBDB *bdb;
  int i, err, len;
  char buf[RECBUFSIZ];
  if(showprgr) printf("<Writing Test of B+ Tree>\n  name=%s  rnum=%d\n\n", name, rnum);
  /* open a database */
  bdb = tcbdbnew();
  if(rnd){
    tcbdbtune(bdb, 77, 256, -1, 0, 0, 0);
    tcbdbsetcache(bdb, rnum / 77, -1);
  } else {
    tcbdbtune(bdb, 101, 256, -1, 0, 0, 0);
    tcbdbsetcache(bdb, 256, 256);
  }
  tcbdbsetxmsiz(bdb, rnum * 32);
  if(!tcbdbopen(bdb, name, BDBOWRITER | BDBOCREAT | BDBOTRUNC)){
    fprintf(stderr, "tcbdbopen failed\n");
    tcbdbdel(bdb);
    return 1;
  }
  err = FALSE;
  /* loop for each record */
  for(i = 1; i <= rnum; i++){
    /* store a record */
    len = sprintf(buf, "%08d", rnd ? myrand() % rnum + 1 : i);
    if(!tcbdbput(bdb, buf, len, buf, len)){
      fprintf(stderr, "tcbdbput failed\n");
      err = TRUE;
      break;
    }
    /* print progression */
    if(showprgr && rnum > 250 && i % (rnum / 250) == 0){
      putchar('.');
      fflush(stdout);
      if(i == rnum || i % (rnum / 10) == 0){
        printf(" (%08d)\n", i);
        fflush(stdout);
      }
    }
  }
  /* close the database */
  if(!tcbdbclose(bdb)){
    fprintf(stderr, "tcbdbclose failed\n");
    tcbdbdel(bdb);
    return 1;
  }
  tcbdbdel(bdb);
  if(showprgr && !err) printf("ok\n\n");
  return err ? 1 : 0;
}
示例#5
0
/* Close a word database object.
   `wdb' specifies the word database object.
   If successful, the return value is true, else, it is false. */
static bool tcwdbcloseimpl(TCWDB *wdb){
  assert(wdb);
  bool err = false;
  if(wdb->cc){
    if((tcmaprnum(wdb->cc) > 0 || tcmaprnum(wdb->dtokens) > 0) && !tcwdbmemsync(wdb, 0))
      err = true;
    tcidsetdel(wdb->dids);
    tcmapdel(wdb->dtokens);
    tcmapdel(wdb->cc);
    wdb->cc = NULL;
  }
  if(!tcbdbclose(wdb->idx)) err = true;
  wdb->open = false;
  return !err;
}
示例#6
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;
}
示例#7
0
/* Open a word database object.
   `wdb' specifies the word database object.
   `path' specifies the path of the database file.
   `omode' specifies the connection mode.
   If successful, the return value is true, else, it is false. */
static bool tcwdbopenimpl(TCWDB *wdb, const char *path, int omode){
  assert(wdb && path);
  int bomode = BDBOREADER;
  if(omode & WDBOWRITER){
    bomode = BDBOWRITER;
    if(omode & WDBOCREAT) bomode |= BDBOCREAT;
    if(omode & WDBOTRUNC) bomode |= BDBOTRUNC;
    int64_t bnum = (wdb->etnum / WDBLMEMB) * 2 + 1;
    int bopts = 0;
    if(wdb->opts & WDBTLARGE) bopts |= BDBTLARGE;
    if(wdb->opts & WDBTDEFLATE) bopts |= BDBTDEFLATE;
    if(wdb->opts & WDBTBZIP) bopts |= BDBTBZIP;
    if(wdb->opts & WDBTTCBS) bopts |= BDBTTCBS;
    if(!tcbdbtune(wdb->idx, WDBLMEMB, WDBNMEMB, bnum, WDBAPOW, WDBFPOW, bopts)) return false;
    if(!tcbdbsetlsmax(wdb->idx, WDBLSMAX)) return false;
  }
  if(wdb->lcnum > 0){
    if(!tcbdbsetcache(wdb->idx, wdb->lcnum, wdb->lcnum / 4 + 1)) return false;
  } else {
    if(!tcbdbsetcache(wdb->idx, (omode & WDBOWRITER) ? WDBLCNUMW : WDBLCNUMR, WDBNCNUM))
      return false;
  }
  if(omode & WDBONOLCK) bomode |= BDBONOLCK;
  if(omode & WDBOLCKNB) bomode |= BDBOLCKNB;
  if(!tcbdbopen(wdb->idx, path, bomode)) return false;
  if((omode & WDBOWRITER) && tcbdbrnum(wdb->idx) < 1){
    memcpy(tcbdbopaque(wdb->idx), WDBMAGICDATA, strlen(WDBMAGICDATA));
  } else if(!(omode & WDBONOLCK) &&
            memcmp(tcbdbopaque(wdb->idx), WDBMAGICDATA, strlen(WDBMAGICDATA))){
    tcbdbclose(wdb->idx);
    tcbdbsetecode(wdb->idx, TCEMETA, __FILE__, __LINE__, __func__);
    return 0;
  }
  if(omode & WDBOWRITER){
    wdb->cc = tcmapnew2(WDBCCBNUM);
    wdb->dtokens = tcmapnew2(WDBDTKNBNUM);
    wdb->dids = tcidsetnew(WDBDIDSBNUM);
  }
  wdb->open = true;
  return true;
}
示例#8
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;
}
示例#9
0
文件: tcbtdb.c 项目: kabacs/goaccess
/* Close the database handle */
int
tc_bdb_close (void *db, char *dbname)
{
    TCBDB *bdb = db;
    int ecode;

    if (bdb == NULL)
        return 1;

    /* close the database */
    if (!tcbdbclose (bdb)) {
        ecode = tcbdbecode (bdb);
        FATAL ("%s", tcbdberrmsg (ecode));
    }
    /* delete the object */
    tcbdbdel (bdb);

    /* remove database file */
    if (!conf.keep_db_files && !tcremovelink (dbname))
        LOG_DEBUG (("Unable to remove DB: %s\n", dbname));
    free (dbname);

    return 0;
}
示例#10
0
/*
   Close files and clean up.
*/
void db_close(void *vhandle)
{
    dbh_t *handle = vhandle;
    TCBDB *dbp;

    if (handle == NULL) return;

    if (DEBUG_DATABASE(1))
	fprintf(dbgout, "(tc) tcbdbclose(%s)\n", handle->name);

    dbp = handle->dbp;

    db_optimize(dbp, handle->name);

    if (!tcbdbclose(dbp))
	print_error(__FILE__, __LINE__, "(tc) tcbdbclose for %s err: %d, %s",
		    handle->name, 
		    tcbdbecode(dbp), tcbdberrmsg(tcbdbecode(dbp)));

    tcbdbdel(dbp);
    handle->dbp = NULL;

    dbh_free(handle);
}
示例#11
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;
}
示例#12
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;
}
示例#13
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;
}
示例#14
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;
}
示例#15
0
/* close */
JNIEXPORT jboolean JNICALL Java_tokyocabinet_BDB_close
(JNIEnv *env, jobject self){
  TCBDB *bdb = (TCBDB *)(intptr_t)(*env)->GetLongField(env, self, bdb_fid_ptr);
  return tcbdbclose(bdb);
}
示例#16
0
文件: tc.c 项目: chinnurtb/MotionDb
static int control(ErlDrvData handle, unsigned int command, char* buf, int count, char** res, int res_size) {
  tcbdb_drv_t *driver = (tcbdb_drv_t*)handle;
  TCBDB *bdb = driver->bdb;

  int index = 1, tp, sz, version;
  char key[MAX_KEY_SIZE], value[MAX_VALUE_SIZE];
  long key_size, value_size;
  long long long_tmp;
  bool rs;
  const char *value_tmp = NULL;

  ei_x_buff x;
  ei_x_new_with_version(&x);
  if (bdb == NULL && command != OPEN)
    return ei_error(&x, "database_not_opened", res, res_size);

  ei_decode_version(buf, &index, &version);

  switch (command) {
    case OPEN:
      // open(Filepath::string())
      if (bdb == NULL) {
        ei_get_type(buf, &index, &tp, &sz);
        if (tp != ERL_STRING_EXT)
          return ei_error(&x, "invalid_argument", res, res_size);

        TCBDB *bdb = tcbdbnew();
        tcbdbsetmutex(bdb);
        tcbdbsetcache(bdb, 104800, 51200);
        tcbdbsetxmsiz(bdb, 1048576);
        tcbdbtune(bdb, 0, 0, 0, 7, -1, BDBTLARGE);

        char *file = driver_alloc(sz + 1);
        ei_decode_string(buf, &index, file);
        rs = tcbdbopen(bdb, file, BDBOWRITER | BDBOCREAT);
        driver_free(file);
        if (!rs)
          return ei_error(&x, tcbdberrmsg(tcbdbecode(bdb)), res, res_size);
        driver->bdb = bdb;
        ei_x_encode_atom(&x, "ok");
      } else
        return ei_error(&x, "database already opened", res, res_size);
      break;

    case CLOSE:
      tcbdbclose(bdb);
      tcbdbdel(bdb);
      driver->bdb = NULL;
      ei_x_encode_atom(&x, "ok");
      break;

    case PUT:
    case PUTDUP:
    case PUTCAT:
    case PUTKEEP:
      // put({Key::binary(), Value::binary()})
      ei_get_type(buf, &index, &tp, &sz);
      if (tp != ERL_SMALL_TUPLE_EXT || sz != 2)
        return ei_error(&x, "invalid_argument", res, res_size);
      ei_decode_tuple_header(buf, &index, &sz);

      ei_get_type(buf, &index, &tp, &sz);
      if (tp != ERL_BINARY_EXT || sz > MAX_KEY_SIZE)
        return ei_error(&x, "invalid_argument", res, res_size);
      ei_decode_binary(buf, &index, &key[0], &key_size);
      ei_get_type(buf, &index, &tp, &sz);
      if (tp != ERL_BINARY_EXT)
        return ei_error(&x, "invalid_argument", res, res_size);
      if (sz <= MAX_VALUE_SIZE) {
        ei_decode_binary(buf, &index, &value[0], &value_size);
        switch (command) {
          case PUT: rs = tcbdbput(bdb, &key[0], key_size, &value[0], value_size); break;
          case PUTDUP: rs = tcbdbputdup(bdb, &key[0], key_size, &value[0], value_size); break;
          case PUTCAT: rs = tcbdbputcat(bdb, &key[0], key_size, &value[0], value_size); break;
          default: rs = tcbdbputkeep(bdb, &key[0], key_size, &value[0], value_size);
        }
      } else {
        void *p = driver_alloc(sz);
        if (p) {
          ei_decode_binary(buf, &index, p, &value_size);
          switch (command) {
            case PUT: rs = tcbdbput(bdb, &key[0], key_size, p, value_size); break;
            case PUTDUP: rs = tcbdbputdup(bdb, &key[0], key_size, p, value_size); break;
            case PUTCAT: rs = tcbdbputcat(bdb, &key[0], key_size, p, value_size); break;
            default: rs = tcbdbputkeep(bdb, &key[0], key_size, p, value_size);
          }
          driver_free(p);
        } else
          return ei_error(&x, "too long value", res, res_size);
      };
      if (!rs)
        return ei_error(&x, tcbdberrmsg(tcbdbecode(bdb)), res, res_size);
      ei_x_encode_atom(&x, "ok");
      break;

    case GET:
      // get(Key::binary()) -> {ok, Value} | {error, not_found}
      ei_get_type(buf, &index, &tp, &sz);
      if (tp != ERL_BINARY_EXT || sz > MAX_KEY_SIZE)
        return ei_error(&x, "invalid_argument", res, res_size);
      ei_decode_binary(buf, &index, &key[0], &key_size);

      value_tmp = tcbdbget3(bdb, &key[0], key_size, &sz);
      ei_x_encode_tuple_header(&x, 2);
      if (value_tmp != NULL) {
        ei_x_encode_atom(&x, "ok");
        ei_x_encode_binary(&x, value_tmp, sz);
      } else {
        ei_x_encode_atom(&x, "error");
        ei_x_encode_atom(&x, "not_found");
      }
      break;

    case GETDUP:
      // get(Key::binary()) -> {ok, Values}
      ei_get_type(buf, &index, &tp, &sz);
      if (tp != ERL_BINARY_EXT || sz > MAX_KEY_SIZE)
        return ei_error(&x, "invalid_argument", res, res_size);
      ei_decode_binary(buf, &index, &key[0], &key_size);

      TCLIST *vals = tcbdbget4(bdb, key, key_size);
      if (vals) {
        ei_x_encode_tuple_header(&x, 2);
        ei_x_encode_atom(&x, "ok");
        int j;
        for (j=0; j<tclistnum(vals); j++) {
          value_tmp = tclistval(vals, j, &sz);
          ei_x_encode_list_header(&x, 1);
          ei_x_encode_binary(&x, value_tmp, sz);
        }
        tclistdel(vals);
        ei_x_encode_empty_list(&x);
      } else {
        ei_x_encode_tuple_header(&x, 2);
        ei_x_encode_atom(&x, "ok");
        ei_x_encode_empty_list(&x);
      }
      break;

    case REMOVE:
      // remove(Keys::list())
      // remove(Key::binary())
      // remove({Key::binary(), Value::binary()})
      ei_get_type(buf, &index, &tp, &sz);
      if (tp == ERL_LIST_EXT) {
        int count, j;
        ei_decode_list_header(buf, &index, &count);
        for (j=0; j<count; j++) {
          ei_get_type(buf, &index, &tp, &sz);
          if (tp != ERL_BINARY_EXT || sz > MAX_KEY_SIZE)
            return ei_error(&x, "invalid_argument", res, res_size);
          ei_decode_binary(buf, &index, &key[0], &key_size);
          if (!tcbdbout3(bdb, &key[0], key_size))
            return ei_error(&x, tcbdberrmsg(tcbdbecode(bdb)), res, res_size);
        }
        ei_x_encode_atom(&x, "ok");
      } else if (tp == ERL_BINARY_EXT && sz <= MAX_KEY_SIZE) {
        ei_decode_binary(buf, &index, &key[0], &key_size);
        tcbdbout3(bdb, &key[0], key_size);
        ei_x_encode_atom(&x, "ok");
      } else if (tp == ERL_SMALL_TUPLE_EXT && sz == 2) {
        ei_decode_tuple_header(buf, &index, &sz);
        // get key
        ei_get_type(buf, &index, &tp, &sz);
        if (tp != ERL_BINARY_EXT || sz > MAX_KEY_SIZE)
          return ei_error(&x, "invalid_argument", res, res_size);
        ei_decode_binary(buf, &index, &key[0], &key_size);
        // get value
        ei_get_type(buf, &index, &tp, &sz);
        if (tp != ERL_BINARY_EXT || sz > MAX_VALUE_SIZE)
          return ei_error(&x, "invalid_argument", res, res_size);
        ei_decode_binary(buf, &index, &value[0], &value_size);
        // remove by key&value
        BDBCUR *cur = tcbdbcurnew(bdb);
        if (!tcbdbcurjump(cur, &key[0], key_size))
          return ei_error(&x, "record_not_found", res, res_size);
        
        bool removed = false, not_found = false;
        while (!removed && !not_found) {
          int cur_key_size, cur_val_size;
          const void *curkey = tcbdbcurkey3(cur, &cur_key_size);
          if (cur_key_size == key_size && memcmp(curkey, key, key_size) == 0) {
            const void *curval = tcbdbcurval3(cur, &cur_val_size);
            if (cur_val_size == value_size && memcmp(curval, value, value_size) == 0) {
              tcbdbcurout(cur);
              removed = true;
            } else
              if (!tcbdbcurnext(cur))
                not_found = true;
          } else not_found = true;
        }
        if (not_found) ei_x_encode_atom(&x, "not_found");
        else ei_x_encode_atom(&x, "ok");
        
      } else
        return ei_error(&x, "invalid_argument", res, res_size);
      break;

    case RANGE:
      /*
       * range({Prefix::binary(), limit:integer()})
       * range({StartKey::binary(), BeginInclusion::boolean(), EndKey::binary(), EndInclusion::binary(), limit:integer()})
       */
      ei_get_type(buf, &index, &tp, &sz);
      if (tp != ERL_SMALL_TUPLE_EXT || sz < 2)
        return ei_error(&x, "invalid_argument", res, res_size);
      ei_decode_tuple_header(buf, &index, &sz);

      ei_get_type(buf, &index, &tp, &sz);
      if (tp == ERL_BINARY_EXT && sz <= MAX_KEY_SIZE) {
        char keys[MAX_KEY_SIZE], keyf[MAX_KEY_SIZE];
        long keys_size, keyf_size;
        int keys_inc, keyf_inc;
        long max = -1;
        TCLIST *range;

        ei_decode_binary(buf, &index, &keys[0], &keys_size);
        ei_get_type(buf, &index, &tp, &sz);
        if (tp == ERL_ATOM_EXT) {
          // range
          ei_decode_boolean(buf, &index, &keys_inc);
          ei_get_type(buf, &index, &tp, &sz);
          if (tp != ERL_BINARY_EXT || sz > MAX_KEY_SIZE)
            return ei_error(&x, "invalid_argument", res, res_size);
          ei_decode_binary(buf, &index, &keyf[0], &keyf_size);
          ei_get_type(buf, &index, &tp, &sz);
          if (tp != ERL_ATOM_EXT)
            return ei_error(&x, "invalid_argument", res, res_size);
          ei_decode_boolean(buf, &index, &keyf_inc);

          ei_get_type(buf, &index, &tp, &sz);
          if (tp != ERL_INTEGER_EXT)
            return ei_error(&x, "invalid_argument", res, res_size);
          ei_decode_long(buf, &index, &max);

          range = tcbdbrange(bdb, &keys[0], keys_size, keys_inc == 1, &keyf[0], keyf_size, keyf_inc == 1, max);
        } else if (tp == ERL_INTEGER_EXT) {
          // prefix
          ei_get_type(buf, &index, &tp, &sz);
          if (tp != ERL_INTEGER_EXT)
            return ei_error(&x, "invalid_argument", res, res_size);
          ei_decode_long(buf, &index, &max);

          range = tcbdbfwmkeys(bdb, &keys[0], keys_size, max);
        } else
          return ei_error(&x, "invalid_argument", res, res_size);

        const char *key;
        int key_size, value_size;
        int idx, cnt = 0, rcount = tclistnum(range);

        ei_x_encode_tuple_header(&x, 2);
        ei_x_encode_atom(&x, "ok");

        BDBCUR *cur = tcbdbcurnew(bdb);
        for (idx=0; idx<rcount; idx++) {
          key = tclistval(range, idx, &key_size);
          TCLIST *vals = tcbdbget4(bdb, key, key_size);
          if (vals) {
            int j;
            for (j=0; j<tclistnum(vals); j++) {
              ei_x_encode_list_header(&x, 1);
              value_tmp = tclistval(vals, j, &value_size);
              ei_x_encode_binary(&x, value_tmp, value_size);
              if (max >= 0 && ++cnt >= max) break;
            }
            tclistdel(vals);
          }
          idx++;
        }
        tcbdbcurdel(cur);
        tclistdel(range);
        ei_x_encode_empty_list(&x);

      } else
        return ei_error(&x, "invalid_argument", res, res_size);
      break;

    case SYNC:
      // sync()
      if (!tcbdbsync(bdb))
        return ei_error(&x, tcbdberrmsg(tcbdbecode(bdb)), res, res_size);
      ei_x_encode_atom(&x, "ok");
      break;

    case INFO:
      // info()
      ei_x_encode_tuple_header(&x, 3);
      ei_x_encode_atom(&x, "ok");
      long_tmp = tcbdbrnum(bdb);
      ei_x_encode_longlong(&x, long_tmp);
      long_tmp = tcbdbfsiz(bdb);
      ei_x_encode_longlong(&x, long_tmp);
      break;

    case ITERATE:
      // Read(none) -> {ok, Key} | {error, not_found}
      // Read(Key::binary()) -> {ok, Key} | {error, not_found}
      ei_get_type(buf, &index, &tp, &sz);
      BDBCUR *cur = tcbdbcurnew(bdb);
      if (tp == ERL_BINARY_EXT && sz <= MAX_KEY_SIZE) {
        ei_decode_binary(buf, &index, &key[0], &key_size);
        rs = tcbdbcurjump(cur, &key[0], key_size) && tcbdbcurnext(cur);
      } else
        rs = tcbdbcurfirst(cur);
      if (rs) {
        int key_size;
        const char *key = tcbdbcurkey3(cur, &key_size);

        ei_x_encode_tuple_header(&x, 2);
        ei_x_encode_atom(&x, "ok");
        ei_x_encode_binary(&x, key, key_size);
        tcbdbcurdel(cur);
      } else {
        tcbdbcurdel(cur);
        return ei_error(&x, "not_found", res, res_size);
      }
      break;

    case VANISH:
      // vanish() -> ok
      if (!tcbdbvanish(bdb))
        return ei_error(&x, tcbdberrmsg(tcbdbecode(bdb)), res, res_size);
      ei_x_encode_atom(&x, "ok");
      break;

    case BACKUP:
      // backup(path::string()) -> ok | {error, Reason}
      ei_get_type(buf, &index, &tp, &sz);
      if (tp == ERL_STRING_EXT) {
        char *file = driver_alloc(sz + 1);
        ei_decode_string(buf, &index, file);
        if (tcbdbcopy(driver->bdb, file))
          ei_x_encode_atom(&x, "ok");
        else
          return ei_error(&x, tcbdberrmsg(tcbdbecode(bdb)), res, res_size);
      } else
        return ei_error(&x, "invalid_argument", res, res_size);
      break;

    default:
      return ei_error(&x, "invalid_command", res, res_size);
  }

  if (res_size < x.index)
    (*res) = (char *)driver_alloc(x.index);
  int n = x.index;
  memcpy(*res, x.buff, x.index);
  ei_x_free(&x);
  return n;
};
示例#17
0
int main(int argc, char **argv){

  if (argc != 3) {
    std::cerr << "Usage: " << argv[0] << " file dbname" << std::endl; 
    exit(1);
  }

  double t1, t2;
  FILE *fp = fopen(argv[1], "r");
  if (fp == NULL) {
    std::cerr << "failed to open " << argv[1] << std::endl;
    exit(1);
  }

  int ecode;
  char *key, *value;

  t1 = gettimeofday_sec();
  TCBDB *bdb = tcbdbnew();
  tcbdbtune(bdb, 5, -1, -1, -1, -1, 0);
  tcbdbsetcache(bdb, 300, 100);

  if (!tcbdbopen(bdb, argv[2], BDBOWRITER | BDBOCREAT)) {
    ecode = tcbdbecode(bdb);
    fprintf(stderr, "open error: %s\n", tcbdberrmsg(ecode));
  }

  char buf[256];
  while (fgets(buf, 256, fp)) {
    char *key = strchr(buf, ' ');
    if (key == NULL) { continue; }
    *key++ = '\0';
    char *valp = strchr(key, ' ');
    if (valp == NULL) { continue; }
    *valp++ = '\0';
    char *val = new char[102401]; // 100K
    sprintf(val, "%0102400d", atoi(valp));

    int size;
    void *data = tcbdbget(bdb, key, strlen(key), &size);
    if (data != NULL) {
      if (strcmp(val, (char *) data) != 0) {
        std::cout << "[error] value incorrect." << std::endl;
      }
      free(data);
    } else {
      std::cout << "[error] entry not found." << std::endl;
    } 
    delete [] val;
  }

  if (!tcbdbclose(bdb)) {
    ecode = tcbdbecode(bdb);
    fprintf(stderr, "close error: %s\n", tcbdberrmsg(ecode));
  }
  tcbdbdel(bdb);
  t2 = gettimeofday_sec();
  std::cout << "get time: " << t2 - t1 << std::endl;

  fclose(fp);

  return 0;
}
示例#18
0
int main(int argc, char* argv[]) {
    // Parse the command line.
    if (argc != 3)
        usage(argv[0]);
    std::string outputFile = argv[2];

    // Open the input file.
    std::cout << "Processing: " << argv[1] << std::endl;
    std::ifstream file(argv[1], std::ios_base::in | std::ios_base::binary);
    if (! file) {
        std::cerr << "ERROR: Could not open input file: " << argv[1]
            << std::endl;
        std::exit(1);
    }

    ::boost::iostreams::filtering_istream in;
    if (file.peek() == 0x1f)
        in.push(::boost::iostreams::gzip_decompressor());
    in.push(file);

    // Initialise the database.
#ifdef QDBM_AS_TOKYOCABINET
    VILLA* db;
    if (! (db = vlopen(outputFile.c_str(),
            VL_OWRITER | VL_OCREAT | VL_OTRUNC | VL_OZCOMP, VL_CMPLEX))) {
        std::cerr << "ERROR: Could not open QDBM database: "
            << outputFile << std::endl;
        std::exit(1);
    }
#else
    TCBDB* db = tcbdbnew();
    if (! tcbdbopen(db, outputFile.c_str(),
            BDBOWRITER | BDBOCREAT | BDBOTRUNC)) {
        std::cerr << "ERROR: Could not open Tokyo Cabinet database: "
            << outputFile << std::endl;
        std::exit(1);
    }
#endif

    // Fill the database with the user-supplied key-value pairs.
    std::string sig, name;
    const char* pos;
    unsigned long tot = 0;
    while (true) {
        in >> sig;
        if (in.eof())
            break;

        std::getline(in, name);
        if (in.eof()) {
            std::cerr << "ERROR: Signature " << sig
                << " is missing a corresponding name.\n\n";
            DB_CLOSE(db);
            usage(argv[0]);
        }

        // Skip initial whitespace in the manifold name (which will
        // always be present, since the previous in >> sig
        // does not eat the separating whitespace).
        pos = name.c_str();
        while (*pos && std::isspace(*pos))
            ++pos;
        if (! *pos) {
            std::cerr << "ERROR: Signature " << sig
                << " has an empty name.\n\n";
            DB_CLOSE(db);
            usage(argv[0]);
        }

#ifdef QDBM_AS_TOKYOCABINET
        if (! vlput(db, sig.c_str(), sig.length(),
                pos, -1 /* strlen */, VL_DDUP)) {
#else
        if (! tcbdbputdup2(db, sig.c_str(), pos)) {
#endif
            std::cerr << "ERROR: Could not store the record for "
                << sig << " in the database." << std::endl;
            DB_CLOSE(db);
            std::exit(1);
        }
        ++tot;
    }

    // Close and tidy up.
#ifdef QDBM_AS_TOKYOCABINET
    if (! vloptimize(db)) {
        std::cerr << "ERROR: Could not optimise QDBM database: "
            << outputFile << std::endl;
        DB_CLOSE(db);
        std::exit(1);
    }

    if (! vlclose(db)) {
        std::cerr << "ERROR: Could not close QDBM database: "
            << outputFile << std::endl;
        std::exit(1);
    }
#else
    // The following call to tcbdboptimise() does not change any options
    // other than the bitwise compression option given in the final argument.
    if (! tcbdboptimize(db, 0, 0, 0, -1, -1, BDBTBZIP)) {
        std::cerr << "ERROR: Could not optimise Tokyo Cabinet database: "
            << outputFile << std::endl;
        std::cerr << "Tokyo cabinet error: " << tcerrmsg(tcbdbecode(db))
            << std::endl;
        DB_CLOSE(db);
        std::exit(1);
    }

    if (! tcbdbclose(db)) {
        std::cerr << "ERROR: Could not close Tokyo Cabinet database: "
            << outputFile << std::endl;
        tcbdbdel(db);
        std::exit(1);
    }
    tcbdbdel(db);
#endif

    std::cout << "Success: " << tot << " records." << std::endl;
    return 0;
}