[mapserver-commits] r12163 - in trunk/mapserver/mapcache: include src

svn at osgeo.org svn at osgeo.org
Fri Aug 26 07:03:30 EDT 2011


Author: tbonfort
Date: 2011-08-26 04:03:29 -0700 (Fri, 26 Aug 2011)
New Revision: 12163

Modified:
   trunk/mapserver/mapcache/include/geocache.h
   trunk/mapserver/mapcache/src/cache_disk.c
   trunk/mapserver/mapcache/src/fastcgi_geocache.c
   trunk/mapserver/mapcache/src/geocache_seed.c
   trunk/mapserver/mapcache/src/mod_geocache.c
   trunk/mapserver/mapcache/src/tileset.c
Log:
fix configure script when disabling fastcgi
thomas.bonfort | 2010-12-03 13:11:02 +0100 (Fri, 03 Dec 2010)

Modified: trunk/mapserver/mapcache/include/geocache.h
===================================================================
--- trunk/mapserver/mapcache/include/geocache.h	2011-08-26 11:03:23 UTC (rev 12162)
+++ trunk/mapserver/mapcache/include/geocache.h	2011-08-26 11:03:29 UTC (rev 12163)
@@ -118,7 +118,7 @@
     * \returns GEOCACHE_LOCKED if \param nonblocking was set to 1 and another instance has already aquired
     *          the lock
     */
-   int (*global_lock_aquire)(geocache_context *ctx, int nonblocking);
+   void (*global_lock_aquire)(geocache_context *ctx, int nonblocking);
    
    /**
     * \brief release a previously aquired lock
@@ -127,7 +127,7 @@
     * calling this function after an unsuccessful call to global_lock_aquire() or from a different thread
     * or process that has called global_lock_aquire() has undefined behavior
     */
-   int (*global_lock_release)(geocache_context *ctx);
+   void (*global_lock_release)(geocache_context *ctx);
    
    apr_pool_t *pool;
    char *_errmsg;
@@ -237,6 +237,8 @@
     * \memberof geocache_cache
     */
    int (*tile_get)(geocache_context *ctx, geocache_tile *tile);
+
+   int (*tile_exists)(geocache_context *ctx, geocache_tile *tile);
    
    /**
     * set tile content to cache
@@ -676,6 +678,20 @@
  */
 void geocache_tileset_tile_bbox(geocache_tile *tile, double *bbox);
 
+
+/**
+ * \brief compute x y value for given lon/lat (dx/dy) and given zoomlevel
+ * @param ctx
+ * @param tileset
+ * @param dx
+ * @param dy
+ * @param z
+ * @param x
+ * @param y
+ */
+void geocache_tileset_get_xy(geocache_context *ctx, geocache_tileset *tileset, double dx, double dy,int z, int *x, int *y);
+
+
 /** @} */
 
 

Modified: trunk/mapserver/mapcache/src/cache_disk.c
===================================================================
--- trunk/mapserver/mapcache/src/cache_disk.c	2011-08-26 11:03:23 UTC (rev 12162)
+++ trunk/mapserver/mapcache/src/cache_disk.c	2011-08-26 11:03:29 UTC (rev 12163)
@@ -10,7 +10,6 @@
 #include <apr_strings.h>
 #include <apr_file_io.h>
 
-
 void _geocache_cache_disk_blank_tile_key(geocache_context *ctx, geocache_tile *tile, unsigned char *color, char **path) {
    *path = apr_psprintf(ctx->pool,"%s/%s/blanks/%02X%02X%02X%02X.%s",
          ((geocache_cache_disk*)tile->tileset->cache)->base_directory,
@@ -76,6 +75,16 @@
    }
 }
 
