From 9a97fceb91e35a2ac4030c665a45f336ef814cbd Mon Sep 17 00:00:00 2001 From: Philip Van Hoof Date: Tue, 22 Feb 2011 12:48:21 +0100 Subject: examples: Added an example that uses async connection construction --- examples/Makefile.am | 10 ++-- examples/async-connection/.gitignore | 1 + examples/async-connection/Makefile.am | 13 +++++ examples/async-connection/async-connection.c | 77 ++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 examples/async-connection/.gitignore create mode 100644 examples/async-connection/Makefile.am create mode 100644 examples/async-connection/async-connection.c (limited to 'examples') diff --git a/examples/Makefile.am b/examples/Makefile.am index 0098884cd..21eb6291f 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,7 +1,7 @@ include $(top_srcdir)/Makefile.decl -SUBDIRS = \ - libtracker-extract \ - libtracker-miner \ - rss-reader \ - class-signal +SUBDIRS = libtracker-extract \ + libtracker-miner \ + rss-reader \ + class-signal \ + async-connection diff --git a/examples/async-connection/.gitignore b/examples/async-connection/.gitignore new file mode 100644 index 000000000..74a09d9c7 --- /dev/null +++ b/examples/async-connection/.gitignore @@ -0,0 +1 @@ +async-connection diff --git a/examples/async-connection/Makefile.am b/examples/async-connection/Makefile.am new file mode 100644 index 000000000..84a22ea9e --- /dev/null +++ b/examples/async-connection/Makefile.am @@ -0,0 +1,13 @@ +include $(top_srcdir)/Makefile.decl + +noinst_PROGRAMS = async-connection + +AM_CPPFLAGS = $(BUILD_CFLAGS) \ + -I$(top_srcdir)/src \ + $(LIBTRACKER_SPARQL_CFLAGS) + +LDADD = $(top_builddir)/src/libtracker-sparql/libtracker-sparql-$(TRACKER_API_VERSION).la \ + $(BUILD_LIBS) \ + $(LIBTRACKER_SPARQL_LIBS) + +async_connection_SOURCES = async-connection.c diff --git a/examples/async-connection/async-connection.c b/examples/async-connection/async-connection.c new file mode 100644 index 000000000..ea60476ea --- /dev/null +++ b/examples/async-connection/async-connection.c @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2009, Nokia + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include +#include + +typedef struct { + GMainLoop *loop; + GTimer *timer; +} LoopTimer; + +static void +on_connection (GObject *object, GAsyncResult *res, gpointer user_data) +{ + LoopTimer *lt = user_data; + GError *error = NULL; + gdouble ct; + TrackerSparqlConnection *con = tracker_sparql_connection_get_finish (res, &error); + + ct = g_timer_elapsed (lt->timer, NULL); + + g_timer_start (lt->timer); + + 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)); + } + g_object_unref (cursor); + g_object_unref (con); + } else { + g_critical ("%s", error->message); + g_error_free (error); + } + + g_print ("Async construction took: %.6f\n", ct); + g_print ("Query took: %.6f\n", g_timer_elapsed (lt->timer, NULL)); + + g_main_loop_quit (lt->loop); +} + +gint +main (gint argc, gchar *argv[]) +{ + LoopTimer lt; + + g_type_init (); + lt.loop = g_main_loop_new (NULL, FALSE); + + lt.timer = g_timer_new (); + + tracker_sparql_connection_get_async (NULL, on_connection, <); + + g_main_loop_run (lt.loop); + + g_timer_destroy (lt.timer); + g_main_loop_unref (lt.loop); + + return 0; +} -- cgit v1.2.1