Skip to content

Commit

Permalink
Cached(ie default) time dimension used instead of requested time dime…
Browse files Browse the repository at this point in the history
…nsion for getfeatureinfo (#311)
  • Loading branch information
TAlonglong authored and jmckenna committed Aug 2, 2024
1 parent ea2d0a0 commit 4df71c3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
6 changes: 6 additions & 0 deletions include/mapcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,12 @@ struct mapcache_dimension {
apr_array_header_t* (*get_all_ogc_formatted_entries)(mapcache_context *ctx, mapcache_dimension *dimension,
mapcache_tileset *tileset, mapcache_extent *extent, mapcache_grid *grid);

/**
* \brief return all value to override default value
*/
apr_array_header_t* (*get_default_value)(mapcache_context *ctx, mapcache_dimension *dimension,
mapcache_tileset *tileset, mapcache_extent *extent, mapcache_grid *grid);

/**
* \brief parse the value given in the configuration
*/
Expand Down
1 change: 1 addition & 0 deletions lib/connection_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ mapcache_pooled_connection* mapcache_connection_pool_get_connection(mapcache_con
pc->private->next = pcc->head;
pc->private->pcc = pcc;

pcc->max_list_size = ctx->config->cp_hmax;
if(count == pcc->max_list_size) {
/* max number of connections atained, we must destroy the last one that was used */
mapcache_pooled_connection *opc;
Expand Down
69 changes: 68 additions & 1 deletion lib/dimension_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ struct mapcache_dimension_postgresql {
char *dbconnection;
char *get_values_for_entry_query;
char *get_all_values_query;
char *get_default_value_query;
apr_hash_t *get_values_indexes;
apr_hash_t *get_all_indexes;
apr_hash_t *get_default_value_indexes;
};

struct postgresql_dimension_conn {
Expand Down Expand Up @@ -74,9 +76,10 @@ static int qparam(mapcache_context *ctx, char *qstring, const char *param, int i
static void parse_queries(mapcache_context *ctx, mapcache_dimension_postgresql *dim) {
const char *keys[9] = {":tileset",":dim",":gridsrs",":minx",":maxx",":miny",":maxy",":start_timestamp",":end_timestamp"};
int i;
int gaidx=1,gvidx=1;
int gaidx=1,gvidx=1,gdidx=1;
dim->get_all_indexes = apr_hash_make(ctx->pool);
dim->get_values_indexes = apr_hash_make(ctx->pool);
dim->get_default_value_indexes = apr_hash_make(ctx->pool);
for(i=0;i<9;i++) {
if(qparam(ctx,dim->get_all_values_query,keys[i],gaidx)) {
apr_hash_set(dim->get_all_indexes,keys[i],APR_HASH_KEY_STRING,INT2VOIDP(gaidx));
Expand All @@ -86,6 +89,12 @@ static void parse_queries(mapcache_context *ctx, mapcache_dimension_postgresql *
apr_hash_set(dim->get_values_indexes,keys[i],APR_HASH_KEY_STRING,INT2VOIDP(gvidx));
gvidx++;
}
if (dim->get_default_value_query){
if(qparam(ctx,dim->get_default_value_query,keys[i],gdidx)) {
apr_hash_set(dim->get_default_value_indexes,keys[i],APR_HASH_KEY_STRING,INT2VOIDP(gdidx));
gdidx++;
}
}
}
}

Expand Down Expand Up @@ -220,6 +229,14 @@ void mapcache_postgresql_dimension_connection_constructor(mapcache_context *ctx,
*conn_ = NULL;
return;
}
if (dim->get_default_value_query){
prepare_query(ctx,conn->pgconn, "get_default_value", dim->get_default_value_query, dim->get_default_value_indexes);
if(GC_HAS_ERROR(ctx)) {
PQfinish(conn->pgconn);
*conn_ = NULL;
return;
}
}
}

void mapcache_postgresql_dimension_connection_destructor(void *conn_)
Expand Down Expand Up @@ -330,6 +347,47 @@ static apr_array_header_t* _mapcache_dimension_postgresql_get_all_entries(mapcac

}

static apr_array_header_t* _mapcache_dimension_postgresql_get_default_entries(mapcache_context *ctx, mapcache_dimension *dim,
mapcache_tileset *tileset, mapcache_extent *extent, mapcache_grid *grid) {
mapcache_dimension_postgresql *sdim = (mapcache_dimension_postgresql*)dim;
PGresult *res;
apr_array_header_t *time_ids = NULL;
mapcache_pooled_connection *pc;
struct postgresql_dimension_conn *conn;
int nParams, *paramLengths,*paramFormats,i;
char **paramValues;

if (sdim->get_default_value_query == NULL){
return NULL;
}
pc = _postgresql_dimension_get_conn(ctx,tileset,sdim);
if (GC_HAS_ERROR(ctx)) {
return NULL;
}
conn = pc->connection;
_mapcache_dimension_postgresql_bind_parameters(ctx,sdim->get_all_indexes,NULL,tileset,extent,grid,0,0,&nParams,&paramValues,&paramLengths,&paramFormats);
if(GC_HAS_ERROR(ctx)) {
_postgresql_dimension_release_conn(ctx, pc);
return NULL;
}
res = PQexecPrepared(conn->pgconn,"get_default_value",nParams,(const char *const*)paramValues,paramLengths,paramFormats,0);
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
//ctx->set_error(ctx, 500, "postgresql query: %s", PQerrorMessage(conn->pgconn));
PQclear(res);
_postgresql_dimension_release_conn(ctx, pc);
return NULL;
}

time_ids = apr_array_make(ctx->pool,0,sizeof(char*));
for(i=0;i<PQntuples(res);i++) {
APR_ARRAY_PUSH(time_ids, char *) = apr_pstrdup(ctx->pool, PQgetvalue(res,i,0));
}
PQclear(res);
_postgresql_dimension_release_conn(ctx, pc);
return time_ids;

}

static void _mapcache_dimension_postgresql_parse_xml(mapcache_context *ctx, mapcache_dimension *dim,
ezxml_t node)
{
Expand Down Expand Up @@ -359,6 +417,14 @@ static void _mapcache_dimension_postgresql_parse_xml(mapcache_context *ctx, mapc
ctx->set_error(ctx,400,"postgresql dimension \"%s\" has no <list_query> node", dim->name);
return;
}
child = ezxml_child(node,"default_query");
if(child) {
dimension->get_default_value_query = apr_pstrdup(ctx->pool, child->txt);
} else {
dimension->get_default_value_query = NULL;
// ctx->set_error(ctx,400,"postgresql dimension \"%s\" has no <default_query> node", dim->name);
// return;
}
parse_queries(ctx,dimension);
//printf("q1: %s\n",dimension->get_all_values_query);
//printf("q2: %s\n",dimension->get_values_for_entry_query);
Expand All @@ -377,6 +443,7 @@ mapcache_dimension* mapcache_dimension_postgresql_create(mapcache_context *ctx,
dimension->dimension.configuration_parse_xml = _mapcache_dimension_postgresql_parse_xml;
dimension->dimension.get_all_entries = _mapcache_dimension_postgresql_get_all_entries;
dimension->dimension.get_all_ogc_formatted_entries = _mapcache_dimension_postgresql_get_all_entries;
dimension->dimension.get_default_value = _mapcache_dimension_postgresql_get_default_entries;
return (mapcache_dimension*)dimension;
#else
ctx->set_error(ctx,400,"postgresql dimension support requires POSTGRESQL support to be built in");
Expand Down
8 changes: 7 additions & 1 deletion lib/service_wms.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ void _create_capabilities_wms(mapcache_context *ctx, mapcache_request_get_capabi
if(tileset->dimensions) {
for(i=0; i<tileset->dimensions->nelts; i++) {
apr_array_header_t *values;
apr_array_header_t *default_value;
int value_idx;
char *dimval = NULL;
mapcache_dimension *dimension = APR_ARRAY_IDX(tileset->dimensions,i,mapcache_dimension*);
Expand All @@ -337,6 +338,11 @@ void _create_capabilities_wms(mapcache_context *ctx, mapcache_request_get_capabi
if(dimval) {
ezxml_set_txt(dimxml,dimval);
}
default_value = dimension->get_default_value(ctx,dimension,tileset,NULL,NULL);
if (default_value){
dimension->default_value = APR_ARRAY_IDX(default_value,0,char *);
ezxml_set_attr(dimxml,"default",dimension->default_value);
}
}
}

Expand Down Expand Up @@ -886,7 +892,7 @@ void _mapcache_service_wms_parse_request(mapcache_context *ctx, mapcache_service
mapcache_dimension *dimension = APR_ARRAY_IDX(tileset->dimensions,i,mapcache_dimension*);
const char *value;
if((value = (char*)apr_table_get(params,dimension->name)) != NULL) {
mapcache_map_set_cached_dimension(ctx,&fi->map,dimension->name,value);
mapcache_map_set_requested_dimension(ctx,&fi->map,dimension->name,value);
GC_CHECK_ERROR(ctx);
}
}
Expand Down

0 comments on commit 4df71c3

Please sign in to comment.