1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
/*
* 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
* 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 <stdlib.h>
#include <libtracker-sparql/tracker-sparql.h>
typedef struct {
TrackerSparqlConnection *connection;
GCancellable *cancellable;
GTimer *timer;
GMainLoop *loop;
} MyData;
static void
cursor_cb (GObject *object,
GAsyncResult *res,
gpointer user_data)
{
TrackerSparqlCursor *cursor;
GError *error = NULL;
MyData *md = user_data;
gboolean more_results;
cursor = TRACKER_SPARQL_CURSOR (object);
more_results = tracker_sparql_cursor_next_finish (cursor,
res,
&error);
if (!error) {
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);
tracker_sparql_cursor_close (cursor);
g_object_unref (cursor);
g_main_loop_quit (md->loop);
}
} else {
g_critical ("Could not run cursor next: %s", error->message);
g_error_free (error);
tracker_sparql_cursor_close (cursor);
g_object_unref (cursor);
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_timer_start (md->timer);
if (!error) {
tracker_sparql_cursor_next_async (cursor,
md->cancellable,
cursor_cb,
md);
} else {
g_critical ("Could not run query: %s", error->message);
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_new_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[])
{
g_autoptr(GFile) store, ontology;
MyData *md;
if (argc > 1) {
store = g_file_new_for_commandline_arg (argv[1]);
} else {
g_print ("Usage: <command> <store-path>\n");
exit (1);
}
md = g_new0 (MyData, 1);
md->loop = g_main_loop_new (NULL, FALSE);
md->timer = g_timer_new ();
md->cancellable = g_cancellable_new ();
ontology = tracker_sparql_get_ontology_nepomuk ();
tracker_sparql_connection_new_async (0,
store,
ontology,
md->cancellable,
connection_cb,
md);
g_main_loop_run (md->loop);
if (md->connection) {
g_object_unref (md->connection);
}
g_cancellable_cancel (md->cancellable);
g_object_unref (md->cancellable);
g_timer_destroy (md->timer);
g_main_loop_unref (md->loop);
g_free (md);
return EXIT_SUCCESS;
}
|