[tracker] examples/async-connection: Updated to be fully async



commit 5fd110e0d90067b292017277f6a5eb4b3edebe78
Author: Martyn Russell <martyn lanedo com>
Date:   Wed Mar 2 11:01:05 2011 +0000

    examples/async-connection: Updated to be fully async

 examples/async-connection/async-connection.c |  155 +++++++++++++++++++++-----
 1 files changed, 126 insertions(+), 29 deletions(-)
---
diff --git a/examples/async-connection/async-connection.c b/examples/async-connection/async-connection.c
index ea60476..e253932 100644
--- a/examples/async-connection/async-connection.c
+++ b/examples/async-connection/async-connection.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, Nokia <ivan frade nokia com>
+ * Copyright (C) 2011, Nokia <ivan frade nokia com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public
@@ -17,61 +17,158 @@
  * Boston, MA  02110-1301, USA.
  */
 
-#include <gio/gio.h>
+#include <stdlib.h>
+
 #include <libtracker-sparql/tracker-sparql.h>
 
 typedef struct {
-	GMainLoop *loop;
+	TrackerSparqlConnection *connection;
+	GCancellable *cancellable;
 	GTimer *timer;
-} LoopTimer;
+	GMainLoop *loop;
+} MyData;
 
 static void
-on_connection (GObject *object, GAsyncResult *res, gpointer user_data)
+cursor_cb (GObject      *object,
+           GAsyncResult *res,
+           gpointer      user_data)
 {
-	LoopTimer *lt = user_data;
+	TrackerSparqlCursor *cursor;
 	GError *error = NULL;
-	gdouble ct;
-	TrackerSparqlConnection *con = tracker_sparql_connection_get_finish (res, &error);
+	MyData *md = user_data;
+	gboolean more_results;
 
-	ct = g_timer_elapsed (lt->timer, NULL);
-
-	g_timer_start (lt->timer);
+	cursor = TRACKER_SPARQL_CURSOR (object);
+	more_results = tracker_sparql_cursor_next_finish (cursor,
+	                                                  res,
+	                                                  &error);
 
 	if (!error) {
-		TrackerSparqlCursor *cursor;
-		cursor = tracker_sparql_connection_query (con, "SELECT ?r { ?r a rdfs:Resource }", NULL, NULL);
-		while (tracker_sparql_cursor_next (cursor, NULL, NULL)) {
-			g_print ("%s\n", tracker_sparql_cursor_get_string (cursor, 0, NULL));
+		static gint i = 0;
+
+		if (more_results) {
+			if (i++ < 5) {
+				if (i == 1) {
+					g_print ("Printing first 5 results:\n");
+				}
+
+				g_print ("  %s\n", tracker_sparql_cursor_get_string (cursor, 0, NULL));
+
+				if (i == 5) {
+					g_print ("  ...\n");
+					g_print ("  Printing nothing for remaining results\n");
+				}
+			}
+
+			tracker_sparql_cursor_next_async (cursor,
+			                                  md->cancellable,
+			                                  cursor_cb,
+			                                  md);
+		} else {
+			g_print ("\n");
+			g_print ("Async cursor next took: %.6f (for all %d results)\n",
+			         g_timer_elapsed (md->timer, NULL), i);
+
+			g_object_unref (cursor);
+			g_main_loop_quit (md->loop);
 		}
-		g_object_unref (cursor);
-		g_object_unref (con);	
 	} else {
-		g_critical ("%s", error->message);
+		g_critical ("Could not run cursor next: %s", error->message);
+
+		if (cursor) {
+			g_object_unref (cursor);
+		}
+
 		g_error_free (error);
+		g_main_loop_quit (md->loop);
 	}
+}
+
+static void
+query_cb (GObject      *object,
+          GAsyncResult *res,
+          gpointer      user_data)
+{
+	TrackerSparqlCursor *cursor;
+	GError *error = NULL;
+	MyData *md = user_data;
+
+	cursor = tracker_sparql_connection_query_finish (TRACKER_SPARQL_CONNECTION (object),
+	                                                 res,
+	                                                 &error);
+	g_print ("Async query took: %.6f\n", g_timer_elapsed (md->timer, NULL));
 
-	g_print ("Async construction took: %.6f\n", ct);
-	g_print ("Query took: %.6f\n", g_timer_elapsed (lt->timer, NULL));
+	g_timer_start (md->timer);
 
-	g_main_loop_quit (lt->loop);
+	if (!error) {
+		tracker_sparql_cursor_next_async (cursor,
+		                                  md->cancellable,
+		                                  cursor_cb,
+		                                  md);
+	} else {
+		g_critical ("Could not run query: %s", error->message);
+
+		if (cursor) {
+			g_object_unref (cursor);
+		}
+
+		g_error_free (error);
+		g_main_loop_quit (md->loop);
+	}
+}
+
+static void
+connection_cb (GObject      *object,
+               GAsyncResult *res,
+               gpointer      user_data)
+{
+	MyData *md = user_data;
+	GError *error = NULL;
+
+	md->connection = tracker_sparql_connection_get_finish (res, &error);
+	g_print ("Async connection took: %.6f\n", g_timer_elapsed (md->timer, NULL));
+
+	g_timer_start (md->timer);
+
+	if (!error) {
+		tracker_sparql_connection_query_async (md->connection,
+		                                       "SELECT ?r { ?r a rdfs:Resource }",
+		                                       md->cancellable,
+		                                       query_cb,
+		                                       md);
+	} else {
+		g_critical ("Could not connect: %s", error->message);
+		g_error_free (error);
+		g_main_loop_quit (md->loop);
+	}
 }
 
 gint
 main (gint argc, gchar *argv[])
 {
-	LoopTimer lt;
+	MyData *md;
 
 	g_type_init ();
-	lt.loop = g_main_loop_new (NULL, FALSE);
 
-	lt.timer = g_timer_new ();
+	md = g_new0 (MyData, 1);
+	md->loop = g_main_loop_new (NULL, FALSE);
+	md->timer = g_timer_new ();
+	md->cancellable = g_cancellable_new ();
 
-	tracker_sparql_connection_get_async (NULL, on_connection, &lt);
+	tracker_sparql_connection_get_async (md->cancellable,
+	                                     connection_cb,
+	                                     md);
 
-	g_main_loop_run (lt.loop);
+	g_main_loop_run (md->loop);
+
+	if (md->connection) {
+		g_object_unref (md->connection);
+	}
 
-	g_timer_destroy (lt.timer);
-	g_main_loop_unref (lt.loop);
+	g_cancellable_cancel (md->cancellable);
+	g_object_unref (md->cancellable);
+	g_timer_destroy (md->timer);
+	g_main_loop_unref (md->loop);
 
-	return 0;
+	return EXIT_SUCCESS;
 }



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