[tracker/libtracker-bus: 35/44] libtracker-bus: Add updateblank support with functional test



commit 849a342cd711144d70430e9755dfac0eae8bffa9
Author: Philip Van Hoof <philip codeminded be>
Date:   Fri Jul 23 13:05:10 2010 +0200

    libtracker-bus: Add updateblank support with functional test

 src/libtracker-bus/Makefile.am                 |    4 +-
 src/libtracker-bus/tracker-bus-array-update.c  |  172 +++++++++++++++++++++++-
 src/libtracker-bus/tracker-bus-fd-update.c     |   71 +---------
 src/libtracker-bus/tracker-bus-shared.c        |   90 ++++++++++++
 src/libtracker-bus/tracker-bus-shared.h        |   26 ++++
 src/libtracker-bus/tracker-bus.vala            |   25 +---
 tests/functional-tests/shared-update-test.vala |   40 +++++-
 7 files changed, 333 insertions(+), 95 deletions(-)
---
diff --git a/src/libtracker-bus/Makefile.am b/src/libtracker-bus/Makefile.am
index 1061415..0ab110b 100644
--- a/src/libtracker-bus/Makefile.am
+++ b/src/libtracker-bus/Makefile.am
@@ -35,7 +35,9 @@ libtracker_bus_la_SOURCES = 	\
 	tracker-bus-fd-update.c				\
 	tracker-bus-fd-update.h				\
 	tracker-bus-array-update.c			\
-	tracker-bus-array-update.h
+	tracker-bus-array-update.h			\
+	tracker-bus-shared.c				\
+	tracker-bus-shared.h
 
 libtracker_bus_la_LDFLAGS = 	\
 	-module -avoid-version -no-undefined
diff --git a/src/libtracker-bus/tracker-bus-array-update.c b/src/libtracker-bus/tracker-bus-array-update.c
index 40121b6..c74adec 100644
--- a/src/libtracker-bus/tracker-bus-array-update.c
+++ b/src/libtracker-bus/tracker-bus-array-update.c
@@ -33,6 +33,94 @@
 
 #include "tracker-bus-array-update.h"
 #include "tracker-bus.h"
