Skip to content
Permalink
Browse files

some ruleset refactoring and cleanup

  • Loading branch information
tomhel committed May 9, 2016
1 parent 19e171b commit 6d1e843217f3cb40760377ca51d887656e05ef08
Showing with 105 additions and 84 deletions.
  1. +24 −3 include/mapcache.h
  2. +21 −65 lib/cache.c
  3. +6 −9 lib/configuration_xml.c
  4. +54 −7 lib/ruleset.c
@@ -1324,21 +1324,42 @@ mapcache_ruleset* mapcache_ruleset_create(apr_pool_t *pool);
* \brief allocate and initialize a new rule
* @param pool
*/
mapcache_rule* mapcache_rule_create(apr_pool_t *pool);
mapcache_rule* mapcache_ruleset_rule_create(apr_pool_t *pool);

/**
* \brief clone a rule
* @param pool
* @param rule
*/
mapcache_rule* mapcache_rule_clone(apr_pool_t *pool, mapcache_rule *rule);
mapcache_rule* mapcache_ruleset_rule_clone(apr_pool_t *pool, mapcache_rule *rule);

/**
* \brief get rule for zoom level, or NULL if none exist
* @param ruleset
* @param zoom_level
*/
mapcache_rule* mapcache_rule_get(mapcache_ruleset *ruleset, int zoom_level);
mapcache_rule* mapcache_ruleset_rule_find(apr_array_header_t *rules, int zoom_level);

/**
* \brief get rule at index, or NULL if none exist
* @param rules
* @param idx
*/
mapcache_rule* mapcache_ruleset_rule_get(apr_array_header_t *rules, int idx);

/**
* \brief check if tile is within visible extent
* @param rule
* @param tile
*/
int mapcache_ruleset_is_visible_tile(mapcache_rule* rule, mapcache_tile *tile);

/**
* \brief check if tile is readonly
* @param rule
* @param tile
*/
int mapcache_ruleset_is_readonly_tile(mapcache_rule* rule, mapcache_tile *tile);


/* in grid.c */
@@ -28,67 +28,23 @@
#include "mapcache.h"
#include <apr_time.h>

mapcache_rule* mapcache_cache_get_rule(mapcache_tile *tile) {
/* get rule for tile if available */
apr_array_header_t *rules = tile->grid_link->rules;
mapcache_rule *rule;

if(!rules) {
return NULL;
}

rule = APR_ARRAY_IDX(rules, tile->z, mapcache_rule*);
return rule;
}

int mapcache_cache_is_visible_tile(mapcache_tile *tile, mapcache_rule* rule) {
/* check if tile is within visible extent */
if(!rule) {
return MAPCACHE_TRUE;
}

if(!rule->visible_limits) {
return MAPCACHE_TRUE;
}

if(tile->x < rule->visible_limits->minx || tile->y < rule->visible_limits->miny ||
tile->x > rule->visible_limits->maxx || tile->y > rule->visible_limits->maxy) {
return MAPCACHE_FALSE;
}

return MAPCACHE_TRUE;
}

int mapcache_cache_is_readonly_tile(mapcache_tile *tile, mapcache_rule* rule) {
/* check if tile is tile readonly */
if(!rule) {
return MAPCACHE_FALSE;
}

if(rule->readonly) {
return MAPCACHE_TRUE;
}

return MAPCACHE_FALSE;
}

