champlain-memory-cache.c 12.8 KB
Newer Older
1
/*
Jiří Techet's avatar
Jiří Techet committed
2
 * Copyright (C) 2010-2013 Jiri Techet <techet@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

Jiří Techet's avatar
Jiří Techet committed
19 20 21 22 23 24 25
/**
 * SECTION:champlain-memory-cache
 * @short_description: Stores and loads cached tiles from the memory
 *
 * #ChamplainMemoryCache is a cache that stores and retrieves tiles from the
 * memory. The cache contents is not preserved between application restarts
 * so this cache serves mostly as a quick access temporary cache to the
Jiří Techet's avatar
Jiří Techet committed
26
 * most recently used tiles.
Jiří Techet's avatar
Jiří Techet committed
27 28
 */

29 30 31 32 33 34 35 36
#define DEBUG_FLAG CHAMPLAIN_DEBUG_CACHE
#include "champlain-debug.h"

#include "champlain-memory-cache.h"

#include <glib.h>
#include <string.h>

37 38 39 40 41 42
struct _ChamplainMemoryCachePrivate
{
  guint size_limit;
  GQueue *queue;
  GHashTable *hash_table;
};
43

44
G_DEFINE_TYPE_WITH_PRIVATE (ChamplainMemoryCache, champlain_memory_cache, CHAMPLAIN_TYPE_TILE_CACHE)
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

enum
{
  PROP_0,
  PROP_SIZE_LIMIT
};

typedef struct
{
  gchar *key;
  gchar *data;
  guint size;
} QueueMember;


static void fill_tile (ChamplainMapSource *map_source,
    ChamplainTile *tile);

static void store_tile (ChamplainTileCache *tile_cache,
    ChamplainTile *tile,
    const gchar *contents,
    gsize size);
static void refresh_tile_time (ChamplainTileCache *tile_cache,
    ChamplainTile *tile);
static void on_tile_filled (ChamplainTileCache *tile_cache,
    ChamplainTile *tile);


