1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
|
/*
* Copyright (C) 2009 Pierre-Luc Beaudoin <pierre-luc@pierlux.com>
*
* 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
*/
/**
* SECTION:champlain-cache
* @short_description: Manages cached tiles
*
* #ChamplainCache is an object to interogate the cache for previously downloaded
* tiles. ChamplainCache is a singleton, there should be only one instance shared
* by all map sources.
*
* Tiles most frequently asked gain in "popularity". This popularity will be taken
* into account when purging the cache.
*
* Unless you are implementing your own ChamplainMapSource, the only function you
* should need are #champlain_cache_purge and #champlain_cache_purge_on_idle.
*/
#include "champlain-cache.h"
#define DEBUG_FLAG CHAMPLAIN_DEBUG_CACHE
#include "champlain-debug.h"
#include "champlain-enum-types.h"
#include "champlain-private.h"
#include <errno.h>
#include <glib.h>
#include <gio/gio.h>
#include <string.h>
#include <sqlite3.h>
G_DEFINE_TYPE (ChamplainCache, champlain_cache, G_TYPE_OBJECT)
#define GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), CHAMPLAIN_TYPE_CACHE, ChamplainCachePrivate))
enum
{
PROP_0,
PROP_SIZE_LIMIT,
};
typedef struct _ChamplainCachePrivate ChamplainCachePrivate;
static ChamplainCache *instance = NULL;
struct _ChamplainCachePrivate {
guint size_limit;
sqlite3 *data;
sqlite3_stmt *stmt_select;
sqlite3_stmt *stmt_update;
GSList *popularity_queue;
guint popularity_id;
};
static gboolean inc_popularity (gpointer data);
static void delete_tile (ChamplainCache *self,
const gchar *filename);
static void
champlain_cache_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
ChamplainCache *self = CHAMPLAIN_CACHE (object);
switch (property_id)
{
case PROP_SIZE_LIMIT:
g_value_set_uint (value, champlain_cache_get_size_limit (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
champlain_cache_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
ChamplainCache *self = CHAMPLAIN_CACHE (object);
switch (property_id)
{
case PROP_SIZE_LIMIT:
champlain_cache_set_size_limit (self, g_value_get_uint (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
champlain_cache_dispose (GObject *object)
{
gint error;
ChamplainCachePrivate *priv = GET_PRIVATE (object);
if (priv->stmt_select)
{
sqlite3_finalize (priv->stmt_select);
priv->stmt_select = NULL;
}
if (priv->stmt_update)
{
sqlite3_finalize (priv->stmt_update);
priv->stmt_update = NULL;
}
if (priv->data != NULL)
{
error = sqlite3_close (priv->data);
if (error != SQLITE_OK)
DEBUG ("Sqlite returned error %d when closing cache.db", error);
priv->data = NULL;
}
G_OBJECT_CLASS (champlain_cache_parent_class)->dispose (object);
}
static void
champlain_cache_finalize (GObject *object)
{
G_OBJECT_CLASS (champlain_cache_parent_class)->finalize (object);
}
static GObject *
champlain_cache_constructor (GType type,
guint n_construct_params,
GObjectConstructParam *construct_params)
{
GObject *retval;
if (instance == NULL)
{
retval = G_OBJECT_CLASS (champlain_cache_parent_class)->constructor
(type, n_construct_params, construct_params);
instance = CHAMPLAIN_CACHE (retval);
g_object_add_weak_pointer (retval, (gpointer) &instance);
}
else
{
retval = g_object_ref (instance);
}
return retval;
}
static void
champlain_cache_class_init (ChamplainCacheClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (ChamplainCachePrivate));
object_class->constructor = champlain_cache_constructor;
object_class->get_property = champlain_cache_get_property;
object_class->set_property = champlain_cache_set_property;
object_class->dispose = champlain_cache_dispose;
object_class->finalize = champlain_cache_finalize;
/**
* ChamplainCache:size-limit:
*
* The cache size limit in bytes.
*
* Note: this new value will not be applied until you call #champlain_cache_purge
*
* Since: 0.4
*/
g_object_class_install_property (object_class,
PROP_SIZE_LIMIT,
g_param_spec_uint ("size-limit",
"Size Limit",
"The cache's size limit (Mb)",
1,
G_MAXINT,
100000000,
G_PARAM_READWRITE));
}
static void
champlain_cache_init (ChamplainCache *self)
{
gchar *path, *filename, *error_msg = NULL;
gint error;
ChamplainCachePrivate *priv = GET_PRIVATE (self);
priv->popularity_queue = NULL;
priv->popularity_id = 0;
#ifdef USE_MAEMO
filename = g_strdup_printf ("/home/user/MyDocs/.maps/champlain-cache.db");
#else
filename = g_build_filename (g_get_user_cache_dir (), "champlain",
"cache.db", NULL);
#endif
champlain_cache_set_size_limit (self, 100000000);
priv->stmt_select = NULL;
priv->stmt_update = NULL;
/* Create, if needed, the cache's dirs */
path = g_path_get_dirname (filename);
if (g_mkdir_with_parents (path, 0700) == -1)
{
if (errno != EEXIST)
{
g_warning ("Unable to create the image cache path '%s': %s",
path, g_strerror (errno));
}
}
error = sqlite3_open_v2 (filename, &priv->data,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if (error == SQLITE_ERROR)
{
DEBUG ("Sqlite returned error %d when opening cache.db", error);
goto cleanup;
}
sqlite3_exec (priv->data,
"PRAGMA synchronous=OFF;"
"PRAGMA count_changes=OFF;",
NULL, NULL, &error_msg);
if (error_msg != NULL)
{
DEBUG ("Set PRAGMA: %s", error_msg);
sqlite3_free (error_msg);
goto cleanup;
}
sqlite3_exec (priv->data,
"CREATE TABLE IF NOT EXISTS tiles ("
"filename TEXT PRIMARY KEY, "
"etag TEXT, "
"popularity INT DEFAULT 1, "
"size INT DEFAULT 0)",
NULL, NULL, &error_msg);
if (error_msg != NULL)
{
DEBUG ("Creating table 'tiles' failed: %s", error_msg);
sqlite3_free (error_msg);
goto cleanup;
}
error = sqlite3_prepare_v2 (priv->data,
"SELECT etag FROM tiles WHERE filename = ?", -1,
&priv->stmt_select, NULL);
if (error != SQLITE_OK)
{
priv->stmt_select = NULL;
DEBUG ("Failed to prepare the select Etag statement, error:%d: %s",
error, sqlite3_errmsg (priv->data));
goto cleanup;
}
error = sqlite3_prepare_v2 (priv->data,
"UPDATE tiles SET popularity = popularity + 1 WHERE filename = ?", -1,
&priv->stmt_update, NULL);
if (error != SQLITE_OK)
{
priv->stmt_update = NULL;
DEBUG ("Failed to prepare the update popularity statement, error: %s",
sqlite3_errmsg (priv->data));
goto cleanup;
}
cleanup:
g_free (filename);
g_free (path);
}
/**
* champlain_cache_dup_default:
*
* Returns: the #ChamplainCache singleton, use #g_object_unref when not neeeded
* anymore.
*
*
* Since: 0.4
*/
ChamplainCache*
champlain_cache_dup_default (void)
{
return g_object_new (CHAMPLAIN_TYPE_CACHE, NULL);
}
/**
* champlain_cache_get_size_limit:
* @self: the #ChamplainCache
*
* Returns: the cache size limit in bytes
*
* Since: 0.4
*/
guint
champlain_cache_get_size_limit (ChamplainCache *self)
{
g_return_val_if_fail(CHAMPLAIN_CACHE (self), FALSE);
ChamplainCachePrivate *priv = GET_PRIVATE (self);
return priv->size_limit;
}
/**
* champlain_cache_set_size_limit:
* @self: the #ChamplainCache
* @size_limit: the cache limit in bytes
*
* Sets the cache size limit in bytes
*
* Since: 0.4
*/
void
champlain_cache_set_size_limit (ChamplainCache *self,
guint size_limit)
{
g_return_if_fail (CHAMPLAIN_CACHE (self));
ChamplainCachePrivate *priv = GET_PRIVATE (self);
priv->size_limit = size_limit;
g_object_notify (G_OBJECT (self), "size-limit");
}
/**
* champlain_cache_fill_tile:
* @self: the #ChamplainCache
* @tile: the #ChamplainTile to fill
*
* Loads data from disk for the given tile
*
* Returns: TRUE if the tile was in cache, false if it needs to be
* loaded from network
*
* Since: 0.4
*/
gboolean
champlain_cache_fill_tile (ChamplainCache *self,
ChamplainTile *tile)
{
g_return_val_if_fail(CHAMPLAIN_CACHE (self), FALSE);
g_return_val_if_fail(CHAMPLAIN_TILE (tile), FALSE);
GFileInfo *info = NULL;
GFile *file = NULL;
GError *error = NULL;
ClutterActor *actor = NULL;
const gchar *filename = NULL;
GTimeVal modified_time = {0,};
int sql_rc = SQLITE_OK;
gboolean cache_hit = FALSE;
ChamplainCachePrivate *priv = GET_PRIVATE (self);
filename = champlain_tile_get_filename (tile);
DEBUG ("fill of %s", filename);
if (!g_file_test (filename, G_FILE_TEST_EXISTS))
goto cleanup;
file = g_file_new_for_path (filename);
info = g_file_query_info (file,
G_FILE_ATTRIBUTE_TIME_MODIFIED,
G_FILE_QUERY_INFO_NONE, NULL, NULL);
g_file_info_get_modification_time (info, &modified_time);
champlain_tile_set_modified_time (tile, &modified_time);
g_object_unref (file);
g_object_unref (info);
/* Retrieve etag */
sql_rc = sqlite3_bind_text (priv->stmt_select, 1, filename, -1, SQLITE_STATIC);
if (sql_rc == SQLITE_ERROR)
{
DEBUG ("Failed to prepare the SQL query for finding the Etag of '%s', error: %s",
filename, sqlite3_errmsg (priv->data));
goto cleanup;
}
sql_rc = sqlite3_step (priv->stmt_select);
if (sql_rc == SQLITE_ROW)
{
const gchar *etag = (const gchar *) sqlite3_column_text (priv->stmt_select, 0);
champlain_tile_set_etag (CHAMPLAIN_TILE (tile), etag);
}
else if (sql_rc == SQLITE_DONE)
{
DEBUG ("'%s' does't have an etag",
filename);
}
else if (sql_rc == SQLITE_ERROR)
{
DEBUG ("Failed to finding the Etag of '%s', %d error: %s",
filename, sql_rc, sqlite3_errmsg (priv->data));
goto cleanup;
}
/* Load the cached version */
actor = clutter_texture_new ();
clutter_texture_set_load_async (CLUTTER_TEXTURE (actor), TRUE);
clutter_texture_set_from_file (CLUTTER_TEXTURE (actor), filename, &error);
if (error == NULL)
{
champlain_tile_set_content (tile, actor, FALSE);
cache_hit = TRUE;
}
else
{
DEBUG ("Failed to load tile %s, error: %s",
filename, error->message);
/* remote from the history */
delete_tile (self, filename);
goto cleanup;
}
priv->popularity_queue = g_slist_prepend (priv->popularity_queue,
g_strdup (filename));
if (priv->popularity_id == 0)
{
g_object_ref (self);
priv->popularity_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
inc_popularity, self,
g_object_unref);
}
cleanup:
sqlite3_reset (priv->stmt_select);
return cache_hit;
}
/**
* champlain_cache_tile_is_expired:
* @self: the #ChamplainCache
* @tile: the #ChamplainTile to fill
*
* Returns: TRUE if the tile should be reloaded from network, FALSE otherwize.
*
* Since: 0.4
*/
gboolean
champlain_cache_tile_is_expired (ChamplainCache *self,
ChamplainTile *tile)
{
GTimeVal now = {0, };
g_return_val_if_fail(CHAMPLAIN_CACHE (self), FALSE);
g_return_val_if_fail(CHAMPLAIN_TILE (tile), FALSE);
const GTimeVal *modified_time = champlain_tile_get_modified_time (tile);
gboolean validate_cache = FALSE;
g_get_current_time (&now);
g_time_val_add (&now, (-24ul * 60ul * 60ul * 1000ul * 1000ul * 7ul)); // Cache expires 7 days
validate_cache = modified_time->tv_sec < now.tv_sec;
DEBUG ("%p is %s expired", tile, (validate_cache ? "": "not"));
return validate_cache;
}
static gboolean
inc_popularity (gpointer data)
{
int sql_rc = SQLITE_OK;
gchar *filename = NULL;
ChamplainCache *cache = CHAMPLAIN_CACHE (data);
ChamplainCachePrivate *priv = GET_PRIVATE (cache);
GSList *last;
if (priv->popularity_queue == NULL)
{
priv->popularity_id = 0;
return FALSE;
}
last = g_slist_last (priv->popularity_queue);
filename = last->data;
DEBUG ("popularity of %s", filename);
sql_rc = sqlite3_bind_text (priv->stmt_update, 1, filename, -1, SQLITE_STATIC);
if (sql_rc != SQLITE_OK)
{
DEBUG ("Failed to set values to the popularity query of '%s', error: %s",
filename, sqlite3_errmsg (priv->data));
goto cleanup;
}
sql_rc = sqlite3_step (priv->stmt_update);
if (sql_rc != SQLITE_DONE)
{
DEBUG ("Failed to update the popularity of '%s', error: %s",
filename, sqlite3_errmsg (priv->data));
goto cleanup;
}
cleanup:
sqlite3_reset (priv->stmt_update);
priv->popularity_queue = g_slist_remove (priv->popularity_queue, filename);
g_free (filename);
/* Ask to be called again until the list is emptied */
return priv->popularity_queue != NULL;
}
static void
delete_tile (ChamplainCache *self,
const gchar *filename)
{
g_return_if_fail (CHAMPLAIN_CACHE (self));
gchar *query, *error = NULL;
GError *gerror;
GFile *file;
ChamplainCachePrivate *priv = GET_PRIVATE (self);
query = sqlite3_mprintf ("DELETE FROM tiles WHERE filename = %Q", filename);
sqlite3_exec (priv->data, query, NULL, NULL, &error);
if (error != NULL)
{
DEBUG ("Deleting tile from db failed: %s", error);
sqlite3_free (error);
}
sqlite3_free (query);
if (!g_file_test (filename, G_FILE_TEST_EXISTS))
return;
file = g_file_new_for_path (filename);
if (!g_file_delete (file, NULL, &gerror))
{
DEBUG ("Deleting tile from disk failed: %s", gerror->message);
g_error_free (gerror);
}
g_object_unref (file);
}
/**
* champlain_cache_update_tile:
* @self: the #ChamplainCache
* @tile: the #ChamplainTile to fill
* @filesize: the filesize on the disk
*
* Update the tile's information in the cache such as Etag and filesize.
* Also increase the tile's popularity.
*
* Since: 0.4
*/
void
champlain_cache_update_tile (ChamplainCache *self,
ChamplainTile *tile,
guint filesize)
{
g_return_if_fail (CHAMPLAIN_CACHE (self));
gchar *query, *error = NULL;
ChamplainCachePrivate *priv = GET_PRIVATE (self);
DEBUG ("Update of %p", tile);
query = sqlite3_mprintf ("REPLACE INTO tiles (filename, etag, size) VALUES (%Q, %Q, %d)",
champlain_tile_get_filename (tile),
champlain_tile_get_etag (tile),
filesize);
sqlite3_exec (priv->data, query, NULL, NULL, &error);
if (error != NULL)
{
DEBUG ("Saving Etag and size failed: %s", error);
sqlite3_free (error);
}
sqlite3_free (query);
}
static gboolean
purge_on_idle (gpointer data)
{
champlain_cache_purge (CHAMPLAIN_CACHE (data));
return FALSE;
}
/**
* champlain_cache_purge_on_idle:
* @self: the #ChamplainCache
*
* Purge the cache from the less popular tiles until cache's size limit is reached.
* This is a non blocking call as the purge will happen when the application is idle
*
* Since: 0.4
*/
void
champlain_cache_purge_on_idle (ChamplainCache *self)
{
g_return_if_fail (CHAMPLAIN_CACHE (self));
g_idle_add (purge_on_idle, self);
}
/**
* champlain_cache_purge:
* @self: the #ChamplainCache
*
* Purge the cache from the less popular tiles until cache's size limit is reached.
*
* Since: 0.4
*/
void
champlain_cache_purge (ChamplainCache *self)
{
g_return_if_fail (CHAMPLAIN_CACHE (self));
ChamplainCachePrivate *priv = GET_PRIVATE (self);
gchar *query;
sqlite3_stmt *stmt;
int rc = 0;
guint current_size = 0;
guint highest_popularity = 0;
gchar *error;
query = "SELECT SUM (size) FROM tiles";
rc = sqlite3_prepare (priv->data, query, strlen (query), &stmt, NULL);
if (rc != SQLITE_OK)
{
DEBUG ("Can't compute cache size %s", sqlite3_errmsg (priv->data));
}
rc = sqlite3_step (stmt);
if (rc != SQLITE_ROW)
{
DEBUG ("Failed to count the total cache consumption %s",
sqlite3_errmsg (priv->data));
sqlite3_finalize (stmt);
return;
}
current_size = sqlite3_column_int (stmt, 0);
if (current_size < priv->size_limit)
{
DEBUG ("Cache doesn't need to be purged at %d bytes", current_size);
sqlite3_finalize (stmt);
return;
}
sqlite3_finalize (stmt);
/* Ok, delete the less popular tiles until size_limit reached */
query = "SELECT filename, size, popularity FROM tiles ORDER BY popularity";
rc = sqlite3_prepare (priv->data, query, strlen (query), &stmt, NULL);
if (rc != SQLITE_OK)
{
DEBUG ("Can't fetch tiles to delete: %s", sqlite3_errmsg(priv->data));
}
rc = sqlite3_step (stmt);
while (rc == SQLITE_ROW && current_size > priv->size_limit)
{
const char *filename = sqlite3_column_text (stmt, 0);
guint size;
filename = sqlite3_column_text (stmt, 0);
size = sqlite3_column_int (stmt, 1);
highest_popularity = sqlite3_column_int (stmt, 2);
DEBUG ("Deleting %s of size %d", filename, size);
delete_tile (self, filename);
current_size -= size;
rc = sqlite3_step (stmt);
}
DEBUG ("Cache size is now %d", current_size);
sqlite3_finalize (stmt);
query = sqlite3_mprintf ("UPDATE tiles SET popularity = popularity - %d",
highest_popularity);
sqlite3_exec (priv->data, query, NULL, NULL, &error);
if (error != NULL)
{
DEBUG ("Updating popularity failed: %s", error);
sqlite3_free (error);
}
sqlite3_free (query);
}
|