From: Florian G. <re...@us...> - 2007-04-14 21:59:55
|
Update of /cvsroot/perfparse/_perfparse/db_tools In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv832/db_tools Modified Files: convert.c Log Message: metricId cache Index: convert.c =================================================================== RCS file: /cvsroot/perfparse/_perfparse/db_tools/convert.c,v retrieving revision 1.30 retrieving revision 1.31 diff -C2 -d -r1.30 -r1.31 *** convert.c 14 Apr 2007 00:34:59 -0000 1.30 --- convert.c 14 Apr 2007 21:59:54 -0000 1.31 *************** *** 61,64 **** --- 61,65 ---- int get_hostId_from_name(const char *host_name, MYSQL *db_mysql_new); int get_serviceId_from_desc(const char *host_name,const char *service_desc,MYSQL *db_mysql_new); + int get_metricId_from_text(const char *host_name,const char *service_desc, const char *metric,MYSQL *db_mysql_new); void end_curses(void); void exit_curses(int endval); *************** *** 69,72 **** --- 70,74 ---- GHashTable *hostsCache=NULL; GHashTable *servicesCache=NULL; + GHashTable *metricCache=NULL; void init_caches(void) { *************** *** 233,236 **** --- 235,287 ---- } + + int get_metricId_from_text(const char *host_name, const char *service_desc, + const char *metric, MYSQL *p_db_mysql_new) { + int *pMid; + int Sid,Mid; + + // use combined string for querying cache + GString *gsCombined=g_string_new(""); + g_string_printf(gsCombined,"%s : %s : %s",host_name,service_desc,metric); + + if((Sid=get_serviceId_from_desc(host_name,service_desc,p_db_mysql_new))==FALSE) { + return(FALSE); + } + + if((pMid=g_hash_table_lookup(metricCache,gsCombined->str))) { + Mid=*pMid; + #ifdef DEBUG + printw("\nMetricId Cache Hit: %s -> %d\n",gsCombined->str,Mid);refresh(); + #endif + return(Mid); + } else { + MYSQL_RES *query_result_new = NULL; + MYSQL_ROW result_row_new; + GString *gsNDBSql; + gsNDBSql = g_string_new (""); + g_string_printf(gsNDBSql, "select metric_id from perfdata_service_metric where " + "service_id=%d and metric='%s'", + Sid,metric); + query_result_new=dbtool_mysql_query(gsNDBSql->str, p_db_mysql_new, query_result_new); + if((result_row_new=mysql_fetch_row(query_result_new))) { + Mid=atoi(result_row_new[0]); + } else { + Mid=FALSE; + } + // cleanup + g_string_free(gsNDBSql,TRUE); + dbtool_freeResult(query_result_new); + // save result in cache + pMid=malloc(sizeof(int)); + *pMid=Mid; + g_hash_table_insert(metricCache,(gpointer) gsCombined->str,pMid); + #ifdef DEBUG + printw("\nMetricId Cache Miss: %s -> %d\n",gsCombined->str,Mid);refresh(); + #endif + g_string_free(gsCombined,TRUE); + return(Mid); + } + } + void |