[epiphany/wip/sync-batch-upload] sync: Change merge result container to GPtrArray



commit 8ea7173f1df531737df105c8fd2578ed4ba77793
Author: Gabriel Ivascu <gabrielivascu gnome org>
Date:   Fri Dec 1 13:46:14 2017 +0200

    sync: Change merge result container to GPtrArray

 lib/sync/ephy-history-manager.c        |   22 +++++++++++++---------
 lib/sync/ephy-open-tabs-manager.c      |    7 +++----
 lib/sync/ephy-password-manager.c       |   23 ++++++++++++-----------
 lib/sync/ephy-sync-service.c           |   19 ++++++++++++-------
 lib/sync/ephy-synchronizable-manager.h |    2 +-
 src/bookmarks/ephy-bookmarks-manager.c |   24 ++++++++++++------------
 6 files changed, 53 insertions(+), 44 deletions(-)
---
diff --git a/lib/sync/ephy-history-manager.c b/lib/sync/ephy-history-manager.c
index 655b7d6..6b99b6c 100644
--- a/lib/sync/ephy-history-manager.c
+++ b/lib/sync/ephy-history-manager.c
@@ -312,7 +312,7 @@ ephy_history_manager_handle_different_id_same_url (EphyHistoryManager *self,
   ephy_history_record_add_visit_time (remote, local_last_visit_time);
 }
 