+#include "tracker-bus-shared.h"
+
+typedef struct {
+	DBusConnection *connection;
+	GCancellable *cancellable;
+	DBusPendingCall *dbus_call;
+	GSimpleAsyncResult *res;
+	gpointer user_data;
+	gulong cancelid;
+} AsyncData;
+
+
+static void
+async_data_free (gpointer data)
+{
+	AsyncData *fad = data;
+
+	if (fad) {
+		if (fad->cancellable) {
+			if (fad->cancelid != 0)
+				g_cancellable_disconnect (fad->cancellable, fad->cancelid);
+			g_object_unref (fad->cancellable);
+		}
+
+		if (fad->connection) {
+			dbus_connection_unref (fad->connection);
+		}
+
+		if (fad->res) {
+			/* Don't free, weak */
+		}
+
+		g_slice_free (AsyncData, fad);
+	}
+}
+
+static AsyncData *
+async_data_new (DBusConnection      *connection,
+                GCancellable        *cancellable,
+                gpointer             user_data)
+{
+	AsyncData *fad = g_slice_new0 (AsyncData);
+
+	fad->connection = dbus_connection_ref (connection);
+	if (cancellable)
+		fad->cancellable = g_object_ref (cancellable);
+	fad->user_data = user_data;
+
+	return fad;
+}
+
+
+
+static void
+sparql_update_callback (DBusPendingCall *call,
+                        void            *user_data)
+{
+	AsyncData *fad = user_data;
+	DBusMessage *reply;
+	GError *error = NULL;
+	GVariant *result;
+
+	/* Check for errors */
+	reply = dbus_pending_call_steal_reply (call);
+
+	if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR) {
+		DBusError dbus_error;
+
+		dbus_error_init (&dbus_error);
+		dbus_set_error_from_message (&dbus_error, reply);
+		dbus_set_g_error (&error, &dbus_error);
+		dbus_error_free (&dbus_error);
+		g_simple_async_result_set_from_error (fad->res, error);
+		g_simple_async_result_complete (fad->res);
+	} else {
+		result = tracker_bus_message_to_variant (reply);
+		g_simple_async_result_set_op_res_gpointer (fad->res, result, NULL);
+		g_simple_async_result_complete (fad->res);
+		g_variant_unref (result);
+	}
+
+	/* Clean up */
+	dbus_message_unref (reply);
+
+	async_data_free (fad);
+
+	dbus_pending_call_unref (call);
+}
 
 void
 tracker_bus_array_sparql_update_blank_async (DBusGConnection       *connection,
@@ -42,9 +130,42 @@ tracker_bus_array_sparql_update_blank_async (DBusGConnection       *connection,
                                              gpointer               user_data)
 {
 #ifdef HAVE_DBUS_FD_PASSING
+	DBusPendingCall *call;
+	DBusMessage *message;
+	AsyncData *fad;
+	DBusMessageIter iter;
+	DBusConnection *con;
+
+	g_return_if_fail (query != NULL);
+
+	con = dbus_g_connection_get_connection (connection);
+	fad = async_data_new (con, cancellable, user_data);
+
+	fad->res = g_simple_async_result_new (NULL, callback, user_data,
+	                                      tracker_bus_array_sparql_update_blank_async);
+
+
+	message = dbus_message_new_method_call (TRACKER_DBUS_SERVICE,
+	                                        TRACKER_DBUS_OBJECT_RESOURCES,
+	                                        TRACKER_DBUS_INTERFACE_RESOURCES,
+	                                        "SparqlUpdateBlank");
+
+	dbus_message_iter_init_append (message, &iter);
+	dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &query);
+	dbus_connection_send_with_reply (con, message, &call, -1);
+	dbus_message_unref (message);
+
+	if (!call) {
+		g_critical ("Could not initiate update: UpdateBlank Unsupported or connection disconnected");
+		g_object_unref (fad->res);
+		async_data_free (fad);
+		return;
+	}
+
+	fad->dbus_call = call;
+
+	dbus_pending_call_set_notify (call, sparql_update_callback, fad, NULL);
 
-	g_critical ("tracker_bus_array_sparql_update_blank_async unimplemented");
-	// todo
 
 #else  /* HAVE_DBUS_FD_PASSING */
 	g_assert_not_reached ();
@@ -58,18 +179,54 @@ tracker_bus_array_sparql_update_blank (DBusGConnection *connection,
                                        GError         **error)
 {
 #ifdef HAVE_DBUS_FD_PASSING
-	DBusMessage *reply;
+	DBusMessage *reply, *message;
 	GVariant *result;
+	DBusMessageIter iter;
+	DBusPendingCall *call;
 
 	g_return_val_if_fail (query != NULL, NULL);
 
-	g_critical ("tracker_bus_array_sparql_update_blank unimplemented");
-	// reply = todo
+	message = dbus_message_new_method_call (TRACKER_DBUS_SERVICE,
+	                                        TRACKER_DBUS_OBJECT_RESOURCES,
+	                                        TRACKER_DBUS_INTERFACE_RESOURCES,
+	                                        "SparqlUpdateBlank");
+
+	dbus_message_iter_init_append (message, &iter);
+	dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &query);
+	dbus_connection_send_with_reply (dbus_g_connection_get_connection (connection),
+	                                 message, &call, -1);
+	dbus_message_unref (message);
+
+	if (!call) {
+		g_set_error (error,
+		             TRACKER_SPARQL_ERROR,
+		             TRACKER_SPARQL_ERROR_UNSUPPORTED,
+		             "UpdateBlank Unsupported or connection disconnected");
+		return NULL;
+	}
+
+	dbus_pending_call_block (call);
+
+	reply = dbus_pending_call_steal_reply (call);
 
 	if (!reply) {
 		return NULL;
 	}
 
+	if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR) {
+		DBusError dbus_error;
+
+		dbus_error_init (&dbus_error);
+		dbus_set_error_from_message (&dbus_error, reply);
+		dbus_set_g_error (error, &dbus_error);
+		dbus_pending_call_unref (call);
+		dbus_error_free (&dbus_error);
+
+		return NULL;
+	}
+
+	dbus_pending_call_unref (call);
+
 	if (g_strcmp0 (dbus_message_get_signature (reply), "aaa{ss}")) {
 		g_set_error (error,
 		             TRACKER_SPARQL_ERROR,
@@ -79,7 +236,8 @@ tracker_bus_array_sparql_update_blank (DBusGConnection *connection,
 		return NULL;
 	}
 
-	//result = message_to_variant (reply);
+	result = tracker_bus_message_to_variant (reply);
+
 	dbus_message_unref (reply);
 
 	return result;
@@ -100,7 +258,7 @@ tracker_bus_array_sparql_update_blank_finish (GAsyncResult     *res,
 		return NULL;
 	}
 
-	return g_object_ref (g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res)));
+	return g_variant_ref (g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res)));
 #else /* HAVE_DBUS_FD_PASSING */
 	g_assert_not_reached ();
 	return NULL;
