[tracker/dbus-fd-experiment: 11/15] Steroids: Simplify query



commit 03fd02f02ff0b8e9f8c4b316f7333ddc00b30cf9
Author: Adrien Bustany <abustany gnome org>
Date:   Thu Jun 3 10:22:57 2010 -0400

    Steroids: Simplify query
    
    This commit simplifies queries dones using DBus FD passing the same way
    as the following commit: the client passes the server the query, and a
    FD where to write the results.

 src/libtracker-client/tracker.c      |   52 +++----
 src/tracker-store/tracker-steroids.c |  259 +++++++++++++---------------------
 2 files changed, 118 insertions(+), 193 deletions(-)
---
diff --git a/src/libtracker-client/tracker.c b/src/libtracker-client/tracker.c
index 60d6cba..98db7da 100644
--- a/src/libtracker-client/tracker.c
+++ b/src/libtracker-client/tracker.c
@@ -1320,17 +1320,26 @@ tracker_resources_sparql_query_iterate (TrackerClient  *client,
 	TrackerClientPrivate *private;
 	DBusConnection *connection;
 	DBusMessage *message;
+	DBusMessageIter iter;
 	DBusMessage *reply;
 	DBusPendingCall *call;
-	guint query_id;
 	DBusError dbus_error;
 	TrackerResultIterator *iterator;
+	int pipefd[2];
 
 	g_return_val_if_fail (TRACKER_IS_CLIENT (client), NULL);
 	g_return_val_if_fail (query, NULL);
 
 	private = TRACKER_CLIENT_GET_PRIVATE (client);
 
+	if (pipe (pipefd) < 0) {
+		g_set_error (error,
+		             TRACKER_CLIENT_ERROR,
+		             TRACKER_CLIENT_ERROR_UNSUPPORTED,
+		             "Cannot open pipe");
+		return NULL;
+	}
+
 	connection = dbus_g_connection_get_connection (private->connection);
 
 	dbus_error_init (&dbus_error);
@@ -1338,42 +1347,18 @@ tracker_resources_sparql_query_iterate (TrackerClient  *client,
 	message = dbus_message_new_method_call (TRACKER_STEROIDS_SERVICE,
 	                                        TRACKER_STEROIDS_PATH,
 	                                        TRACKER_STEROIDS_INTERFACE,
-	                                        "PrepareQuery");
-	dbus_message_append_args (message,
-	                          DBUS_TYPE_STRING, &query,
-	                          DBUS_TYPE_INVALID);
-	reply = dbus_connection_send_with_reply_and_block (connection,
-	                                                   message,
-	                                                   -1,
-	                                                   &dbus_error);
-	dbus_message_unref (message);
-
-	if (!reply) {
-		dbus_set_g_error (error, &dbus_error);
-		return NULL;
-	}
-
-	iterator = g_slice_new0 (TrackerResultIterator);
-	iterator->buffer_size = TRACKER_STEROIDS_BUFFER_SIZE;
+	                                        "Query");
 
-	dbus_message_get_args (reply,
-	                       &dbus_error,
-	                       DBUS_TYPE_UNIX_FD, &iterator->fd,
-	                       DBUS_TYPE_UINT32,  &query_id,
-	                       DBUS_TYPE_INVALID);
-	dbus_message_unref (reply);
+	dbus_message_iter_init_append (message, &iter);
+	dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &query);
+	dbus_message_iter_append_basic (&iter, DBUS_TYPE_UNIX_FD, &pipefd[1]);
 
-	message = dbus_message_new_method_call (TRACKER_STEROIDS_SERVICE,
-	                                        TRACKER_STEROIDS_PATH,
-	                                        TRACKER_STEROIDS_INTERFACE,
-	                                        "Fetch");
-	dbus_message_append_args (message,
-	                          DBUS_TYPE_UINT32, &query_id,
-	                          DBUS_TYPE_INVALID);
 	dbus_connection_send_with_reply (connection,
 	                                 message,
 	                                 &call,
 	                                 -1);
+	dbus_message_unref (message);
+	close (pipefd[1]);
 
 	if (!call) {
 		g_set_error (error,
@@ -1383,7 +1368,11 @@ tracker_resources_sparql_query_iterate (TrackerClient  *client,
 		return NULL;
 	}
 
+	iterator = g_slice_new0 (TrackerResultIterator);
+	iterator->fd = pipefd[0];
+	iterator->buffer_size = TRACKER_STEROIDS_BUFFER_SIZE;
 	iterator->buffer = g_malloc (TRACKER_STEROIDS_BUFFER_SIZE);
+
 	if (iterator_buffer_fill (iterator) < 0) {
 		g_set_error (error,
 		             TRACKER_CLIENT_ERROR,
@@ -1392,6 +1381,7 @@ tracker_resources_sparql_query_iterate (TrackerClient  *client,
 		tracker_result_iterator_free (iterator);
 		return NULL;
 	}
+
 	iterator->buffer_index = 0;
 	iterator->rc = iterator_buffer_read_int (iterator);
 
diff --git a/src/tracker-store/tracker-steroids.c b/src/tracker-store/tracker-steroids.c
index 9456834..bdd5f6e 100644
--- a/src/tracker-store/tracker-steroids.c
+++ b/src/tracker-store/tracker-steroids.c
@@ -48,7 +48,6 @@ typedef struct {
 typedef struct {
 	TrackerSteroids *parent;
 	DBusMessage *call_message;
-	char *query;
 	int fd;
 	unsigned int send_buffer_index;
 	char send_buffer[TRACKER_STEROIDS_BUFFER_SIZE];
@@ -97,10 +96,6 @@ destroy_client_info (gpointer user_data)
 	dbus_message_unref (info->call_message);
 	dbus_connection_unref (info->connection);
 
-	if (info->query) {
-		g_free (info->query);
-	}
-
 	if (info->fd) {
 		close (info->fd);
 	}
@@ -196,42 +191,48 @@ query_callback (gpointer  inthread_data,
                 GError   *error,
                 gpointer  user_data)
 {
-//	InThreadPtr *ptr  = inthread_data;
-//	ClientInfo  *info = user_data;
-//	TrackerSteroidsPrivate *priv = TRACKER_STEROIDS_GET_PRIVATE (info->parent);
-//	DBusMessage *reply;
-//
-//	if (info->fd) {
-//		close (info->fd);
-//	}
-//
-//	if (ptr && ptr->error) {
-//		/* Client is still there, but query failed */
-//		tracker_dbus_request_failed (info->request_id,
-//		                             info->context,
-//		                             &ptr->error,
-//		                             NULL);
-//		dbus_g_method_return_error (info->context, ptr->error);
-//		g_error_free (ptr->error);
-//	} else if (error) {
-//		/* Client has disappeared */
-//		tracker_dbus_request_failed (info->request_id,
-//		                             info->context,
-//		                             &error,
-//		                             NULL);
-//		dbus_g_method_return_error (info->context, error);
-//	} else {
-//		tracker_dbus_request_success (info->request_id,
-//		                              info->context);
-//		reply = dbus_g_method_get_reply (info->context);
-//		dbus_g_method_send_reply (info->context, reply);
-//	}
-//
-//	if (ptr) {
-//		g_slice_free (InThreadPtr, ptr);
-//	}
-//
-//	g_hash_table_remove (priv->clients, info);
+	InThreadPtr *ptr  = inthread_data;
+	ClientInfo  *info = user_data;
+	DBusMessage *reply;
+
+	if (info->fd) {
+		close (info->fd);
+	}
+
+	if (ptr && ptr->error) {
+		/* Client is still there, but query failed */
+		tracker_dbus_request_failed (info->request_id,
+		                             NULL,
+		                             &ptr->error,
+		                             NULL);
+		reply = dbus_message_new_error (info->call_message,
+		                                TRACKER_STEROIDS_INTERFACE ".QueryError",
+		                                ptr->error->message);
+		dbus_connection_send (info->connection, reply, NULL);
+		dbus_message_unref (reply);
+		g_error_free (ptr->error);
+	} else if (error) {
+		/* Client has disappeared */
+		tracker_dbus_request_failed (info->request_id,
+		                             NULL,
+		                             &error,
+		                             NULL);
+		reply = dbus_message_new_error (info->call_message,
+		                                TRACKER_STEROIDS_INTERFACE ".QueryError",
+		                                error->message);
+		dbus_connection_send (info->connection, reply, NULL);
+		dbus_message_unref (reply);
+	} else {
+		tracker_dbus_request_success (info->request_id,
+		                              NULL);
+		reply = dbus_message_new_method_return (info->call_message);
+		dbus_connection_send (info->connection, reply, NULL);
+		dbus_message_unref (reply);
+	}
+
+	if (ptr) {
+		g_slice_free (InThreadPtr, ptr);
+	}
 }
 
 static void
@@ -393,128 +394,56 @@ query_inthread (TrackerDBCursor *cursor,
 	return ptr;
 }
 
-//static ClientInfo*
-//tracker_steroids_prepare (TrackerSteroids        *steroids,
-//                          DBusGMethodInvocation  *context,
-//                          gboolean                read,
-//                          GError                **error)
-//{
-//	TrackerSteroidsPrivate *priv = TRACKER_STEROIDS_GET_PRIVATE (steroids);
-//	guint                   request_id;
-//	gchar                  *sender;
-//	int                     pipefd[2];
-//	DBusMessage            *reply;
-//	DBusMessageIter         iter;
-//	GError *inner_error = NULL;
-//	ClientInfo             *info;
-//
-//	request_id = tracker_dbus_get_next_request_id ();
-//
-//	tracker_dbus_request_new (request_id,
-//	                          context,
-//	                          "%s()",
-//	                          __FUNCTION__);
-//
-//	if (pipe (pipefd) < 0) {
-//		g_set_error (&inner_error, TRACKER_DBUS_ERROR, 0, "Cannot open pipe");
-//
-//		tracker_dbus_request_failed (request_id,
-//		                             context,
-//		                             &inner_error,
-//		                             NULL);
-//		dbus_g_method_return_error (context, inner_error);
-//		g_propagate_error (error, inner_error);
-//
-//		return NULL;
-//	}
-//
-//	info = g_slice_new0 (ClientInfo);
-//	info->parent = steroids;
-//	info->fd = pipefd[1 - read];
-//
-//	sender = dbus_g_method_get_sender (context);
-//
-//	reply = dbus_g_method_get_reply (context);
-//
-//	dbus_message_iter_init_append (reply, &iter);
-//
-//	if (!dbus_message_iter_append_basic (&iter,
-//	                                     DBUS_TYPE_UNIX_FD,
-//	                                     &pipefd[read])) {
-//		g_critical ("FD passing not supported");
-//
-//		g_set_error (&inner_error, TRACKER_DBUS_ERROR, 0, "FD passing not supported");
-//
-//		tracker_dbus_request_failed (request_id,
-//		                             context,
-//		                             &inner_error,
-//		                             NULL);
-//		dbus_g_method_return_error (context, inner_error);
-//		g_propagate_error (error, inner_error);
-//
-//		g_slice_free (ClientInfo, info);
-//		g_free (sender);
-//		return NULL;
-//	}
-//
-//	dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &request_id);
-//
-//	g_hash_table_insert (priv->clients,
-//	                     GUINT_TO_POINTER (request_id),
-//	                     info);
-//
-//	tracker_dbus_request_success (request_id, context);
-//	dbus_g_method_send_reply (context, reply);
-//
-//	g_free (sender);
-//
-//	return info;
-//}
-
 static void
-tracker_steroids_fetch (TrackerSteroids *steroids,
+tracker_steroids_query (TrackerSteroids *steroids,
 						DBusConnection  *connection,
 						DBusMessage     *message)
 {
-//	TrackerSteroidsPrivate *priv = TRACKER_STEROIDS_GET_PRIVATE (steroids);
-//	ClientInfo             *info;
-//	guint                   request_id;
-//	gchar                  *sender;
-//	GError                 *inner_error = NULL;
-//
-//	request_id = tracker_dbus_get_next_request_id ();
-//
-//	tracker_dbus_request_new (request_id,
-//	                          context,
-//	                          "%s(): %u",
-//	                          __FUNCTION__,
-//	                          query_id);
-//
-//	info = g_hash_table_lookup (priv->clients,
-//	                            GUINT_TO_POINTER (query_id));
-//
-//	if (!info) {
-//		g_set_error (&inner_error, TRACKER_DBUS_ERROR, 0, "Wrong query id");
-//		tracker_dbus_request_failed (request_id,
-//		                             context,
-//		                             &inner_error,
-//		                             NULL);
-//		dbus_g_method_return_error (info->context, inner_error);
-//		g_propagate_error (error, inner_error);
-//
-//		return;
-//	}
-//
-//	info->request_id = request_id;
-//	info->context = context;
-//
-//	sender = dbus_g_method_get_sender (context);
-//
-//	tracker_store_sparql_query (info->query, TRACKER_STORE_PRIORITY_HIGH,
-//	                            query_inthread, query_callback, sender,
-//	                            info, destroy_client_info);
-//
-//	g_free (sender);
+	ClientInfo  *info;
+	guint        request_id;
+	const gchar *sender;
+	DBusMessage *reply;
+	DBusError    dbus_error;
+	gchar       *query;
+
+	if (g_strcmp0 (dbus_message_get_signature (message), DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UNIX_FD_AS_STRING)) {
+		reply = dbus_message_new_error_printf (message,
+		                                       DBUS_ERROR_UNKNOWN_METHOD_NAME,
+		                                       DBUS_ERROR_UNKNOWN_METHOD_MESSAGE,
+		                                       "Query",
+		                                       dbus_message_get_signature (message),
+		                                       dbus_message_get_interface (message));
+		dbus_connection_send (connection, message, NULL);
+		dbus_message_unref (reply);
+		return;
+	}
+
+	request_id = tracker_dbus_get_next_request_id ();
+
+	tracker_dbus_request_new (request_id,
+	                          NULL,
+	                          "%s()",
+	                          __FUNCTION__);
+
+	info = g_slice_new0 (ClientInfo);
+	info->parent = steroids;
+	info->connection = dbus_connection_ref (connection);
+	info->call_message = dbus_message_ref (message);
+	info->request_id = request_id;
+
+	dbus_error_init (&dbus_error);
+
+	dbus_message_get_args (message,
+	                       &dbus_error,
+	                       DBUS_TYPE_STRING, &query,
+	                       DBUS_TYPE_UNIX_FD, &info->fd,
+	                       DBUS_TYPE_INVALID);
+
+	sender = dbus_message_get_sender (message);
+
+	tracker_store_sparql_query (query, TRACKER_STORE_PRIORITY_HIGH,
+	                            query_inthread, query_callback, sender,
+	                            info, destroy_client_info);
 }
 
 static void
@@ -529,6 +458,7 @@ tracker_steroids_update (TrackerSteroids *steroids,
 	static char             query_size_buffer[sizeof(int)];
 	int                     query_size;
 	DBusMessage            *reply;
+	gchar                  *query;
 
 	if (g_strcmp0 (dbus_message_get_signature (message), DBUS_TYPE_UNIX_FD_AS_STRING)) {
 		reply = dbus_message_new_error_printf (message,
@@ -569,14 +499,14 @@ tracker_steroids_update (TrackerSteroids *steroids,
 	query_size = buffer_read_int (query_size_buffer);
 
 	/* We malloc one more char to ensure string is 0 terminated */
-	info->query = g_malloc0 ((1 + query_size) * sizeof (char));
+	query = g_malloc0 ((1 + query_size) * sizeof (char));
 
-	buffer_read (info->fd, info->query, query_size);
+	buffer_read (info->fd, query, query_size);
 
 	close (info->fd);
 	info->fd = 0;
 
-	tracker_store_sparql_update (info->query, TRACKER_STORE_PRIORITY_HIGH, FALSE,
+	tracker_store_sparql_update (query, TRACKER_STORE_PRIORITY_HIGH, FALSE,
 	                             update_callback, sender,
 	                             info, destroy_client_info);
 }
@@ -598,6 +528,11 @@ tracker_steroids_connection_filter (DBusConnection *connection,
 		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 	}
 
+	if (!g_strcmp0 ("Query", dbus_message_get_member (message))) {
+		tracker_steroids_query (steroids, connection, message);
+		return DBUS_HANDLER_RESULT_HANDLED;
+	}
+
 	if (!g_strcmp0 ("Update", dbus_message_get_member (message))) {
 		tracker_steroids_update (steroids, connection, message);
 		return DBUS_HANDLER_RESULT_HANDLED;



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