#define _XOPEN_SOURCE extern "C" { #include #include #include #include #include #include #include } #define die(dbh,hdl) do { \ if (hdl) \ mapi_explain_result(hdl,stderr); \ else if (dbh) \ mapi_explain(dbh,stderr); \ else \ fprintf(stderr,"command failed\n"); \ return(-1); \ } while (0) void fx1(int which); char* Tables[] = {"testTable","status",NULL}; int initDataBase(Mapi* pDbh,char* pDbName, char* pAddress, char** pTables,char* pPortNr); int wrDataBaseWoStatus(char* pTable,char* pSensorName, char* pTsEnd,char* pState,char* pAuxData, Mapi* pDbh); int closeDatabase(Mapi* pDbh); int convert(char* pTsEnd,char* pTsConverted); int main(int argc, char **argv) { char *pArg=argv[1]; int which = (int)strtol(pArg,NULL,10); fx1(which); printf("Finished\n"); return(0); } void fx1(int which) { Mapi dbh = NULL; struct tm nowTime; time_t tv_sec; struct tm *tmp; char theTime[100]; char pState[100]; memset(pState,0,sizeof(pState)); char pAux[100]; int state=0; int aux=0; char sensorname[50]; memset(sensorname,0,sizeof(sensorname)); sprintf(sensorname,"Sensor%d",which); if(initDataBase(&dbh,"testDB","127.0.0.1",Tables,"50000")<0) return; Mapi* pdbh=(Mapi*)(&dbh); sprintf(pState,"%d",state); int i=0; for(int n=0;n<1000000000;n++) { printf("%d iteration %d\n",which,i); i++; struct timeval tp; char outputStr[50]={0}; gettimeofday(&tp,NULL); tv_sec=tp.tv_sec; tmp = localtime(&tv_sec); memcpy(&nowTime,tmp,sizeof(nowTime)); memset(theTime,0,sizeof(theTime)); strftime(theTime,sizeof(theTime),"%Y%m%d_%H%M%S",&nowTime); /* timestr hold time stamp */ sprintf(outputStr,"%s.%ld",theTime,(tp.tv_usec/1000)); state = 0; sprintf(pState,"%d",state); sprintf(pAux,"%d",aux); aux++; while (wrDataBaseWoStatus("testTable",sensorname,outputStr,pState,pAux,pdbh)<0) { printf("%s:Couldn't write1 %s try again \n",sensorname,pAux); // usleep(100000); } state = 1; sprintf(pState,"%d",state); while (wrDataBaseWoStatus("testTable",sensorname,outputStr,pState,pAux,pdbh)<0) { printf("%s:Couldn't write2 %s try again \n",sensorname,pAux); } usleep(100000); } if (dbh == NULL) return; closeDatabase(&dbh); } int initDataBase(Mapi* pDbh,char* pDbName, char* pAddress, char** pTables,char* pPortNr) { MapiHdl hdl = NULL; char workString[1000]= {0}; if (pDbh == NULL) return(-1); *pDbh = mapi_connect(pAddress, atoi(pPortNr), "monetdb", "monetdb", "sql", pDbName); if (*pDbh == NULL || mapi_error(*pDbh)) die(*pDbh,hdl); /* sql */ /* switch autocommit to ON : 1 */ if (mapi_setAutocommit(*pDbh, 1) != MOK || mapi_error(*pDbh)) die(*pDbh,hdl); while(*pTables != NULL) { sprintf(workString,"create table %s (Id serial,sensorName varchar(50),tsBegin timestamp,tsEnd timestamp,state int,other varchar(10))",*pTables); hdl = mapi_query(*pDbh, workString); mapi_close_handle(hdl); pTables++; } sprintf(workString,"declare v int"); hdl = mapi_query(*pDbh, workString); mapi_close_handle(hdl); return(0); } int wrDataBaseWoStatus(char* pTable,char* pSensorName, char* pTsEnd,char* pState,char* pAuxData, Mapi* pDbh) { char workString[1000]= {0}; char TsConverted[30]={0}; MapiHdl hdl = NULL; if (*pDbh==NULL) return(-1); printf("TsEnd=%s ",pTsEnd); if(convert(pTsEnd,&TsConverted[0])<0) { return(-1); } printf("TsConverted=%s\n",TsConverted); sprintf(workString,"start transaction;update %s set tsEnd='%s' where id = (select max(id) from %s where sensorname='%s');insert into %s (sensorName, tsBegin,state,other) values('%s','%s','%s','%s'); commit;", \ pTable,TsConverted,pTable,pSensorName,pTable,pSensorName,TsConverted,pState,pAuxData); printf("%s\n",workString); if ((hdl = mapi_query(*pDbh, workString)) == NULL || mapi_error(*pDbh)) die(*pDbh,hdl); if (mapi_close_handle(hdl) != MOK) die(*pDbh,hdl); if (mapi_error(*pDbh)) die(*pDbh,hdl); return 0; } int closeDatabase(Mapi* pDbh) { if(pDbh == NULL) return(-1); if(*pDbh == NULL) return(-1); mapi_destroy(*pDbh); *pDbh = NULL; return(0); } int convert(char* pTsEnd,char* pTsConverted) { // notes: // pTsEnd is a 30 char array // Format : '20070926_134739_300' convert to '2007-09-26 13:47:39.300' char Year[5]= {0}; char Mon[3]={0}; char Day[3]={0}; char Hr[3]={0}; char Min[3]={0}; char Sec[3]={0}; char Msec[4]={0}; // Parse input for compatibility if(pTsEnd[8]!='_') { sprintf(pTsConverted,"%s",pTsEnd); return(0); } memcpy(Year,pTsEnd,4); memcpy(Mon,pTsEnd+4,2); memcpy(Day,pTsEnd+6,2); memcpy(Hr,pTsEnd+9,2); memcpy(Min,pTsEnd+11,2); memcpy(Sec,pTsEnd+13,2); memcpy(Msec,pTsEnd+16,3); sprintf(pTsConverted,"%s-%s-%s %s:%s:%s.%s",Year,Mon,Day,Hr,Min,Sec,Msec); return(0); }