[tracker/async-init-direct] libtracker-data, -bus, -sparql: Asynchronous init of direct backend



commit d91ef13ec7610ec1c6425301f6999c2f995b24b4
Author: Philip Van Hoof <philip codeminded be>
Date:   Thu Feb 17 17:51:45 2011 +0100

    libtracker-data, -bus, -sparql: Asynchronous init of direct backend

 src/libtracker-bus/tracker-bus.vala        |    6 ++
 src/libtracker-data/libtracker-data.vapi   |    1 +
 src/libtracker-data/tracker-data-manager.c |  111 ++++++++++++++++++++++++++++
 src/libtracker-data/tracker-data-manager.h |   14 ++++
 src/libtracker-direct/tracker-direct.vala  |   25 ++++++-
 src/libtracker-sparql/tracker-backend.vala |   20 +++++-
 6 files changed, 174 insertions(+), 3 deletions(-)
---
diff --git a/src/libtracker-bus/tracker-bus.vala b/src/libtracker-bus/tracker-bus.vala
index b0b3137..4e43bcb 100644
--- a/src/libtracker-bus/tracker-bus.vala
+++ b/src/libtracker-bus/tracker-bus.vala
@@ -89,6 +89,12 @@ public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
 		initialized = true;
 	}
 
+	public override void init () throws Sparql.Error, IOError, DBusError {
+	}
+
+	public async override void init_async () throws Sparql.Error, IOError, DBusError {
+	}
+
 	~Connection () {
 		initialized = false;
 	}
