📄 db.c
字号:
/* 仿件平件弘窗 勾五犯□正矛□旦及 隶[ 1999 Aug 14 Created by ringo 犯□正矛□旦反1 及伉件弁及溥匹忡绣今木月[ */#define _DB_C_#include "db.h"#include "main.h"#include "util.h"#include "version.h"#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <dirent.h>#include <errno.h>#include <string.h>#include <sys/stat.h>/* 侬 巨件玄伉及 length *///#define CHARVALUE_MAX 1024#define MAXTABLE 16// Spock 2000/10/12#define CHARVALUE_MAX 256 // DB 字串资料的buffer大小#define KEY_MAX 64 // DB Key字串的buffer大小#define HASH_SIZE 65536 // Hash table 一次增加的Entry数量#define HASH_PRIME 65521 // Hash function 使用的质数#define DBINIT_SIZE 16384 // DB 每次配置Entry的数量// Spock end/* 犯□正矛□旦及伉件弁及邰豳1蜊毛丐日歹允[ */struct dbentry{ int use;// unsigned int keyhash; /* 腹绸平□及甩永扑亘戊□玉 */ int ivalue; /* 旦戊失[玄永皿 NODE 反 -1 匹} 允屯化及旦戊失反 0 动晓匹卅中午中仃卅中*/// int nextind; /* -1 分匀凶日 毛啦 允月 */ // Spock 2000/10/12 int prev; // 前一个dbentry, -1表示此项为head int next; // 下一个dbentry, -1表示此项为tail char key[KEY_MAX]; char charvalue[CHARVALUE_MAX];// char key[64]; /* 腹绸平□午卅月 侬 */// int charvalue_index; /* 侬 田永白央毛今允index */ // Spock end};// Spock 2000/10/12// Database hashtablestruct hashentry{ char key[KEY_MAX]; // 索引key值 int use; // 是否已被使用 int dbind; // 指向 dbentry 的 index int prev; // 同一key值的上一个 hashentry, -1为head int next; // 同一key值的下一个 hashentry, -1为tail};// Spock endtypedef enum{ DB_INT_SORTED, DB_STRING,}DBTYPE;/* 1蜊及犯□正矛□旦毛丐日歹允 */struct table{ int use; // 0:未使用 1:已使用 DBTYPE type; /* DB及潘 */ char name[32]; /* 犯□正矛□旦及 蟆 */ int num; /* 巨件玄伉及醒 */ int toplinkindex; // Spock 2000/10/12 struct hashentry *hashtable; int hashsize; int updated; // 0:dbflush後未更新 1:已更新 int ent_finder; // 指向最後一次配置的 hashentry // Spock end};struct dbentry *master_buf; /* 巨件玄伉筏盛迕 */int dbsize = 0; /* 赓0匹}1,2,4,8,16...*/static int dbent_finder = 0;struct table dbt[MAXTABLE];static void dbShowAllTable(void);// Spock 2000/10/12int dbHash(char* s){ char *p; unsigned int h= 0 ,g; for( p = s ; *p ; p ++ ){ h = ( h<< 4 ) + (*p); if( (g = h & 0xf0000000) != 0){ h = h ^ (g>>24); h = h ^ g; } } return h % HASH_PRIME;}// Spock end/* Spock deleted 2000/10/12struct charvalue{ int use; char buf[CHARVALUE_MAX];};struct charvalue *charvalue_buf;int charvaluesize=0;*//* 侬 田永白央□毛傀舰允月 *//* Spock deleted 2000/10/12intreallocCharValue(void){ struct charvalue *previous = charvalue_buf; struct charvalue *newbuf; int new_charvaluesize; if( charvaluesize == 0 ){ new_charvaluesize = 1; } else { new_charvaluesize = charvaluesize * 2; } newbuf = ( struct charvalue *) calloc( 1, new_charvaluesize * sizeof( struct charvalue )); if( newbuf == NULL ){ log( "reallocCharValue: memory shortage!! new_charvaluesize:%d\n", new_charvaluesize ); return -1; } memset( newbuf, 0 , new_charvaluesize * sizeof( struct charvalue )); if( previous) memcpy( (char*)newbuf, (char*)previous, charvaluesize * sizeof( struct charvalue )); free( previous ); charvaluesize = new_charvaluesize; charvalue_buf = newbuf; log( "reallocCharValue: " "new_charvaluesize:%d Old address:%x New address:%x\n", new_charvaluesize , (unsigned int )previous, (unsigned int)newbuf ); return 0;}*//* 侬 田永白央□毛1蜊歹曰丐化月[ 凶曰卅仁卅匀凶日realloc允月[ *//* Spock deleted 2000/10/12static int charvalue_finder=0;static intdbAllocCharValue( void ){ int i; for(i=0;i<charvaluesize;i++){ charvalue_finder++; if( charvalue_finder == charvaluesize ) charvalue_finder =0; if( charvalue_buf[charvalue_finder].use == 0 ){ charvalue_buf[charvalue_finder].use =1; charvalue_buf[charvalue_finder].buf[0] = 0; return charvalue_finder; } } log( "dbAllocCharValue: charvalue array full. reallocating....\n" ); if( reallocCharValue() < 0 ){ log( "dbAllocCharValue: reallocation fail\n"); } else { return dbAllocCharValue(); } return -1;}*//* charvalue 井日/卞袄毛必永玄/本永玄允月 int index : charvalue index *//* Spock deleted 2000/10/12static char *dbGetString( int index ){ return charvalue_buf[index].buf;}static intdbSetString( int index , char *data ){ int l = strlen(data); if( l >= (sizeof( charvalue_buf[0].buf )-1)) return -1; memcpy( charvalue_buf[index].buf , data, l+1 ); return 0;}*//* DB及云云五今互凶日氏仁卅匀凶日歹曰丐化卅云允[中引及扔奶术及2 卞允月 0分匀凶日1卞允月 */static intreallocDB( void ){ struct dbentry *previous = master_buf; struct dbentry *newbuf; int new_dbsize;/* Spock deleted 2000/10/12 if( dbsize == 0 ){ new_dbsize = 1; } else { new_dbsize = dbsize * 2; }*/ // Spock+1 2000/10/12 new_dbsize = dbsize + DBINIT_SIZE; newbuf = (struct dbentry* ) calloc( 1, new_dbsize * sizeof( struct dbentry) ); /* 丢乒伉凶曰卅中 */ if( newbuf == NULL ){ log( "重新分配数据: 内存不足!!! 新数据大小: %d\n", new_dbsize ); return -1; } /* 衙中幻丹井日蕙仄中 卞戊疋□仄化 */ memset( newbuf , 0 , new_dbsize * sizeof( struct dbentry ) ); /* Spock deleted 2000/10/19 if( previous )memcpy( (char*)newbuf, (char*)previous, dbsize * sizeof( struct dbentry )); // 衙中幻丹毛荸 仄 free( previous ); */ // Spock 2000/10/19 if ( dbsize > 0 ) { memcpy( newbuf , previous , dbsize * sizeof(struct dbentry)); free( previous ); } // Spock end dbent_finder = dbsize; // 将 dbent_finder 指向未使用的 entry dbsize = new_dbsize; master_buf = newbuf; log( "重新分配数据: 新数据大小:%d 旧地址: %x 新地址:%x\n", new_dbsize , (unsigned int)previous, (unsigned int)newbuf ); return 0;}/* allocate a node */static int//dbAllocNode( DBTYPE type )// Spock +1 2000/10/13dbAllocNode(){ int i; for(i=0;i<dbsize;i++){ dbent_finder ++; if( dbent_finder == dbsize ) { dbent_finder = 0; } if( master_buf[dbent_finder].use == 0 ){ master_buf[dbent_finder].use = 1; /* Spock deleted 2000/10/12 // int 匹手尥笛树 及啃卞stringbuffer毛 勾仪卞允月 kawata if( type == DB_STRING || type == DB_INT_SORTED){ if( ( master_buf[dbent_finder].charvalue_index = dbAllocCharValue() ) < 0 ){ // 侬 田永白央□互凶曰卅中冗 return -1; } } */ return dbent_finder; } } log( "数据进入队列失败. 重新分配中....\n" ); if( reallocDB() < 0 ){ log( "重新分配失败\n" ); } else { //return dbAllocNode( type ); // Spock 2000/10/13 master_buf[dbent_finder].use = 1; log( "数据探测=%d\n" , dbent_finder ); return dbent_finder; // Spock end } return -1;}static voiddbReleaseNode( int index ){ // Spock 2000/10/12 int prev = master_buf[index].prev; int next = master_buf[index].next; master_buf[index].use = 0; if ( prev >= 0 ) master_buf[prev].next = next; if ( next >= 0 ) master_buf[next].prev = prev; // Spock end /* Spock deleted 2000/10/12 if( master_buf[index].charvalue_index >= 0 ) { charvalue_buf[ master_buf[index].charvalue_index].use = 0; } */}voiddbShowLink( int topind ){ int cur = topind; log( "开始从 %d 链接数据\n", cur ); /* Spock deleted 2000/10/19 for(;;){ if( cur == -1 )break; */ // Spock +1 2000/10/19 while ( cur >= 0 ) { if( master_buf[cur].use == 0 ){ log( "dbShowLink: use is 0! key:%s\n", master_buf[cur].key ); return; } // Spock +1 2000/10/12 log( "%s %i\n", master_buf[cur].key, master_buf[cur].ivalue ); /* Spock deleted 2000/10/12 log( "%s %u %i\n", master_buf[cur].key , master_buf[cur].keyhash, master_buf[cur].ivalue ); */ cur = master_buf[cur].next; }}// Spock 2000/10/13static intreallocHash( int dbi ){ struct hashentry *previous = dbt[dbi].hashtable; struct hashentry *newbuf; int new_hashsize; new_hashsize = dbt[dbi].hashsize + HASH_SIZE; newbuf = (struct hashentry* ) calloc( 1, new_hashsize * sizeof( struct hashentry) ); if( newbuf == NULL ){ log( "重新分配无用信息: 内存不足!!! 新无用信息大小: %d\n", new_hashsize ); return -1; } memset( newbuf , 0 , new_hashsize * sizeof( struct hashentry ) ); if( previous ) { memcpy( newbuf, previous, dbt[dbi].hashsize * sizeof( struct hashentry )); free( previous ); } if ( dbt[dbi].hashsize > HASH_PRIME ) dbt[dbi].ent_finder = dbt[dbi].hashsize; else dbt[dbi].ent_finder = HASH_PRIME; dbt[dbi].hashsize = new_hashsize; dbt[dbi].hashtable = newbuf; log( "重新分配无用信息: 新无用信息大小:%d 旧地址: %x 新地址:%x\n", new_hashsize , (unsigned int)previous, (unsigned int)newbuf ); return 0;}static int tableGetEntry( int dbi , char *k ){ int hashkey = dbHash( k ); struct hashentry *hash = dbt[dbi].hashtable; if ( hash[hashkey].use == 0 ) return -1; while ( 1 ) {// if ( hash[hashkey].use == 1 && strcmp( hash[hashkey].key , k ) == 0 ){ if ( hash[hashkey].use == 1 ){ if( strcmp( hash[hashkey].key , k ) == 0 )return hashkey; } hashkey = hash[hashkey].next; if ( hashkey <= 0 ){// log("err not found hash[%x] -%s!\n", hashkey, k) return -1; } }}static int tableInsertNode( int dbi , char *k , int dbind ){ int hashkey = dbHash( k ); int hashnext = -1; int i; struct hashentry *hash = dbt[dbi].hashtable; if ( hash[hashkey].use == 0 ) { strcpy( hash[hashkey].key , k ); hash[hashkey].use = 1; hash[hashkey].dbind = dbind; hash[hashkey].prev = -1; hash[hashkey].next = -1; dbt[dbi].num++; return hashkey; }else { for ( i=0; i<dbt[dbi].hashsize-HASH_PRIME; i++ ){ dbt[dbi].ent_finder++; if ( dbt[dbi].ent_finder >= dbt[dbi].hashsize ) dbt[dbi].ent_finder = HASH_PRIME; if ( hash[dbt[dbi].ent_finder].use == 0 ) { hashnext = dbt[dbi].ent_finder; break; } } if ( hashnext < HASH_PRIME ) { log( "tableInsertNode: hashentry array full. reallocating....\n" ); if( reallocHash( dbi ) < 0 ){ log( "tableInsertNode: reallocation fail\n" ); return -1; } else { hash = dbt[dbi].hashtable; hashnext = dbt[dbi].ent_finder; } } strcpy( hash[hashnext].key , k ); hash[hashnext].use = 1; hash[hashnext].dbind = dbind; hash[hashnext].prev = hashkey; hash[hashnext].next = hash[hashkey].next; if ( hash[hashkey].next >= 0 ) hash[hash[hashkey].next].prev = hashnext; hash[hashkey].next = hashnext; dbt[dbi].num++; return hashnext; } }static voidtableReleaseNode( int dbi , int ind ){ dbt[dbi].hashtable[ind].use = 0; if ( dbt[dbi].hashtable[ind].prev >= 0 ){ dbt[dbi].hashtable[dbt[dbi].hashtable[ind].prev].next = dbt[dbi].hashtable[ind].next; } if ( dbt[dbi].hashtable[ind].next >= 0 ){ dbt[dbi].hashtable[dbt[dbi].hashtable[ind].next].prev = dbt[dbi].hashtable[ind].prev; } dbt[dbi].num--;}// Spock end/* 伉件弁及玄永皿毛芨尹日木凶日}平□毛 曰卞用□玉毛腹绸允月[ 心勾井日卅中桦宁反巨仿□匹反卅中及匹0 *//* Spock deleted 2000/10/13static intdbExtractNodeByKey( int topind , char *k ){ int cur = topind; int prev = -1; unsigned int h = hashpjw( k ); // 伉件弁互坞匹手心勾井日卅中分仃卅及匹0毛井尹允 if( topind == -1 ) return 0; for(;;){ if( cur == -1 )break; if( master_buf[cur].keyhash == h && strcmp( master_buf[cur].key , k ) == 0 ){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -