[PATCH 08/13] cache: Use transactions to perform cache changes



Instead of performing each insertion/modification in its own sql transaction,
group all of them in a big transaction, so performance is greatly improved.

Signed-off-by: Juan A. Suarez Romero <jasuarez igalia com>
---
 src/grl-media-cache.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/src/grl-media-cache.c b/src/grl-media-cache.c
index b1d53ec..c2efac5 100644
--- a/src/grl-media-cache.c
+++ b/src/grl-media-cache.c
@@ -95,6 +95,7 @@ enum {
 struct _GrlMediaCachePrivate {
   gchar *cache_id;
   GList *extra_keys;
+  gboolean on_transaction;
   gboolean persistent;
   gboolean force_db_removal;
   sqlite3 *db;
@@ -550,6 +551,12 @@ grl_media_cache_insert_media (GrlMediaCache *cache,
   g_free (extra_header);
   g_free (extra_value);
 
+  /* Begin a transaction */
+  if (!cache->priv->on_transaction) {
+    sqlite3_exec(cache->priv->db, "BEGIN", 0, 0, 0);
+    cache->priv->on_transaction = TRUE;
+  }
+
   if (sqlite3_prepare_v2 (cache->priv->db,
                           sql_sentence,
                           strlen (sql_sentence),
@@ -642,6 +649,12 @@ grl_media_cache_get_media (GrlMediaCache *cache,
                                   cache->priv->cache_id,
                                   media_id);
 
+  /* Finish pending transactions */
+  if (cache->priv->on_transaction) {
+    sqlite3_exec(cache->priv->db, "COMMIT", 0, 0, 0);
+    cache->priv->on_transaction = FALSE;
+  }
+
   if (sqlite3_prepare_v2 (cache->priv->db,
                           sql_sentence,
                           strlen (sql_sentence),
@@ -707,6 +720,12 @@ grl_media_cache_search (GrlMediaCache *cache,
                                   condition? "WHERE": "",
                                   condition? condition: "");
 
+  /* Finish pending transactions */
+  if (cache->priv->on_transaction) {
+    sqlite3_exec(cache->priv->db, "COMMIT", 0, 0, 0);
+    cache->priv->on_transaction = FALSE;
+  }
+
   if (sqlite3_prepare_v2 (cache->priv->db,
                           sql_sentence,
                           strlen (sql_sentence),
-- 
1.7.4



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]