C语言解析日志,存储数据到伯克利DB 2
#编译程序 gcc -o historydb historydb.c -ldb #将2013年8月9日的用户记录写入数据库 (程序自动识别新用户入库,跳过老用户) ./historydb -f 20130809.userlist 20130809 #查询guest1985215666654在2013年8月8日有没有访问业务bch2000 ./historydb -s "bch2000 guest1985215666654" 20130808
#include <assert.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <db.h> #include <sys/types.h> #include <getopt.h> #define DATABASE "/work/stat/read/newstat/test/history.db" struct ViewDate { int year; int month; int day; }; struct ViewData { struct ViewDate date; }; void setDate(struct ViewDate *date,char *s) { assert(s != NULL); char tmp[10]={0}; strncpy(tmp,s,4); date->year = atoi(tmp); memset(tmp,0,10); strncpy(tmp,s+4,2); date->month = atoi(tmp); memset(tmp,0,10); strncpy(tmp,s+6,2); date->day = atoi(tmp); //printf("%d - %d - %d \n",date->year, date->month, date->day); } int compareViewData(struct ViewData *data , struct ViewData *storedData) { int dataArray [] = {data->date.year, data->date.month, data->date.day}; int storedDataArray [] = {storedData->date.year, storedData->date.month, storedData->date.day}; int i = 0; for(;i<sizeof(dataArray) / sizeof(int); i++) { if(storedDataArray[i] > dataArray[i]) { return 0; } } return 1; } void printViewData(struct ViewData *data) { printf("print view data : %d - %d - %d \n",data->date.year, data->date.month, data->date.day); } char *trim(char *s) { int i = strlen(s); for(;i>0;i--) { if(s[i]==' ' || s[i]=='\n' || s[i]=='\0' || s[i]=='\t') { s[i] = '\0'; } else { break; } } return s; } DB *openDb() { int ret; DB *dbp = NULL; ret = db_create(&dbp, NULL, 0); if(ret != 0) { fprintf(stderr,"create Db error!\n"); exit(1); } ret = dbp->open(dbp, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE,0664); if(ret != 0) { fprintf(stderr,"open Db error!\n"); exit(1); } return dbp; } int saveViewInfo(DB *dbp, char *user, struct ViewData *data) { int ret; DBT key,saveData; memset(&key, 0, sizeof(key)); memset(&saveData, 0, sizeof(saveData)); key.data = user; key.size = strlen(user) + 1; saveData.data = data; saveData.size = sizeof(*data); if(findViewInfo(dbp, user, data)==1) { //printf("already has %s \n", user); return 1; } ret = dbp->put(dbp, NULL, &key, &saveData, 0); if(ret == 0) { //printViewData(saveData.data); return 1; } else { //key storead failed return 0; } } int findViewInfo(DB *dbp, char *user, struct ViewData *data) { int ret; DBT key, storedData; memset(&key, 0, sizeof(key)); memset(&storedData, 0, sizeof(storedData)); key.data = user; key.size = strlen(user) + 1; ret = dbp->get(dbp, NULL, &key, &storedData, 0); if(ret == 0 && compareViewData(data, (struct ViewData *)storedData.data)==1) { //found user return 1; } //not found return 0; } void saveViewFile(char *filename, struct ViewData *data)//20130815.save.ul { DB *dbp = openDb(); char buffer[1024 * 4]; FILE *fp = fopen(filename,"r"); assert(fp != NULL); while(fgets((char*)buffer, 1024*4, fp)!=NULL) { char *user = (char*)buffer; saveViewInfo(dbp, trim(user), data); memset(&buffer, 0, 1024 * 4); } dbp->close(dbp, 0); } void hasViewInfo(char *user, struct ViewData *data) { DB *dbp = openDb(); assert(user != NULL); user = trim(user); if(findViewInfo(dbp, user, data)==1) { printf("found %s\n",user); } else { printf("not found %s\n",user); } dbp->close(dbp, 0); } int main (int argc, char *argv[]) { int oc; extern char *optarg; extern int optind, opterr, optopt; struct ViewData viewData; memset(&viewData, 0, sizeof(viewData)); while((oc=getopt(argc,argv,"f:s:")) != -1) { setDate(&viewData.date,argv[optind]); switch(oc) { case 'f': saveViewFile(optarg, &viewData); break; case 's': hasViewInfo(optarg, &viewData); break; } } return 0; }