static void
champlain_memory_cache_get_property (GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  ChamplainMemoryCache *memory_cache = CHAMPLAIN_MEMORY_CACHE (object);

  switch (property_id)
    {
    case PROP_SIZE_LIMIT:
      g_value_set_uint (value, champlain_memory_cache_get_size_limit (memory_cache));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}


static void
champlain_memory_cache_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  ChamplainMemoryCache *memory_cache = CHAMPLAIN_MEMORY_CACHE (object);

  switch (property_id)
    {
    case PROP_SIZE_LIMIT:
      champlain_memory_cache_set_size_limit (memory_cache, g_value_get_uint (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}


static void
champlain_memory_cache_dispose (GObject *object)
{
  G_OBJECT_CLASS (champlain_memory_cache_parent_class)->dispose (object);
}


static void
champlain_memory_cache_finalize (GObject *object)
{
  ChamplainMemoryCache *memory_cache = CHAMPLAIN_MEMORY_CACHE (object);

125
  champlain_memory_cache_clean (memory_cache);
126
  g_queue_free (memory_cache->priv->queue);
127
  g_hash_table_destroy (memory_cache->priv->hash_table);
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

  G_OBJECT_CLASS (champlain_memory_cache_parent_class)->finalize (object);
}


static void
champlain_memory_cache_class_init (ChamplainMemoryCacheClass *klass)
{
  ChamplainMapSourceClass *map_source_class = CHAMPLAIN_MAP_SOURCE_CLASS (klass);
  ChamplainTileCacheClass *tile_cache_class = CHAMPLAIN_TILE_CACHE_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GParamSpec *pspec;

  object_class->finalize = champlain_memory_cache_finalize;
  object_class->dispose = champlain_memory_cache_dispose;
  object_class->get_property = champlain_memory_cache_get_property;
  object_class->set_property = champlain_memory_cache_set_property;

Jiří Techet's avatar
Jiří Techet committed
146
  /**
147 148
   * ChamplainMemoryCache:size-limit:
   *
149
   * The maximum number of tiles that are stored in the cache.
150
   *
Jiří Techet's avatar
Jiří Techet committed
151
   * Since: 0.8
152 153
   */
  pspec = g_param_spec_uint ("size-limit",
Jiří Techet's avatar
Jiří Techet committed
154 155 156 157 158 159
        "Size Limit",
        "Maximal number of stored tiles",
        1,
        G_MAXINT,
        100,
        G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
160 161 162 163 164 165 166 167 168 169
  g_object_class_install_property (object_class, PROP_SIZE_LIMIT, pspec);

  tile_cache_class->store_tile = store_tile;
  tile_cache_class->refresh_tile_time = refresh_tile_time;
  tile_cache_class->on_tile_filled = on_tile_filled;

  map_source_class->fill_tile = fill_tile;
}


Jiří Techet's avatar
Jiří Techet committed
170 171
/**
 * champlain_memory_cache_new_full:
172
 * @size_limit: maximum number of tiles stored in the cache
Jiří Techet's avatar
Jiří Techet committed
173 174 175 176 177 178 179 180
 * @renderer: the #ChamplainRenderer used for tiles rendering
 *
 * Constructor of #ChamplainMemoryCache.
 *
 * Returns: a constructed #ChamplainMemoryCache
 *
 * Since: 0.8
 */
181
ChamplainMemoryCache *
182 183
champlain_memory_cache_new_full (guint size_limit,
    ChamplainRenderer *renderer)
184 185 186 187
{
  ChamplainMemoryCache *cache;

  cache = g_object_new (CHAMPLAIN_TYPE_MEMORY_CACHE,
Jiří Techet's avatar
Jiří Techet committed
188 189 190
        "size-limit", size_limit,
        "renderer", renderer,
        NULL);
191 192 193 194 195 196 197 198

  return cache;
}


static void
champlain_memory_cache_init (ChamplainMemoryCache *memory_cache)
{
199
  ChamplainMemoryCachePrivate *priv = champlain_memory_cache_get_instance_private (memory_cache);
200 201 202 203

  memory_cache->priv = priv;

  priv->queue = g_queue_new ();
204
  priv->hash_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
205 206 207
}


Jiří Techet's avatar
Jiří Techet committed
208 209 210 211
/**
 * champlain_memory_cache_get_size_limit:
 * @memory_cache: a #ChamplainMemoryCache
 *
212
 * Gets the maximum number of tiles stored in the cache.
Jiří Techet's avatar
Jiří Techet committed
213
 *
214
 * Returns: maximum number of stored tiles
Jiří Techet's avatar
Jiří Techet committed
215 216 217
 *
 * Since: 0.8
 */
218 219 220 221 222 223 224 225 226
guint
champlain_memory_cache_get_size_limit (ChamplainMemoryCache *memory_cache)
{
  g_return_val_if_fail (CHAMPLAIN_IS_MEMORY_CACHE (memory_cache), 0);

  return memory_cache->priv->size_limit;
}


Jiří Techet's avatar
Jiří Techet committed
227 228 229
/**
 * champlain_memory_cache_set_size_limit:
 * @memory_cache: a #ChamplainMemoryCache
230
 * @size_limit: maximum number of tiles stored in the cache
Jiří Techet's avatar
Jiří Techet committed
231
 *
232
 * Sets the maximum number of tiles stored in the cache.
Jiří Techet's avatar
Jiří Techet committed
233 234 235
 *
 * Since: 0.8
 */
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
void
champlain_memory_cache_set_size_limit (ChamplainMemoryCache *memory_cache,
    guint size_limit)
{
  g_return_if_fail (CHAMPLAIN_IS_MEMORY_CACHE (memory_cache));

  ChamplainMemoryCachePrivate *priv = memory_cache->priv;

  priv->size_limit = size_limit;
  g_object_notify (G_OBJECT (memory_cache), "size-limit");
}


static gchar *
generate_queue_key (ChamplainMemoryCache *memory_cache,
    ChamplainTile *tile)
{
  g_return_val_if_fail (CHAMPLAIN_IS_MEMORY_CACHE (memory_cache), NULL);
  g_return_val_if_fail (CHAMPLAIN_IS_TILE (tile), NULL);

  ChamplainMapSource *map_source = CHAMPLAIN_MAP_SOURCE (memory_cache);
257
  gchar *key;
258

259
  key = g_strdup_printf ("%d/%d/%d/%s",
Jiří Techet's avatar
Jiří Techet committed
260 261 262 263
        champlain_tile_get_zoom_level (tile),
        champlain_tile_get_x (tile),
        champlain_tile_get_y (tile),
        champlain_map_source_get_id (map_source));
264
  return key;
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
}


static void
move_queue_member_to_head (GQueue *queue, GList *link)
{
  g_queue_unlink (queue, link);
  g_queue_push_head_link (queue, link);
}


static void
delete_queue_member (QueueMember *member, gpointer user_data)
{
  if (member)
    {
      g_free (member->key);
      g_free (member->data);
283
      g_slice_free (QueueMember, member);
284 285 286 287 288 289
    }
}


static void
tile_rendered_cb (ChamplainTile *tile,
290 291 292
    gpointer data,
    guint size,
    gboolean error,
293
    ChamplainMapSource *map_source)
294
{
295 296 297
  ChamplainMapSource *next_source;

  g_signal_handlers_disconnect_by_func (tile, tile_rendered_cb, map_source);
298

299
  next_source = champlain_map_source_get_next_source (map_source);
300

301
  if (!error)
302 303
    {
      if (CHAMPLAIN_IS_TILE_CACHE (next_source))
Jiří Techet's avatar
Jiří Techet committed
304
        champlain_tile_cache_on_tile_filled (CHAMPLAIN_TILE_CACHE (next_source), tile);
305

306
      champlain_tile_set_fade_in (tile, FALSE);
307 308 309
      champlain_tile_set_state (tile, CHAMPLAIN_STATE_DONE);
      champlain_tile_display_content (tile);
    }
310
  else if (next_source)
311 312
    champlain_map_source_fill_tile (next_source, tile);

313
  g_object_unref (map_source);
314
  g_object_unref (tile);
315 316 317 318 319 320 321 322 323 324 325 326
}


static void
fill_tile (ChamplainMapSource *map_source,
    ChamplainTile *tile)
{
  g_return_if_fail (CHAMPLAIN_IS_MEMORY_CACHE (map_source));
  g_return_if_fail (CHAMPLAIN_IS_TILE (tile));

  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);

327 328 329
  if (champlain_tile_get_state (tile) == CHAMPLAIN_STATE_DONE)
    return;

330 331 332 333 334 335
  if (champlain_tile_get_state (tile) != CHAMPLAIN_STATE_LOADED)
    {
      ChamplainMemoryCache *memory_cache = CHAMPLAIN_MEMORY_CACHE (map_source);
      ChamplainMemoryCachePrivate *priv = memory_cache->priv;
      ChamplainRenderer *renderer;
      GList *link;
336
      gchar *key;
337

338 339 340
      key = generate_queue_key (memory_cache, tile);
      link = g_hash_table_lookup (priv->hash_table, key);
      g_free (key);
341 342 343 344 345 346 347 348 349 350
      if (link)
        {
          QueueMember *member = link->data;

          move_queue_member_to_head (priv->queue, link);

          renderer = champlain_map_source_get_renderer (map_source);

          g_return_if_fail (CHAMPLAIN_IS_RENDERER (renderer));

351 352
          g_object_ref (map_source);
          g_object_ref (tile);
353

354
          g_signal_connect (tile, "render-complete", G_CALLBACK (tile_rendered_cb), map_source);
355

356
          champlain_renderer_set_data (renderer, (guint8*) member->data, member->size);
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
          champlain_renderer_render (renderer, tile);

          return;
        }
    }

  if (CHAMPLAIN_IS_MAP_SOURCE (next_source))
    champlain_map_source_fill_tile (next_source, tile);
  else if (champlain_tile_get_state (tile) == CHAMPLAIN_STATE_LOADED)
    {
      /* if we have some content, use the tile even if it wasn't validated */
      champlain_tile_set_state (tile, CHAMPLAIN_STATE_DONE);
      champlain_tile_display_content (tile);
    }
}


static void
store_tile (ChamplainTileCache *tile_cache,
    ChamplainTile *tile,
    const gchar *contents,
    gsize size)
{
  g_return_if_fail (CHAMPLAIN_IS_MEMORY_CACHE (tile_cache));

382 383
  ChamplainMapSource *map_source = CHAMPLAIN_MAP_SOURCE (tile_cache);
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
384 385 386
  ChamplainMemoryCache *memory_cache = CHAMPLAIN_MEMORY_CACHE (tile_cache);
  ChamplainMemoryCachePrivate *priv = memory_cache->priv;
  GList *link;
387
  gchar *key;
388

389 390
  key = generate_queue_key (memory_cache, tile);
  link = g_hash_table_lookup (priv->hash_table, key);
391
  if (link)
392 393 394 395
    {
      move_queue_member_to_head (priv->queue, link);
      g_free (key);
    }
396 397 398 399 400 401 402
  else
    {
      QueueMember *member;

      if (priv->queue->length >= priv->size_limit)
        {
          member = g_queue_pop_tail (priv->queue);
403
          g_hash_table_remove (priv->hash_table, member->key);
404 405 406
          delete_queue_member (member, NULL);
        }

407
      member = g_slice_new (QueueMember);
408
      member->key = key;
409 410 411 412
      member->data = g_memdup (contents, size);
      member->size = size;

      g_queue_push_head (priv->queue, member);
413
      g_hash_table_insert (priv->hash_table, g_strdup (key), g_queue_peek_head_link (priv->queue));
414
    }
415

416 417
  if (CHAMPLAIN_IS_TILE_CACHE (next_source))
    champlain_tile_cache_store_tile (CHAMPLAIN_TILE_CACHE (next_source), tile, contents, size);
418 419 420 421 422 423 424
}


static void
refresh_tile_time (ChamplainTileCache *tile_cache,
    ChamplainTile *tile)
{
425 426 427 428 429 430 431
  g_return_if_fail (CHAMPLAIN_IS_MEMORY_CACHE (tile_cache));

  ChamplainMapSource *map_source = CHAMPLAIN_MAP_SOURCE (tile_cache);
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);

  if (CHAMPLAIN_IS_TILE_CACHE (next_source))
    champlain_tile_cache_refresh_tile_time (CHAMPLAIN_TILE_CACHE (next_source), tile);
432 433 434
}


Jiří Techet's avatar
Jiří Techet committed
435 436 437 438 439 440 441 442
/**
 * champlain_memory_cache_clean:
 * @memory_cache: a #ChamplainMemoryCache
 *
 * Cleans the contents of the cache.
 *
 * Since: 0.8
 */
443 444 445 446 447 448 449
void
champlain_memory_cache_clean (ChamplainMemoryCache *memory_cache)
{
  ChamplainMemoryCachePrivate *priv = memory_cache->priv;

  g_queue_foreach (priv->queue, (GFunc) delete_queue_member, NULL);
  g_queue_clear (priv->queue);
450 451
  g_hash_table_destroy (memory_cache->priv->hash_table);
  priv->hash_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
}


static void
on_tile_filled (ChamplainTileCache *tile_cache,
    ChamplainTile *tile)
{
  g_return_if_fail (CHAMPLAIN_IS_MEMORY_CACHE (tile_cache));
  g_return_if_fail (CHAMPLAIN_IS_TILE (tile));

  ChamplainMapSource *map_source = CHAMPLAIN_MAP_SOURCE (tile_cache);
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
  ChamplainMemoryCache *memory_cache = CHAMPLAIN_MEMORY_CACHE (tile_cache);
  ChamplainMemoryCachePrivate *priv = memory_cache->priv;
  GList *link;
467
  gchar *key;
468

469 470 471
  key = generate_queue_key (memory_cache, tile);
  link = g_hash_table_lookup (priv->hash_table, key);
  g_free (key);
472 473 474 475 476 477
  if (link)
    move_queue_member_to_head (priv->queue, link);

  if (CHAMPLAIN_IS_TILE_CACHE (next_source))
    champlain_tile_cache_on_tile_filled (CHAMPLAIN_TILE_CACHE (next_source), tile);
}