diff --git a/src/libtracker-bus/tracker-bus-fd-update.c b/src/libtracker-bus/tracker-bus-fd-update.c
index 8440b7e..2838cef 100644
--- a/src/libtracker-bus/tracker-bus-fd-update.c
+++ b/src/libtracker-bus/tracker-bus-fd-update.c
@@ -34,6 +34,7 @@
 
 #include "tracker-bus-fd-update.h"
 #include "tracker-bus.h"
+#include "tracker-bus-shared.h"
 
 #ifdef HAVE_DBUS_FD_PASSING
 
@@ -111,69 +112,6 @@ fast_async_data_new (DBusConnection    *connection,
 	return data;
 }
 
-/*
-static GHashTable *
-unmarshal_hash_table (DBusMessageIter *iter)
-{
-	GHashTable *result;
-	DBusMessageIter subiter, subsubiter;
-
-	result = g_hash_table_new_full (g_str_hash,
-	                                g_str_equal,
-	                                (GDestroyNotify) g_free,
-	                                (GDestroyNotify) g_free);
-
-	dbus_message_iter_recurse (iter, &subiter);
-
-	while (dbus_message_iter_get_arg_type (&subiter) != DBUS_TYPE_INVALID) {
-		const gchar *key, *value;
-
-		dbus_message_iter_recurse (&subiter, &subsubiter);
-		dbus_message_iter_get_basic (&subsubiter, &key);
-		dbus_message_iter_next (&subsubiter);
-		dbus_message_iter_get_basic (&subsubiter, &value);
-		g_hash_table_insert (result, g_strdup (key), g_strdup (value));
-
-		dbus_message_iter_next (&subiter);
-	}
-
-	return result;
-}
-
-static void
-free_inner_array (gpointer elem)
-{
-	g_ptr_array_free (elem, TRUE);
-}*/
-
-static GVariant*
-message_to_variant (DBusMessage *message)
-{
-	GVariant *result = NULL;
-	DBusMessageIter iter, subiter, subsubiter;
-
-//	result = g_ptr_array_new_with_free_func (free_inner_array);
-	dbus_message_iter_init (message, &iter);
-	dbus_message_iter_recurse (&iter, &subiter);
-
-	while (dbus_message_iter_get_arg_type (&subiter) != DBUS_TYPE_INVALID) {
-//		GPtrArray *inner_array;
-
-//		inner_array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_hash_table_destroy);
-//		g_ptr_array_add (result, inner_array);
-		dbus_message_iter_recurse (&subiter, &subsubiter);
-
-		while (dbus_message_iter_get_arg_type (&subsubiter) != DBUS_TYPE_INVALID) {
-//			g_ptr_array_add (inner_array, unmarshal_hash_table (&subsubiter));
-			dbus_message_iter_next (&subsubiter);
-		}
-
-		dbus_message_iter_next (&subiter);
-	}
-
-	return result;
-}
-
 static void
 sparql_update_fast_callback (DBusPendingCall *call,
                              void            *user_data)
@@ -213,10 +151,9 @@ sparql_update_fast_callback (DBusPendingCall *call,
 		g_simple_async_result_complete (fad->res);
 		break;
 	case FAST_UPDATE_BLANK:
-		result = message_to_variant (reply);
+		result = tracker_bus_message_to_variant (reply);
 		g_simple_async_result_set_op_res_gpointer (fad->res, result, NULL);
 		g_simple_async_result_complete (fad->res);
-		dbus_message_unref (reply);
 		g_variant_unref (result);
 
 		break;
@@ -463,7 +400,7 @@ tracker_bus_fd_sparql_update_blank_finish (GAsyncResult     *res,
 		return NULL;
 	}
 
-	return g_object_ref (g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res)));
+	return g_variant_ref (g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res)));
 #else /* HAVE_DBUS_FD_PASSING */
 	g_assert_not_reached ();
 	return NULL;
@@ -497,7 +434,7 @@ tracker_bus_fd_sparql_update_blank (DBusGConnection *connection,
 		return NULL;
 	}
 
-	result = message_to_variant (reply);
+	result = tracker_bus_message_to_variant (reply);
 	dbus_message_unref (reply);
 
 	return result;
diff --git a/src/libtracker-bus/tracker-bus-shared.c b/src/libtracker-bus/tracker-bus-shared.c
new file mode 100644
index 0000000..fbfd1d5
--- /dev/null
+++ b/src/libtracker-bus/tracker-bus-shared.c
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2006, Jamie McCracken <jamiemcc gnome org>
+ * Copyright (C) 2008-2010, Nokia <ivan frade nokia com>
+ * Copyright (C) 2010, Codeminded BVBA <philip codeminded be>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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 "config.h"
+
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <glib-object.h>
+
+#include <libtracker-common/tracker-common.h>
+#include <libtracker-sparql/tracker-sparql.h>
+
+#include "tracker-bus-shared.h"
+
+GVariant*
+tracker_bus_message_to_variant (DBusMessage *message)
+{
+	GVariantBuilder builder;
+	DBusMessageIter iter, subiter, subsubiter;
+
+	/* TODO: This is probably wrong, especially the hashtable part */
+
+	/*aaa{ss}*/
+
+	g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); /* a */
+
+	dbus_message_iter_init (message, &iter);
+	dbus_message_iter_recurse (&iter, &subiter);
+
+	while (dbus_message_iter_get_arg_type (&subiter) != DBUS_TYPE_INVALID) {
+
+		g_variant_builder_open (&builder, G_VARIANT_TYPE_ARRAY); /* a */
+
+		dbus_message_iter_recurse (&subiter, &subsubiter);
+
+		while (dbus_message_iter_get_arg_type (&subsubiter) != DBUS_TYPE_INVALID) {
+			DBusMessageIter s_subiter, s_subsubiter;
+
+			dbus_message_iter_recurse (&subsubiter, &s_subiter);
+
+			while (dbus_message_iter_get_arg_type (&s_subiter) != DBUS_TYPE_INVALID) {
+				const gchar *key, *value;
+
+				g_variant_builder_open (&builder, G_VARIANT_TYPE_ARRAY); /* a */
+				g_variant_builder_open (&builder, G_VARIANT_TYPE_DICTIONARY); /* {ss} */
+
+				dbus_message_iter_recurse (&s_subiter, &s_subsubiter);
+				dbus_message_iter_get_basic (&s_subsubiter, &key);
+				dbus_message_iter_next (&s_subsubiter);
+				dbus_message_iter_get_basic (&s_subsubiter, &value);
+
+				g_variant_builder_add (&builder, "{ss}", key, value);
+
+				dbus_message_iter_next (&s_subiter);
+
+				g_variant_builder_close (&builder);
+				g_variant_builder_close (&builder);
+			}
+
+			dbus_message_iter_next (&subsubiter);
+		}
+
+		g_variant_builder_close (&builder);
+
+		dbus_message_iter_next (&subiter);
+	}
+
+	return g_variant_builder_end (&builder);
+}
diff --git a/src/libtracker-bus/tracker-bus-shared.h b/src/libtracker-bus/tracker-bus-shared.h
new file mode 100644
index 0000000..85b2dff
--- /dev/null
+++ b/src/libtracker-bus/tracker-bus-shared.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2006, Jamie McCracken <jamiemcc gnome org>
+ * Copyright (C) 2008-2010, Nokia <ivan frade nokia com>
+ * Copyright (C) 2010, Codeminded BVBA <philip codeminded be>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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.
+ */
+
+G_BEGIN_DECLS
+
+GVariant* tracker_bus_message_to_variant (DBusMessage *message);
+
+G_END_DECLS
diff --git a/src/libtracker-bus/tracker-bus.vala b/src/libtracker-bus/tracker-bus.vala
index 64ee654..a53bd5b 100644
--- a/src/libtracker-bus/tracker-bus.vala
+++ b/src/libtracker-bus/tracker-bus.vala
@@ -140,30 +140,21 @@ public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
 	public override GLib.Variant? update_blank (string sparql, Cancellable? cancellable = null) throws Sparql.Error {
 		GLib.Variant res = null;
 
-		try {
-			if (use_steroids) {
-				res = tracker_bus_fd_sparql_update_blank (connection, sparql);
-			} else {
-				res = tracker_bus_array_sparql_update_blank (connection, sparql);
-			}
-		} catch (DBus.Error e) {
-			throw new Sparql.Error.INTERNAL (e.message);
+		if (use_steroids) {
+			res = tracker_bus_fd_sparql_update_blank (connection, sparql);
+		} else {
+			res = tracker_bus_array_sparql_update_blank (connection, sparql);
 		}
-
 		return res;
 	}
 
 	public async override GLib.Variant? update_blank_async (string sparql, int priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws Sparql.Error {
 		GLib.Variant res = null;
 
-		try {
-			if (use_steroids) {
-				res = yield tracker_bus_fd_sparql_update_blank_async (connection, sparql, cancellable);
-			} else {
-				res = yield tracker_bus_array_sparql_update_blank_async (connection, sparql, cancellable);
-			}
-		} catch (DBus.Error e) {
-			throw new Sparql.Error.INTERNAL (e.message);
+		if (use_steroids) {
+			res = yield tracker_bus_fd_sparql_update_blank_async (connection, sparql, cancellable);
+		} else {
+			res = yield tracker_bus_array_sparql_update_blank_async (connection, sparql, cancellable);
 		}
 
 		return res;
diff --git a/tests/functional-tests/shared-update-test.vala b/tests/functional-tests/shared-update-test.vala
index 3d6c57f..caa7bc4 100644
--- a/tests/functional-tests/shared-update-test.vala
+++ b/tests/functional-tests/shared-update-test.vala
@@ -30,6 +30,14 @@ public class TestApp : GLib.Object {
 		return (0);
 	}
 
+	int iter_variant (GLib.Variant variant) {
+
+		// TODO: Rest the return value, also check tracker_bus_message_to_variant
+		// in libtracker-bus/tracker-bus-shared.c
+
+		return 0;
+	}
+
 	void update_query () {
 		Cursor cursor;
 		int a;
@@ -48,7 +56,6 @@ public class TestApp : GLib.Object {
 			warning ("Couldn't query: %s", eb.message);
 			res = -1;
 		}
-
 	}
 
 	async void update_query_async () {
@@ -56,28 +63,55 @@ public class TestApp : GLib.Object {
 		int a;
 
 		try {
-			yield con.update_async ("INSERT { <test01> a nie:InformationElement ; nie:title 'test01' }");
+			yield con.update_async ("INSERT { <test02> a nie:InformationElement ; nie:title 'test01' }");
 		} catch (Tracker.Sparql.Error ea) {
 			warning ("Couldn't update: %s", ea.message);
 			res = -1;
 		}
 
 		try {
-			cursor = con.query ("SELECT ?title WHERE { <test01> nie:title ?title }");
+			cursor = con.query ("SELECT ?title WHERE { <test02> nie:title ?title }");
 			a = iter_cursor (cursor);
 		} catch (Tracker.Sparql.Error eb) {
 			warning ("Couldn't query: %s", eb.message);
 			res = -1;
 		}
+	}
+
+	void update_blank_query () {
+		GLib.Variant variant;
+		int a;
 
+		try {
+			variant = con.update_blank ("INSERT { _:a1 a nie:InformationElement  . _:b1 a nie:InformationElement . _:c1 a nie:InformationElement }");
+			a = iter_variant (variant);
+		} catch (Tracker.Sparql.Error ea) {
+			warning ("Couldn't update: %s", ea.message);
+			res = -1;
+		}
+	}
+
+	async void update_blank_query_async () {
+		GLib.Variant variant;
+		int a;
+
+		try {
+			variant = yield con.update_blank_async ("INSERT { _:a2 a nie:InformationElement  . _:b2 a nie:InformationElement . _:c2 a nie:InformationElement }");
+			a = iter_variant (variant);
+		} catch (Tracker.Sparql.Error ea) {
+			warning ("Couldn't update: %s", ea.message);
+			res = -1;
+		}
 	}
 
 	void do_sync_tests () {
 		update_query ();
+		update_blank_query ();
 	}
 
 	async void do_async_tests () {
 		yield update_query_async ();
+		yield update_blank_query_async ();
 
 		print ("Async tests done, now I can quit the mainloop\n");
 		loop.quit ();



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