diff --git a/src/libtracker-data/libtracker-data.vapi b/src/libtracker-data/libtracker-data.vapi
index f5176b4..5d92807 100644
--- a/src/libtracker-data/libtracker-data.vapi
+++ b/src/libtracker-data/libtracker-data.vapi
@@ -197,6 +197,7 @@ namespace Tracker {
 	[CCode (cheader_filename = "libtracker-data/tracker-data-manager.h")]
 	namespace Data.Manager {
 		public bool init (DBManagerFlags flags, [CCode (array_length = false)] string[]? test_schema, out bool first_time, bool journal_check, uint select_cache_size, uint update_cache_size, BusyCallback? busy_callback, string? busy_status) throws DBInterfaceError;
+		public async bool init_async (DBManagerFlags flags, [CCode (array_length = false)] string[]? test_schema, bool journal_check, uint select_cache_size, uint update_cache_size, BusyCallback? busy_callback, string? busy_status) throws DBInterfaceError;
 		public void shutdown ();
 	}
 
diff --git a/src/libtracker-data/tracker-data-manager.c b/src/libtracker-data/tracker-data-manager.c
index 2cab0bb..16f229f 100644
--- a/src/libtracker-data/tracker-data-manager.c
+++ b/src/libtracker-data/tracker-data-manager.c
@@ -74,6 +74,18 @@ static gchar    *ontologies_dir;
 static gboolean  initialized;
 static gboolean  in_journal_replay;
 
+typedef struct {
+	TrackerDBManagerFlags flags;
+	gchar **test_schema;
+	gboolean first_time; /* Unused atm */
+	gboolean journal_check;
+	guint select_cache_size;
+	guint update_cache_size;
+	TrackerBusyCallback busy_callback;
+	gpointer busy_user_data;
+	gchar *busy_operation;
+	gboolean result;
+} InitAsyncData;
 
 typedef struct {
 	const gchar *from;
@@ -4125,6 +4137,105 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
 }
 
 
+
+static void
+tracker_data_manager_init_thread (GSimpleAsyncResult *result,
+                                  GObject            *object,
+                                  GCancellable       *cancellable)
+{
+	InitAsyncData *data;
+	gboolean result_b;
+	GError *internal_error = NULL;
+
+	data = g_simple_async_result_get_op_res_gpointer (result);
+
+	result_b = tracker_data_manager_init (data->flags,
+	                                      (const gchar **) data->test_schema,
+	                                      &data->first_time, /* Unused atm */
+	                                      data->journal_check,
+	                                      data->select_cache_size,
+	                                      data->update_cache_size,
+	                                      data->busy_callback,
+	                                      data->busy_user_data,
+	                                      data->busy_operation,
+	                                      &internal_error);
+
+	if (internal_error) {
+		g_simple_async_result_set_from_error (result, internal_error);
+		g_error_free (internal_error);
+	}
+
+	data->result = result_b;
+}
+
+static void
+init_async_data_free (gpointer data)
+{
+	InitAsyncData *d = data;
+
+	g_strfreev (d->test_schema);
+	g_free (d->busy_operation);
+
+	g_free (data);
+}
+
+void
+tracker_data_manager_init_async (TrackerDBManagerFlags   flags,
+                                 const gchar           **test_schema,
+                                 gboolean                journal_check,
+                                 guint                   select_cache_size,
+                                 guint                   update_cache_size,
+                                 TrackerBusyCallback     busy_callback,
+                                 gpointer                busy_user_data,
+                                 const gchar            *busy_operation,
+                                 GAsyncReadyCallback     callback,
+                                 gpointer                user_data)
+{
+	GSimpleAsyncResult *result;
+	InitAsyncData *data = g_new0 (InitAsyncData, 1);
+
+	data->flags = flags;
+	data->test_schema = g_strdupv ((gchar **) test_schema);
+	data->journal_check = journal_check;
+	data->select_cache_size = select_cache_size;
+	data->update_cache_size = update_cache_size;
+	data->busy_callback = busy_callback;
+	data->busy_user_data = busy_user_data;
+	data->busy_operation = g_strdup (busy_operation);
+
+	result = g_simple_async_result_new (NULL,
+	                                    callback,
+	                                    user_data,
+	                                    (gpointer) tracker_data_manager_init_async);
+
+	g_simple_async_result_set_op_res_gpointer (result,
+	                                           data,
+	                                           init_async_data_free);
+
+	g_simple_async_result_run_in_thread (result,
+	                                     tracker_data_manager_init_thread,
+	                                     0,
+	                                     NULL);
+}
+
+gboolean
+tracker_data_manager_init_finish (GAsyncResult  *result,
+                                  GError       **error)
+{
+	InitAsyncData *data;
+	GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (result);
+
+	g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
+
+	if (g_simple_async_result_propagate_error (res, error)) {
+		return FALSE;
+	}
+
+	data = g_simple_async_result_get_op_res_gpointer (res);
+
+	return data->result;
+}
+
 void
 tracker_data_manager_shutdown (void)
 {
diff --git a/src/libtracker-data/tracker-data-manager.h b/src/libtracker-data/tracker-data-manager.h
index 71c40b1..b8b1e05 100644
--- a/src/libtracker-data/tracker-data-manager.h
+++ b/src/libtracker-data/tracker-data-manager.h
@@ -55,6 +55,20 @@ gboolean tracker_data_manager_init                   (TrackerDBManagerFlags   fl
                                                       gpointer                busy_user_data,
                                                       const gchar            *busy_operation,
                                                       GError                **error);
+
+void     tracker_data_manager_init_async             (TrackerDBManagerFlags   flags,
+                                                      const gchar           **test_schema,
+                                                      gboolean                journal_check,
+                                                      guint                   select_cache_size,
+                                                      guint                   update_cache_size,
+                                                      TrackerBusyCallback     busy_callback,
+                                                      gpointer                busy_user_data,
+                                                      const gchar            *busy_operation,
+                                                      GAsyncReadyCallback     callback,
+                                                      gpointer                user_data);
+gboolean tracker_data_manager_init_finish            (GAsyncResult           *result,
+                                                      GError                **error);
+
 void     tracker_data_manager_shutdown               (void);
 gboolean tracker_data_manager_reload                 (TrackerBusyCallback     busy_callback,
                                                       gpointer                busy_user_data,
diff --git a/src/libtracker-direct/tracker-direct.vala b/src/libtracker-direct/tracker-direct.vala
index d6493e8..4d11e40 100644
--- a/src/libtracker-direct/tracker-direct.vala
+++ b/src/libtracker-direct/tracker-direct.vala
@@ -23,6 +23,9 @@ public class Tracker.Direct.Connection : Tracker.Sparql.Connection {
 
 	public Connection () throws GLib.Error
 	requires (!initialized) {
+	}
+
+	public override void init () throws Sparql.Error, IOError, DBusError {
 		uint select_cache_size = 100;
 		string env_cache_size = Environment.get_variable ("TRACKER_SPARQL_CACHE_SIZE");
 
@@ -30,8 +33,28 @@ public class Tracker.Direct.Connection : Tracker.Sparql.Connection {
 			select_cache_size = env_cache_size.to_int();
 		}
 
-		Data.Manager.init (DBManagerFlags.READONLY, null, null, false, select_cache_size, 0, null, null);
+		try {
+			Data.Manager.init (DBManagerFlags.READONLY, null, null, false, select_cache_size, 0, null, null);
+		} catch (DBInterfaceError e) {
+			throw new Sparql.Error.INTERNAL (e.message);
+		}
+
+		initialized = true;
+	}
+
+	public async override void init_async () throws Sparql.Error, IOError, DBusError {
+		uint select_cache_size = 100;
+		string env_cache_size = Environment.get_variable ("TRACKER_SPARQL_CACHE_SIZE");
+
+		if (env_cache_size != null) {
+			select_cache_size = env_cache_size.to_int();
+		}
 
+		try {
+			yield Data.Manager.init_async (DBManagerFlags.READONLY, null, false, select_cache_size, 0, null, null);
+		} catch (DBInterfaceError e) {
+			throw new Sparql.Error.INTERNAL (e.message);
+		}
 		initialized = true;
 	}
 
diff --git a/src/libtracker-sparql/tracker-backend.vala b/src/libtracker-sparql/tracker-backend.vala
index 8eb832c..758fbb5 100644
--- a/src/libtracker-sparql/tracker-backend.vala
+++ b/src/libtracker-sparql/tracker-backend.vala
@@ -57,7 +57,15 @@ class Tracker.Sparql.Backend : Connection {
 
 		try {
 			debug ("Constructing connection, direct_only=%s", direct_only ? "true" : "false");
-			load_plugins (direct_only);
+			if (load_plugins (direct_only)) {
+				debug ("Waiting for backend to become available synchronously...");
+				if (direct != null) {
+					direct.init ();
+				} else {
+					bus.init ();
+				}
+				debug ("Backend is ready");
+			}
 		} catch (GLib.Error e) {
 			throw new Sparql.Error.INTERNAL (e.message);
 		}
@@ -77,7 +85,15 @@ class Tracker.Sparql.Backend : Connection {
 
 		try {
 			debug ("Constructing connection, direct_only=%s", direct_only ? "true" : "false");
-			load_plugins (direct_only);
+			if (load_plugins (direct_only)) {
+				debug ("Waiting for backend to become available asynchronously...");
+				if (direct != null) {
+					yield direct.init_async ();
+				} else {
+					yield bus.init_async ();
+				}
+				debug ("Backend is ready");
+			}
 		} catch (GLib.Error e) {
 			throw new Sparql.Error.INTERNAL (e.message);
 		}



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