Commit 0b0ab3bb authored by James Westman's avatar James Westman
Browse files

file-cache: Minor cleanup

- Use g_autoptr
- Turn some g_debug into g_warning
parent 051be8f0
Pipeline #284502 passed with stage
in 2 minutes and 18 seconds
......@@ -70,6 +70,7 @@ G_DEFINE_TYPE_WITH_PRIVATE (ShumateFileCache, shumate_file_cache, G_TYPE_OBJECT)
typedef char sqlite_str;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (sqlite_str, sqlite3_free);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (sqlite3_stmt, sqlite3_finalize);
static void finalize_sql (ShumateFileCache *file_cache);
......@@ -598,26 +599,23 @@ delete_tile (ShumateFileCache *file_cache, const char *filename)
ShumateFileCachePrivate *priv = shumate_file_cache_get_instance_private (file_cache);
g_return_if_fail (SHUMATE_IS_FILE_CACHE (file_cache));
char *query, *error = NULL;
GError *gerror = NULL;
GFile *file;
query = sqlite3_mprintf ("DELETE FROM tiles WHERE filename = %Q", filename);
sqlite3_exec (priv->db, query, NULL, NULL, &error);
if (error != NULL)
{
g_debug ("Deleting tile from db failed: %s", error);
sqlite3_free (error);
}
sqlite3_free (query);
g_autoptr(sqlite_str) query = NULL;
g_autoptr(sqlite_str) sql_error = NULL;
g_autoptr(GError) gerror = NULL;
g_autoptr(GFile) file = NULL;
file = g_file_new_for_path (filename);
if (!g_file_delete (file, NULL, &gerror))
{
g_debug ("Deleting tile from disk failed: %s", gerror->message);
g_error_free (gerror);
}
g_object_unref (file);
query = sqlite3_mprintf ("DELETE FROM tiles WHERE filename = %Q", filename);
sqlite3_exec (priv->db, query, NULL, NULL, &sql_error);
if (sql_error != NULL)
{
g_debug ("Deleting tile from db failed: %s", sql_error);
}
}
......@@ -631,25 +629,24 @@ purge_cache (GTask *task,
ShumateFileCachePrivate *priv = shumate_file_cache_get_instance_private (self);
char *query;
sqlite3_stmt *stmt;
g_autoptr(sqlite3_stmt) stmt = NULL;
int rc = 0;
guint current_size = 0;
guint highest_popularity = 0;
char *error;
g_autoptr(sqlite_str) error = NULL;
query = "SELECT SUM (size) FROM tiles";
rc = sqlite3_prepare (priv->db, query, strlen (query), &stmt, NULL);
if (rc != SQLITE_OK)
{
g_debug ("Can't compute cache size %s", sqlite3_errmsg (priv->db));
g_warning ("Can't compute cache size %s", sqlite3_errmsg (priv->db));
}
rc = sqlite3_step (stmt);
if (rc != SQLITE_ROW)
{
g_debug ("Failed to count the total cache consumption %s",
g_warning ("Failed to count the total cache consumption %s",
sqlite3_errmsg (priv->db));
sqlite3_finalize (stmt);
return;
}
......@@ -657,7 +654,6 @@ purge_cache (GTask *task,
if (current_size < priv->size_limit)
{
g_debug ("Cache doesn't need to be purged at %d bytes", current_size);
sqlite3_finalize (stmt);
return;
}
......@@ -668,7 +664,7 @@ purge_cache (GTask *task,
rc = sqlite3_prepare (priv->db, query, strlen (query), &stmt, NULL);
if (rc != SQLITE_OK)
{
g_debug ("Can't fetch tiles to delete: %s", sqlite3_errmsg (priv->db));
g_warning ("Can't fetch tiles to delete: %s", sqlite3_errmsg (priv->db));
}
rc = sqlite3_step (stmt);
......@@ -690,14 +686,12 @@ purge_cache (GTask *task,
}
g_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->db, query, NULL, NULL, &error);
if (error != NULL)
{
g_debug ("Updating popularity failed: %s", error);
g_warning ("Updating popularity failed: %s", error);
sqlite3_free (error);
}
sqlite3_free (query);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment