⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcbdb.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 5 页
字号:
  if(!tcbdbcacheadjust(bdb)) err = true;  if(!tchdbtranvoid(bdb->hdb)) err = true;  BDBUNLOCKMETHOD(bdb);  return !err;}/* Get the file path of a B+ tree database object. */const char *tcbdbpath(TCBDB *bdb){  assert(bdb);  if(!BDBLOCKMETHOD(bdb, false)) return NULL;  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return NULL;  }  const char *rv = tchdbpath(bdb->hdb);  BDBUNLOCKMETHOD(bdb);  return rv;}/* Get the number of records of a B+ tree database object. */uint64_t tcbdbrnum(TCBDB *bdb){  assert(bdb);  if(!BDBLOCKMETHOD(bdb, false)) return 0;  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return 0;  }  uint64_t rv = bdb->rnum;  BDBUNLOCKMETHOD(bdb);  return rv;}/* Get the size of the database file of a B+ tree database object. */uint64_t tcbdbfsiz(TCBDB *bdb){  assert(bdb);  if(!BDBLOCKMETHOD(bdb, false)) return 0;  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return 0;  }  uint64_t rv = tchdbfsiz(bdb->hdb);  BDBUNLOCKMETHOD(bdb);  return rv;}/* Create a cursor object. */BDBCUR *tcbdbcurnew(TCBDB *bdb){  assert(bdb);  BDBCUR *cur;  TCMALLOC(cur, sizeof(*cur));  cur->bdb = bdb;  cur->clock = 0;  cur->id = 0;  cur->kidx = 0;  cur->vidx = 0;  return cur;}/* Delete a cursor object. */void tcbdbcurdel(BDBCUR *cur){  assert(cur);  TCFREE(cur);}/* Move a cursor object to the first record. */bool tcbdbcurfirst(BDBCUR *cur){  assert(cur);  TCBDB *bdb = cur->bdb;  if(!BDBLOCKMETHOD(bdb, false)) return false;  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  bool rv = tcbdbcurfirstimpl(cur);  bool adj = TCMAPRNUM(bdb->leafc) > bdb->lcnum || TCMAPRNUM(bdb->nodec) > bdb->ncnum;  BDBUNLOCKMETHOD(bdb);  if(adj && BDBLOCKMETHOD(bdb, true)){    if(!bdb->tran && !tcbdbcacheadjust(bdb)) rv = false;    BDBUNLOCKMETHOD(bdb);  }  return rv;}/* Move a cursor object to the last record. */bool tcbdbcurlast(BDBCUR *cur){  assert(cur);  TCBDB *bdb = cur->bdb;  if(!BDBLOCKMETHOD(bdb, false)) return false;  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  bool rv = tcbdbcurlastimpl(cur);  bool adj = TCMAPRNUM(bdb->leafc) > bdb->lcnum || TCMAPRNUM(bdb->nodec) > bdb->ncnum;  BDBUNLOCKMETHOD(bdb);  if(adj && BDBLOCKMETHOD(bdb, true)){    if(!bdb->tran && !tcbdbcacheadjust(bdb)) rv = false;    BDBUNLOCKMETHOD(bdb);  }  return rv;}/* Move a cursor object to the front of records corresponding a key. */bool tcbdbcurjump(BDBCUR *cur, const void *kbuf, int ksiz){  assert(cur && kbuf && ksiz >= 0);  TCBDB *bdb = cur->bdb;  if(!BDBLOCKMETHOD(bdb, false)) return false;  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  bool rv = tcbdbcurjumpimpl(cur, kbuf, ksiz, true);  bool adj = TCMAPRNUM(bdb->leafc) > bdb->lcnum || TCMAPRNUM(bdb->nodec) > bdb->ncnum;  BDBUNLOCKMETHOD(bdb);  if(adj && BDBLOCKMETHOD(bdb, true)){    if(!bdb->tran && !tcbdbcacheadjust(bdb)) rv = false;    BDBUNLOCKMETHOD(bdb);  }  return rv;}/* Move a cursor object to the front of records corresponding a key string. */bool tcbdbcurjump2(BDBCUR *cur, const char *kstr){  assert(cur && kstr);  return tcbdbcurjump(cur, kstr, strlen(kstr));}/* Move a cursor object to the previous record. */bool tcbdbcurprev(BDBCUR *cur){  assert(cur);  TCBDB *bdb = cur->bdb;  if(!BDBLOCKMETHOD(bdb, false)) return false;  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  if(cur->id < 1){    tcbdbsetecode(bdb, TCENOREC, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  bool rv = tcbdbcurprevimpl(cur);  bool adj = TCMAPRNUM(bdb->leafc) > bdb->lcnum || TCMAPRNUM(bdb->nodec) > bdb->ncnum;  BDBUNLOCKMETHOD(bdb);  if(adj && BDBLOCKMETHOD(bdb, true)){    if(!bdb->tran && !tcbdbcacheadjust(bdb)) rv = false;    BDBUNLOCKMETHOD(bdb);  }  return rv;}/* Move a cursor object to the next record. */bool tcbdbcurnext(BDBCUR *cur){  assert(cur);  TCBDB *bdb = cur->bdb;  if(!BDBLOCKMETHOD(bdb, false)) return false;  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  if(cur->id < 1){    tcbdbsetecode(bdb, TCENOREC, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  bool rv = tcbdbcurnextimpl(cur);  bool adj = TCMAPRNUM(bdb->leafc) > bdb->lcnum || TCMAPRNUM(bdb->nodec) > bdb->ncnum;  BDBUNLOCKMETHOD(bdb);  if(adj && BDBLOCKMETHOD(bdb, true)){    if(!bdb->tran && !tcbdbcacheadjust(bdb)) rv = false;    BDBUNLOCKMETHOD(bdb);  }  return rv;}/* Insert a record around a cursor object. */bool tcbdbcurput(BDBCUR *cur, const void *vbuf, int vsiz, int cpmode){  assert(cur && vbuf && vsiz >= 0);  TCBDB *bdb = cur->bdb;  if(!BDBLOCKMETHOD(bdb, true)) return false;  if(!bdb->open || !bdb->wmode){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  if(cur->id < 1){    tcbdbsetecode(bdb, TCENOREC, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  bool rv = tcbdbcurputimpl(cur, vbuf, vsiz, cpmode);  BDBUNLOCKMETHOD(bdb);  return rv;}/* Insert a string record around a cursor object. */bool tcbdbcurput2(BDBCUR *cur, const char *vstr, int cpmode){  assert(cur && vstr);  return tcbdbcurput(cur, vstr, strlen(vstr), cpmode);}/* Delete the record where a cursor object is. */bool tcbdbcurout(BDBCUR *cur){  assert(cur);  TCBDB *bdb = cur->bdb;  if(!BDBLOCKMETHOD(bdb, true)) return false;  if(!bdb->open || !bdb->wmode){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  if(cur->id < 1){    tcbdbsetecode(bdb, TCENOREC, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  bool rv = tcbdbcuroutimpl(cur);  BDBUNLOCKMETHOD(bdb);  return rv;}/* Get the key of the record where the cursor object is. */void *tcbdbcurkey(BDBCUR *cur, int *sp){  assert(cur && sp);  TCBDB *bdb = cur->bdb;  if(!BDBLOCKMETHOD(bdb, false)) return false;  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  if(cur->id < 1){    tcbdbsetecode(bdb, TCENOREC, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  const char *kbuf, *vbuf;  int ksiz, vsiz;  char *rv;  if(tcbdbcurrecimpl(cur, &kbuf, &ksiz, &vbuf, &vsiz)){    TCMEMDUP(rv, kbuf, ksiz);    *sp = ksiz;  } else {    rv = NULL;  }  BDBUNLOCKMETHOD(bdb);  return rv;}/* Get the key string of the record where the cursor object is. */char *tcbdbcurkey2(BDBCUR *cur){  assert(cur);  int ksiz;  return tcbdbcurkey(cur, &ksiz);}/* Get the key of the record where the cursor object is, as a volatile buffer. */const void *tcbdbcurkey3(BDBCUR *cur, int *sp){  assert(cur && sp);  TCBDB *bdb = cur->bdb;  if(!BDBLOCKMETHOD(bdb, false)) return false;  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  if(cur->id < 1){    tcbdbsetecode(bdb, TCENOREC, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  const char *kbuf, *vbuf;  int ksiz, vsiz;  const char *rv;  if(tcbdbcurrecimpl(cur, &kbuf, &ksiz, &vbuf, &vsiz)){    rv = kbuf;    *sp = ksiz;  } else {    rv = NULL;  }  BDBUNLOCKMETHOD(bdb);  return rv;}/* Get the value of the record where the cursor object is. */void *tcbdbcurval(BDBCUR *cur, int *sp){  assert(cur && sp);  TCBDB *bdb = cur->bdb;  if(!BDBLOCKMETHOD(bdb, false)) return false;  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  if(cur->id < 1){    tcbdbsetecode(bdb, TCENOREC, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  const char *kbuf, *vbuf;  int ksiz, vsiz;  char *rv;  if(tcbdbcurrecimpl(cur, &kbuf, &ksiz, &vbuf, &vsiz)){    TCMEMDUP(rv, vbuf, vsiz);    *sp = vsiz;  } else {    rv = NULL;  }  BDBUNLOCKMETHOD(bdb);  return rv;}/* Get the value string of the record where the cursor object is. */char *tcbdbcurval2(BDBCUR *cur){  assert(cur);  int vsiz;  return tcbdbcurval(cur, &vsiz);}/* Get the value of the record where the cursor object is, as a volatile buffer. */const void *tcbdbcurval3(BDBCUR *cur, int *sp){  assert(cur && sp);  TCBDB *bdb = cur->bdb;  if(!BDBLOCKMETHOD(bdb, false)) return false;  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  if(cur->id < 1){    tcbdbsetecode(bdb, TCENOREC, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  const char *kbuf, *vbuf;  int ksiz, vsiz;  const char *rv;  if(tcbdbcurrecimpl(cur, &kbuf, &ksiz, &vbuf, &vsiz)){    rv = vbuf;    *sp = vsiz;  } else {    rv = NULL;  }  BDBUNLOCKMETHOD(bdb);  return rv;}/* Get the key and the value of the record where the cursor object is. */bool tcbdbcurrec(BDBCUR *cur, TCXSTR *kxstr, TCXSTR *vxstr){  assert(cur && kxstr && vxstr);  TCBDB *bdb = cur->bdb;  if(!BDBLOCKMETHOD(bdb, false)) return false;  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  if(cur->id < 1){    tcbdbsetecode(bdb, TCENOREC, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  const char *kbuf, *vbuf;  int ksiz, vsiz;  bool rv;  if(tcbdbcurrecimpl(cur, &kbuf, &ksiz, &vbuf, &vsiz)){    tcxstrclear(kxstr);    TCXSTRCAT(kxstr, kbuf, ksiz);    tcxstrclear(vxstr);    TCXSTRCAT(vxstr, vbuf, vsiz);    rv = true;  } else {    rv = false;  }  BDBUNLOCKMETHOD(bdb);  return rv;}/************************************************************************************************* * features for experts *************************************************************************************************//* Set the error code of a B+ tree database object. */void tcbdbsetecode(TCBDB *bdb, int ecode, const char *filename, int line, const char *func){  assert(bdb && filename && line >= 1 && func);  tchdbsetecode(bdb->hdb, ecode, filename, line, func);}/* Set the file descriptor for debugging output. */void tcbdbsetdbgfd(TCBDB *bdb, int fd){  assert(bdb && fd >= 0);  tchdbsetdbgfd(bdb->hdb, fd);}/* Get the file descriptor for debugging output. */int tcbdbdbgfd(TCBDB *bdb){  assert(bdb);  return tchdbdbgfd(bdb->hdb);}/* Check whether mutual exclusion control is set to a B+ tree database object. */bool tcbdbhasmutex(TCBDB *bdb){  assert(bdb);  return bdb->mmtx != NULL;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -