[tracker/dbus-fd-experiment-gio: 24/41] Steroids client: Add async versions



commit 746cc015458c44226d8bd721fe6639e1320eb517
Author: Adrien Bustany <abustany gnome org>
Date:   Thu Jun 3 20:31:16 2010 -0400

    Steroids client: Add async versions
    
    This commit adds async versions for all the _iterate and _fast versions
    of the tracker_resources_* functions.

 src/libtracker-client/tracker.c |  258 +++++++++++++++++++++++++++++++++++++--
 src/libtracker-client/tracker.h |   20 +++
 2 files changed, 267 insertions(+), 11 deletions(-)
---
diff --git a/src/libtracker-client/tracker.c b/src/libtracker-client/tracker.c
index e6492be..0e24620 100644
--- a/src/libtracker-client/tracker.c
+++ b/src/libtracker-client/tracker.c
@@ -113,6 +113,10 @@ typedef struct {
 	GList *writeback_callbacks;
 
 	gboolean is_constructed;
+
+#ifdef HAVE_DBUS_FD_PASSING
+	GThreadPool *thread_pool;
+#endif
 } TrackerClientPrivate;
 
 typedef struct {
@@ -174,10 +178,28 @@ struct TrackerResultIterator {
 
 #ifdef HAVE_DBUS_FD_PASSING
 typedef enum {
-	TRACKER_UPDATE,
-	TRACKER_UPDATE_BLANK,
-	TRACKER_UPDATE_BATCH
-} TrackerUpdateType;
+	FAST_QUERY,
+	FAST_UPDATE,
+	FAST_UPDATE_BLANK,
+	FAST_UPDATE_BATCH
+} FastOperationType;
+
+typedef struct {
+	FastOperationType      operation;
+	gchar                 *query;
+	GError                *error;
+	gpointer               user_data;
+	union {
+		GPtrArray             *result_gptrarray;
+		TrackerResultIterator *result_iterator;
+	};
+	union {
+		TrackerReplyGPtrArray  gptrarray_callback;
+		TrackerReplyVoid       void_callback;
+		TrackerReplyArray      array_callback;
+		TrackerReplyIterator   iterator_callback;
+	};
+} FastAsyncData;
 #endif
 
 static gboolean is_service_available (void);
@@ -259,6 +281,74 @@ writeback_cb (DBusGProxy       *proxy,
 	}
 }
 
+static gboolean
+thread_callback (gpointer data)
+{
+	FastAsyncData *async_data = data;
+
+	switch (async_data->operation) {
+		case FAST_QUERY:
+			(* async_data->iterator_callback) (async_data->result_iterator,
+			                                   async_data->error,
+			                                   async_data->user_data);
+			break;
+		case FAST_UPDATE:
+		case FAST_UPDATE_BATCH:
+			(* async_data->void_callback) (async_data->error,
+			                               async_data->user_data);
+			break;
+		case FAST_UPDATE_BLANK:
+			(* async_data->gptrarray_callback) (async_data->result_gptrarray,
+			                                    async_data->error,
+			                                    async_data->user_data);
+			break;
+		default:
+			g_assert_not_reached ();
+	}
+
+	g_free (async_data->query);
+
+	if (async_data->error) {
+		g_error_free (async_data->error);
+	}
+
+	return FALSE;
+}
+
+static void
+thread_dispatch (gpointer data, gpointer user_data)
+{
+	FastAsyncData *async_data = data;
+	TrackerClient *client = user_data;
+
+	switch (async_data->operation) {
+		case FAST_QUERY:
+			async_data->result_iterator = tracker_resources_sparql_query_iterate (client,
+			                                                                      async_data->query,
+			                                                                      &async_data->error);
+			break;
+		case FAST_UPDATE:
+			tracker_resources_sparql_update_fast (client,
+			                                      async_data->query,
+			                                      &async_data->error);
+			break;
+		case FAST_UPDATE_BLANK:
+			async_data->result_gptrarray = tracker_resources_sparql_update_blank_fast (client,
+			                                                                           async_data->query,
+			                                                                           &async_data->error);
+			break;
+		case FAST_UPDATE_BATCH:
+			tracker_resources_batch_sparql_update_fast (client,
+			                                            async_data->query,
+			                                            &async_data->error);
+			break;
+		default:
+			g_assert_not_reached ();
+	}
+
+	g_idle_add (thread_callback, async_data);
+}
+
 static void
 tracker_client_class_init (TrackerClientClass *klass)
 {
@@ -317,6 +407,12 @@ client_finalize (GObject *object)
 	if (private->pending_calls) {
 		g_hash_table_unref (private->pending_calls);
 	}
+
+	if (private->thread_pool) {
+		g_thread_pool_free (private->thread_pool,
+		                    TRUE,
+		                    TRUE);
+	}
 }
 
 static void
@@ -422,6 +518,14 @@ client_constructed (GObject *object)
 	                         TRACKER_TYPE_STR_STRV_MAP,
 	                         G_TYPE_INVALID);
 