int mapcache_cache_tile_get(mapcache_context *ctx, mapcache_cache *cache, mapcache_tile *tile) {
int i,rv;
mapcache_rule *rule = mapcache_cache_get_rule(tile);
mapcache_rule *rule = mapcache_ruleset_rule_get(tile->grid_link->rules, tile->z);
#ifdef DEBUG
ctx->log(ctx,MAPCACHE_DEBUG,"calling tile_get on cache (%s): (tileset=%s, grid=%s, z=%d, x=%d, y=%d",cache->name,tile->tileset->name,tile->grid_link->grid->name,tile->z,tile->x, tile->y);
#endif

/* if tile is outside visible extent, create a blank tile and return */
if (mapcache_cache_is_visible_tile(tile, rule) == MAPCACHE_FALSE) {
int tile_sx, tile_sy;
tile_sx = tile->grid_link->grid->tile_sx;
tile_sy = tile->grid_link->grid->tile_sy;
tile->encoded_data = tile->tileset->format->create_empty_image(ctx, tile->tileset->format, tile_sx, tile_sy, rule->hidden_color);
if(GC_HAS_ERROR(ctx)) {
return MAPCACHE_FAILURE;
}
return MAPCACHE_SUCCESS;
if (mapcache_ruleset_is_visible_tile(rule, tile) == MAPCACHE_FALSE) {
int tile_sx, tile_sy;
tile_sx = tile->grid_link->grid->tile_sx;
tile_sy = tile->grid_link->grid->tile_sy;
tile->encoded_data = tile->tileset->format->create_empty_image(ctx, tile->tileset->format, tile_sx, tile_sy, rule->hidden_color);
if(GC_HAS_ERROR(ctx)) {
return MAPCACHE_FAILURE;
}
return MAPCACHE_SUCCESS;
}

for(i=0;i<=cache->retry_count;i++) {
@@ -112,13 +68,13 @@ int mapcache_cache_tile_get(mapcache_context *ctx, mapcache_cache *cache, mapcac

void mapcache_cache_tile_delete(mapcache_context *ctx, mapcache_cache *cache, mapcache_tile *tile) {
int i;
mapcache_rule *rule = mapcache_cache_get_rule(tile);
mapcache_rule *rule = mapcache_ruleset_rule_get(tile->grid_link->rules, tile->z);
#ifdef DEBUG
ctx->log(ctx,MAPCACHE_DEBUG,"calling tile_delete on cache (%s): (tileset=%s, grid=%s, z=%d, x=%d, y=%d",cache->name,tile->tileset->name,tile->grid_link->grid->name,tile->z,tile->x, tile->y);
#endif

/* if tile is readonly, return */
if (mapcache_cache_is_readonly_tile(tile, rule) == MAPCACHE_TRUE) {
/* if tile has a readonly rule, return */
if (mapcache_ruleset_is_readonly_tile(rule, tile) == MAPCACHE_TRUE) {
return;
}

@@ -144,14 +100,14 @@ void mapcache_cache_tile_delete(mapcache_context *ctx, mapcache_cache *cache, ma

int mapcache_cache_tile_exists(mapcache_context *ctx, mapcache_cache *cache, mapcache_tile *tile) {
int i,rv;
mapcache_rule *rule = mapcache_cache_get_rule(tile);
mapcache_rule *rule = mapcache_ruleset_rule_get(tile->grid_link->rules, tile->z);
#ifdef DEBUG
ctx->log(ctx,MAPCACHE_DEBUG,"calling tile_exists on cache (%s): (tileset=%s, grid=%s, z=%d, x=%d, y=%d",cache->name,tile->tileset->name,tile->grid_link->grid->name,tile->z,tile->x, tile->y);
#endif

/* if tile is outside visible limits return TRUE
a blank tile will be returned on subsequent get call on cache */
if (mapcache_cache_is_visible_tile(tile, rule) == MAPCACHE_FALSE) {
if (mapcache_ruleset_is_visible_tile(rule, tile) == MAPCACHE_FALSE) {
return MAPCACHE_TRUE;
}

@@ -176,13 +132,13 @@ int mapcache_cache_tile_exists(mapcache_context *ctx, mapcache_cache *cache, map

void mapcache_cache_tile_set(mapcache_context *ctx, mapcache_cache *cache, mapcache_tile *tile) {
int i;
mapcache_rule *rule = mapcache_cache_get_rule(tile);
mapcache_rule *rule = mapcache_ruleset_rule_get(tile->grid_link->rules, tile->z);
#ifdef DEBUG
ctx->log(ctx,MAPCACHE_DEBUG,"calling tile_set on cache (%s): (tileset=%s, grid=%s, z=%d, x=%d, y=%d",cache->name,tile->tileset->name,tile->grid_link->grid->name,tile->z,tile->x, tile->y);
#endif

/* if tile is readonly, return */
if (mapcache_cache_is_readonly_tile(tile, rule) == MAPCACHE_TRUE) {
/* if tile has a readonly rule, return */
if (mapcache_ruleset_is_readonly_tile(rule, tile) == MAPCACHE_TRUE) {
return;
}

@@ -213,10 +169,10 @@ void mapcache_cache_tile_multi_set(mapcache_context *ctx, mapcache_cache *cache,
tiles[0].z,tiles[0].x, tiles[0].y);
#endif

/* if any tile is readonly, return */
/* if any tile has a readonly rule, return */
for(i = 0; i < ntiles; i++) {
mapcache_rule *rule = mapcache_cache_get_rule(tiles+i);
if (mapcache_cache_is_readonly_tile(tiles+i, rule) == MAPCACHE_TRUE) {
mapcache_rule *rule = mapcache_ruleset_rule_get((tiles+i)->grid_link->rules, (tiles+i)->z);
if (mapcache_ruleset_is_readonly_tile(rule, tiles+i) == MAPCACHE_TRUE) {
return;
}
}
@@ -179,24 +179,21 @@ void parseRuleset(mapcache_context *ctx, ezxml_t node, mapcache_cfg *config)

/* parse rules */
for(cur_node = ezxml_child(node,"rule"), i = 0; cur_node; cur_node = cur_node->next, i++) {
mapcache_rule *rule = mapcache_rule_create(ctx->pool);
mapcache_rule *rule = mapcache_ruleset_rule_create(ctx->pool);
ezxml_t visible_extent = ezxml_child(cur_node, "visible_extent");
ezxml_t readonly = ezxml_child(cur_node, "readonly");
char* zoom_level = (char*)ezxml_attr(cur_node, "zoom_level");

if(zoom_level && *zoom_level) {
int j;
int z = atoi(zoom_level);
if (z < 0) {
ctx->set_error(ctx, 400, "zoom_level is negative in rule %d", i+1);
return;
}
/* check for duplicate rule for this zoom level */
for (j = 0; j < i; j++) {
if(APR_ARRAY_IDX(ruleset->rules, j, mapcache_rule*)->zoom_level == z) {
ctx->set_error(ctx, 400, "found duplicate rule for zoom_level %d", z);
return;
}
if(mapcache_ruleset_rule_find(ruleset->rules, z) != NULL) {
ctx->set_error(ctx, 400, "found duplicate rule for zoom_level %d", z);
return;
}
rule->zoom_level = z;
} else {
@@ -792,10 +789,10 @@ void parseTileset(mapcache_context *ctx, ezxml_t node, mapcache_cfg *config)
gridlink->rules = apr_array_make(ctx->pool,grid->nlevels,sizeof(mapcache_rule*));

for(i = 0; i < grid->nlevels; i++) {
mapcache_rule *rule = mapcache_rule_get(ruleset, i);
mapcache_rule *rule = mapcache_ruleset_rule_find(ruleset->rules, i);

if(rule) {
mapcache_rule *rule_clone = mapcache_rule_clone(ctx->pool, rule);
mapcache_rule *rule_clone = mapcache_ruleset_rule_clone(ctx->pool, rule);
if(rule->visible_extent) {
mapcache_grid_compute_limits_at_level(grid,rule_clone->visible_extent,rule_clone->visible_limits,tolerance,i);
}
@@ -42,7 +42,7 @@ mapcache_ruleset* mapcache_ruleset_create(apr_pool_t *pool)
/*
* allocate and initialize a new rule
*/
mapcache_rule* mapcache_rule_create(apr_pool_t *pool)
mapcache_rule* mapcache_ruleset_rule_create(apr_pool_t *pool)
{
mapcache_rule* rule = (mapcache_rule*)apr_pcalloc(pool, sizeof(mapcache_rule));
rule->zoom_level = -1;
@@ -56,9 +56,9 @@ mapcache_rule* mapcache_rule_create(apr_pool_t *pool)
/*
* clone a rule
*/
mapcache_rule* mapcache_rule_clone(apr_pool_t *pool, mapcache_rule *rule)
mapcache_rule* mapcache_ruleset_rule_clone(apr_pool_t *pool, mapcache_rule *rule)
{
mapcache_rule* clone = mapcache_rule_create(pool);
mapcache_rule* clone = mapcache_ruleset_rule_create(pool);

clone->zoom_level = rule->zoom_level;
clone->hidden_color = rule->hidden_color;
@@ -78,16 +78,63 @@ mapcache_rule* mapcache_rule_clone(apr_pool_t *pool, mapcache_rule *rule)
}

/*
* get rule for zoom level, or NULL if none exist
* find rule for zoom level, or NULL if none exist
*/
mapcache_rule* mapcache_rule_get(mapcache_ruleset *ruleset, int zoom_level)
mapcache_rule* mapcache_ruleset_rule_find(apr_array_header_t *rules, int zoom_level)
{
int i;
mapcache_rule* rule;
for(i = 0; i < ruleset->rules->nelts; i++) {
if ((rule = APR_ARRAY_IDX(ruleset->rules, i, mapcache_rule*))->zoom_level == zoom_level) {

if (!rules) {
return NULL;
}

for(i = 0; i < rules->nelts; i++) {
if ((rule = APR_ARRAY_IDX(rules, i, mapcache_rule*))->zoom_level == zoom_level) {
return rule;
}
}
return NULL;
}

/*
* get rule at index, or NULL if none exist
*/
mapcache_rule* mapcache_ruleset_rule_get(apr_array_header_t *rules, int idx)
{
mapcache_rule *rule;

if(!rules || idx < 0 || idx >= rules->nelts) {
return NULL;
}

rule = APR_ARRAY_IDX(rules, idx, mapcache_rule*);
return rule;
}

/*
* check if tile is within visible extent
*/
int mapcache_ruleset_is_visible_tile(mapcache_rule* rule, mapcache_tile *tile) {
if(!rule || !rule->visible_limits) {
return MAPCACHE_TRUE;
}

if(tile->x < rule->visible_limits->minx || tile->y < rule->visible_limits->miny ||
tile->x > rule->visible_limits->maxx || tile->y > rule->visible_limits->maxy) {
return MAPCACHE_FALSE;
}

return MAPCACHE_TRUE;
}

/*
* check if tile is readonly
*/
int mapcache_ruleset_is_readonly_tile(mapcache_rule* rule, mapcache_tile *tile) {
if(rule && rule->readonly) {
return MAPCACHE_TRUE;
}

return MAPCACHE_FALSE;
}

0 comments on commit 6d1e843

Please sign in to comment.
You can’t perform that action at this time.