Ignore:
Timestamp:
Aug 7, 2017, 2:56:24 PM (7 years ago)
Author:
djay
Message:

Update the source code for HPC support. Automatically adding nested outputs for the HPC support (should this be available for every support?). Add capability to store the metadata in the Collection DataBase?. Addition of the zcfg2sql to import any existing ZCFG file into the Collection DB. Add the support to invoke a callback (for history purpose) in case a [callback] section contains at least one parameter defined (url). Add support to convert maps and map to JSON (for callback use only by now). Fix some memory leaks (some are still there).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/prototype-v0/zoo-project/zoo-kernel/sqlapi.c

    r822 r839  
    4343OGRDataSource
    4444#endif
    45  *zoo_DS = NULL;
     45 **zoo_DS = NULL;
    4646
    4747/**
     
    7070  int i=0;
    7171  maps* cconf=getMaps(conf,key);
     72  if(cconf==NULL)
     73    return "-1";
    7274  int len=0;
    7375  for(i=0;i<6;i++){
    7476    map* tmp=getMap(cconf->content,keywords[i]);
    75     fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
    76     fflush(stderr);
    7777    if(tmp!=NULL){
    78       fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
    79       fflush(stderr);
    8078      if(res==NULL){
    8179        res=(char*)malloc((strlen(keywords[i])+strlen(tmp->value)+4)*sizeof(char));
     
    8987        len+=strlen(res1);
    9088        res[len]=0;
    91         fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
    92         fflush(stderr);
    9389        free(res1);
    9490      }
    9591    }
    96     fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
    97     fflush(stderr);
    98   }
    99   fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
    100   fflush(stderr);
     92  }
    10193  map* tmp=getMap(cconf->content,"type");
    10294  if(tmp!=NULL){
     
    125117 * @see createInitString
    126118 */
    127 void _init_sql(maps* conf,const char* key){
     119int _init_sql(maps* conf,const char* key){
    128120  char* sqlInitString=_createInitString(conf,key);
     121  if(strncmp(sqlInitString,"-1",2)==0)
     122    return -1;
    129123  OGRSFDriver *poDriver = NULL;
    130124  OGRRegisterAll();
    131 
     125  int zoo_ds_nb=0;
     126  map* dsNb=getMapFromMaps(conf,"lenv","ds_nb");
     127  if(dsNb==NULL){
     128    setMapInMaps(conf,"lenv","ds_nb","1");
     129  }else{
     130    zoo_ds_nb=atoi(dsNb->value);
     131    char* tmp=(char*)malloc(11*sizeof(char));
     132    sprintf(tmp,"%d",zoo_ds_nb+1);
     133    setMapInMaps(conf,"lenv","ds_nb",(const char*)tmp);
     134    free(tmp);
     135  }
     136  if(zoo_DS==NULL)
     137    zoo_DS=
    132138#if GDAL_VERSION_MAJOR >= 2
    133   zoo_DS = (GDALDataset*) GDALOpenEx( sqlInitString,
     139      (GDALDataset**) malloc(sizeof(GDALDataset*))
     140#else
     141      (OGRDataSource**) malloc(sizeof(OGRDataSource*))
     142#endif
     143      ;
     144  else
     145    zoo_DS=     
     146#if GDAL_VERSION_MAJOR >= 2
     147      (GDALDataset**)realloc(zoo_DS,(zoo_ds_nb+1)*sizeof(GDALDataset*))
     148#else
     149      (OGRDataSource**)realloc(zoo_DS,(zoo_ds_nb+1)*sizeof(OGRDataSource*))
     150#endif
     151      ;
     152 
     153#if GDAL_VERSION_MAJOR >= 2
     154  zoo_DS[zoo_ds_nb] = (GDALDataset*) GDALOpenEx( sqlInitString,
    134155                                      GDAL_OF_UPDATE | GDAL_OF_VECTOR,
    135156                                      NULL, NULL, NULL );
    136157#else
    137   zoo_DS = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver);
    138 #endif
    139   if( zoo_DS == NULL ){
     158  zoo_DS[zoo_ds_nb] = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver);
     159#endif
     160  if( zoo_DS[zoo_ds_nb] == NULL ){
    140161#ifdef DEBUG
    141162    fprintf(stderr,"sqlInitString: %s FAILED !\n",sqlInitString);
     
    143164#endif
    144165    free(sqlInitString);
    145     setMapInMaps(conf,"lenv","message","Failed to connect to the database backend");
    146     return;
     166    setMapInMaps(conf,"lenv","dbIssue","1");
     167    setMapInMaps(conf,"lenv","message",_("Failed to connect to the database backend"));
     168    return -2;
    147169  }
    148170#ifdef DEBUG
     
    150172  fflush(stderr);
    151173#endif
     174  fprintf(stderr,"sqlInitString: %s SUCEED %d !\n",sqlInitString,zoo_ds_nb);
     175  fflush(stderr); 
    152176  free(sqlInitString);
     177  zoo_ds_nb++;
     178  return zoo_ds_nb;
    153179}
    154180
     
    159185 * @see createInitString
    160186 */
    161 void init_sql(maps* conf){
     187int init_sql(maps* conf){
    162188  return _init_sql(conf,"database");
    163189}
     
    168194 * @param conf the maps containing the setting of the main.cfg file
    169195 */
    170 void close_sql(maps* conf){
     196void close_sql(maps* conf,int cid){
    171197  if( zoo_ResultSet != NULL )
    172     zoo_DS->ReleaseResultSet( zoo_ResultSet );
     198    zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet );
    173199  if(zoo_DS!=NULL){
    174200#if GDAL_VERSION_MAJOR >= 2
    175     GDALClose(zoo_DS);
    176 #else
    177     OGRDataSource::DestroyDataSource( zoo_DS );
    178 #endif
    179     zoo_DS=NULL;
     201    GDALClose(zoo_DS[cid]);
     202#else
     203    OGRDataSource::DestroyDataSource( zoo_DS[cid] );
     204#endif
     205    zoo_DS[cid]=NULL;
    180206  }
    181207}
     
    196222 * @return NULL in case of failure or an OGRLayer pointer if the query succeed.
    197223 */
    198 OGRLayer *fetchSql(maps* conf,const char* sqlQuery){
     224OGRLayer *fetchSql(maps* conf,int cid,const char* sqlQuery){
     225  if(zoo_DS[cid]==NULL)
     226    return NULL;
    199227  OGRLayer *res=NULL;
    200   res = zoo_DS->ExecuteSQL( sqlQuery, NULL, NULL);
     228#ifdef DEBUG
     229  fprintf(stderr,"************************* %s %s %d\n\n",sqlQuery,__FILE__,__LINE__);
     230  fflush(stderr);
     231#endif
     232  res = zoo_DS[cid]->ExecuteSQL( sqlQuery, NULL, NULL);
    201233  return res;
    202234}
    203235
    204 void cleanFetchSql(maps* conf,OGRLayer *objects){
     236void cleanFetchSql(maps* conf,int cid,OGRLayer *objects){
    205237  if( objects != NULL ){
    206     zoo_DS->ReleaseResultSet( objects );
     238    zoo_DS[cid]->ReleaseResultSet( objects );
    207239    objects=NULL;
    208240  }
     
    216248 * @return -1 in case of failure and 1 if the query succeed.
    217249 */
    218 int execSql(maps* conf,const char* sqlQuery){
     250int execSql(maps* conf,int cid,const char* sqlQuery){
    219251  int res=-1;
    220   zoo_ResultSet = zoo_DS->ExecuteSQL( sqlQuery, NULL, NULL);
     252  zoo_ResultSet = zoo_DS[cid]->ExecuteSQL( sqlQuery, NULL, NULL);
    221253  if( zoo_ResultSet != NULL ){
    222254    res=1;
    223255  }
    224   zoo_DS->ReleaseResultSet( zoo_ResultSet );
    225256  return res;
    226257}
     
    233264 * @return -1 in case of failure and 1 if the query succeed.
    234265 */
    235 void cleanUpResultSet(const maps* conf){
     266void cleanUpResultSet(const maps* conf,int cid){
    236267  if( zoo_ResultSet != NULL ){
    237     zoo_DS->ReleaseResultSet( zoo_ResultSet );
     268    zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet );
    238269    zoo_ResultSet=NULL;
    239270  }
     271}
     272
     273#ifdef RELY_ON_DB
     274int getCurrentId(maps* conf){
     275  int res=0;
     276  map* dsNb=getMapFromMaps(conf,"lenv","ds_nb");
     277  if(dsNb!=NULL)
     278    res=atoi(dsNb->value);
     279  return res;
    240280}
    241281
     
    249289 */
    250290void recordStoredFile(maps* conf,const char* filename,const char* type,const char* name){
     291  int zoo_ds_nb=getCurrentId(conf);
    251292  map *uusid=getMapFromMaps(conf,"lenv","usid");
    252293  map *schema=getMapFromMaps(conf,"database","schema");
     
    256297  else
    257298    sprintf(sqlQuery,"INSERT INTO %s.files (uuid,filename,nature,name) VALUES ('%s','%s','%s',NULL);",schema->value,uusid->value,filename,type);
    258   execSql(conf,sqlQuery);
    259   free(sqlQuery);
    260   cleanUpResultSet(conf);
     299  execSql(conf,zoo_ds_nb-1,sqlQuery);
     300  free(sqlQuery);
     301  cleanUpResultSet(conf,zoo_ds_nb-1);
    261302}
    262303
     
    267308 */
    268309void recordServiceStatus(maps* conf){
     310  int zoo_ds_nb=getCurrentId(conf);
    269311  map *sid=getMapFromMaps(conf,"lenv","sid");
    270312  map *osid=getMapFromMaps(conf,"lenv","osid");
     
    284326          osid->value,
    285327          wpsStatus[2]);
    286   execSql(conf,sqlQuery);
    287   free(sqlQuery);
    288   cleanUpResultSet(conf);
     328  execSql(conf,zoo_ds_nb-1,sqlQuery);
     329  free(sqlQuery);
     330  cleanUpResultSet(conf,zoo_ds_nb-1);
    289331}
    290332
     
    296338 */
    297339void recordResponse(maps* conf,char* filename){
     340  int zoo_ds_nb=getCurrentId(conf);
    298341  FILE *file = fopen (filename, "rb");
    299342  fseek (file, 0, SEEK_END);
     
    308351  char *sqlQuery=(char*)malloc((strlen(schema->value)+flen+strlen(sid->value)+57+1)*sizeof(char));
    309352  sprintf(sqlQuery,"INSERT INTO %s.responses (content,uuid) VALUES ($$%s$$,$$%s$$);",schema->value,tmps,sid->value);
    310   execSql(conf,sqlQuery);
     353  execSql(conf,zoo_ds_nb-1,sqlQuery);
    311354  free(sqlQuery);
    312355  free(tmps);
    313   cleanUpResultSet(conf);
    314 }
    315 
    316 #ifdef RELY_ON_DB
     356  cleanUpResultSet(conf,zoo_ds_nb-1);
     357}
     358
    317359/**
    318360 * Update the current status of the running service.
     
    322364 */
    323365int _updateStatus(maps* conf){
     366  int zoo_ds_nb=getCurrentId(conf);
    324367  map *sid=getMapFromMaps(conf,"lenv","usid");
    325368  map *p=getMapFromMaps(conf,"lenv","status");
     
    328371  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(msg->value)+strlen(p->value)+strlen(sid->value)+64+1)*sizeof(char));
    329372  sprintf(sqlQuery,"UPDATE %s.services set status=$$%s$$,message=$$%s$$ where uuid=$$%s$$;",schema->value,p->value,msg->value,sid->value);
    330   fflush(stderr);
    331   if( zoo_DS == NULL )
     373  if( zoo_ds_nb==
     374#ifdef META_DB
     375      1
     376#else
     377      0
     378#endif
     379      ){
    332380    init_sql(conf);
    333   execSql(conf,sqlQuery);
    334   cleanUpResultSet(conf);
     381    zoo_ds_nb++;
     382  }
     383  execSql(conf,zoo_ds_nb-1,sqlQuery);
     384  cleanUpResultSet(conf,zoo_ds_nb-1);
    335385  free(sqlQuery);
    336386  return 0;
     
    345395 */
    346396char* _getStatus(maps* conf,char* pid){
     397  int zoo_ds_nb=getCurrentId(conf);
    347398  map *schema=getMapFromMaps(conf,"database","schema");
    348399  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
    349400  sprintf(sqlQuery,"select status||'|'||message from %s.services where uuid=$$%s$$;",schema->value,pid);
    350   if( zoo_DS == NULL )
     401  if( zoo_ds_nb==
     402#ifdef META_DB
     403      1
     404#else
     405      0
     406#endif
     407      ){
    351408    init_sql(conf);
    352   execSql(conf,sqlQuery);
     409    zoo_ds_nb++;
     410  }
     411  execSql(conf,zoo_ds_nb-1,sqlQuery);
    353412  OGRFeature  *poFeature = NULL;
    354413  const char *tmp1;
     
    363422    OGRFeature::DestroyFeature( poFeature );
    364423  }
    365   cleanUpResultSet(conf);
     424  cleanUpResultSet(conf,zoo_ds_nb-1);
    366425  free(sqlQuery);
    367426  return (char*)tmp1;
     
    376435 */
    377436char* _getStatusFile(maps* conf,char* pid){
     437  int zoo_ds_nb=getCurrentId(conf);
    378438  map *schema=getMapFromMaps(conf,"database","schema");
    379439  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+82+1)*sizeof(char));
     
    381441          "select content from %s.responses where uuid=$$%s$$"
    382442          " order by creation_time desc limit 1",schema->value,pid);
    383   if( zoo_DS == NULL )
     443  if( zoo_ds_nb==
     444#ifdef META_DB
     445      1
     446#else
     447      0
     448#endif
     449      ){
    384450    init_sql(conf);
    385   execSql(conf,sqlQuery);
     451    zoo_ds_nb++;
     452  }
     453  execSql(conf,zoo_ds_nb-1,sqlQuery);
    386454  OGRFeature  *poFeature = NULL;
    387455  const char *tmp1;
    388456  int hasRes=-1;
    389   while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
    390     for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
    391       if( poFeature->IsFieldSet( iField ) ){
    392         tmp1=strdup(poFeature->GetFieldAsString( iField ));
    393         hasRes=1;
     457  if(zoo_ResultSet!=NULL){
     458      while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
     459        for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
     460          if( poFeature->IsFieldSet( iField ) ){
     461            tmp1=strdup(poFeature->GetFieldAsString( iField ));
     462            hasRes=1;
     463          }
     464          else
     465            tmp1=NULL;
     466        }
     467        OGRFeature::DestroyFeature( poFeature );
    394468      }
    395       else
    396         tmp1=NULL;
    397     }
    398     OGRFeature::DestroyFeature( poFeature );
    399469  }
    400470  if(hasRes<0)
    401471    tmp1=NULL;
    402   cleanUpResultSet(conf);
     472  else
     473    cleanUpResultSet(conf,zoo_ds_nb-1);
    403474  free(sqlQuery);
    404475  return (char*)tmp1;
     
    412483 */
    413484void removeService(maps* conf,char* pid){
     485  int zoo_ds_nb=getCurrentId(conf);
    414486  map *schema=getMapFromMaps(conf,"database","schema");
    415487  char *sqlQuery=(char*)
    416488    malloc((strlen(pid)+strlen(schema->value)+38+1)
    417489           *sizeof(char));
    418   if( zoo_DS == NULL )
     490  if( zoo_ds_nb==
     491#ifdef META_DB
     492      1
     493#else
     494      0
     495#endif
     496      ){
    419497    init_sql(conf);
     498    zoo_ds_nb++;
     499  }
    420500  sprintf(sqlQuery,
    421501          "DELETE FROM %s.services where uuid=$$%s$$;",
    422502          schema->value,pid);
    423   execSql(conf,sqlQuery);
    424   cleanUpResultSet(conf);
    425   close_sql(conf);
     503  execSql(conf,zoo_ds_nb-1,sqlQuery);
     504  cleanUpResultSet(conf,zoo_ds_nb-1);
     505  close_sql(conf,zoo_ds_nb-1);
    426506  free(sqlQuery);
    427507  end_sql();
     
    434514 */
    435515void unhandleStatus(maps* conf){
     516  int zoo_ds_nb=getCurrentId(conf);
    436517  map *schema=getMapFromMaps(conf,"database","schema");
    437518  map *sid=getMapFromMaps(conf,"lenv","usid");
     
    447528          " where uuid=$$%s$$;",
    448529          schema->value,(fstate!=NULL?fstate->value:"Failed"),sid->value);
    449   execSql(conf,sqlQuery);
    450   cleanUpResultSet(conf);
    451   close_sql(conf);
     530  execSql(conf,zoo_ds_nb-1,sqlQuery);
     531  cleanUpResultSet(conf,zoo_ds_nb-1);
     532  close_sql(conf,zoo_ds_nb-1);
    452533  free(sqlQuery);
    453534  end_sql();
     
    462543 */
    463544char* getStatusId(maps* conf,char* pid){
     545  int zoo_ds_nb=getCurrentId(conf);
    464546  map *schema=getMapFromMaps(conf,"database","schema");
    465547  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
     
    467549          "select osid from %s.services where uuid=$$%s$$",
    468550          schema->value,pid);
    469   if( zoo_DS == NULL )
     551  if( zoo_ds_nb==0 ){
    470552    init_sql(conf);
    471   execSql(conf,sqlQuery);
     553    zoo_ds_nb++;
     554  }
     555  execSql(conf,zoo_ds_nb-1,sqlQuery);
    472556  OGRFeature  *poFeature = NULL;
    473557  const char *tmp1;
     
    486570    tmp1=NULL;
    487571  free(sqlQuery);
     572  cleanUpResultSet(conf,zoo_ds_nb-1);
    488573  return (char*)tmp1;
    489574}
     
    496581 */
    497582void readFinalRes(maps* conf,char* pid,map* statusInfo){
     583  int zoo_ds_nb=getCurrentId(conf);
    498584  map *schema=getMapFromMaps(conf,"database","schema");
    499585  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
     
    503589  if( zoo_DS == NULL )
    504590    init_sql(conf);
    505   execSql(conf,sqlQuery);
     591  execSql(conf,zoo_ds_nb-1,sqlQuery);
    506592  OGRFeature  *poFeature = NULL;
    507593  int hasRes=-1;
     
    531617int isRunning(maps* conf,char* pid){
    532618  int res=0;
     619  int zoo_ds_nb=getCurrentId(conf);
    533620  map *schema=getMapFromMaps(conf,"database","schema");
    534621  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+73+1)*sizeof(char));
    535622  sprintf(sqlQuery,"select count(*) as t from %s.services where uuid=$$%s$$ and end_time is null;",schema->value,pid);
    536   if( zoo_DS == NULL )
     623  if( zoo_ds_nb == 0 ){
    537624    init_sql(conf);
    538   execSql(conf,sqlQuery);
     625    zoo_ds_nb++;
     626  }
     627  execSql(conf,zoo_ds_nb-1,sqlQuery);
    539628  OGRFeature  *poFeature = NULL;
    540629  const char *tmp1;
     
    549638    OGRFeature::DestroyFeature( poFeature );
    550639  }
    551   cleanUpResultSet(conf);
     640  cleanUpResultSet(conf,zoo_ds_nb-1);
    552641  free(sqlQuery);
    553642  return res;
Note: See TracChangeset for help on using the changeset viewer.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png