+#ifdef HAVE_DBUS_FD_PASSING
+	private->thread_pool = g_thread_pool_new (thread_dispatch,
+	                                          object,
+	                                          5,
+	                                          FALSE,
+	                                          NULL);
+#endif
+
 	private->is_constructed = TRUE;
 }
 
@@ -888,7 +992,7 @@ end_of_results:
 static DBusMessage*
 sparql_update_fast (TrackerClient      *client,
                     const gchar        *query,
-                    TrackerUpdateType   type,
+                    FastOperationType   type,
                     GError            **error)
 {
 	TrackerClientPrivate *private;
@@ -922,13 +1026,13 @@ sparql_update_fast (TrackerClient      *client,
 	dbus_error_init (&dbus_error);
 
 	switch (type) {
-		case TRACKER_UPDATE:
+		case FAST_UPDATE:
 			dbus_method = "Update";
 			break;
-		case TRACKER_UPDATE_BLANK:
+		case FAST_UPDATE_BLANK:
 			dbus_method = "UpdateBlank";
 			break;
-		case TRACKER_UPDATE_BATCH:
+		case FAST_UPDATE_BATCH:
 			dbus_method = "BatchUpdate";
 			break;
 		default:
@@ -1810,7 +1914,7 @@ tracker_resources_sparql_update_fast (TrackerClient  *client,
 #ifdef HAVE_DBUS_FD_PASSING
 	DBusMessage *reply;
 
-	reply = sparql_update_fast (client, query, TRACKER_UPDATE, error);
+	reply = sparql_update_fast (client, query, FAST_UPDATE, error);
 
 	if (!reply) {
 		return;
@@ -1855,7 +1959,7 @@ tracker_resources_sparql_update_blank_fast (TrackerClient  *client,
 	DBusMessageIter iter, subiter, subsubiter;
 	GPtrArray *result;
 
-	reply = sparql_update_fast (client, query, TRACKER_UPDATE_BLANK, error);
+	reply = sparql_update_fast (client, query, FAST_UPDATE_BLANK, error);
 
 	if (!reply) {
 		return NULL;
@@ -1935,7 +2039,7 @@ tracker_resources_batch_sparql_update_fast (TrackerClient  *client,
 #ifdef HAVE_DBUS_FD_PASSING
 	DBusMessage *reply;
 
-	reply = sparql_update_fast (client, query, TRACKER_UPDATE_BATCH, error);
+	reply = sparql_update_fast (client, query, FAST_UPDATE_BATCH, error);
 
 	if (!reply) {
 		return;
@@ -2090,6 +2194,39 @@ tracker_resources_sparql_query_async (TrackerClient         *client,
 	return cb->id;
 }
 
+guint
+tracker_resources_sparql_query_iterate_async (TrackerClient         *client,
+                                              const gchar           *query,
+                                              TrackerReplyIterator   callback,
+                                              gpointer               user_data)
+{
+	TrackerClientPrivate *private;
+	FastAsyncData *data;
+	GError *error = NULL;
+
+	g_return_val_if_fail (TRACKER_IS_CLIENT (client), 0);
+	g_return_val_if_fail (query != NULL, 0);
+	g_return_val_if_fail (callback != NULL, 0);
+
+	private = TRACKER_CLIENT_GET_PRIVATE (client);
+
+	data = g_slice_new0 (FastAsyncData);
+	data->operation = FAST_QUERY;
+	data->query = g_strdup (query);
+	data->iterator_callback = callback;
+	data->user_data = user_data;
+
+	g_thread_pool_push (private->thread_pool, data, &error);
+
+	if (error) {
+		g_critical ("Could not create thread: %s", error->message);
+		g_error_free (error);
+		return 0;
+	}
+
+	return 0;
+}
+
 /**
  * tracker_resources_sparql_update_async:
  * @client: a #TrackerClient.
@@ -2136,6 +2273,39 @@ tracker_resources_sparql_update_async (TrackerClient    *client,
 }
 
 guint
+tracker_resources_sparql_update_fast_async (TrackerClient    *client,
+                                            const gchar      *query,
+                                            TrackerReplyVoid  callback,
+                                            gpointer          user_data)
+{
+	TrackerClientPrivate *private;
+	FastAsyncData *data;
+	GError *error = NULL;
+
+	g_return_val_if_fail (TRACKER_IS_CLIENT (client), 0);
+	g_return_val_if_fail (query != NULL, 0);
+	g_return_val_if_fail (callback != NULL, 0);
+
+	private = TRACKER_CLIENT_GET_PRIVATE (client);
+
+	data = g_slice_new0 (FastAsyncData);
+	data->operation = FAST_UPDATE;
+	data->query = g_strdup (query);
+	data->void_callback = callback;
+	data->user_data = user_data;
+
+	g_thread_pool_push (private->thread_pool, data, &error);
+
+	if (error) {
+		g_critical ("Could not create thread: %s", error->message);
+		g_error_free (error);
+		return 0;
+	}
+
+	return 0;
+}
+
+guint
 tracker_resources_sparql_update_blank_async (TrackerClient         *client,
                                              const gchar           *query,
                                              TrackerReplyGPtrArray  callback,
@@ -2166,6 +2336,39 @@ tracker_resources_sparql_update_blank_async (TrackerClient         *client,
 	return cb->id;
 }
 
+guint
+tracker_resources_sparql_update_blank_fast_async (TrackerClient         *client,
+                                                  const gchar           *query,
+                                                  TrackerReplyGPtrArray  callback,
+                                                  gpointer               user_data)
+{
+	TrackerClientPrivate *private;
+	FastAsyncData *data;
+	GError *error = NULL;
+
+	g_return_val_if_fail (TRACKER_IS_CLIENT (client), 0);
+	g_return_val_if_fail (query != NULL, 0);
+	g_return_val_if_fail (callback != NULL, 0);
+
+	private = TRACKER_CLIENT_GET_PRIVATE (client);
+
+	data = g_slice_new0 (FastAsyncData);
+	data->operation = FAST_UPDATE_BLANK;
+	data->query = g_strdup (query);
+	data->gptrarray_callback = callback;
+	data->user_data = user_data;
+
+	g_thread_pool_push (private->thread_pool, data, &error);
+
+	if (error) {
+		g_critical ("Could not create thread: %s", error->message);
+		g_error_free (error);
+		return 0;
+	}
+
+	return 0;
+}
+
 /**
  * tracker_resources_batch_sparql_update_async:
  * @client: a #TrackerClient.
@@ -2211,6 +2414,39 @@ tracker_resources_batch_sparql_update_async (TrackerClient    *client,
 	return cb->id;
 }
 
+guint
+tracker_resources_batch_sparql_update_fast_async (TrackerClient    *client,
+                                                  const gchar      *query,
+                                                  TrackerReplyVoid  callback,
+                                                  gpointer          user_data)
+{
+	TrackerClientPrivate *private;
+	FastAsyncData *data;
+	GError *error = NULL;
+
+	g_return_val_if_fail (TRACKER_IS_CLIENT (client), 0);
+	g_return_val_if_fail (query != NULL, 0);
+	g_return_val_if_fail (callback != NULL, 0);
+
+	private = TRACKER_CLIENT_GET_PRIVATE (client);
+
+	data = g_slice_new0 (FastAsyncData);
+	data->operation = FAST_UPDATE_BLANK;
+	data->query = g_strdup (query);
+	data->void_callback = callback;
+	data->user_data = user_data;
+
+	g_thread_pool_push (private->thread_pool, data, &error);
+
+	if (error) {
+		g_critical ("Could not create thread: %s", error->message);
+		g_error_free (error);
+		return 0;
+	}
+
+	return 0;
+}
+
 /**
  * tracker_resources_batch_commit_async:
  * @client: a #TrackerClient.
diff --git a/src/libtracker-client/tracker.h b/src/libtracker-client/tracker.h
index 0ae85d0..3bb397e 100644
--- a/src/libtracker-client/tracker.h
+++ b/src/libtracker-client/tracker.h
@@ -95,6 +95,10 @@ typedef void (*TrackerReplyGPtrArray) (GPtrArray *result,
 typedef void (*TrackerReplyVoid)      (GError    *error,
                                        gpointer   user_data);
 
+typedef void (*TrackerReplyIterator)  (TrackerResultIterator *iterator,
+                                       GError                *error,
+                                       gpointer               user_data);
+
 /**
  * TrackerWritebackCallback:
  * @resources: a hash table where each key is the uri of a resources which
@@ -175,18 +179,34 @@ guint          tracker_resources_sparql_query_async        (TrackerClient
                                                             const gchar            *query,
                                                             TrackerReplyGPtrArray   callback,
                                                             gpointer                user_data);
+guint         tracker_resources_sparql_query_iterate_async (TrackerClient          *client,
+                                                            const gchar            *query,
+                                                            TrackerReplyIterator    callback,
+                                                            gpointer                user_data);
 guint          tracker_resources_sparql_update_async       (TrackerClient          *client,
                                                             const gchar            *query,
                                                             TrackerReplyVoid        callback,
                                                             gpointer                user_data);
+guint          tracker_resources_sparql_update_fast_async  (TrackerClient          *client,
+                                                            const gchar            *query,
+                                                            TrackerReplyVoid        callback,
+                                                            gpointer                user_data);
 guint          tracker_resources_sparql_update_blank_async (TrackerClient          *client,
                                                             const gchar            *query,
                                                             TrackerReplyGPtrArray   callback,
                                                             gpointer                user_data);
+guint     tracker_resources_sparql_update_blank_fast_async (TrackerClient          *client,
+                                                            const gchar            *query,
+                                                            TrackerReplyGPtrArray   callback,
+                                                            gpointer                user_data);
 guint          tracker_resources_batch_sparql_update_async (TrackerClient          *client,
                                                             const gchar            *query,
                                                             TrackerReplyVoid        callback,
                                                             gpointer                user_data);
+guint     tracker_resources_batch_sparql_update_fast_async (TrackerClient          *client,
+                                                            const gchar            *query,
+                                                            TrackerReplyVoid        callback,
+                                                            gpointer                user_data);
 guint          tracker_resources_batch_commit_async        (TrackerClient          *client,
                                                             TrackerReplyVoid        callback,
                                                             gpointer                user_data);



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