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

📄 tcbdb.c

📁 高性能嵌入式数据库在高并发的环境下使用最好是64位系统比较好
💻 C
📖 第 1 页 / 共 5 页
字号:
  assert(bdb);  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    return false;  }  bool err = false;  if(TCMAPRNUM(bdb->leafc) > 0){    bool clk = BDBLOCKCACHE(bdb);    TCMAP *leafc = bdb->leafc;    tcmapiterinit(leafc);    int rsiz;    const void *buf;    while((buf = tcmapiternext(leafc, &rsiz)) != NULL){      if(!tcbdbleafcacheout(bdb, (BDBLEAF *)tcmapiterval(buf, &rsiz))) err = true;    }    if(clk) BDBUNLOCKCACHE(bdb);  }  if(TCMAPRNUM(bdb->nodec) > 0){    bool clk = BDBLOCKCACHE(bdb);    TCMAP *nodec = bdb->nodec;    tcmapiterinit(nodec);    int rsiz;    const void *buf;    while((buf = tcmapiternext(nodec, &rsiz)) != NULL){      if(!tcbdbnodecacheout(bdb, (BDBNODE *)tcmapiterval(buf, &rsiz))) err = true;    }    if(clk) BDBUNLOCKCACHE(bdb);  }  return !err;}/* Get the comparison function of a B+ tree database object. */BDBCMP tcbdbcmpfunc(TCBDB *bdb){  assert(bdb);  return bdb->cmp;}/* Get the opaque object for the comparison function of a B+ tree database object. */void *tcbdbcmpop(TCBDB *bdb){  assert(bdb);  return bdb->cmpop;}/* Get the maximum number of cached leaf nodes of a B+ tree database object. */uint32_t tcbdblmemb(TCBDB *bdb){  assert(bdb);  return bdb->lmemb;}/* Get the maximum number of cached non-leaf nodes of a B+ tree database object. */uint32_t tcbdbnmemb(TCBDB *bdb){  assert(bdb);  return bdb->nmemb;}/* Get the number of the leaf nodes of B+ tree database object. */uint64_t tcbdblnum(TCBDB *bdb){  assert(bdb);  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    return 0;  }  return bdb->lnum;}/* Get the number of the non-leaf nodes of B+ tree database object. */uint64_t tcbdbnnum(TCBDB *bdb){  assert(bdb);  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    return 0;  }  return bdb->nnum;}/* Get the number of elements of the bucket array of a B+ tree database object. */uint64_t tcbdbbnum(TCBDB *bdb){  assert(bdb);  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    return 0;  }  return tchdbbnum(bdb->hdb);}/* Get the record alignment of a B+ tree database object. */uint32_t tcbdbalign(TCBDB *bdb){  assert(bdb);  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    return 0;  }  return tchdbalign(bdb->hdb);}/* Get the maximum number of the free block pool of a B+ tree database object. */uint32_t tcbdbfbpmax(TCBDB *bdb){  assert(bdb);  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    return 0;  }  return tchdbfbpmax(bdb->hdb);}/* Get the inode number of the database file of a B+ tree database object. */uint64_t tcbdbinode(TCBDB *bdb){  assert(bdb);  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    return 0;  }  return tchdbinode(bdb->hdb);}/* Get the modification time of the database file of a B+ tree database object. */time_t tcbdbmtime(TCBDB *bdb){  assert(bdb);  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    return 0;  }  return tchdbmtime(bdb->hdb);}/* Get the additional flags of a B+ tree database object. */uint8_t tcbdbflags(TCBDB *bdb){  assert(bdb);  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    return 0;  }  return tchdbflags(bdb->hdb);}/* Get the options of a B+ tree database object. */uint8_t tcbdbopts(TCBDB *bdb){  assert(bdb);  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    return 0;  }  return bdb->opts;}/* Get the pointer to the opaque field of a B+ tree database object. */char *tcbdbopaque(TCBDB *bdb){  assert(bdb);  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    return 0;  }  return tchdbopaque(bdb->hdb) + BDBOPAQUESIZ;}/* Get the number of used elements of the bucket array of a B+ tree database object. */uint64_t tcbdbbnumused(TCBDB *bdb){  assert(bdb);  if(!bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    return 0;  }  return tchdbbnumused(bdb->hdb);}/* Set the maximum size of each leaf node. */bool tcbdbsetlsmax(TCBDB *bdb, uint32_t lsmax){  assert(bdb);  if(bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    return false;  }  bdb->lsmax = lsmax;  return true;}/* Set the capacity number of records. */bool tcbdbsetcapnum(TCBDB *bdb, uint64_t capnum){  assert(bdb);  if(bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    return false;  }  bdb->capnum = capnum;  return true;}/* Set the custom codec functions of a B+ tree database object. */bool tcbdbsetcodecfunc(TCBDB *bdb, BDBCODEC enc, void *encop, BDBCODEC dec, void *decop){  assert(bdb && enc && dec);  if(!BDBLOCKMETHOD(bdb, true)) return false;  if(bdb->open){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  bool rv = tchdbsetcodecfunc(bdb->hdb, enc, encop, dec, decop);  BDBUNLOCKMETHOD(bdb);  return rv;}/* Store a new record into a B+ tree database object with backward duplication. */bool tcbdbputdupback(TCBDB *bdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz){  assert(bdb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);  if(!BDBLOCKMETHOD(bdb, true)) return false;  if(!bdb->open || !bdb->wmode){    tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);    BDBUNLOCKMETHOD(bdb);    return false;  }  bool rv = tcbdbputimpl(bdb, kbuf, ksiz, vbuf, vsiz, BDBPDDUPB);  BDBUNLOCKMETHOD(bdb);  return rv;}/* Store a new string record into a B+ tree database object with backward duplication. */bool tcbdbputdupback2(TCBDB *bdb, const char *kstr, const char *vstr){  assert(bdb && kstr && vstr);  return tcbdbputdupback(bdb, kstr, strlen(kstr), vstr, strlen(vstr));}/* Move a cursor object to the rear of records corresponding a key. */bool tcbdbcurjumpback(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, false);  BDBUNLOCKMETHOD(bdb);  return rv;}/* Move a cursor object to the rear of records corresponding a key string. */bool tcbdbcurjumpback2(BDBCUR *cur, const char *kstr){  assert(cur && kstr);  return tcbdbcurjumpback(cur, kstr, strlen(kstr));}/* Compare keys of two records by lexical order. */int tcbdbcmplexical(const char *aptr, int asiz, const char *bptr, int bsiz, void *op){  assert(aptr && asiz >= 0 && bptr && bsiz >= 0);  int rv;  TCCMPLEXICAL(rv, aptr, asiz, bptr, bsiz);  return rv;}/* Compare two keys as decimal strings of real numbers. */int tcbdbcmpdecimal(const char *aptr, int asiz, const char *bptr, int bsiz, void *op){  assert(aptr && asiz >= 0 && bptr && bsiz >= 0);  int sign;  int64_t anum = 0;  sign = 1;  if(asiz > 0 && *aptr == '-'){    aptr++;    asiz--;    sign = -1;  }  for(int i = 0; i < asiz; i++){    int c = aptr[i];    if(c < '0' || c > '9') continue;    anum = anum * 10 + c - '0';  }  anum *= sign;  int64_t bnum = 0;  sign = 1;  if(bsiz > 0 && *bptr == '-'){    bptr++;    bsiz--;    sign = -1;  }  for(int i = 0; i < bsiz; i++){    int c = bptr[i];    if(c < '0' || c > '9') continue;    bnum = bnum * 10 + c - '0';  }  bnum *= sign;  return (anum < bnum) ? -1 : anum > bnum;}/* Compare two keys as 32-bit integers in the native byte order. */int tcbdbcmpint32(const char *aptr, int asiz, const char *bptr, int bsiz, void *op){  assert(aptr && bptr);  int32_t anum, bnum;  if(asiz == sizeof(int32_t)){    memcpy(&anum, aptr, sizeof(int32_t));  } else if(asiz < sizeof(int32_t)){    memset(&anum, 0, sizeof(int32_t));    memcpy(&anum, aptr, asiz);  } else {    memcpy(&anum, aptr, sizeof(int32_t));  }  if(bsiz == sizeof(int32_t)){    memcpy(&bnum, bptr, sizeof(int32_t));  } else if(bsiz < sizeof(int32_t)){    memset(&bnum, 0, sizeof(int32_t));    memcpy(&bnum, bptr, bsiz);  } else {    memcpy(&bnum, bptr, sizeof(int32_t));  }  return (anum < bnum) ? -1 : anum > bnum;}/* Compare two keys as 64-bit integers in the native byte order. */int tcbdbcmpint64(const char *aptr, int asiz, const char *bptr, int bsiz, void *op){  assert(aptr && bptr);  int64_t anum, bnum;  if(asiz == sizeof(int64_t)){    memcpy(&anum, aptr, sizeof(int64_t));  } else if(asiz < sizeof(int64_t)){    memset(&anum, 0, sizeof(int64_t));    memcpy(&anum, aptr, asiz);  } else {    memcpy(&anum, aptr, sizeof(int64_t));  }  if(bsiz == sizeof(int64_t)){    memcpy(&bnum, bptr, sizeof(int64_t));  } else if(bsiz < sizeof(int64_t)){    memset(&bnum, 0, sizeof(int64_t));    memcpy(&bnum, bptr, bsiz);  } else {    memcpy(&bnum, bptr, sizeof(int64_t));  }  return (anum < bnum) ? -1 : anum > bnum;}/************************************************************************************************* * private features *************************************************************************************************//* Clear all members.   `bdb' specifies the B+ tree database object. */static void tcbdbclear(TCBDB *bdb){  assert(bdb);  bdb->mmtx = NULL;  bdb->cmtx = NULL;  bdb->tmtx = NULL;  bdb->hdb = NULL;  bdb->opaque = NULL;  bdb->open = false;  bdb->wmode = false;  bdb->lmemb = BDBDEFLMEMB;  bdb->nmemb = BDBDEFNMEMB;  bdb->opts = 0;  bdb->root = 0;  bdb->first = 0;  bdb->last = 0;  bdb->lnum = 0;  bdb->nnum = 0;  bdb->rnum = 0;  bdb->leafc = NULL;  bdb->nodec = NULL;  bdb->cmp = NULL;  bdb->cmpop = NULL;  bdb->lcnum = BDBDEFLCNUM;  bdb->ncnum = BDBDEFNCNUM;  bdb->lsmax = 0;  bdb->lschk = 0;  bdb->capnum = 0;  bdb->hist = NULL;  bdb->hnum = 0;  bdb->hleaf = 0;  bdb->lleaf = 0;  bdb->tran = false;  bdb->rbopaque = NULL;  bdb->cnt_saveleaf = -1;  bdb->cnt_loadleaf = -1;  bdb->cnt_killleaf = -1;  bdb->cnt_adjleafc = -1;  bdb->cnt_savenode = -1;  bdb->cnt_loadnode = -1;  bdb->cnt_adjnodec = -1;  TCDODEBUG(bdb->cnt_saveleaf = 0);  TCDODEBUG(bdb->cnt_loadleaf = 0);  TCDODEBUG(bdb->cnt_killleaf = 0);  TCDODEBUG(bdb->cnt_adjleafc = 0);  TCDODEBUG(bdb->cnt_savenode = 0);  TCDODEBUG(bdb->cnt_loadnode = 0);  TCDODEBUG(bdb->cnt_adjnodec = 0);}/* Serialize meta data into the opaque field.   `bdb' specifies the B+ tree database object. */static void tcdumpmeta(TCBDB *bdb){  assert(bdb);  memset(bdb->opaque, 0, 64);  char *wp = bdb->opaque;  if(bdb->cmp == tcbdbcmplexical){    *(uint8_t *)(wp++) = 0x0;  } else if(bdb->cmp == tcbdbcmpdecimal){    *(uint8_t *)(wp++) = 0x1;  } else if(bdb->cmp == tcbdbcmpint32){    *(uint8_t *)(wp++) = 0x2;  } else if(bdb->cmp == tcbdbcmpint64){    *(uint8_t *)(wp++) = 0x3;  } else {    *(uint8_t *)(wp++) = 0xff;  }  wp += 7;  uint32_t lnum;  lnum = bdb->lmemb;  lnum = TCHTOIL(lnum);  memcpy(wp, &lnum, sizeof(lnum));  wp += sizeof(lnum);  lnum = bdb->nmemb;  lnum = TCHTOIL(lnum);  memcpy(wp, &lnum, sizeof(lnum));  wp += sizeof(lnum);  uint64_t llnum;

⌨️ 快捷键说明

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