1. Basic Introduction
Tokyo Cabinet is the key value database (official homepage: http://fallabs.com/tokyocabinet/), developed and widely used by the Japanese, I am working on a Japanese social networking site (looks like Japan's largest in China ). Zhang banquet's blog elaborated on its application in Kingsoft.
The main implementations of toky cabinet databases include: (1) Hash structure; (2) B + tree structure; (3) fixed-length structure; (4) Abstract structure.
Since the implementation of the key value database involves data structure, Memory organization, file read/write, and many other aspects, you can understand the implementation details, the data structure and program design skills are greatly improved. Therefore, we plan to analyze the source code of Tokyo cabinet. This section mainly analyzes abstract databases.
2. Abstract Database
Abstract databases mainly implement hash databases and B + tree databases through memory. It mainly analyzes the hash structure.
2.1 main data structure of the abastrat Database
Analyze the initialization of tcadb.
/* Create an abstract database object. */<br/> tcadb * tcadbnew (void) {<br/> tcadb * ADB; <br/> tcmalloc (ADB, sizeof (* ADB )); <br/> ADB-> omode = adbovoid; <br/> ADB-> MDB = NULL; <br/> ADB-> NDB = NULL; <br/> ADB-> HDB = NULL; <br/> ADB-> bdb = NULL; <br/> ADB-> FDB = NULL; <br/> ADB-> TDB = NULL; <br/> ADB-> capnum =-1; <br/> ADB-> capsiz =-1; <br/> ADB-> capcnt = 0; <br/> ADB-> cur = NULL; <br/> ADB-> skel = NULL; <br/> return ADB; <br/>}
Different types of databases are created based on different initialization input parameters.
2.2open tcadb Database
Open the database through tcadbopen and initialize the tcadb struct pointer.
The tcmdb struct is initialized because the hash database is implemented.
/* Open an abstract database. */<br/> bool tcadbopen (tcadb * ADB, const char * Name) {<br/> assert (ADB & name ); <br/>/* open the file according to the configuration */<br/> If (ADB-> omode! = Adbovoid) return false; <br/>/* it should be possible to configure database-related parameters, and then use # To split */<br/> tclist * elems = tcstrsplit (name, "#"); <br/>/* obtain the actual path. If it is *, It is the memory database */<br/> char * Path = tclistshift2 (elems ); <br/> If (! Path) {<br/> tclistdel (elems); <br/> return false; <br/>}< br/>/* set it to on-memory hash database, <br/> * allocate memory for MDB */<br/> else if (! Tcstricmp (path, "*") {<br/> ADB-> MDB = bnum> 0? Tcmdbnew2 (bnum): tcmdbnew (); <br/> ADB-> capnum = capnum; <br/> ADB-> capsiz = capsiz; <br/> ADB-> capcnt = 0; <br/> ADB-> omode = adbomdb;
2.2.1 tcmdb Structure
The tcmdb structure is on-memory hash database.
/* Hash database structure */<br/> typedef struct {/* type of structure for a on-memory hash database */<br/> void ** mmtxs; /* mutexes for Method */<br/> void * imtx;/* mutex for iterator */<br/> tcmap ** maps; /* internal map objects */<br/> int ITER;/* index of maps for the iterator */<br/>} tcmdb;
The tcmap structure is a map array.
/* Tcpmap Data Structure */<br/> typedef struct {/* type of structure for a map */<br/> tcmaprec ** buckets; /* bucket array */<br/> tcmaprec * First;/* pointer to the first element */<br/> tcmaprec * last; /* pointer to the last element */<br/> tcmaprec * cur;/* pointer to the current element */<br/> uint32_t bnum; /* Number of buckets */<br/> uint64_t rnum;/* Number of records */<br/> uint64_t msiz; /* total size of records */<br/>} tcmap;
The map structure contains the hash buckets array.