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

📄 tchdb.h

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 H
📖 第 1 页 / 共 3 页
字号:
/************************************************************************************************* * The hash database API of Tokyo Cabinet *                                                      Copyright (C) 2006-2009 Mikio Hirabayashi * This file is part of Tokyo Cabinet. * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of * the GNU Lesser General Public License as published by the Free Software Foundation; either * version 2.1 of the License or any later version.  Tokyo Cabinet is distributed in the hope * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public * License for more details. * You should have received a copy of the GNU Lesser General Public License along with Tokyo * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA. *************************************************************************************************/#ifndef _TCHDB_H                         /* duplication check */#define _TCHDB_H#if defined(__cplusplus)#define __TCHDB_CLINKAGEBEGIN extern "C" {#define __TCHDB_CLINKAGEEND }#else#define __TCHDB_CLINKAGEBEGIN#define __TCHDB_CLINKAGEEND#endif__TCHDB_CLINKAGEBEGIN#include <stdlib.h>#include <stdbool.h>#include <stdint.h>#include <time.h>#include <limits.h>#include <math.h>#include <tcutil.h>/************************************************************************************************* * API *************************************************************************************************/typedef struct {                         /* type of structure for a hash database */  void *mmtx;                            /* mutex for method */  void *rmtxs;                           /* mutexes for records */  void *dmtx;                            /* mutex for the while database */  void *tmtx;                            /* mutex for transaction */  void *wmtx;                            /* mutex for write ahead logging */  void *eckey;                           /* key for thread specific error code */  uint8_t type;                          /* database type */  uint8_t flags;                         /* additional flags */  uint64_t bnum;                         /* number of the bucket array */  uint8_t apow;                          /* power of record alignment */  uint8_t fpow;                          /* power of free block pool number */  uint8_t opts;                          /* options */  char *path;                            /* path of the database file */  int fd;                                /* file descriptor of the database file */  uint32_t omode;                        /* open mode */  uint64_t rnum;                         /* number of the records */  uint64_t fsiz;                         /* size of the database file */  uint64_t frec;                         /* offset of the first record */  uint64_t lrec;                         /* offset of the last record */  uint64_t iter;                         /* offset of the iterator */  char *map;                             /* pointer to the mapped memory */  uint64_t msiz;                         /* size of the mapped memory */  uint64_t xmsiz;                        /* size of the extra mapped memory */  uint64_t xfsiz;                        /* extra size of the file for mapped memory */  uint32_t *ba32;                        /* 32-bit bucket array */  uint64_t *ba64;                        /* 64-bit bucket array */  uint32_t align;                        /* record alignment */  uint32_t runit;                        /* record reading unit */  bool zmode;                            /* whether compression is used */  int32_t fbpmax;                        /* maximum number of the free block pool */  void *fbpool;                          /* free block pool */  int32_t fbpnum;                        /* number of the free block pool */  int32_t fbpmis;                        /* number of missing retrieval of the free block pool */  bool async;                            /* whether asynchronous storing is called */  TCXSTR *drpool;                        /* delayed record pool */  TCXSTR *drpdef;                        /* deferred records of the delayed record pool */  uint64_t drpoff;                       /* offset of the delayed record pool */  TCMDB *recc;                           /* cache for records */  uint32_t rcnum;                        /* maximum number of cached records */  TCCODEC enc;                           /* pointer to the encoding function */  void *encop;                           /* opaque object for the encoding functions */  TCCODEC dec;                           /* pointer to the decoding function */  void *decop;                           /* opaque object for the decoding functions */  int ecode;                             /* last happened error code */  bool fatal;                            /* whether a fatal error occured */  uint64_t inode;                        /* inode number */  time_t mtime;                          /* modification time */  bool tran;                             /* whether in the transaction */  int walfd;                             /* file descriptor of write ahead logging */  uint64_t walend;                       /* end offset of write ahead logging */  int dbgfd;                             /* file descriptor for debugging */  int64_t cnt_writerec;                  /* tesing counter for record write times */  int64_t cnt_reuserec;                  /* tesing counter for record reuse times */  int64_t cnt_moverec;                   /* tesing counter for record move times */  int64_t cnt_readrec;                   /* tesing counter for record read times */  int64_t cnt_searchfbp;                 /* tesing counter for FBP search times */  int64_t cnt_insertfbp;                 /* tesing counter for FBP insert times */  int64_t cnt_splicefbp;                 /* tesing counter for FBP splice times */  int64_t cnt_dividefbp;                 /* tesing counter for FBP divide times */  int64_t cnt_mergefbp;                  /* tesing counter for FBP merge times */  int64_t cnt_reducefbp;                 /* tesing counter for FBP reduce times */  int64_t cnt_appenddrp;                 /* tesing counter for DRP append times */  int64_t cnt_deferdrp;                  /* tesing counter for DRP defer times */  int64_t cnt_flushdrp;                  /* tesing counter for DRP flush times */  int64_t cnt_adjrecc;                   /* tesing counter for record cache adjust times */} TCHDB;enum {                                   /* enumeration for additional flags */  HDBFOPEN = 1 << 0,                     /* whether opened */  HDBFFATAL = 1 << 1                     /* whetehr with fatal error */};enum {                                   /* enumeration for tuning options */  HDBTLARGE = 1 << 0,                    /* use 64-bit bucket array */  HDBTDEFLATE = 1 << 1,                  /* compress each record with Deflate */  HDBTBZIP = 1 << 2,                     /* compress each record with BZIP2 */  HDBTTCBS = 1 << 3,                     /* compress each record with TCBS */  HDBTEXCODEC = 1 << 4                   /* compress each record with custom functions */};enum {                                   /* enumeration for open modes */  HDBOREADER = 1 << 0,                   /* open as a reader */  HDBOWRITER = 1 << 1,                   /* open as a writer */  HDBOCREAT = 1 << 2,                    /* writer creating */  HDBOTRUNC = 1 << 3,                    /* writer truncating */  HDBONOLCK = 1 << 4,                    /* open without locking */  HDBOLCKNB = 1 << 5,                    /* lock without blocking */  HDBOTSYNC = 1 << 6                     /* synchronize every transaction */};/* Get the message string corresponding to an error code.   `ecode' specifies the error code.   The return value is the message string of the error code. */const char *tchdberrmsg(int ecode);/* Create a hash database object.   The return value is the new hash database object. */TCHDB *tchdbnew(void);/* Delete a hash database object.   `hdb' specifies the hash database object.   If the database is not closed, it is closed implicitly.  Note that the deleted object and its   derivatives can not be used anymore. */void tchdbdel(TCHDB *hdb);/* Get the last happened error code of a hash database object.   `hdb' specifies the hash database object.   The return value is the last happened error code.   The following error codes are defined: `TCESUCCESS' for success, `TCETHREAD' for threading   error, `TCEINVALID' for invalid operation, `TCENOFILE' for file not found, `TCENOPERM' for no   permission, `TCEMETA' for invalid meta data, `TCERHEAD' for invalid record header, `TCEOPEN'   for open error, `TCECLOSE' for close error, `TCETRUNC' for trunc error, `TCESYNC' for sync   error, `TCESTAT' for stat error, `TCESEEK' for seek error, `TCEREAD' for read error,   `TCEWRITE' for write error, `TCEMMAP' for mmap error, `TCELOCK' for lock error, `TCEUNLINK'   for unlink error, `TCERENAME' for rename error, `TCEMKDIR' for mkdir error, `TCERMDIR' for   rmdir error, `TCEKEEP' for existing record, `TCENOREC' for no record found, and `TCEMISC' for   miscellaneous error. */int tchdbecode(TCHDB *hdb);/* Set mutual exclusion control of a hash database object for threading.   `hdb' specifies the hash database object which is not opened.   If successful, the return value is true, else, it is false.   Note that the mutual exclusion control is needed if the object is shared by plural threads and   this function should should be called before the database is opened. */bool tchdbsetmutex(TCHDB *hdb);/* Set the tuning parameters of a hash database object.   `hdb' specifies the hash database object which is not opened.   `bnum' specifies the number of elements of the bucket array.  If it is not more than 0, the   default value is specified.  The default value is 131071.  Suggested size of the bucket array   is about from 0.5 to 4 times of the number of all records to be stored.   `apow' specifies the size of record alignment by power of 2.  If it is negative, the default   value is specified.  The default value is 4 standing for 2^4=16.   `fpow' specifies the maximum number of elements of the free block pool by power of 2.  If it   is negative, the default value is specified.  The default value is 10 standing for 2^10=1024.   `opts' specifies options by bitwise-or: `HDBTLARGE' specifies that the size of the database   can be larger than 2GB by using 64-bit bucket array, `HDBTDEFLATE' specifies that each record   is compressed with Deflate encoding, `HDBTBZIP' specifies that each record is compressed with   BZIP2 encoding, `HDBTTCBS' specifies that each record is compressed with TCBS encoding.   If successful, the return value is true, else, it is false.   Note that the tuning parameters should be set before the database is opened. */bool tchdbtune(TCHDB *hdb, int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts);/* Set the caching parameters of a hash database object.   `hdb' specifies the hash database object which is not opened.   `rcnum' specifies the maximum number of records to be cached.  If it is not more than 0, the   record cache is disabled.  It is disabled by default.   If successful, the return value is true, else, it is false.   Note that the caching parameters should be set before the database is opened. */bool tchdbsetcache(TCHDB *hdb, int32_t rcnum);/* Set the size of the extra mapped memory of a hash database object.   `hdb' specifies the hash database object which is not opened.   `xmsiz' specifies the size of the extra mapped memory.  If it is not more than 0, the extra   mapped memory is disabled.  The default size is 67108864.   If successful, the return value is true, else, it is false.   Note that the mapping parameters should be set before the database is opened. */bool tchdbsetxmsiz(TCHDB *hdb, int64_t xmsiz);/* Open a database file and connect a hash database object.   `hdb' specifies the hash database object which is not opened.   `path' specifies the path of the database file.   `omode' specifies the connection mode: `HDBOWRITER' as a writer, `HDBOREADER' as a reader.   If the mode is `HDBOWRITER', the following may be added by bitwise-or: `HDBOCREAT', which   means it creates a new database if not exist, `HDBOTRUNC', which means it creates a new   database regardless if one exists, `HDBOTSYNC', which means every transaction synchronizes   updated contents with the device.  Both of `HDBOREADER' and `HDBOWRITER' can be added to by   bitwise-or: `HDBONOLCK', which means it opens the database file without file locking, or   `HDBOLCKNB', which means locking is performed without blocking.   If successful, the return value is true, else, it is false. */bool tchdbopen(TCHDB *hdb, const char *path, int omode);/* Close a hash database object.   `hdb' specifies the hash database object.   If successful, the return value is true, else, it is false.   Update of a database is assured to be written when the database is closed.  If a writer opens   a database but does not close it appropriately, the database will be broken. */bool tchdbclose(TCHDB *hdb);/* Store a record into a hash database object.   `hdb' specifies the hash database object connected as a writer.   `kbuf' specifies the pointer to the region of the key.   `ksiz' specifies the size of the region of the key.   `vbuf' specifies the pointer to the region of the value.   `vsiz' specifies the size of the region of the value.   If successful, the return value is true, else, it is false.   If a record with the same key exists in the database, it is overwritten. */bool tchdbput(TCHDB *hdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);/* Store a string record into a hash database object.   `hdb' specifies the hash database object connected as a writer.   `kstr' specifies the string of the key.   `vstr' specifies the string of the value.   If successful, the return value is true, else, it is false.   If a record with the same key exists in the database, it is overwritten. */bool tchdbput2(TCHDB *hdb, const char *kstr, const char *vstr);/* Store a new record into a hash database object.   `hdb' specifies the hash database object connected as a writer.   `kbuf' specifies the pointer to the region of the key.   `ksiz' specifies the size of the region of the key.   `vbuf' specifies the pointer to the region of the value.   `vsiz' specifies the size of the region of the value.   If successful, the return value is true, else, it is false.   If a record with the same key exists in the database, this function has no effect. */bool tchdbputkeep(TCHDB *hdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);/* Store a new string record into a hash database object.   `hdb' specifies the hash database object connected as a writer.   `kstr' specifies the string of the key.   `vstr' specifies the string of the value.   If successful, the return value is true, else, it is false.   If a record with the same key exists in the database, this function has no effect. */bool tchdbputkeep2(TCHDB *hdb, const char *kstr, const char *vstr);/* Concatenate a value at the end of the existing record in a hash database object.

⌨️ 快捷键说明

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