2 * Copyright (C) 2011, Nokia <ivan.frade@nokia.com>
3 * Copyright (C) 2015, Noel Power <nopower@suse.com>
4 * Copyright (C) 2016, Ralph Boehme <slow@samba.org.>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include "lib/util/debug.h"
24 #include "lib/cmdline/cmdline.h"
27 * glib uses TRUE and FALSE which was redefined by "includes.h" to be
28 * unusable, undefine so glib can establish its own working
34 #include <libtracker-sparql/tracker-sparql.h>
35 #include "lib/tevent_glib_glue.h"
37 enum loop_type
{TEVENT_LOOP
, GLIB_LOOP
};
40 enum loop_type loop_type
;
41 TrackerSparqlConnection
*connection
;
42 GCancellable
*cancellable
;
45 struct tevent_context
*ev
;
46 struct tevent_glib_glue
*glue
;
49 static void cleanup(struct test_state
*state
)
51 g_cancellable_cancel(state
->cancellable
);
52 g_object_unref(state
->cancellable
);
53 g_timer_destroy(state
->timer
);
54 if (state
->connection
!= NULL
) {
55 g_object_unref(state
->connection
);
56 state
->connection
= NULL
;
58 if (state
->loop_type
== GLIB_LOOP
) {
59 g_main_loop_quit(state
->loop
);
61 samba_tevent_glib_glue_quit(state
->glue
);
65 static void cursor_cb(GObject
*object
,
69 struct test_state
*state
= talloc_get_type_abort(
70 user_data
, struct test_state
);
71 TrackerSparqlCursor
*cursor
= NULL
;
73 gboolean more_results
;
76 cursor
= TRACKER_SPARQL_CURSOR(object
);
77 more_results
= tracker_sparql_cursor_next_finish(cursor
,
81 g_critical("Could not run cursor next: %s", error
->message
);
84 g_object_unref(cursor
);
94 g_print("\nAsync cursor next took: %.6f (for all %d results)\n",
95 g_timer_elapsed (state
->timer
, NULL
), i
);
97 g_object_unref(cursor
);
103 int num_cols
= tracker_sparql_cursor_get_n_columns(cursor
);
107 g_print("Printing first 5 results:\n");
109 for (col
= 0; col
< num_cols
; col
++) {
110 g_print(" %s ", tracker_sparql_cursor_get_string(
112 if (col
== num_cols
-1 ) {
119 g_print(" Printing nothing for remaining results\n");
123 tracker_sparql_cursor_next_async(cursor
,
129 static void query_cb(GObject
*object
,
133 struct test_state
*state
= talloc_get_type_abort(
134 user_data
, struct test_state
);
135 TrackerSparqlCursor
*cursor
= NULL
;
136 GError
*error
= NULL
;
138 g_print("Async query took: %.6f\n", g_timer_elapsed(state
->timer
, NULL
));
140 cursor
= tracker_sparql_connection_query_finish(
141 TRACKER_SPARQL_CONNECTION(object
),
145 g_critical("Could not run query: %s", error
->message
);
148 g_object_unref(cursor
);
156 g_timer_start(state
->timer
);
158 tracker_sparql_cursor_next_async(cursor
,
164 static void connection_cb(GObject
*object
,
168 struct test_state
*state
= talloc_get_type_abort(
169 user_data
, struct test_state
);
170 GError
*error
= NULL
;
172 g_print("Async connection took: %.6f\n",
173 g_timer_elapsed(state
->timer
, NULL
));
175 state
->connection
= tracker_sparql_connection_get_finish(res
, &error
);
177 g_critical("Could not connect: %s", error
->message
);
183 g_timer_start(state
->timer
);
185 tracker_sparql_connection_query_async(
187 "SELECT ?name nie:mimeType(?s) nfo:fileName(?s) "
188 "WHERE { {?s nie:url ?name}}",
194 static void debug_fn(void *private_data
,
195 enum tevent_debug_level level
,
202 int main(int argc
, const char **argv
)
204 TALLOC_CTX
*mem_ctx
= NULL
;
205 struct test_state
*state
= NULL
;
209 struct poptOption long_options
[] = {
212 .longName
= "tevent",
214 .argInfo
= POPT_ARG_NONE
,
216 .descrip
= "Use tevent loop",
221 .argInfo
= POPT_ARG_NONE
,
223 .descrip
= "Use glib loop",
230 mem_ctx
= talloc_new(NULL
);
231 if (mem_ctx
== NULL
) {
235 state
= talloc_zero(mem_ctx
, struct test_state
);
240 state
->loop_type
= TEVENT_LOOP
;
244 ok
= samba_cmdline_init(mem_ctx
,
245 SAMBA_CMDLINE_CONFIG_CLIENT
,
246 true /* require_smbconf */);
248 TALLOC_FREE(mem_ctx
);
252 pc
= samba_popt_get_context(getprogname(),
256 POPT_CONTEXT_KEEP_FIRST
);
258 TALLOC_FREE(mem_ctx
);
262 while ((c
= poptGetNextOpt(pc
)) != -1) {
265 state
->loop_type
= GLIB_LOOP
;
268 state
->loop_type
= TEVENT_LOOP
;
270 case POPT_ERROR_BADOPT
:
271 fprintf(stderr
, "\nInvalid option %s: %s\n\n",
272 poptBadOption(pc
, 0), poptStrerror(c
));
273 poptPrintUsage(pc
, stderr
, 0);
278 if (state
->loop_type
== GLIB_LOOP
) {
279 state
->loop
= g_main_loop_new(NULL
, false);
281 state
->ev
= tevent_context_init(mem_ctx
);
282 if (CHECK_DEBUGLVL(10)) {
283 tevent_set_debug(state
->ev
, debug_fn
, NULL
);
285 state
->glue
= samba_tevent_glib_glue_create(
286 mem_ctx
, state
->ev
, g_main_context_default());
287 if (state
->glue
== NULL
) {
288 printf("tevent_glib_glue_create failed\n");
293 state
->timer
= g_timer_new();
294 state
->cancellable
= g_cancellable_new();
296 tracker_sparql_connection_get_async(state
->cancellable
,
300 if (state
->loop_type
== GLIB_LOOP
) {
301 printf("entering g_main_loop_run\n");
302 g_main_loop_run(state
->loop
);
304 printf("entering tevent_loop_wait\n");
305 tevent_loop_wait(state
->ev
);
307 TALLOC_FREE(state
->glue
);
308 TALLOC_FREE(state
->ev
);
311 TALLOC_FREE(mem_ctx
);