tokyo cabinet源代码分析2

来源:互联网 发布:李克勤护花使者 知乎 编辑:程序博客网 时间:2024/04/28 21:31

tokyo cabinet(以下简称TC)作为一个程序库,其中并没有main函数,那么我们该如何去开始代码阅读工作,这里的基本思路如下:

1. 首先明确TC中数据类型

2. 按照example的示例程序了解TC的运行过程。

TC中仅仅包含了8个头文件,各个头文件作用如下:

1. Md5.h 定义md5算法中需要使用的数据类型,和md5函数原型声明

2. Myconf.h 程序配置信息,通用头文件等

3. Tcadb.h 定义Abstract db类型,并声明相关操作函数

4. Tcbdb.h 定义B+ tree database类型,并声明相关操作函数

5. Tcfdb.h 定义fixed-length database类型,并声明相关操作函数

6. Tchdb.h 定义 Hash database类型,并声明相关操作函数

7. Tctdb.h 定义table database类型,并声明相关操作函数

8. Tcutil.h 定义tc中使用到的数据类型,例如TCLIST,TCXSTR等,并声明相关类型的操作函数。

1. 字符串TCXSTR

typedef struct {                         /* type of structure for an extensible string object */  char *ptr;                             /* pointer to the region */  int size;                              /* size of the region */  int asize;                             /* size of the allocated region */} TCXSTR;

2. List类型 TCLIST

// Data type TCLIST// arr1, arr2, arr3 .................. arrN// [start ... start + num - 1 ]typedef struct {                         /* type of structure for an array list */  TCLISTDATUM *array;                    /* array of data */  int anum;                              /* number of the elements of the array */  int start;                             /* start index of used elements */  int num;                               /* number of used elements */} TCLIST;

pointer list:

typedef struct {                         /* type of structure for a pointer list */  void **array;                          /* array of pointers */  int anum;                              /* number of the elements of the array */  int start;                             /* start index of used elements */  int num;                               /* number of used elements */} TCPTRLIST;

3. hashmap类型TCMAP

typedef struct _TCMAPREC {               /* type of structure for an element of a map */  int32_t ksiz;                          /* size of the region of the key */  int32_t vsiz;                          /* size of the region of the value */  struct _TCMAPREC *left;                /* pointer to the left child */  struct _TCMAPREC *right;               /* pointer to the right child */  struct _TCMAPREC *prev;                /* pointer to the previous element */  struct _TCMAPREC *next;                /* pointer to the next element */} TCMAPREC;typedef struct {                         /* type of structure for a map */  TCMAPREC **buckets;                    /* bucket array */  TCMAPREC *first;                       /* pointer to the first element */  TCMAPREC *last;                        /* pointer to the last element */  TCMAPREC *cur;                         /* pointer to the current element */  uint32_t bnum;                         /* number of buckets */  uint64_t rnum;                         /* number of records */  uint64_t msiz;                         /* total size of records */} TCMAP;
4.ordered tree

typedef struct _TCTREEREC {              /* type of structure for an element of a tree */  int32_t ksiz;                          /* size of the region of the key */  int32_t vsiz;                          /* size of the region of the value */  struct _TCTREEREC *left;               /* pointer to the left child */  struct _TCTREEREC *right;              /* pointer to the right child */} TCTREEREC;typedef struct {                         /* type of structure for a tree */  TCTREEREC *root;                       /* pointer to the root element */  TCTREEREC *cur;                        /* pointer to the current element */  uint64_t rnum;                         /* number of records */  uint64_t msiz;                         /* total size of records */  TCCMP cmp;                             /* pointer to the comparison function */  void *cmpop;                           /* opaque object for the comparison function */} TCTREE;

5. on-memory hash database

typedef struct {                         /* type of structure for a on-memory hash database */  void **mmtxs;                          /* mutexes for method */  void *imtx;                            /* mutex for iterator */  TCMAP **maps;                          /* internal map objects */  int iter;                              /* index of maps for the iterator */} TCMDB;

6.on-memory tree database

typedef struct {                         /* type of structure for a on-memory tree database */  void *mmtx;                            /* mutex for method */  TCTREE *tree;                          /* internal tree object */} TCNDB;

7. 内存池

typedef struct {                         /* type of an element of memory pool */  void *ptr;                             /* pointer */  void (*del)(void *);                   /* deleting function */} TCMPELEM;typedef struct {                         /* type of structure for a memory pool object */  void *mutex;                           /* mutex for operations */  TCMPELEM *elems;                       /* array of elements */  int anum;                              /* number of the elements of the array */  int num;                               /* number of used elements */} TCMPOOL;

8. 分布式一致哈希类型

typedef struct {                         /* type of structure for a consistent hashing node */  uint32_t seq;                          /* sequential number */  uint32_t hash;                         /* hash value */} TCCHIDXNODE;typedef struct {                         /* type of structure for a consistent hashing object */  TCCHIDXNODE *nodes;                    /* node array */  int nnum;                              /* number of the node array */} TCCHIDX;

9. database类型:abstract database