+int _geocache_cache_disk_has_tile(geocache_context *ctx, geocache_tile *tile) {
+   char *filename;
+   apr_file_t *f;
+   _geocache_cache_disk_tile_key(ctx, tile, &filename);
+   if(apr_file_open(&f, filename, APR_FOPEN_READ,APR_OS_DEFAULT, ctx->pool) == APR_SUCCESS)
+       return GEOCACHE_TRUE;
+   else
+       return GEOCACHE_FALSE;
+}
+
 /**
  * \brief lock the given tile so other processes know it is being processed
  * 
@@ -418,6 +427,7 @@
    cache->symlink_blank = 0;
    cache->cache.type = GEOCACHE_CACHE_DISK;
    cache->cache.tile_get = _geocache_cache_disk_get;
+   cache->cache.tile_exists = _geocache_cache_disk_has_tile;
    cache->cache.tile_set = _geocache_cache_disk_set;
    cache->cache.configuration_check = _geocache_cache_disk_configuration_check;
    cache->cache.configuration_parse = _geocache_cache_disk_configuration_parse;

Modified: trunk/mapserver/mapcache/src/fastcgi_geocache.c
===================================================================
--- trunk/mapserver/mapcache/src/fastcgi_geocache.c	2011-08-26 11:03:23 UTC (rev 12162)
+++ trunk/mapserver/mapcache/src/fastcgi_geocache.c	2011-08-26 11:03:29 UTC (rev 12163)
@@ -70,29 +70,26 @@
    ctx->ctx.log = fcgi_context_log;
 }
 
-int geocache_fcgi_mutex_aquire(geocache_context *r, int nonblocking) {
+void geocache_fcgi_mutex_aquire(geocache_context *gctx, int nonblocking) {
    int ret;
-   geocache_context_fcgi_request *ctx = (geocache_context_fcgi_request*)r;
+   geocache_context_fcgi_request *ctx = (geocache_context_fcgi_request*)gctx;
    ret = apr_proc_mutex_lock(ctx->ctx.mutex);
    if(ret != APR_SUCCESS) {
-      r->set_error(r, GEOCACHE_MUTEX_ERROR, "failed to aquire mutex lock");
-      return GEOCACHE_FAILURE;
+      gctx->set_error(gctx, GEOCACHE_MUTEX_ERROR, "failed to aquire mutex lock");
+      return;
    }
-   apr_pool_cleanup_register(r->pool, ctx->ctx.mutex, (void*)apr_proc_mutex_unlock, apr_pool_cleanup_null);
-
-   return GEOCACHE_SUCCESS;
+   apr_pool_cleanup_register(gctx->pool, ctx->ctx.mutex, (void*)apr_proc_mutex_unlock, apr_pool_cleanup_null);
 }
 
-int geocache_fcgi_mutex_release(geocache_context *r) {
+void geocache_fcgi_mutex_release(geocache_context *gctx) {
    int ret;
-   geocache_context_fcgi_request *ctx = (geocache_context_fcgi_request*)r;
+   geocache_context_fcgi_request *ctx = (geocache_context_fcgi_request*)gctx;
    ret = apr_proc_mutex_unlock(ctx->ctx.mutex);
    if(ret != APR_SUCCESS) {
-      r->set_error(r, GEOCACHE_MUTEX_ERROR,  "failed to release mutex");
-      return GEOCACHE_FAILURE;
+      gctx->set_error(gctx, GEOCACHE_MUTEX_ERROR,  "failed to release mutex");
+      return;
    }
-   apr_pool_cleanup_kill(r->pool, ctx->ctx.mutex, (void*)apr_proc_mutex_unlock);
-   return GEOCACHE_SUCCESS;
+   apr_pool_cleanup_kill(gctx->pool, ctx->ctx.mutex, (void*)apr_proc_mutex_unlock);
 }
 
 void init_fcgi_request_context(geocache_context_fcgi_request *ctx) {

Modified: trunk/mapserver/mapcache/src/geocache_seed.c
===================================================================
--- trunk/mapserver/mapcache/src/geocache_seed.c	2011-08-26 11:03:23 UTC (rev 12162)
+++ trunk/mapserver/mapcache/src/geocache_seed.c	2011-08-26 11:03:29 UTC (rev 12163)
@@ -1,7 +1,30 @@
 #include "geocache.h"
 #include <apr_thread_proc.h>
+#include <apr_thread_mutex.h>
 #include <apr_getopt.h>
 
+
+typedef struct geocache_context_seeding geocache_context_seeding;
+struct geocache_context_seeding{
+    geocache_context ctx;
+    int (*get_next_tile)(geocache_context_seeding *ctx, geocache_tile *tile, geocache_context *tmpctx);
+    apr_thread_mutex_t *mutex;
+    geocache_tileset *tileset;
+    int minzoom;
+    int maxzoom;
+    double *extent;
+    int nextx,nexty,nextz;
+};
+
+typedef struct {
+    int firstx;
+    int firsty;
+    int lastx;
+    int lasty;
+} gc_tiles_for_zoom;
+
+gc_tiles_for_zoom seed_tiles[100]; //TODO: bug here if more than 100 zoomlevels, or if multithreaded usage on multiple tilesets
+
 static const apr_getopt_option_t seed_options[] = {
     /* long-option, short-option, has-arg flag, description */
     { "config", 'c', TRUE, "configuration file"},