-static GList *
+static GPtrArray *
 ephy_history_manager_handle_initial_merge (EphyHistoryManager *self,
                                            GHashTable         *records_ht_id,
                                            GHashTable         *records_ht_url,
@@ -321,7 +321,7 @@ ephy_history_manager_handle_initial_merge (EphyHistoryManager *self,
   EphyHistoryRecord *record;
   GHashTableIter iter;
   gpointer key, value;
-  GList *to_upload = NULL;
+  GPtrArray *to_upload;
   const char *remote_id;
   const char *remote_url;
   gint64 remote_last_visit_time;
@@ -329,6 +329,8 @@ ephy_history_manager_handle_initial_merge (EphyHistoryManager *self,
 
   g_assert (EPHY_IS_HISTORY_MANAGER (self));
 
+  to_upload = g_ptr_array_new_with_free_func (g_object_unref);
+
   /* A history record is uniquely identified by its sync ID or by its URL. When
    * importing history records from server, we may encounter duplicates either
    * by ID or by URL. We start from the assumption that same ID means same URL
@@ -352,7 +354,7 @@ ephy_history_manager_handle_initial_merge (EphyHistoryManager *self,
                                         EPHY_PAGE_VISIT_LINK, FALSE);
 
       if (ephy_history_record_add_visit_time (l->data, local_last_visit_time))
-        to_upload = g_list_prepend (to_upload, g_object_ref (l->data));
+        g_ptr_array_add (to_upload, g_object_ref (l->data));
 
       g_hash_table_remove (records_ht_id, remote_id);
     } else {
@@ -362,7 +364,7 @@ ephy_history_manager_handle_initial_merge (EphyHistoryManager *self,
         /* Different ID, same URL. Keep local ID. */
         g_signal_emit_by_name (self, "synchronizable-deleted", l->data);
         ephy_history_manager_handle_different_id_same_url (self, record, l->data);
-        to_upload = g_list_prepend (to_upload, g_object_ref (l->data));
+        g_ptr_array_add (to_upload, g_object_ref (l->data));
         g_hash_table_remove (records_ht_id, ephy_history_record_get_id (record));
       } else {
         /* Different ID, different URL. This is a new record. */
@@ -377,12 +379,12 @@ ephy_history_manager_handle_initial_merge (EphyHistoryManager *self,
   /* Set the remaining local records to be uploaded to server. */
   g_hash_table_iter_init (&iter, records_ht_id);
   while (g_hash_table_iter_next (&iter, &key, &value))
-    to_upload = g_list_prepend (to_upload, g_object_ref (value));
+    g_ptr_array_add (to_upload, g_object_ref (value));
 
   return to_upload;
 }
 
-static GList *
+static GPtrArray *
 ephy_history_manager_handle_regular_merge (EphyHistoryManager  *self,
                                            GHashTable          *records_ht_id,
                                            GHashTable          *records_ht_url,
@@ -390,7 +392,7 @@ ephy_history_manager_handle_regular_merge (EphyHistoryManager  *self,
                                            GList               *updated_records)
 {
   EphyHistoryRecord *record;
-  GList *to_upload = NULL;
+  GPtrArray *to_upload;
   const char *remote_id;
   const char *remote_url;
   gint64 remote_last_visit_time;
@@ -398,6 +400,8 @@ ephy_history_manager_handle_regular_merge (EphyHistoryManager  *self,
 
   g_assert (EPHY_IS_HISTORY_MANAGER (self));
 
+  to_upload = g_ptr_array_new_with_free_func (g_object_unref);
+
   for (GList *l = deleted_records; l && l->data; l = l->next) {
     remote_id = ephy_history_record_get_id (l->data);
     remote_url = ephy_history_record_get_uri (l->data);
@@ -443,7 +447,7 @@ ephy_history_manager_handle_regular_merge (EphyHistoryManager  *self,
         /* Different ID, same URL. Keep local ID. */
         g_signal_emit_by_name (self, "synchronizable-deleted", l->data);
         ephy_history_manager_handle_different_id_same_url (self, record, l->data);
-        to_upload = g_list_prepend (to_upload, g_object_ref (l->data));
+        g_ptr_array_add (to_upload, g_object_ref (l->data));
       } else {
         /* Different ID, different URL. This is a new record. */
         if (remote_last_visit_time > 0)
@@ -465,7 +469,7 @@ merge_history_cb (EphyHistoryService    *service,
 {
   GHashTable *records_ht_id = NULL;
   GHashTable *records_ht_url = NULL;
-  GList *to_upload = NULL;
+  GPtrArray *to_upload = NULL;
 
   if (!success) {
     g_warning ("Failed to retrieve URLs in history");
diff --git a/lib/sync/ephy-open-tabs-manager.c b/lib/sync/ephy-open-tabs-manager.c
index 76c888b..4b58a23 100644
--- a/lib/sync/ephy-open-tabs-manager.c
+++ b/lib/sync/ephy-open-tabs-manager.c
@@ -257,8 +257,7 @@ synchronizable_manager_merge (EphySynchronizableManager              *manager,
                               gpointer                                user_data)
 {
   EphyOpenTabsManager *self = EPHY_OPEN_TABS_MANAGER (manager);
-  EphyOpenTabsRecord *local_tabs;
-  GList *to_upload = NULL;
+  GPtrArray *to_upload;
   char *device_bso_id;
 
   device_bso_id = ephy_sync_utils_get_device_bso_id ();
@@ -276,8 +275,8 @@ synchronizable_manager_merge (EphySynchronizableManager              *manager,
   /* Only upload the local open tabs, we don't want to alter open tabs of
    * other clients. Also, overwrite any previous value by doing a force upload.
    */
-  local_tabs = ephy_open_tabs_manager_get_local_tabs (self);
-  to_upload = g_list_prepend (to_upload, local_tabs);
+  to_upload = g_ptr_array_new_with_free_func (g_object_unref);
+  g_ptr_array_add (to_upload, ephy_open_tabs_manager_get_local_tabs (self));
 
   g_free (device_bso_id);
 
diff --git a/lib/sync/ephy-password-manager.c b/lib/sync/ephy-password-manager.c
index 6695666..9d4ca54 100644
--- a/lib/sync/ephy-password-manager.c
+++ b/lib/sync/ephy-password-manager.c
@@ -861,14 +861,14 @@ delete_record_by_id (GList      *records,
   return records;
 }
 
-static GList *
+static GPtrArray *
 ephy_password_manager_handle_initial_merge (EphyPasswordManager *self,
                                             GList               *local_records,
                                             GList               *remote_records)
 {
   EphyPasswordRecord *record;
   GHashTable *dont_upload;
-  GList *to_upload = NULL;
+  GPtrArray *to_upload;
   const char *remote_id;
   const char *remote_origin;
   const char *remote_target_origin;
@@ -890,6 +890,7 @@ ephy_password_manager_handle_initial_merge (EphyPasswordManager *self,
    * same tuple but same tuple does not necessarily mean same ID. This is what
    * our merge logic is based on.
    */
+  to_upload = g_ptr_array_new_with_free_func (g_object_unref);
   dont_upload = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
 
   for (GList *l = remote_records; l && l->data; l = l->next) {
@@ -969,7 +970,7 @@ ephy_password_manager_handle_initial_merge (EphyPasswordManager *self,
   for (GList *l = local_records; l && l->data; l = l->next) {
     record = EPHY_PASSWORD_RECORD (l->data);
     if (!g_hash_table_contains (dont_upload, ephy_password_record_get_id (record)))
-      to_upload = g_list_prepend (to_upload, g_object_ref (record));
+      g_ptr_array_add (to_upload, g_object_ref (record));
   }
 
   g_hash_table_unref (dont_upload);
@@ -977,14 +978,14 @@ ephy_password_manager_handle_initial_merge (EphyPasswordManager *self,
   return to_upload;
 }
 
-static GList *
+static GPtrArray *
 ephy_password_manager_handle_regular_merge (EphyPasswordManager  *self,
                                             GList               **local_records,
                                             GList                *deleted_records,
                                             GList                *updated_records)
 {
   EphyPasswordRecord *record;
-  GList *to_upload = NULL;
+  GPtrArray *to_upload;
   const char *remote_id;
   const char *remote_origin;
   const char *remote_target_origin;
@@ -996,6 +997,8 @@ ephy_password_manager_handle_regular_merge (EphyPasswordManager  *self,
 
   g_assert (EPHY_IS_PASSWORD_MANAGER (self));
 
+  to_upload = g_ptr_array_new_with_free_func (g_object_unref);
+
   for (GList *l = deleted_records; l && l->data; l = l->next) {
     remote_id = ephy_password_record_get_id (l->data);
     record = get_record_by_id (*local_records, remote_id);
@@ -1031,7 +1034,7 @@ ephy_password_manager_handle_regular_merge (EphyPasswordManager  *self,
         local_timestamp = ephy_password_record_get_time_password_changed (record);
         if (local_timestamp > remote_timestamp) {
           /* Local record is newer. Keep it, upload it and delete remote record from server. */
-          to_upload = g_list_prepend (to_upload, g_object_ref (record));
+          g_ptr_array_add (to_upload, g_object_ref (record));
           g_signal_emit_by_name (self, "synchronizable-deleted", l->data);
         } else {
           /* Remote record is newer. Forget local record and store remote record. */
@@ -1052,15 +1055,13 @@ merge_cb (GList    *records,
           gpointer  user_data)
 {
   MergePasswordsAsyncData *data = (MergePasswordsAsyncData *)user_data;
-  GList *to_upload = NULL;
+  GPtrArray *to_upload;
 
   if (data->is_initial)
-    to_upload = ephy_password_manager_handle_initial_merge (data->manager,
-                                                            records,
+    to_upload = ephy_password_manager_handle_initial_merge (data->manager, records,
                                                             data->remotes_updated);
   else
-    to_upload = ephy_password_manager_handle_regular_merge (data->manager,
-                                                            &records,
+    to_upload = ephy_password_manager_handle_regular_merge (data->manager, &records,
                                                             data->remotes_deleted,
                                                             data->remotes_updated);
 
diff --git a/lib/sync/ephy-sync-service.c b/lib/sync/ephy-sync-service.c
index b66fb97..65326d5 100644
--- a/lib/sync/ephy-sync-service.c
+++ b/lib/sync/ephy-sync-service.c
@@ -1245,21 +1245,26 @@ ephy_sync_service_upload_synchronizable (EphySyncService           *self,
 }
 
 static void
-merge_collection_finished_cb (GList    *to_upload,
-                              gboolean  should_force,
-                              gpointer  user_data)
+merge_collection_finished_cb (GPtrArray *to_upload,
+                              gboolean   should_force,
+                              gpointer   user_data)
 {
   SyncCollectionAsyncData *data = (SyncCollectionAsyncData *)user_data;
 
-  for (GList *l = to_upload; l && l->data; l = l->next)
+  if (!to_upload || to_upload->len == 0)
+    goto out;
+
+  for (guint i = 0; i < to_upload->len; i++) {
     ephy_sync_service_upload_synchronizable (data->service, data->manager,
-                                             l->data, should_force);
+                                             g_ptr_array_index (to_upload, i),
+                                             should_force);
+  }
 
+out:
   if (data->is_last)
     g_signal_emit (data->service, signals[SYNC_FINISHED], 0);
-
   if (to_upload)
-    g_list_free_full (to_upload, g_object_unref);
+    g_ptr_array_unref (to_upload);
   sync_collection_async_data_free (data);
 }
 
diff --git a/lib/sync/ephy-synchronizable-manager.h b/lib/sync/ephy-synchronizable-manager.h
index 8e1804b..8c83e52 100644
--- a/lib/sync/ephy-synchronizable-manager.h
+++ b/lib/sync/ephy-synchronizable-manager.h
@@ -30,7 +30,7 @@ G_BEGIN_DECLS
 
 G_DECLARE_INTERFACE (EphySynchronizableManager, ephy_synchronizable_manager, EPHY, SYNCHRONIZABLE_MANAGER, 
GObject)
 
-typedef void (*EphySynchronizableManagerMergeCallback) (GList *to_upload, gboolean should_force, gpointer 
user_data);
+typedef void (*EphySynchronizableManagerMergeCallback) (GPtrArray *to_upload, gboolean should_force, 
gpointer user_data);
 
 struct _EphySynchronizableManagerInterface {
   GTypeInterface parent_iface;
diff --git a/src/bookmarks/ephy-bookmarks-manager.c b/src/bookmarks/ephy-bookmarks-manager.c
index ae0ce13..0abec5b 100644
--- a/src/bookmarks/ephy-bookmarks-manager.c
+++ b/src/bookmarks/ephy-bookmarks-manager.c
@@ -730,11 +730,11 @@ synchronizable_manager_save (EphySynchronizableManager *manager,
                                              NULL);
 }
 
-static GList *
+static GPtrArray *
 ephy_bookmarks_manager_handle_initial_merge (EphyBookmarksManager *self,
                                              GList                *remote_bookmarks)
 {
-  GList *to_upload = NULL;
+  GPtrArray *to_upload;
   EphyBookmark *bookmark;
   GSequence *bookmarks;
   GSequenceIter *iter;
@@ -743,6 +743,7 @@ ephy_bookmarks_manager_handle_initial_merge (EphyBookmarksManager *self,
 
   g_assert (EPHY_IS_BOOKMARKS_MANAGER (self));
 
+  to_upload = g_ptr_array_new_with_free_func (g_object_unref);
   dont_upload = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
 
   for (GList *l = remote_bookmarks; l && l->data; l = l->next) {
@@ -810,7 +811,7 @@ next:
        !g_sequence_iter_is_end (iter); iter = g_sequence_iter_next (iter)) {
     bookmark = g_sequence_get (iter);
     if (!g_hash_table_contains (dont_upload, ephy_bookmark_get_id (bookmark)))
-      to_upload = g_list_prepend (to_upload, g_object_ref (bookmark));
+      g_ptr_array_add (to_upload, g_object_ref (bookmark));
   }
 
   /* Commit changes to file. */
@@ -822,17 +823,19 @@ next:
   return to_upload;
 }
 
-static GList *
+static GPtrArray *
 ephy_bookmarks_manager_handle_regular_merge (EphyBookmarksManager *self,
                                              GList                *updated_bookmarks,
                                              GList                *deleted_bookmarks)
 {
-  GList *to_upload = NULL;
+  GPtrArray *to_upload;
   EphyBookmark *bookmark;
   double timestamp;
 
   g_assert (EPHY_IS_BOOKMARKS_MANAGER (self));
 
+  to_upload = g_ptr_array_new_with_free_func (g_object_unref);
+
   for (GList *l = deleted_bookmarks; l && l->data; l = l->next) {
     bookmark = ephy_bookmarks_manager_get_bookmark_by_id (self, ephy_bookmark_get_id (l->data));
     if (bookmark)
@@ -874,7 +877,7 @@ ephy_bookmarks_manager_handle_regular_merge (EphyBookmarksManager *self,
         ephy_bookmarks_manager_copy_tags_from_bookmark (self, bookmark, l->data);
         timestamp = ephy_synchronizable_get_server_time_modified (l->data);
         ephy_synchronizable_set_server_time_modified (EPHY_SYNCHRONIZABLE (bookmark), timestamp);
-        to_upload = g_list_prepend (to_upload, g_object_ref (bookmark));
+        g_ptr_array_add (to_upload, g_object_ref (bookmark));
       } else {
         /* Different id, different url. Add remote bookmark. */
         ephy_bookmarks_manager_add_bookmark_internal (self, l->data, FALSE);
@@ -906,15 +909,12 @@ synchronizable_manager_merge (EphySynchronizableManager              *manager,
                               gpointer                                user_data)
 {
   EphyBookmarksManager *self = EPHY_BOOKMARKS_MANAGER (manager);
-  GList *to_upload = NULL;
+  GPtrArray *to_upload;
 
   if (is_initial)
-    to_upload = ephy_bookmarks_manager_handle_initial_merge (self,
-                                                             remotes_updated);
+    to_upload = ephy_bookmarks_manager_handle_initial_merge (self, remotes_updated);
   else
-    to_upload = ephy_bookmarks_manager_handle_regular_merge (self,
-                                                             remotes_updated,
-                                                             remotes_deleted);
+    to_upload = ephy_bookmarks_manager_handle_regular_merge (self, remotes_updated, remotes_deleted);
 
   callback (to_upload, FALSE, user_data);
 }


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