/*
ランキング機 つきデータベースの ?�
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 hashtable
struct 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 end
typedef 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/12
int 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/12
struct charvalue
{
int use;
char buf[CHARVALUE_MAX];
};
struct charvalue *charvalue_buf;
int charvaluesize=0;
*/
/*
? バッファーを拡?する
*/
/* Spock deleted 2000/10/12
int
reallocCharValue(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/12
static int charvalue_finder=0;
static int
dbAllocCharValue( 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/12
static char *
dbGetString( int index )
{
return charvalue_buf[index].buf;
}
static int
dbSetString( 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 int
reallocDB( 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/13
dbAllocNode()
{
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
}
re
评论0