[grilo-plugins/wip/mschraal/tracker3-resolve-async] tracker3: Make resolve async




commit 2f4139c286ed451dabc22bc798c7fb4c38237c44
Author: Marinus Schraal <mschraal gnome org>
Date:   Mon Aug 16 21:13:30 2021 +0200

    tracker3: Make resolve async
    
    tracker_sparql_cursor_next is sync and can block, make it use the async
    API instead.

 src/tracker3/grl-tracker-source-api.c | 82 ++++++++++++++++++++++++++---------
 1 file changed, 61 insertions(+), 21 deletions(-)
---
diff --git a/src/tracker3/grl-tracker-source-api.c b/src/tracker3/grl-tracker-source-api.c
index 807a464f..aa4d100b 100644
--- a/src/tracker3/grl-tracker-source-api.c
+++ b/src/tracker3/grl-tracker-source-api.c
@@ -339,6 +339,61 @@ TRACKER_QUERY_CB(GrlSourceQuerySpec, query, QUERY)
 TRACKER_QUERY_CB(GrlSourceBrowseSpec, browse, BROWSE)
 TRACKER_QUERY_CB(GrlSourceSearchSpec, search, SEARCH)
 
+static void
+tracker_resolve_result_cb (GObject      *source_object,
+                           GAsyncResult *result,
+                           GrlTrackerOp *os)
+{
+  TrackerSparqlCursor  *cursor = TRACKER_SPARQL_CURSOR (source_object);
+  gint                  col;
+  GError               *tracker_error = NULL, *error = NULL;
+  GrlSourceResolveSpec *rs = (GrlSourceResolveSpec *) os->data;
+
+  GRL_ODEBUG ("%s", __FUNCTION__);
+
+  if (!tracker_sparql_cursor_next_finish (cursor,
+                                          result,
+                                          &tracker_error)) {
+    if (tracker_error != NULL) {
+      GRL_WARNING ("\terror in parsing resolve id=%u : %s",
+                   rs->operation_id, tracker_error->message);
+
+      if (!g_error_matches (tracker_error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+        error = g_error_new (GRL_CORE_ERROR,
+                             GRL_CORE_ERROR_RESOLVE_FAILED,
+                             _("Failed to resolve: %s"),
+                             tracker_error->message);
+
+      rs->callback (rs->source, rs->operation_id, rs->media, rs->user_data, NULL);
+
+      g_clear_error (&error);
+      g_error_free (tracker_error);
+    } else {
+      GRL_ODEBUG ("\tend of parsing id=%u :)", rs->operation_id);
+
+      rs->callback (rs->source, rs->operation_id, rs->media, rs->user_data, NULL);
+    }
+
+    grl_tracker_op_free (os);
+    g_object_unref (cursor);
+    return;
+  }
+
+  /* Translate Sparql result into Grilo result */
+  for (col = 0 ; col < tracker_sparql_cursor_get_n_columns (cursor) ; col++) {
+    fill_grilo_media_from_sparql (GRL_TRACKER_SOURCE (rs->source),
+                                  rs->media, cursor, col);
+  }
+  set_title_from_filename (rs->media);
+
+  rs->callback (rs->source, rs->operation_id, rs->media, rs->user_data, NULL);
+
+  /* Schedule the next row to parse */
+  tracker_sparql_cursor_next_async (cursor, NULL,
+                                    (GAsyncReadyCallback) tracker_resolve_result_cb,
+                                    (gpointer) os);
+}
+
 static void
 tracker_resolve_cb (GObject      *source_object,
                     GAsyncResult *result,
@@ -346,7 +401,6 @@ tracker_resolve_cb (GObject      *source_object,
 {
   TrackerSparqlStatement *statement = TRACKER_SPARQL_STATEMENT (source_object);
   GrlSourceResolveSpec *rs = (GrlSourceResolveSpec *) os->data;
-  gint                  col;
   GError               *tracker_error = NULL, *error = NULL;
   TrackerSparqlCursor  *cursor;
 
@@ -355,23 +409,6 @@ tracker_resolve_cb (GObject      *source_object,
   cursor = tracker_sparql_statement_execute_finish (statement,
                                                     result, &tracker_error);
 
-  if (!cursor)
-    goto end_operation;
-
-  if (tracker_sparql_cursor_next (cursor, NULL, &tracker_error)) {
-    /* Translate Sparql result into Grilo result */
-    for (col = 0 ; col < tracker_sparql_cursor_get_n_columns (cursor) ; col++) {
-      fill_grilo_media_from_sparql (GRL_TRACKER_SOURCE (rs->source),
-                                    rs->media, cursor, col);
-    }
-    set_title_from_filename (rs->media);
-
-    rs->callback (rs->source, rs->operation_id, rs->media, rs->user_data, NULL);
-  } else if (!tracker_error) {
-    rs->callback (rs->source, rs->operation_id, rs->media, rs->user_data, NULL);
-  }
-
- end_operation:
   if (tracker_error) {
     GRL_WARNING ("Could not execute sparql resolve query : %s",
                  tracker_error->message);
@@ -385,11 +422,14 @@ tracker_resolve_cb (GObject      *source_object,
 
     g_error_free (tracker_error);
     g_error_free (error);
-  }
+    grl_tracker_op_free (os);
 
-  g_clear_object (&cursor);
+    return;
+  }
 
-  grl_tracker_op_free (os);
+  tracker_sparql_cursor_next_async (cursor, NULL,
+                                    (GAsyncReadyCallback) tracker_resolve_result_cb,
+                                    (gpointer) os);
 }
 
 static void


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