@@ -13,13 +36,139 @@
     { NULL, 0, 0, NULL },
 };
 
-static void* APR_THREAD_FUNC doseed(apr_thread_t *thd, void *data) {
-    int thread_id = (int)data;
-    int i;
-    for(i=0;i<10;i++) {
-        printf("thread %d: %d\n",thread_id,i);
+void geocache_context_seeding_lock_aquire(geocache_context *gctx, int blocking) {
+    int ret;
+    geocache_context_seeding *ctx = (geocache_context_seeding*)gctx;
+    ret = apr_thread_mutex_lock(ctx->mutex);
+    if(ret != APR_SUCCESS) {
+        gctx->set_error(gctx,GEOCACHE_MUTEX_ERROR, "failed to lock mutex");
+        return;
     }
-    apr_thread_exit(thd,APR_SUCCESS);
+    apr_pool_cleanup_register(gctx->pool, ctx->mutex, (void*)apr_thread_mutex_unlock, apr_pool_cleanup_null);
+}
+
+void geocache_context_seeding_lock_release(geocache_context *gctx) {
+    int ret;
+    geocache_context_seeding *ctx = (geocache_context_seeding*)gctx;
+    ret = apr_thread_mutex_unlock(ctx->mutex);
+    if(ret != APR_SUCCESS) {
+        gctx->set_error(gctx,GEOCACHE_MUTEX_ERROR, "failed to unlock mutex");
+        return;
+    }
+    apr_pool_cleanup_kill(gctx->pool, ctx->mutex, (void*)apr_thread_mutex_unlock);
+}
+
+void geocache_context_seeding_log(geocache_context *ctx, geocache_log_level level, char *msg, ...) {
+    va_list args;
+    va_start(args,msg);
+    vfprintf(stderr,msg,args);
+    va_end(args);
+}
+
+int tile_exists(geocache_context *ctx, geocache_tileset *tileset,
+                    int x, int y, int z,
+                    geocache_context *tmpctx) {
+    geocache_tile tile;
+    tile.x = x;
+    tile.y = y;
+    tile.z = z;
+    tile.tileset = tileset;
+    return tileset->cache->tile_exists(tmpctx,&tile);
+}
+
+int geocache_context_seeding_get_next_tile(geocache_context_seeding *ctx, geocache_tile *tile, geocache_context *tmpcontext) {
+    geocache_context *gctx= (geocache_context*)ctx;
+
+    gctx->global_lock_aquire(gctx,0);
+    if(ctx->nextz > ctx->maxzoom) {
+        gctx->global_lock_release(gctx);
+        return GEOCACHE_FAILURE;
+        //we have no tiles left to process
+    }
+    
+    tile->x = ctx->nextx;
+    tile->y = ctx->nexty;
+    tile->z = ctx->nextz;
+
+
+
+    while(1) {
+        ctx->nextx += ctx->tileset->metasize_x;
+        if(ctx->nextx > seed_tiles[tile->z].lastx) {
+            ctx->nexty += ctx->tileset->metasize_y;
+            if(ctx->nexty > seed_tiles[tile->z].lasty) {
+                ctx->nextz += 1;
+                if(ctx->nextz > ctx->maxzoom) break;
+                ctx->nexty = seed_tiles[ctx->nextz].firsty;
+            }
+            ctx->nextx = seed_tiles[ctx->nextz].firstx;
+        }
+        if(! tile_exists(gctx, ctx->tileset, ctx->nextx, ctx->nexty, ctx->nextz,tmpcontext))
+            break;
+    }
+    gctx->global_lock_release(gctx);
+    return GEOCACHE_SUCCESS;
+}
+
+void geocache_context_seeding_init(geocache_context_seeding *ctx,
+        geocache_cfg *cfg,
+        geocache_tileset *tileset,
+        int minzoom, int maxzoom,
+        double *extent) {
+    int ret;
+    geocache_context *gctx = (geocache_context*)ctx;
+    geocache_context_init(gctx);
+    ret = apr_thread_mutex_create(&ctx->mutex,APR_THREAD_MUTEX_DEFAULT,gctx->pool);
+    if(ret != APR_SUCCESS) {
+        gctx->set_error(gctx,GEOCACHE_MUTEX_ERROR,"failed to create mutex");
+        return;
+    }
+    gctx->global_lock_aquire = geocache_context_seeding_lock_aquire;
+    gctx->global_lock_release = geocache_context_seeding_lock_release;
+    gctx->log = geocache_context_seeding_log;
+    ctx->get_next_tile = geocache_context_seeding_get_next_tile;
+    ctx->extent = extent;
+    ctx->minzoom = minzoom;
+    ctx->maxzoom = maxzoom;
+    ctx->tileset = tileset;
+}
+
+void dummy_lock_aquire(geocache_context *ctx, int nonblocking){
+
+}
+
+void dummy_lock_release(geocache_context *ctx) {
+
+}
+
+void dummy_log(geocache_context *ctx, geocache_log_level level, char *msg, ...) {
+
+}
+
+static void* APR_THREAD_FUNC doseed(apr_thread_t *thread, void *data) {
+    geocache_context_seeding *ctx = (geocache_context_seeding*)data;
+    geocache_context *gctx = (geocache_context*)ctx;
+    geocache_tile *tile = geocache_tileset_tile_create(gctx->pool, ctx->tileset);
+    geocache_context tile_ctx;
+    geocache_context_init(&tile_ctx);
+    tile_ctx.global_lock_aquire = dummy_lock_aquire;
+    tile_ctx.global_lock_release = dummy_lock_release;
+    tile_ctx.log = geocache_context_seeding_log;
+    apr_pool_create(&tile_ctx.pool,NULL);
+    while(GEOCACHE_SUCCESS == ctx->get_next_tile(ctx,tile,&tile_ctx)) {
+        geocache_tileset_tile_get(&tile_ctx,tile);
+        if(tile_ctx.get_error(&tile_ctx)) {
+            gctx->set_error(gctx,tile_ctx.get_error(&tile_ctx),tile_ctx.get_error_message(&tile_ctx));
+            apr_thread_exit(thread,GEOCACHE_FAILURE);
+        }
+        apr_pool_clear(tile_ctx.pool);
+    }
+
+    if(gctx->get_error(gctx)) {
+        apr_thread_exit(thread,GEOCACHE_FAILURE);
+    }
+    
+    apr_thread_exit(thread,GEOCACHE_SUCCESS);
     return NULL;
 }
 
@@ -41,7 +190,8 @@
     const char *configfile=NULL;
     geocache_cfg *cfg = NULL;
     geocache_tileset *tileset;
-    geocache_context ctx;
+    geocache_context_seeding ctx;
+    geocache_context *gctx = (geocache_context*)&ctx;
     apr_thread_t **threads;
     apr_threadattr_t *thread_attrs;
     const char *tileset_name=NULL;
@@ -52,10 +202,10 @@
     int rv,n;
     const char *optarg;
     apr_initialize();
-    apr_pool_create_core(&ctx.pool);
-    geocache_context_init(&ctx);
-    cfg = geocache_configuration_create(ctx.pool);
-    apr_getopt_init(&opt, ctx.pool, argc, argv);
+    apr_pool_create_core(&gctx->pool);
+    geocache_context_init(gctx);
+    cfg = geocache_configuration_create(gctx->pool);
+    apr_getopt_init(&opt, gctx->pool, argc, argv);
     /* parse the all options based on opt_option[] */
     while ((rv = apr_getopt_long(opt, seed_options, &optch, &optarg)) == APR_SUCCESS) {
         switch (optch) {
@@ -72,13 +222,13 @@
                 nthreads = (int)strtol(optarg, NULL, 10);
                 break;
             case 'e':
-                if ( GEOCACHE_SUCCESS != geocache_util_extract_double_list(&ctx, (char*)optarg, ',', &extent, &n) ||
+                if ( GEOCACHE_SUCCESS != geocache_util_extract_double_list(gctx, (char*)optarg, ',', &extent, &n) ||
                         n != 4 || extent[0] >= extent[2] || extent[1] >= extent[3] ) {
                     return usage(argv[0], "failed to parse extent, expecting comma separated 4 doubles");
                 }
                 break;
             case 'z':
-                if ( GEOCACHE_SUCCESS != geocache_util_extract_int_list(&ctx, (char*)optarg, ',', &zooms, &n) ||
+                if ( GEOCACHE_SUCCESS != geocache_util_extract_int_list(gctx, (char*)optarg, ',', &zooms, &n) ||
                         n != 2 || zooms[0] >= zooms[1]) {
                     return usage(argv[0], "failed to parse zooms, expecting comma separated 2 ints");
                 }
@@ -92,9 +242,9 @@
     if( ! configfile ) {
         return usage(argv[0],"config not specified");
     } else {
-        geocache_configuration_parse(&ctx,configfile,cfg);
-        if(ctx.get_error(&ctx))
-            return usage(argv[0],ctx.get_error_message(&ctx));
+        geocache_configuration_parse(gctx,configfile,cfg);
+        if(gctx->get_error(gctx))
+            return usage(argv[0],gctx->get_error_message(gctx));
     }
 
     if( ! tileset_name ) {
@@ -104,21 +254,46 @@
         if(!tileset) {
             return usage(argv[0], "tileset not found in configuration");
         }
+        if(!zooms) {
+            zooms = (int*)apr_pcalloc(gctx->pool,2*sizeof(int));
+            zooms[0] = 1;
+            zooms[1] = tileset->levels;
+        }
+        if(!extent) {
+            extent = (double*)apr_pcalloc(gctx->pool,4*sizeof(double));
+            extent[0] = tileset->extent[0];
+            extent[1] = tileset->extent[1];
+            extent[2] = tileset->extent[2];
+            extent[3] = tileset->extent[3];
+        }
     }
 
+    geocache_context_seeding_init(&ctx,cfg,tileset,zooms[0],zooms[1],extent);
+    for(n=zooms[0];n<=zooms[1];n++) {
+        geocache_tileset_get_xy(gctx,tileset,tileset->extent[0],tileset->extent[1],
+                n,&seed_tiles[n].firstx,&seed_tiles[n].firsty);
+        geocache_tileset_get_xy(gctx,tileset,tileset->extent[2],tileset->extent[3],
+                n,&seed_tiles[n].lastx,&seed_tiles[n].lasty);
+    }
+    ctx.nextz = zooms[0];
+    ctx.nextx = seed_tiles[zooms[0]].firstx;
+    ctx.nexty = seed_tiles[zooms[0]].firsty;
+
     if( ! nthreads ) {
         return usage(argv[0],"failed to parse nthreads, must be int");
     } else {
-        apr_threadattr_create(&thread_attrs, ctx.pool);
-        threads = (apr_thread_t**)apr_pcalloc(ctx.pool, nthreads*sizeof(apr_thread_t*));
+        apr_threadattr_create(&thread_attrs, gctx->pool);
+        threads = (apr_thread_t**)apr_pcalloc(gctx->pool, nthreads*sizeof(apr_thread_t*));
         for(n=0;n<nthreads;n++) {
-            apr_thread_create(&threads[n], thread_attrs, doseed, (void*)n, ctx.pool);
+            apr_thread_create(&threads[n], thread_attrs, doseed, (void*)&ctx, gctx->pool);
         }
         for(n=0;n<nthreads;n++) {
             apr_thread_join(&rv, threads[n]);
         }
-
     }
+    if(gctx->get_error(gctx)) {
+        gctx->log(gctx,GEOCACHE_ERROR,gctx->get_error_message(gctx));
+    }
 
 
     return 0;

Modified: trunk/mapserver/mapcache/src/mod_geocache.c
===================================================================
--- trunk/mapserver/mapcache/src/mod_geocache.c	2011-08-26 11:03:23 UTC (rev 12162)
+++ trunk/mapserver/mapcache/src/mod_geocache.c	2011-08-26 11:03:29 UTC (rev 12163)
@@ -70,30 +70,27 @@
    va_end(args);
 }
 
-int geocache_util_mutex_aquire(geocache_context *r, int nonblocking) {
+void geocache_util_mutex_aquire(geocache_context *gctx, int nonblocking) {
    int ret;
-   geocache_context_apache_request *ctx = (geocache_context_apache_request*)r;
+   geocache_context_apache_request *ctx = (geocache_context_apache_request*)gctx;
    geocache_server_cfg *cfg = ap_get_module_config(ctx->request->server->module_config, &geocache_module);
    ret = apr_global_mutex_lock(cfg->mutex);
    if(ret != APR_SUCCESS) {
-      ap_log_error(APLOG_MARK, APLOG_CRIT, 0, ctx->request->server, "failed to aquire mutex lock");
-      return HTTP_INTERNAL_SERVER_ERROR;
+      gctx->set_error(gctx,GEOCACHE_MUTEX_ERROR,"failed to lock mutex");
+      return;
    }
-   apr_pool_cleanup_register(r->pool, cfg->mutex, (void*)apr_global_mutex_unlock, apr_pool_cleanup_null);
-   return GEOCACHE_SUCCESS;
+   apr_pool_cleanup_register(gctx->pool, cfg->mutex, (void*)apr_global_mutex_unlock, apr_pool_cleanup_null);
 }
 
-int geocache_util_mutex_release(geocache_context *r) {
+void geocache_util_mutex_release(geocache_context *gctx) {
    int ret;
-   geocache_context_apache_request *ctx = (geocache_context_apache_request*)r;
+   geocache_context_apache_request *ctx = (geocache_context_apache_request*)gctx;
    geocache_server_cfg *cfg = ap_get_module_config(ctx->request->server->module_config, &geocache_module);
    ret = apr_global_mutex_unlock(cfg->mutex);
    if(ret != APR_SUCCESS) {
-      ap_log_error(APLOG_MARK, APLOG_CRIT, 0, ctx->request->server, "failed to release mutex");
-      return HTTP_INTERNAL_SERVER_ERROR;
+      gctx->set_error(gctx,GEOCACHE_MUTEX_ERROR,"failed to unlock mutex");
+      return;
    }
-   apr_pool_cleanup_kill(r->pool, cfg->mutex, (void*)apr_global_mutex_unlock);
-   return GEOCACHE_SUCCESS;
 }
 
 void init_apache_request_context(geocache_context_apache_request *ctx) {

Modified: trunk/mapserver/mapcache/src/tileset.c
===================================================================
--- trunk/mapserver/mapcache/src/tileset.c	2011-08-26 11:03:23 UTC (rev 12162)
+++ trunk/mapserver/mapcache/src/tileset.c	2011-08-26 11:03:29 UTC (rev 12163)
@@ -52,6 +52,13 @@
    }
 }
 
+void geocache_tileset_get_xy(geocache_context *ctx, geocache_tileset *tileset, double dx, double dy,
+        int z, int *x, int *y) {
+    double res = tileset->resolutions[z];
+    *x = (int)((dx - tileset->extent[0]) / (res * tileset->tile_sx));
+    *y = (int)((dy - tileset->extent[1]) / (res * tileset->tile_sy));
+}
+
 /*
  * for each of the metatile's tiles, ask the underlying cache to lock it
  */
@@ -222,6 +229,7 @@
    }
    ret = tile->tileset->cache->tile_get(ctx, tile);
    GC_CHECK_ERROR(ctx);
+
    if(ret == GEOCACHE_CACHE_MISS) {
       /* the tile does not exist, we must take action before re-asking for it */
 
@@ -232,6 +240,7 @@
        * - if the lock does not exist, then this thread should do the rendering
        * - if the lock exists, we should wait for the other thread to finish
        */
+
       ctx->global_lock_aquire(ctx,0);
       GC_CHECK_ERROR(ctx);
 



More information about the mapserver-commits mailing list