C语言解析日志,存储数据到伯克利DB

编译命令

gcc -o dbwriter dbwriter.c -ldb

dbwriter.c

#include <assert.h>

#include <stdlib.h>

#include <stdio.h>

#include <db.h>

#include <sys/types.h>



#define DATABASE "demo.db"



/*

    功能:解析日志提取数据(pv)

日志格式:pp.cn guest:123456 100

*/

int parseLog(char *s,char **pv)

{

        int flag = 0;

        while(*s)

        {

                if(*s==' ')

                {

                        if(flag==0)

                        {

                                s++;

                                flag++;

                                continue;

                        }

                        else

                        {

                                *s='\0';

                                *pv = s+1;

                                return;

                        }

                }

                s++;

        }

}



/*

功能:打开伯克利DB的连接

*/

DB *openDb()

{

        int ret;

        DB *dbp = NULL;



        ret = db_create(&dbp, NULL, 0);

        if(ret != 0)

        {

                exit(1);

        }



        ret = dbp->open(dbp, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE,0664);



        if(ret != 0)

        {

                exit(1);

        }



        return dbp;

}



/*

功能:存储用户的浏览信息

*/

int saveViewInfo(DB *dbp, char *user, char *pv)

{

        int ret;

        DBT key , data;

        char oldpv[100] = {0};

        char newpv[100] = {0};



        //如果用户之前已经访问过了,那么PV等于之前的PV+现在的PV

        if(findViewInfo(dbp, user, &oldpv) == 1)

        {

                sprintf(&newpv,"%d", atoi(pv) + atoi(oldpv));

                printf("new new is %s\n", newpv);

        }

        else

        {

                strcpy(newpv,pv);

        }



        //初始化数据

        memset(&key, 0, sizeof(key));

        memset(&data, 0, sizeof(data));



        key.data = user;

        key.size = strlen(user) + 1;



        data.data = newpv;

        data.size = strlen(newpv) + 1;



        //写入DB

        ret = dbp->put(dbp, NULL, &key, &data, 0);



        if(ret == 0)

        {

                printf("db: %s key stored. value is %s\n", (char*)key.data, (char*)data.data);

                return 1;

        }

        else

        {

                //dbp->err(dbp, ret "DB->put");

                fprintf(stderr,"save Db error!\n");

                return 0;

        }



}



/*

功能:查找是否已经存在浏览信息

*/

int findViewInfo(DB *dbp, char *user, char *pv)

{

        int ret;

        DBT key, data;



        memset(&key, 0, sizeof(key));

        memset(&data, 0, sizeof(data));



        key.data = user;

        key.size = strlen(user) + 1;

        printf("get : %s , it %d \n", key.data, key.size);



        ret = dbp->get(dbp, NULL, &key, &data, 0);



        if(ret == 0)

        {

                //从data里把数据复制出来,因为data是栈上数据,不能拿出去使用

                strncpy(pv,data.data,data.size);

                printf("get : key is %s : data is %s \n" , (char*)key.data, (char*) data.data);

                return 1;

        }

        else

        {

                fprintf(stderr,"read Db error!\n");

                return 0;

        }

}



int main (int argc, char **argv)

{

        DB *dbp = NULL;

        FILE *fp = NULL;

        char buffer[1024 * 4];





        dbp = openDb();

        fp = fopen("20130815.ul","r");

        assert(fp != NULL);                                                                                                                

        

        while(fgets((char*)buffer, 1024*4,fp)!=NULL)                                                                                            

        {                                                                                                                                  

                char *user;                                                                                                                

                char *pv;

                char newpv[100] = {0};



                user = (char*)buffer;                                                                                                  

                parseLog(buffer,&pv);

                saveViewInfo(dbp, user, pv);

                findViewInfo(dbp,user,&newpv);

                printf("now pv is %s\n", newpv);

                memset(&buffer, 0, 1024 * 4);

        }                                                                                                                                  

 

        dbp->close(dbp, 0);

}

 

你可能感兴趣的:(C语言)