[tracker/fixed-cursor] tracker-store: Moved the iter over the cursor to the thread that prepared the query



commit 34cf2da36c6f4a654a8f8eb37780ccfb96b2ea56
Author: Philip Van Hoof <philip codeminded be>
Date:   Mon Apr 26 15:24:35 2010 +0200

    tracker-store: Moved the iter over the cursor to the thread that prepared the query

 src/tracker-store/tracker-resources.c |   69 ++++++++++++++++++++++-----------
 src/tracker-store/tracker-store.c     |   68 ++++++++++++++++++--------------
 src/tracker-store/tracker-store.h     |    7 +++-
 3 files changed, 91 insertions(+), 53 deletions(-)
---
diff --git a/src/tracker-store/tracker-resources.c b/src/tracker-store/tracker-resources.c
index 9a0755f..d09c6a1 100644
--- a/src/tracker-store/tracker-resources.c
+++ b/src/tracker-store/tracker-resources.c
@@ -87,8 +87,14 @@ typedef struct {
 typedef struct {
 	DBusGMethodInvocation *context;
 	guint request_id;
+	DBusMessage *reply;
 } TrackerDBusMethodInfo;
 
+typedef struct {
+	DBusMessage *reply;
+	GError *error;
+	gpointer user_data;
+} InThreadPtr;
 
 static void tracker_resources_finalize (GObject *object);
 
@@ -176,7 +182,7 @@ tracker_resources_load (TrackerResources         *object,
                         GError                  **error)
 {
 	TrackerDBusMethodInfo   *info;
-	guint               request_id;
+	guint                    request_id;
 	GFile  *file;
 
 	request_id = tracker_dbus_get_next_request_id ();
@@ -203,8 +209,35 @@ tracker_resources_load (TrackerResources         *object,
 }
 
 static void
-query_callback (TrackerDBCursor *cursor, GError *error, gpointer user_data)
+query_callback (gpointer inthread_data, GError *error, gpointer user_data)
 {
+	InThreadPtr *ptr = inthread_data;
+	TrackerDBusMethodInfo *info = user_data;
+
+	if (ptr->error) {
+		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 {
+
+		tracker_dbus_request_success (info->request_id,
+		                              info->context);
+
+		dbus_g_method_send_reply (info->context, ptr->reply);
+	}
+
+	g_slice_free (InThreadPtr, ptr);
+}
+
+static GStaticMutex dbus_mutex = G_STATIC_MUTEX_INIT;
+
+static gpointer
+query_inthread (TrackerDBCursor *cursor, GError *error, gpointer user_data)
+{
+	InThreadPtr *ptr = g_slice_new0 (InThreadPtr);
 	TrackerDBusMethodInfo *info = user_data;
 	DBusMessage *reply;
 	DBusMessageIter iter, rows_iter;
@@ -214,15 +247,11 @@ query_callback (TrackerDBCursor *cursor, GError *error, gpointer user_data)
 	gboolean cont;
 
 	if (error) {
-		tracker_dbus_request_failed (info->request_id,
-		                             info->context,
-		                             &error,
-		                             NULL);
-		dbus_g_method_return_error (info->context, error);
-		return;
+		ptr->error = g_error_copy (error);
+		return ptr;
 	}
 
-	reply = dbus_g_method_get_reply (info->context);
+	reply = info->reply;
 
 	dbus_message_iter_init_append (reply, &iter);
 
@@ -270,21 +299,14 @@ query_callback (TrackerDBCursor *cursor, GError *error, gpointer user_data)
 
 	dbus_message_iter_close_container (&iter, &rows_iter);
 
-	if (loop_error == NULL) {
-		tracker_dbus_request_success (info->request_id,
-		                              info->context);
-
-		dbus_g_method_send_reply (info->context, reply);
+	if (loop_error) {
+		ptr->error = loop_error;
+		ptr->reply = NULL;
 	} else {
-		dbus_message_unref (reply);
-		tracker_dbus_request_failed (info->request_id,
-		                             info->context,
-		                             &loop_error,
-		                             NULL);
-		dbus_g_method_return_error (info->context, loop_error);
-
-		g_error_free (loop_error);
+		ptr->reply = reply;
 	}
+
+	return ptr;
 }
 
 void
@@ -314,11 +336,12 @@ tracker_resources_sparql_query (TrackerResources         *self,
 
 	info->request_id = request_id;
 	info->context = context;
+	info->reply = dbus_g_method_get_reply (context);
 
 	sender = dbus_g_method_get_sender (context);
 
 	tracker_store_sparql_query (query, TRACKER_STORE_PRIORITY_HIGH,
-	                            query_callback, sender,
+	                            query_inthread, query_callback, sender,
 	                            info, destroy_method_info);
 
 	g_free (sender);
diff --git a/src/tracker-store/tracker-store.c b/src/tracker-store/tracker-store.c
index 539293c..5998365 100644
--- a/src/tracker-store/tracker-store.c
+++ b/src/tracker-store/tracker-store.c
@@ -67,27 +67,30 @@ typedef struct {
 	TrackerStoreTaskType  type;
 	union {
 		struct {
-			gchar                   *query;
-			TrackerDBCursor         *cursor;
-			GThread			*running_thread;
-			GTimer			*timer;
+			gchar       *query;
+			GThread     *running_thread;
+			GTimer      *timer;
+			gpointer     thread_data;
 		} query;
 		struct {
-			gchar                   *query;
-			gboolean                 batch;
-			GPtrArray               *blank_nodes;
+			gchar        *query;
+			gboolean      batch;
+			GPtrArray    *blank_nodes;
 		} update;
 		struct {
-			gboolean           in_progress;
-			gchar             *path;
+			gboolean      in_progress;
+			gchar        *path;
 		} turtle;
 	} data;
-	gchar			  *client_id;
-	GError                    *error;
-	gpointer                   user_data;
-	GDestroyNotify             destroy;
+	gchar                *client_id;
+	GError               *error;
+	gpointer              user_data;
+	GDestroyNotify        destroy;
 	union {
-		TrackerStoreSparqlQueryCallback       query_callback;
+		struct {
+			TrackerStoreSparqlQueryCallback   query_callback;
+			TrackerStoreSparqlQueryInThread   in_thread;
+		} query;
 		TrackerStoreSparqlUpdateCallback      update_callback;
 		TrackerStoreSparqlUpdateBlankCallback update_blank_callback;
 		TrackerStoreCommitCallback            commit_callback;
@@ -116,12 +119,11 @@ store_task_free (TrackerStoreTask *task)
 {
 	if (task->type == TRACKER_STORE_TASK_TYPE_TURTLE) {
 		g_free (task->data.turtle.path);
-        } else if (task->type == TRACKER_STORE_TASK_TYPE_QUERY) {
+	} else if (task->type == TRACKER_STORE_TASK_TYPE_QUERY) {
 		g_free (task->data.query.query);
-
-                if (task->data.query.timer) {
-                        g_timer_destroy (task->data.query.timer);
-                }
+		if (task->data.query.timer) {
+			g_timer_destroy (task->data.query.timer);
+		}
 	} else {
 		g_free (task->data.update.query);
 	}
@@ -330,13 +332,8 @@ task_finish_cb (gpointer data)
 	task = data;
 
 	if (task->type == TRACKER_STORE_TASK_TYPE_QUERY) {
-		if (task->callback.query_callback) {
-			task->callback.query_callback (task->data.query.cursor, task->error, task->user_data);
-		}
-
-		if (task->data.query.cursor) {
-			g_object_unref (task->data.query.cursor);
-			task->data.query.cursor = NULL;
+		if (task->callback.query.query_callback) {
+			task->callback.query.query_callback (task->data.query.thread_data, task->error, task->user_data);
 		}
 
 		if (task->error) {
@@ -421,9 +418,20 @@ pool_dispatch_cb (gpointer data,
 	task = data;
 
 	if (task->type == TRACKER_STORE_TASK_TYPE_QUERY) {
+		TrackerDBCursor *cursor;
+
 		task->data.query.running_thread = g_thread_self ();
-		task->data.query.cursor = tracker_data_query_sparql_cursor (task->data.query.query, &task->error);
+		cursor = tracker_data_query_sparql_cursor (task->data.query.query, &task->error);
+
+		if (task->callback.query.in_thread) {
+			task->data.query.thread_data = task->callback.query.in_thread (cursor, task->error, task->user_data);
+		} else {
+			task->data.query.thread_data = NULL;
+		}
+
+		g_object_unref (cursor);
 		task->data.query.running_thread = NULL;
+
 	} else if (task->type == TRACKER_STORE_TASK_TYPE_UPDATE) {
 		if (task->data.update.batch) {
 			begin_batch (private);
@@ -690,6 +698,7 @@ tracker_store_queue_commit (TrackerStoreCommitCallback callback,
 void
 tracker_store_sparql_query (const gchar *sparql,
                             TrackerStorePriority priority,
+                            TrackerStoreSparqlQueryInThread in_thread,
                             TrackerStoreSparqlQueryCallback callback,
                             const gchar *client_id,
                             gpointer user_data,
@@ -707,7 +716,8 @@ tracker_store_sparql_query (const gchar *sparql,
 	task->type = TRACKER_STORE_TASK_TYPE_QUERY;
 	task->data.query.query = g_strdup (sparql);
 	task->user_data = user_data;
-	task->callback.query_callback = callback;
+	task->callback.query.query_callback = callback;
+	task->callback.query.in_thread = in_thread;
 	task->destroy = destroy;
 	task->client_id = g_strdup (client_id);
 
@@ -862,7 +872,7 @@ tracker_store_unreg_batches (const gchar *client_id)
 					}
 
 					if (task->type == TRACKER_STORE_TASK_TYPE_QUERY) {
-						task->callback.query_callback (NULL, error, task->user_data);
+						task->callback.query.query_callback (NULL, error, task->user_data);
 					} else if (task->type == TRACKER_STORE_TASK_TYPE_UPDATE) {
 						task->callback.update_callback (error, task->user_data);
 					} else if (task->type == TRACKER_STORE_TASK_TYPE_UPDATE_BLANK) {
diff --git a/src/tracker-store/tracker-store.h b/src/tracker-store/tracker-store.h
index 87ca4d4..4b3a870 100644
--- a/src/tracker-store/tracker-store.h
+++ b/src/tracker-store/tracker-store.h
@@ -35,7 +35,11 @@ typedef enum {
 	TRACKER_STORE_N_PRIORITIES
 } TrackerStorePriority;
 
-typedef void (* TrackerStoreSparqlQueryCallback)       (TrackerDBCursor *cursor,
+typedef void (* TrackerStoreSparqlQueryCallback)       (gpointer         data,
+                                                        GError          *error,
+                                                        gpointer         user_data);
+typedef gpointer
+             (* TrackerStoreSparqlQueryInThread)       (TrackerDBCursor *cursor,
                                                         GError          *error,
                                                         gpointer         user_data);
 typedef void (* TrackerStoreSparqlUpdateCallback)      (GError          *error,
@@ -55,6 +59,7 @@ void         tracker_store_queue_commit           (TrackerStoreCommitCallback ca
                                                    GDestroyNotify destroy);
 void         tracker_store_sparql_query           (const gchar   *sparql,
                                                    TrackerStorePriority priority,
+                                                   TrackerStoreSparqlQueryInThread in_thread,
                                                    TrackerStoreSparqlQueryCallback callback,
                                                    const gchar   *client_id,
                                                    gpointer       user_data,



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