[gnome-photos/wip/search: 33/35] Shed redundant SPARQL queries and PhotosViewModel updates



commit 72544b982c444c27d09fb0f0249f353e47cd0d0b
Author: Debarshi Ray <debarshir gnome org>
Date:   Tue Jan 28 10:20:10 2014 +0100

    Shed redundant SPARQL queries and PhotosViewModel updates
    
    We were refreshing all the PhotosTrackerControllers and clearing
    PhotosItemManager multiple times when the active collection changed.
    This is too much. Only those components that are tied to the current
    mode should react.
    
    This means we should only refresh the PhotosTrackerController
    corresponding to the current mode, and instead of clearing the whole
    manager we should only clear the relevant PhotosViewModel.
    
    Since we are not clearing PhotosItemManager on every refresh, we will
    miss some "object-added" signals because the items found while
    refreshing might already be part of the manager. We need to adjust for
    that.
    
    We will also need to be more strict when adding items to the model.
    Particularly when there is an active collection in one of the modes.

 src/photos-item-manager.c                   |    9 ++++-
 src/photos-tracker-collections-controller.c |   21 +++++++++++
 src/photos-tracker-controller.c             |   10 -----
 src/photos-tracker-favorites-controller.c   |   21 +++++++++++
 src/photos-tracker-search-controller.c      |   25 +++++++++++++
 src/photos-view-model.c                     |   50 +++++++++++++++++++++++++--
 6 files changed, 121 insertions(+), 15 deletions(-)
