No issues found
1 /*
2 * Copyright (C) 2011, Nokia <ivan.frade@nokia.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include <stdlib.h>
21
22 #include <libtracker-sparql/tracker-sparql.h>
23
24 typedef struct {
25 TrackerSparqlConnection *connection;
26 GCancellable *cancellable;
27 GTimer *timer;
28 GMainLoop *loop;
29 } MyData;
30
31 static void
32 cursor_cb (GObject *object,
33 GAsyncResult *res,
34 gpointer user_data)
35 {
36 TrackerSparqlCursor *cursor;
37 GError *error = NULL;
38 MyData *md = user_data;
39 gboolean more_results;
40
41 cursor = TRACKER_SPARQL_CURSOR (object);
42 more_results = tracker_sparql_cursor_next_finish (cursor,
43 res,
44 &error);
45
46 if (!error) {
47 static gint i = 0;
48
49 if (more_results) {
50 if (i++ < 5) {
51 if (i == 1) {
52 g_print ("Printing first 5 results:\n");
53 }
54
55 g_print (" %s\n", tracker_sparql_cursor_get_string (cursor, 0, NULL));
56
57 if (i == 5) {
58 g_print (" ...\n");
59 g_print (" Printing nothing for remaining results\n");
60 }
61 }
62
63 tracker_sparql_cursor_next_async (cursor,
64 md->cancellable,
65 cursor_cb,
66 md);
67 } else {
68 g_print ("\n");
69 g_print ("Async cursor next took: %.6f (for all %d results)\n",
70 g_timer_elapsed (md->timer, NULL), i);
71
72 g_object_unref (cursor);
73 g_main_loop_quit (md->loop);
74 }
75 } else {
76 g_critical ("Could not run cursor next: %s", error->message);
77
78 if (cursor) {
79 g_object_unref (cursor);
80 }
81
82 g_error_free (error);
83 g_main_loop_quit (md->loop);
84 }
85 }
86
87 static void
88 query_cb (GObject *object,
89 GAsyncResult *res,
90 gpointer user_data)
91 {
92 TrackerSparqlCursor *cursor;
93 GError *error = NULL;
94 MyData *md = user_data;
95
96 cursor = tracker_sparql_connection_query_finish (TRACKER_SPARQL_CONNECTION (object),
97 res,
98 &error);
99 g_print ("Async query took: %.6f\n", g_timer_elapsed (md->timer, NULL));
100
101 g_timer_start (md->timer);
102
103 if (!error) {
104 tracker_sparql_cursor_next_async (cursor,
105 md->cancellable,
106 cursor_cb,
107 md);
108 } else {
109 g_critical ("Could not run query: %s", error->message);
110
111 if (cursor) {
112 g_object_unref (cursor);
113 }
114
115 g_error_free (error);
116 g_main_loop_quit (md->loop);
117 }
118 }
119
120 static void
121 connection_cb (GObject *object,
122 GAsyncResult *res,
123 gpointer user_data)
124 {
125 MyData *md = user_data;
126 GError *error = NULL;
127
128 md->connection = tracker_sparql_connection_get_finish (res, &error);
129 g_print ("Async connection took: %.6f\n", g_timer_elapsed (md->timer, NULL));
130
131 g_timer_start (md->timer);
132
133 if (!error) {
134 tracker_sparql_connection_query_async (md->connection,
135 "SELECT ?r { ?r a rdfs:Resource }",
136 md->cancellable,
137 query_cb,
138 md);
139 } else {
140 g_critical ("Could not connect: %s", error->message);
141 g_error_free (error);
142 g_main_loop_quit (md->loop);
143 }
144 }
145
146 gint
147 main (gint argc, gchar *argv[])
148 {
149 MyData *md;
150
151 md = g_new0 (MyData, 1);
152 md->loop = g_main_loop_new (NULL, FALSE);
153 md->timer = g_timer_new ();
154 md->cancellable = g_cancellable_new ();
155
156 tracker_sparql_connection_get_async (md->cancellable,
157 connection_cb,
158 md);
159
160 g_main_loop_run (md->loop);
161
162 if (md->connection) {
163 g_object_unref (md->connection);
164 }
165
166 g_cancellable_cancel (md->cancellable);
167 g_object_unref (md->cancellable);
168 g_timer_destroy (md->timer);
169 g_main_loop_unref (md->loop);
170
171 g_free (md);
172
173 return EXIT_SUCCESS;
174 }