// Abstract database// TCADB = Tokyo Cabinet Abstract dbtypedef struct {                         /* type of structure for an abstract database */  int omode;                             /* open mode */  TCMDB *mdb;                            /* on-memory hash database object */  TCNDB *ndb;                            /* on-memory tree database object */  TCHDB *hdb;                            /* hash database object */  TCBDB *bdb;                            /* B+ tree database object */  TCFDB *fdb;                            /* fixed-length databae object */  TCTDB *tdb;                            /* table database object */  int64_t capnum;                        /* capacity number of records */  int64_t capsiz;                        /* capacity size of using memory */  uint32_t capcnt;                       /* count for capacity check */  BDBCUR *cur;                           /* cursor of B+ tree */  void *skel;                            /* skeleton database */} TCADB;

10. TCHDB

// TCHDB = Tokyo Cabinet Hash databasetypedef 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 *wmtx;                            /* mutex for write ahead logging */  void *eckey;                           /* key for thread specific error code */  char *rpath;                           /* real path for locking */  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 dfcur;                        /* offset of the cursor for defragmentation */  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 */  uint32_t dfunit;                       /* unit step number of auto defragmentation */  uint32_t dfcnt;                        /* counter of auto defragmentation */  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 */  volatile int64_t cnt_writerec;         /* tesing counter for record write times */  volatile int64_t cnt_reuserec;         /* tesing counter for record reuse times */  volatile int64_t cnt_moverec;          /* tesing counter for record move times */  volatile int64_t cnt_readrec;          /* tesing counter for record read times */  volatile int64_t cnt_searchfbp;        /* tesing counter for FBP search times */  volatile int64_t cnt_insertfbp;        /* tesing counter for FBP insert times */  volatile int64_t cnt_splicefbp;        /* tesing counter for FBP splice times */  volatile int64_t cnt_dividefbp;        /* tesing counter for FBP divide times */  volatile int64_t cnt_mergefbp;         /* tesing counter for FBP merge times */  volatile int64_t cnt_reducefbp;        /* tesing counter for FBP reduce times */  volatile int64_t cnt_appenddrp;        /* tesing counter for DRP append times */  volatile int64_t cnt_deferdrp;         /* tesing counter for DRP defer times */  volatile int64_t cnt_flushdrp;         /* tesing counter for DRP flush times */  volatile int64_t cnt_adjrecc;          /* tesing counter for record cache adjust times */  volatile int64_t cnt_defrag;           /* tesing counter for defragmentation times */  volatile int64_t cnt_shiftrec;         /* tesing counter for record shift times */  volatile int64_t cnt_trunc;            /* tesing counter for truncation times */} TCHDB;

11. B+ tree database

// B+ tree database// TCBDB = Tokyo Cabinet B+ databasetypedef struct {                         /* type of structure for a B+ tree database */  void *mmtx;                            /* mutex for method */  void *cmtx;                            /* mutex for cache */  TCHDB *hdb;                            /* internal database object */  char *opaque;                          /* opaque buffer */  bool open;                             /* whether the internal database is opened */  bool wmode;                            /* whether to be writable */  uint32_t lmemb;                        /* number of members in each leaf */  uint32_t nmemb;                        /* number of members in each node */  uint8_t opts;                          /* options */  uint64_t root;                         /* ID number of the root page */  uint64_t first;                        /* ID number of the first leaf */  uint64_t last;                         /* ID number of the last leaf */  uint64_t lnum;                         /* number of leaves */  uint64_t nnum;                         /* number of nodes */  uint64_t rnum;                         /* number of records */  TCMAP *leafc;                          /* cache for leaves */  TCMAP *nodec;                          /* cache for nodes */  TCCMP cmp;                             /* pointer to the comparison function */  void *cmpop;                           /* opaque object for the comparison function */  uint32_t lcnum;                        /* maximum number of cached leaves */  uint32_t ncnum;                        /* maximum number of cached nodes */  uint32_t lsmax;                        /* maximum size of each leaf */  uint32_t lschk;                        /* counter for leaf size checking */  uint64_t capnum;                       /* capacity number of records */  uint64_t *hist;                        /* history array of visited nodes */  int hnum;                              /* number of element of the history array */  volatile uint64_t hleaf;               /* ID number of the leaf referred by the history */  volatile uint64_t lleaf;               /* ID number of the last visited leaf */  bool tran;                             /* whether in the transaction */  char *rbopaque;                        /* opaque for rollback */  volatile uint64_t clock;               /* logical clock */  volatile int64_t cnt_saveleaf;         /* tesing counter for leaf save times */  volatile int64_t cnt_loadleaf;         /* tesing counter for leaf load times */  volatile int64_t cnt_killleaf;         /* tesing counter for leaf kill times */  volatile int64_t cnt_adjleafc;         /* tesing counter for node cache adjust times */  volatile int64_t cnt_savenode;         /* tesing counter for node save times */  volatile int64_t cnt_loadnode;         /* tesing counter for node load times */  volatile int64_t cnt_adjnodec;         /* tesing counter for node cache adjust times */} TCBDB;


原创粉丝点击