---
diff --git a/src/photos-item-manager.c b/src/photos-item-manager.c
index c75cb26..1b254c4 100644
--- a/src/photos-item-manager.c
+++ b/src/photos-item-manager.c
@@ -266,11 +266,16 @@ void
 photos_item_manager_add_item (PhotosItemManager *self, TrackerSparqlCursor *cursor)
 {
   PhotosBaseItem *item = NULL;
+  GObject *object;
   const gchar *id;
 
   id = tracker_sparql_cursor_get_string (cursor, PHOTOS_QUERY_COLUMNS_URN, NULL);
-  if (photos_base_manager_get_object_by_id (PHOTOS_BASE_MANAGER (self), id) != NULL)
-    goto out;
+  object = photos_base_manager_get_object_by_id (PHOTOS_BASE_MANAGER (self), id);
+  if (object != NULL)
+    {
+      g_signal_emit_by_name (self, "object-added", object);
+      goto out;
+    }
 
   item = photos_item_manager_create_item (self, cursor);
   photos_base_manager_add_object (PHOTOS_BASE_MANAGER (self), G_OBJECT (item));
diff --git a/src/photos-tracker-collections-controller.c b/src/photos-tracker-collections-controller.c
index aa9c48b..120f45f 100644
--- a/src/photos-tracker-collections-controller.c
+++ b/src/photos-tracker-collections-controller.c
@@ -31,6 +31,7 @@
 struct _PhotosTrackerCollectionsControllerPrivate
 {
   PhotosBaseManager *col_mngr;
+  PhotosModeController *mode_cntrlr;
   PhotosOffsetController *offset_cntrlr;
 };
 
@@ -40,6 +41,19 @@ G_DEFINE_TYPE_WITH_PRIVATE (PhotosTrackerCollectionsController,
                             PHOTOS_TYPE_TRACKER_CONTROLLER);
 
 
+static void
+photos_tracker_collections_controller_col_active_changed (PhotosTrackerCollectionsController *self)
+{
+  PhotosWindowMode mode;
+
+  mode = photos_mode_controller_get_window_mode (self->priv->mode_cntrlr);
+  if (mode != PHOTOS_WINDOW_MODE_COLLECTIONS)
+    return;
+
+  photos_tracker_controller_refresh_for_object (PHOTOS_TRACKER_CONTROLLER (self));
+}
+
+
 static PhotosOffsetController *
 photos_tracker_collections_controller_get_offset_controller (PhotosTrackerController *trk_cntrlr)
 {
@@ -93,6 +107,7 @@ photos_tracker_collections_controller_dispose (GObject *object)
   PhotosTrackerCollectionsControllerPrivate *priv = self->priv;
 
   g_clear_object (&priv->col_mngr);
+  g_clear_object (&priv->mode_cntrlr);
   g_clear_object (&priv->offset_cntrlr);
 
   G_OBJECT_CLASS (photos_tracker_collections_controller_parent_class)->dispose (object);
@@ -108,6 +123,12 @@ photos_tracker_collections_controller_init (PhotosTrackerCollectionsController *
   priv = self->priv;
 
   priv->col_mngr = photos_collection_manager_dup_singleton ();
+  g_signal_connect_swapped (priv->col_mngr,
+                            "active-changed",
+                            G_CALLBACK (photos_tracker_collections_controller_col_active_changed),
+                            self);
+
+  priv->mode_cntrlr = photos_mode_controller_dup_singleton ();
   priv->offset_cntrlr = photos_offset_collections_controller_dup_singleton ();
 }
 
diff --git a/src/photos-tracker-controller.c b/src/photos-tracker-controller.c
index 09b1cff..c1c3c9d 100644
--- a/src/photos-tracker-controller.c
+++ b/src/photos-tracker-controller.c
@@ -28,7 +28,6 @@
 #include <glib.h>
 #include <glib/gi18n.h>
 
-#include "photos-collection-manager.h"
 #include "photos-enums.h"
 #include "photos-item-manager.h"
 #include "photos-marshalers.h"
@@ -43,7 +42,6 @@ struct _PhotosTrackerControllerPrivate
 {
   GCancellable *cancellable;
   GError *queue_error;
-  PhotosBaseManager *col_mngr;
   PhotosBaseManager *item_mngr;
   PhotosBaseManager *src_mngr;
   PhotosModeController *mode_cntrlr;
@@ -249,7 +247,6 @@ photos_tracker_controller_refresh_internal (PhotosTrackerController *self, gint
     }
 
   photos_tracker_controller_set_query_status (self, TRUE);
-  photos_base_manager_clear (priv->item_mngr);
   photos_tracker_controller_perform_current_query (self);
 }
 
@@ -329,7 +326,6 @@ photos_tracker_controller_dispose (GObject *object)
   PhotosTrackerController *self = PHOTOS_TRACKER_CONTROLLER (object);
   PhotosTrackerControllerPrivate *priv = self->priv;
 
-  g_clear_object (&priv->col_mngr);
   g_clear_object (&priv->item_mngr);
   g_clear_object (&priv->src_mngr);
   g_clear_object (&priv->mode_cntrlr);
@@ -384,12 +380,6 @@ photos_tracker_controller_init (PhotosTrackerController *self)
   priv->cancellable = g_cancellable_new ();
   priv->item_mngr = photos_item_manager_dup_singleton ();
 
-  priv->col_mngr = photos_collection_manager_dup_singleton ();
-  g_signal_connect_swapped (priv->col_mngr,
-                            "active-changed",
-                            G_CALLBACK (photos_tracker_controller_refresh_for_object),
-                            self);
-
   priv->src_mngr = photos_source_manager_dup_singleton ();
   g_signal_connect_swapped (priv->src_mngr,
                             "object-added",
diff --git a/src/photos-tracker-favorites-controller.c b/src/photos-tracker-favorites-controller.c
index 41f21d9..24795c1 100644
--- a/src/photos-tracker-favorites-controller.c
+++ b/src/photos-tracker-favorites-controller.c
@@ -31,6 +31,7 @@
 struct _PhotosTrackerFavoritesControllerPrivate
 {
   PhotosBaseManager *col_mngr;
+  PhotosModeController *mode_cntrlr;
   PhotosOffsetController *offset_cntrlr;
 };
 
@@ -40,6 +41,19 @@ G_DEFINE_TYPE_WITH_PRIVATE (PhotosTrackerFavoritesController,
                             PHOTOS_TYPE_TRACKER_CONTROLLER);
 
 
+static void
+photos_tracker_favorites_controller_col_active_changed (PhotosTrackerFavoritesController *self)
+{
+  PhotosWindowMode mode;
+
+  mode = photos_mode_controller_get_window_mode (self->priv->mode_cntrlr);
+  if (mode != PHOTOS_WINDOW_MODE_FAVORITES)
+    return;
+
+  photos_tracker_controller_refresh_for_object (PHOTOS_TRACKER_CONTROLLER (self));
+}
+
+
 static PhotosOffsetController *
 photos_tracker_favorites_controller_get_offset_controller (PhotosTrackerController *trk_cntrlr)
 {
@@ -93,6 +107,7 @@ photos_tracker_favorites_controller_dispose (GObject *object)
   PhotosTrackerFavoritesControllerPrivate *priv = self->priv;
 
   g_clear_object (&priv->col_mngr);
+  g_clear_object (&priv->mode_cntrlr);
   g_clear_object (&priv->offset_cntrlr);
 
   G_OBJECT_CLASS (photos_tracker_favorites_controller_parent_class)->dispose (object);
@@ -108,6 +123,12 @@ photos_tracker_favorites_controller_init (PhotosTrackerFavoritesController *self
   priv = self->priv;
 
   priv->col_mngr = photos_collection_manager_dup_singleton ();
+  g_signal_connect_swapped (priv->col_mngr,
+                            "active-changed",
+                            G_CALLBACK (photos_tracker_favorites_controller_col_active_changed),
+                            self);
+
+  priv->mode_cntrlr = photos_mode_controller_dup_singleton ();
   priv->offset_cntrlr = photos_offset_favorites_controller_dup_singleton ();
 }
 
diff --git a/src/photos-tracker-search-controller.c b/src/photos-tracker-search-controller.c
index 319d207..5cfd068 100644
--- a/src/photos-tracker-search-controller.c
+++ b/src/photos-tracker-search-controller.c
@@ -25,6 +25,7 @@
 
 #include "config.h"
 
+#include "photos-collection-manager.h"
 #include "photos-mode-controller.h"
 #include "photos-offset-search-controller.h"
 #include "photos-query-builder.h"
@@ -35,8 +36,10 @@
 
 struct _PhotosTrackerSearchControllerPrivate
 {
+  PhotosBaseManager *col_mngr;
   PhotosBaseManager *src_mngr;
   PhotosBaseManager *srch_typ_mngr;
+  PhotosModeController *mode_cntrlr;
   PhotosOffsetController *offset_cntrlr;
 };
 
@@ -46,6 +49,19 @@ G_DEFINE_TYPE_WITH_PRIVATE (PhotosTrackerSearchController,
                             PHOTOS_TYPE_TRACKER_CONTROLLER);
 
 
+static void
+photos_tracker_search_controller_col_active_changed (PhotosTrackerSearchController *self)
+{
+  PhotosWindowMode mode;
+
+  mode = photos_mode_controller_get_window_mode (self->priv->mode_cntrlr);
+  if (mode != PHOTOS_WINDOW_MODE_SEARCH)
+    return;
+
+  photos_tracker_controller_refresh_for_object (PHOTOS_TRACKER_CONTROLLER (self));
+}
+
+
 static PhotosOffsetController *
 photos_tracker_search_controller_get_offset_controller (PhotosTrackerController *trk_cntrlr)
 {
@@ -88,8 +104,10 @@ photos_tracker_search_controller_dispose (GObject *object)
   PhotosTrackerSearchController *self = PHOTOS_TRACKER_SEARCH_CONTROLLER (object);
   PhotosTrackerSearchControllerPrivate *priv = self->priv;
 
+  g_clear_object (&priv->col_mngr);
   g_clear_object (&priv->src_mngr);
   g_clear_object (&priv->srch_typ_mngr);
+  g_clear_object (&priv->mode_cntrlr);
   g_clear_object (&priv->offset_cntrlr);
 
   G_OBJECT_CLASS (photos_tracker_search_controller_parent_class)->dispose (object);
@@ -104,6 +122,12 @@ photos_tracker_search_controller_init (PhotosTrackerSearchController *self)
   self->priv = photos_tracker_search_controller_get_instance_private (self);
   priv = self->priv;
 
+  priv->col_mngr = photos_collection_manager_dup_singleton ();
+  g_signal_connect_swapped (priv->col_mngr,
+                            "active-changed",
+                            G_CALLBACK (photos_tracker_search_controller_col_active_changed),
+                            self);
+
   priv->src_mngr = photos_source_manager_dup_singleton ();
   g_signal_connect_swapped (priv->src_mngr,
                             "active-changed",
@@ -116,6 +140,7 @@ photos_tracker_search_controller_init (PhotosTrackerSearchController *self)
                             G_CALLBACK (photos_tracker_controller_refresh_for_object),
                             self);
 
+  priv->mode_cntrlr = photos_mode_controller_dup_singleton ();
   priv->offset_cntrlr = photos_offset_search_controller_dup_singleton ();
 }
 
diff --git a/src/photos-view-model.c b/src/photos-view-model.c
index c9a3a4a..5a64684 100644
--- a/src/photos-view-model.c
+++ b/src/photos-view-model.c
@@ -28,10 +28,15 @@
 #include "photos-collection-manager.h"
 #include "photos-enums.h"
 #include "photos-item-manager.h"
+#include "photos-mode-controller.h"
 #include "photos-offset-collections-controller.h"
 #include "photos-offset-favorites-controller.h"
 #include "photos-offset-overview-controller.h"
 #include "photos-offset-search-controller.h"
+#include "photos-tracker-collections-controller.h"
+#include "photos-tracker-favorites-controller.h"
+#include "photos-tracker-overview-controller.h"
+#include "photos-tracker-search-controller.h"
 #include "photos-view-model.h"
 
 
@@ -39,7 +44,9 @@ struct _PhotosViewModelPrivate
 {
   PhotosBaseManager *col_mngr;
   PhotosBaseManager *item_mngr;
+  PhotosModeController *mode_cntrlr;
   PhotosOffsetController *offset_cntrlr;
+  PhotosTrackerController *trk_cntrlr;
   PhotosWindowMode mode;
   gchar *row_ref_key;
   gint n_rows;
@@ -140,8 +147,17 @@ static void
 photos_view_model_clear (PhotosViewModel *self)
 {
   PhotosViewModelPrivate *priv = self->priv;
+  GHashTable *items;
+  GHashTableIter iter;
+  PhotosBaseItem *item;
+
+  items = photos_base_manager_get_objects (priv->item_mngr);
+  g_hash_table_iter_init (&iter, items);
+  while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &item))
+    g_object_set_data (G_OBJECT (item), priv->row_ref_key, NULL);
 
   gtk_list_store_clear (GTK_LIST_STORE (self));
+
   priv->n_rows = 0;
   priv->oldest_mtime = G_MAXINT64;
 }
@@ -216,7 +232,7 @@ photos_view_model_info_updated (PhotosBaseItem *item, gpointer user_data)
       is_collection = photos_base_item_is_collection (item);
       if (!is_collection && row_ref != NULL && active_collection == NULL)
         photos_view_model_object_removed (self, G_OBJECT (item));
-      else if (is_collection  && row_ref == NULL)
+      else if (is_collection  && row_ref == NULL && active_collection == NULL)
         photos_view_model_add_item (self, item);
     }
   else if (priv->mode == PHOTOS_WINDOW_MODE_FAVORITES)
@@ -258,15 +274,22 @@ photos_view_model_object_added (PhotosViewModel *self, GObject *object)
 {
   PhotosBaseItem *item = PHOTOS_BASE_ITEM (object);
   PhotosViewModelPrivate *priv = self->priv;
+  PhotosWindowMode mode;
   GObject *active_collection;
+  GtkTreeRowReference *row_ref;
   gboolean is_collection;
   gboolean is_favorite;
 
+  row_ref = (GtkTreeRowReference *) g_object_get_data (G_OBJECT (item), priv->row_ref_key);
+  if (row_ref != NULL)
+    return;
+
   active_collection = photos_base_manager_get_active_object (priv->col_mngr);
   is_collection = photos_base_item_is_collection (item);
   is_favorite = photos_base_item_is_favorite (item);
+  mode = photos_mode_controller_get_window_mode (priv->mode_cntrlr);
 
-  if (active_collection == NULL)
+  if (active_collection == NULL || priv->mode != mode)
     {
       if ((priv->mode == PHOTOS_WINDOW_MODE_COLLECTIONS && !is_collection)
           || (priv->mode == PHOTOS_WINDOW_MODE_FAVORITES && !is_favorite)
@@ -282,6 +305,16 @@ photos_view_model_object_added (PhotosViewModel *self, GObject *object)
 
 
 static void
+photos_view_model_query_status_changed (PhotosViewModel *self, gboolean query_status)
+{
+  if (query_status == FALSE)
+    return;
+
+  photos_view_model_clear (self);
+}
+
+
+static void
 photos_view_model_constructed (GObject *object)
 {
   PhotosViewModel *self = PHOTOS_VIEW_MODEL (object);
@@ -293,18 +326,22 @@ photos_view_model_constructed (GObject *object)
     {
     case PHOTOS_WINDOW_MODE_COLLECTIONS:
       priv->offset_cntrlr = photos_offset_collections_controller_dup_singleton ();
+      priv->trk_cntrlr = photos_tracker_collections_controller_dup_singleton ();
       break;
 
     case PHOTOS_WINDOW_MODE_FAVORITES:
       priv->offset_cntrlr = photos_offset_favorites_controller_dup_singleton ();
+      priv->trk_cntrlr = photos_tracker_favorites_controller_dup_singleton ();
       break;
 
     case PHOTOS_WINDOW_MODE_OVERVIEW:
       priv->offset_cntrlr = photos_offset_overview_controller_dup_singleton ();
+      priv->trk_cntrlr = photos_tracker_overview_controller_dup_singleton ();
       break;
 
     case PHOTOS_WINDOW_MODE_SEARCH:
       priv->offset_cntrlr = photos_offset_search_controller_dup_singleton ();
+      priv->trk_cntrlr = photos_tracker_search_controller_dup_singleton ();
       break;
 
     default:
@@ -314,7 +351,11 @@ photos_view_model_constructed (GObject *object)
 
   g_signal_connect_swapped (priv->item_mngr, "object-added", G_CALLBACK (photos_view_model_object_added), 
self);
   g_signal_connect_swapped (priv->item_mngr, "object-removed", G_CALLBACK 
(photos_view_model_object_removed), self);
-  g_signal_connect_swapped (priv->item_mngr, "clear", G_CALLBACK (photos_view_model_clear), self);
+
+  g_signal_connect_swapped (priv->trk_cntrlr,
+                            "query-status-changed",
+                            G_CALLBACK (photos_view_model_query_status_changed),
+                            self);
 }
 
 
@@ -332,7 +373,9 @@ photos_view_model_dispose (GObject *object)
 
   g_clear_object (&priv->col_mngr);
   g_clear_object (&priv->item_mngr);
+  g_clear_object (&priv->mode_cntrlr);
   g_clear_object (&priv->offset_cntrlr);
+  g_clear_object (&priv->trk_cntrlr);
 
   G_OBJECT_CLASS (photos_view_model_parent_class)->dispose (object);
 }
@@ -390,6 +433,7 @@ photos_view_model_init (PhotosViewModel *self)
 
   priv->col_mngr = photos_collection_manager_dup_singleton ();
   priv->item_mngr = photos_item_manager_dup_singleton ();
+  priv->mode_cntrlr = photos_mode_controller_dup_singleton ();
 
   priv->oldest_mtime = G_MAXINT64;
 }


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