Prefetch

Topics: Windows Forms
Jan 5, 2012 at 11:41 PM

Hi Radioman,

I have created a winform application to simplify prefetch of maps. (v5)

GMaps.Instance.Mode = AccessMode.ServerOnly;               

for (int i = Convert.ToInt32(textBoxZoomFrom.Text); i <= Convert.ToInt32(textBoxZoomTo.Text); i++)               

{                   

TilePrefetcher obj = new TilePrefetcher();                   

obj.Owner = this;                   

obj.ShowCompleteMessage = false;                   

obj.Start(area, i, map.MapProvider, 100);               

}

When I "disconnect" by using: GMaps.Instance.Mode = AccessMode.CacheOnly;

I do not have the maps on the zoom levels I just prefetched.

I tried to compile GMap.NET.Core and WindowsForms from the december source code. Tried it on DemoWindowsForm from Dec20 source code. It does not work (cache is not prefetched)...

Any idea what I did wrong?

Thanks

Jan 6, 2012 at 1:39 AM

public PureImage GetImageFrom(GMapProvider provider, GPoint pos, int zoom, out Exception result)
      {
         PureImage ret = null;
         result = null;

         try
         {
            var rtile = new RawTile(provider.DbId, pos, zoom);

            // let't check memmory first
            if(UseMemoryCache)
            {
               var m = MemoryCache.GetTileFromMemoryCache(rtile);
               if(m != null)
               {
                  if(GMapProvider.TileImageProxy != null)
                  {
                     ret = GMapProvider.TileImageProxy.FromArray(m);
                     if(ret == null)
                     {
#if DEBUG
                        Debug.WriteLine("Image disposed in MemoryCache o.O, should never happen ;} " + new RawTile(provider.DbId, pos, zoom));
                        if(Debugger.IsAttached)
                        {
                           Debugger.Break();
                        }
#endif
                        m = null;
                     }
                  }
               }
            }

            if(ret == null)
            {
               if(Mode != AccessMode.ServerOnly)
               {
                  if(PrimaryCache != null)
                  {
                     // hold writer for 5s
                     if(CacheOnIdleRead)
                     {
                        Interlocked.Exchange(ref readingCache, 5);
                     }

                     ret = PrimaryCache.GetImageFromCache(provider.DbId, pos, zoom);
                     if(ret != null)
                     {
                        if(UseMemoryCache)
                        {
                           MemoryCache.AddTileToMemoryCache(rtile, ret.Data.GetBuffer());
                        }
                        return ret;
                     }
                  }

                  if(SecondaryCache != null)
                  {
                     // hold writer for 5s
                     if(CacheOnIdleRead)
                     {
                        Interlocked.Exchange(ref readingCache, 5);
                     }

                     ret = SecondaryCache.GetImageFromCache(provider.DbId, pos, zoom);
                     if(ret != null)
                     {
                        if(UseMemoryCache)
                        {
                           MemoryCache.AddTileToMemoryCache(rtile, ret.Data.GetBuffer());
                        }
                        EnqueueCacheTask(new CacheQueueItem(rtile, ret.Data.GetBuffer(), CacheUsage.First));
                        return ret;
                     }
                  }
               }

               if(Mode != AccessMode.CacheOnly)
               {
                  ret = provider.GetTileImage(pos, zoom);
                  {
                     // Enqueue Cache
                     if(ret != null)
                     {
                        if(UseMemoryCache)
                        {
                           MemoryCache.AddTileToMemoryCache(rtile, ret.Data.GetBuffer());
                        }

                        if(Mode != AccessMode.ServerOnly)
                        {
                           EnqueueCacheTask(new CacheQueueItem(rtile, ret.Data.GetBuffer(), CacheUsage.Both));
                        }
                     }
                  }
               }
               else
               {
                  result = noDataException;
               }
            }
         }
         catch(Exception ex)
         {
            result = ex;
            ret = null;
            Debug.WriteLine("GetImageFrom: " + ex.ToString());
         }

         return ret;
      }

      readonly Exception noDataException = new Exception("No data in local tile cache...");
   }

the code used to explain how to prefetch the tiles in Source Code in the GMaps class

I also study the prefetcher class recently

It may be usefull for you.

Thanks

 

Jan 6, 2012 at 11:13 AM

Hi chinsanyl,

Thanks for your code but I do not see how it will help me as the cache doesn't work when I compile the source (demowindowsform) myself.

I would rather suspect a configuration parameter is missing somewhere.

By the way, where is the cache folder located in windows 7?

Thanks.

Jan 9, 2012 at 1:09 PM

demos use the same code, and they work just fine, you just have to wait until all data is written to your  disk

Jan 10, 2012 at 11:42 AM

Hi Radioman,

What do you mean by "wait"?

Does this have something to do with "100" in obj.Start(area, i, map.MapProvider, 100);  ?

The prefetch process will do jump to the next tile until the image of the actual tile is complete, right?

In that case, what is the "100" for?

Thanks

Jan 10, 2012 at 12:05 PM

downloading tiles is done one by one, but caching occurs asynchronously, meaning you can download faster than saving thus needing more time, i'll modify demos so it will be clear when it's done

Dec 17, 2012 at 9:10 AM

well, I look into the source code and found that besides the prefecth, there are two cache classes,MsSQLPureImageCache and SQLitePureImageCache, how to use them to cache the map?

I also have a question about the prefecth method, it is hours to cache the map,it is so slowly! Before caching I changed the CacheLocation, then I cache the map,after that, how to access the cached map?

Dec 17, 2012 at 1:00 PM

just use that cache location

Dec 18, 2012 at 12:43 AM

well, radioman can you tell me about the reason for just use the cache location? If that ,tell me about how to access the cache map in my define location.