[tracker/async-get-connection] libtracker-sparql: Added get_async() and get_direct_async() for Connection



commit 89604954a39f785523fd756563b04fd9b696df8b
Author: Martyn Russell <martyn lanedo com>
Date:   Fri Sep 10 18:07:34 2010 +0100

    libtracker-sparql: Added get_async() and get_direct_async() for Connection
    
    Currently everythign is async and working down to the Backend which
    needs some way of calling the module_init() can waiting for the store
    asynchronously. This needs a bit more thought.

 examples/class-signal/class-signal.c               |    2 +-
 src/libtracker-direct/tracker-direct.vala          |    3 +-
 src/libtracker-miner/tracker-miner-object.c        |    2 +-
 src/libtracker-sparql/tracker-backend.vala         |   36 ++++++-
 src/libtracker-sparql/tracker-connection.vala      |  117 ++++++++++++++++----
 src/miners/fs/tracker-miner-files-index.c          |    2 +-
 src/tracker-utils/tracker-import.c                 |    2 +-
 src/tracker-utils/tracker-info.c                   |    2 +-
 src/tracker-utils/tracker-search.c                 |    2 +-
 src/tracker-utils/tracker-sparql.c                 |    2 +-
 src/tracker-utils/tracker-stats.c                  |    2 +-
 src/tracker-utils/tracker-tag.c                    |    2 +-
 src/tracker-writeback/tracker-writeback-consumer.c |    2 +-
 tests/functional-tests/default-update-test.vala    |   52 ++++++++-
 tests/tracker-steroids/tracker-test.c              |    2 +-
 15 files changed, 183 insertions(+), 47 deletions(-)
---
diff --git a/examples/class-signal/class-signal.c b/examples/class-signal/class-signal.c
index eedb974..e2263a8 100644
--- a/examples/class-signal/class-signal.c
+++ b/examples/class-signal/class-signal.c
@@ -111,7 +111,7 @@ main (gint argc, gchar *argv[])
 
 	g_type_init ();
 	loop = g_main_loop_new (NULL, FALSE);
-	con = tracker_sparql_connection_get (&error);
+	con = tracker_sparql_connection_get (NULL, &error);
 	connection = dbus_bus_get_private (DBUS_BUS_SESSION, NULL);
 	dbus_bus_request_name (connection, TRACKER_SERVICE, 0, NULL);
 	dbus_connection_add_filter (connection, message_filter, NULL, NULL);
diff --git a/src/libtracker-direct/tracker-direct.vala b/src/libtracker-direct/tracker-direct.vala
index dda2c82..a2107a3 100644
--- a/src/libtracker-direct/tracker-direct.vala
+++ b/src/libtracker-direct/tracker-direct.vala
@@ -20,6 +20,7 @@
 [DBus (name = "org.freedesktop.Tracker1.Status", timeout = 2147483647 /* INT_MAX */)]
 interface Tracker.Direct.Status : GLib.Object {
 	public abstract void wait () throws DBus.Error;
+	public abstract async void wait_async () throws DBus.Error;
 }
 
 public class Tracker.Direct.Connection : Tracker.Sparql.Connection {
@@ -30,10 +31,10 @@ public class Tracker.Direct.Connection : Tracker.Sparql.Connection {
 	requires (!initialized) {
 		try {
 			var connection = DBus.Bus.get (DBus.BusType.SESSION);
-
 			var status = (Status) connection.get_object (TRACKER_DBUS_SERVICE,
 			                                             TRACKER_DBUS_OBJECT_STATUS,
 			                                             TRACKER_DBUS_INTERFACE_STATUS);
+
 			status.wait ();
 		} catch (DBus.Error e) {
 			throw new Sparql.Error.INTERNAL ("Unable to initialize database");
diff --git a/src/libtracker-miner/tracker-miner-object.c b/src/libtracker-miner/tracker-miner-object.c
index 073fb35..53972f7 100644
--- a/src/libtracker-miner/tracker-miner-object.c
+++ b/src/libtracker-miner/tracker-miner-object.c
@@ -255,7 +255,7 @@ tracker_miner_init (TrackerMiner *miner)
 
 	miner->private = priv = TRACKER_MINER_GET_PRIVATE (miner);
 
-	priv->connection = tracker_sparql_connection_get (&error);
+	priv->connection = tracker_sparql_connection_get (NULL, &error);
 	g_assert_no_error (error);
 
 	priv->pauses = g_hash_table_new_full (g_direct_hash,
diff --git a/src/libtracker-sparql/tracker-backend.vala b/src/libtracker-sparql/tracker-backend.vala
index 8c8e19e..1dc9245 100644
--- a/src/libtracker-sparql/tracker-backend.vala
+++ b/src/libtracker-sparql/tracker-backend.vala
@@ -29,13 +29,40 @@ class Tracker.Sparql.Backend : Connection {
 
 	private delegate Tracker.Sparql.Connection ModuleInitFunc ();
 
-	public Backend (bool direct_only = false) throws Sparql.Error
-	requires (!initialized) {
-		if (!Module.supported ()) {
-		    return;
+	public Backend () throws Sparql.Error
+	requires (Module.supported ()) {
+	}
+
+	public override void init (bool direct_only = false, Cancellable? cancellable = null) throws Sparql.Error, IOError {
+		if (initialized) {
+			// Don't error or require this, > 1 new Tracker.Sparql.Connection
+			// objects can be created and if they are, then we don't need to do
+			// anything on subsequent init() calls. We just return the already
+			// created direct or bus objects
+			return;
+		}
+		
+		try {
+			debug ("%s(): direct_only=%s", Log.METHOD, direct_only ? "true" : "false");
+			load_plugins (direct_only);
+		} catch (GLib.Error e) {
+			throw new Sparql.Error.INTERNAL (e.message);
+		}
+
+		initialized = true;
+	}
+
+	public async override void init_async (bool direct_only = false, Cancellable? cancellable = null) throws Sparql.Error, IOError {
+		if (initialized) {
+			// Don't error or require this, > 1 new Tracker.Sparql.Connection
+			// objects can be created and if they are, then we don't need to do
+			// anything on subsequent init() calls. We just return the already
+			// created direct or bus objects
+			return;
 		}
 
 		try {
+			debug ("%s(): direct_only=%s", Log.METHOD, direct_only ? "true" : "false");
 			load_plugins (direct_only);
 		} catch (GLib.Error e) {
 			throw new Sparql.Error.INTERNAL (e.message);
@@ -232,6 +259,7 @@ class Tracker.Sparql.Backend : Connection {
 			}
 
 			ModuleInitFunc module_init = (ModuleInitFunc) function;
+			
 			assert (module_init != null);
 
 			// We don't want our modules to ever unload
diff --git a/src/libtracker-sparql/tracker-connection.vala b/src/libtracker-sparql/tracker-connection.vala
index 2ed3fb0..ec41ce4 100644
--- a/src/libtracker-sparql/tracker-connection.vala
+++ b/src/libtracker-sparql/tracker-connection.vala
@@ -84,8 +84,75 @@ public abstract class Tracker.Sparql.Connection : Object {
 	static weak Connection? singleton;
 	static bool log_initialized;
 
+	private static new Connection get_internal (bool is_direct_only = false, Cancellable? cancellable = null) throws Sparql.Error, IOError {
+		if (singleton != null) {
+			assert (direct_only == is_direct_only);
+			return singleton;
+		}
+
+		log_init ();
+
+		if (cancellable != null && cancellable.is_cancelled ()) {
+			throw new IOError.CANCELLED ("Operation was cancelled");
+		}
+
+		/* the True is to assert that direct only is required */
+		Connection result = new Backend ();
+		result.init (is_direct_only, cancellable);
+
+		if (cancellable != null && cancellable.is_cancelled ()) {
+			throw new IOError.CANCELLED ("Operation was cancelled");
+		}
+
+		direct_only = is_direct_only;
+		singleton = result;
+		result.add_weak_pointer ((void**) (&singleton));
+		return singleton;
+	}
+
+	private async static new Connection get_internal_async (bool is_direct_only = false, Cancellable? cancellable = null) throws Sparql.Error, IOError {
+		if (singleton != null) {
+			assert (direct_only == is_direct_only);
+			return singleton;
+		}
+
+		log_init ();
+
+		if (cancellable != null && cancellable.is_cancelled ()) {
+			throw new IOError.CANCELLED ("Operation was cancelled");
+		}
+
+		/* the True is to assert that direct only is required */
+		Connection result = new Backend ();
+		yield result.init_async (is_direct_only, cancellable);
+
+		if (cancellable != null && cancellable.is_cancelled ()) {
+			throw new IOError.CANCELLED ("Operation was cancelled");
+		}
+
+		direct_only = true;
+		singleton = result;
+		result.add_weak_pointer ((void**) (&singleton));
+		return singleton;
+	}
+
+	/**
+	 * tracker_sparql_connection_get_direct_async:
+	 * @cancellable: a #GCancellable used to cancel the operation
+	 * @error: #GError for error reporting.
+	 *
+	 * See tracker_sparql_connection_get().
+	 *
+	 * Returns: a new #TrackerSparqlConnection. Call g_object_unref() on the
+	 * object when no longer used.
+	 */
+	public async static new Connection get_async (Cancellable? cancellable = null) throws Sparql.Error, IOError {
+		return yield get_internal_async (false, cancellable);
+	}
+
 	/**
 	 * tracker_sparql_connection_get:
+	 * @cancellable: a #GCancellable used to cancel the operation
 	 * @error: #GError for error reporting.
 	 *
 	 * Returns a new #TrackerSparqlConnection, which will use the best method
@@ -109,22 +176,27 @@ public abstract class Tracker.Sparql.Connection : Object {
 	 * Returns: a new #TrackerSparqlConnection. Call g_object_unref() on the
 	 * object when no longer used.
 	 */
-	public static new Connection get () throws Sparql.Error {
-		if (singleton != null) {
-			assert (!direct_only);
-			return singleton;
-		} else {
-			log_init ();
+	public static new Connection get (Cancellable? cancellable = null) throws Sparql.Error, IOError {
+		return get_internal (false, cancellable);
+	}
 
-			var result = new Backend ();
-			singleton = result;
-			result.add_weak_pointer ((void**) (&singleton));
-			return result;
-		}
+	/**
+	 * tracker_sparql_connection_get_direct_async:
+	 * @cancellable: a #GCancellable used to cancel the operation
+	 * @error: #GError for error reporting.
+	 *
+	 * See tracker_sparql_connection_get_direct().
+	 *
+	 * Returns: a new #TrackerSparqlConnection. Call g_object_unref() on the
+	 * object when no longer used.
+	 */
+	public async static Connection get_direct_async (Cancellable? cancellable = null) throws Sparql.Error, IOError {
+		return yield get_internal_async (true, cancellable);
 	}
 
 	/**
 	 * tracker_sparql_connection_get_direct:
+	 * @cancellable: a #GCancellable used to cancel the operation
 	 * @error: #GError for error reporting.
 	 *
 	 * Returns a new #TrackerSparqlConnection, which uses direct-access method
@@ -138,19 +210,8 @@ public abstract class Tracker.Sparql.Connection : Object {
 	 * Returns: a new #TrackerSparqlConnection. Call g_object_unref() on the
 	 * object when no longer used.
 	 */
-	public static Connection get_direct () throws Sparql.Error {
-		if (singleton != null) {
-			assert (direct_only);
-			return singleton;
-		} else {
-			log_init ();
-
-			var result = new Backend (true /* direct_only */);
-			direct_only = true;
-			singleton = result;
-			result.add_weak_pointer ((void**) (&singleton));
-			return result;
-		}
+	public static new Connection get_direct (Cancellable? cancellable = null) throws Sparql.Error, IOError {
+		return get_internal (true, cancellable);
 	}
 
 	private static void log_init () {
@@ -202,6 +263,14 @@ public abstract class Tracker.Sparql.Connection : Object {
 		/* do nothing */
 	}
 
+	public virtual void init (bool direct_only = false, Cancellable? cancellable = null) throws Sparql.Error, IOError {
+		warning ("Interface 'init' not implemented");
+	}
+
+	public async virtual void init_async (bool direct_only = false, Cancellable? cancellable = null) throws Sparql.Error, IOError {
+		warning ("Interface 'init_async' not implemented");
+	}
+
 	/**
 	 * tracker_sparql_connection_query:
 	 * @self: a #TrackerSparqlConnection
diff --git a/src/miners/fs/tracker-miner-files-index.c b/src/miners/fs/tracker-miner-files-index.c
index 27d79cd..ef724a3 100644
--- a/src/miners/fs/tracker-miner-files-index.c
+++ b/src/miners/fs/tracker-miner-files-index.c
@@ -242,7 +242,7 @@ tracker_miner_files_index_reindex_mime_types (TrackerMinerFilesIndex  *object,
 	                          __FUNCTION__,
 	                          len);
 
-	connection = tracker_sparql_connection_get (&inner_error);
+	connection = tracker_sparql_connection_get (NULL, &inner_error);
 
 	if (!connection) {
 		tracker_dbus_request_failed (request_id,
diff --git a/src/tracker-utils/tracker-import.c b/src/tracker-utils/tracker-import.c
index 682ced4..918e051 100644
--- a/src/tracker-utils/tracker-import.c
+++ b/src/tracker-utils/tracker-import.c
@@ -108,7 +108,7 @@ main (int argc, char **argv)
 		g_thread_init (NULL);
 	}
 
-	connection = tracker_sparql_connection_get (&error);
+	connection = tracker_sparql_connection_get (NULL, &error);
 
 	if (!connection) {
 		g_printerr ("%s: %s\n",
diff --git a/src/tracker-utils/tracker-info.c b/src/tracker-utils/tracker-info.c
index 7892752..5c39724 100644
--- a/src/tracker-utils/tracker-info.c
+++ b/src/tracker-utils/tracker-info.c
@@ -201,7 +201,7 @@ main (int argc, char **argv)
 		g_thread_init (NULL);
 	}
 
-	connection = tracker_sparql_connection_get (&error);
+	connection = tracker_sparql_connection_get (NULL, &error);
 
 	if (!connection) {
 		g_printerr ("%s: %s\n",
diff --git a/src/tracker-utils/tracker-search.c b/src/tracker-utils/tracker-search.c
index 2280d9a..71096e4 100644
--- a/src/tracker-utils/tracker-search.c
+++ b/src/tracker-utils/tracker-search.c
@@ -1268,7 +1268,7 @@ main (int argc, char **argv)
 
 	g_option_context_free (context);
 
-	connection = tracker_sparql_connection_get (&error);
+	connection = tracker_sparql_connection_get (NULL, &error);
 
 	if (!connection) {
 		g_printerr ("%s: %s\n",
diff --git a/src/tracker-utils/tracker-sparql.c b/src/tracker-utils/tracker-sparql.c
index f489951..5ff7911 100644
--- a/src/tracker-utils/tracker-sparql.c
+++ b/src/tracker-utils/tracker-sparql.c
@@ -260,7 +260,7 @@ main (int argc, char **argv)
 		g_thread_init (NULL);
 	}
 
-	connection = tracker_sparql_connection_get (&error);
+	connection = tracker_sparql_connection_get (NULL, &error);
 
 	if (!connection) {
 		g_printerr ("%s: %s\n",
diff --git a/src/tracker-utils/tracker-stats.c b/src/tracker-utils/tracker-stats.c
index f6c9ecb..64d33de 100644
--- a/src/tracker-utils/tracker-stats.c
+++ b/src/tracker-utils/tracker-stats.c
@@ -85,7 +85,7 @@ main (int argc, char **argv)
 		g_thread_init (NULL);
 	}
 
-	connection = tracker_sparql_connection_get (&error);
+	connection = tracker_sparql_connection_get (NULL, &error);
 
 	if (!connection) {
 		g_printerr ("%s: %s\n",
diff --git a/src/tracker-utils/tracker-tag.c b/src/tracker-utils/tracker-tag.c
index 200daf2..b3a9a8c 100644
--- a/src/tracker-utils/tracker-tag.c
+++ b/src/tracker-utils/tracker-tag.c
@@ -937,7 +937,7 @@ main (int argc, char **argv)
 		g_thread_init (NULL);
 	}
 
-	connection = tracker_sparql_connection_get (&error);
+	connection = tracker_sparql_connection_get (NULL, &error);
 
 	if (!connection) {
 		g_printerr ("%s: %s\n",
diff --git a/src/tracker-writeback/tracker-writeback-consumer.c b/src/tracker-writeback/tracker-writeback-consumer.c
index d141bff..2c67f0a 100644
--- a/src/tracker-writeback/tracker-writeback-consumer.c
+++ b/src/tracker-writeback/tracker-writeback-consumer.c
@@ -82,7 +82,7 @@ tracker_writeback_consumer_init (TrackerWritebackConsumer *consumer)
 
 	priv = TRACKER_WRITEBACK_CONSUMER_GET_PRIVATE (consumer);
 
-	priv->connection = tracker_sparql_connection_get (&error);
+	priv->connection = tracker_sparql_connection_get (NULL, &error);
 
 	if (!priv->connection) {
 		g_printerr ("%s: %s\n",
diff --git a/tests/functional-tests/default-update-test.vala b/tests/functional-tests/default-update-test.vala
index 59ec0e8..acf93cc 100644
--- a/tests/functional-tests/default-update-test.vala
+++ b/tests/functional-tests/default-update-test.vala
@@ -1,14 +1,52 @@
-int
-main( string[] args )
-{
-	int res = -1;
+using Tracker.Sparql;
 
+private static int res;
+private static MainLoop loop;
+
+private async void get_connection (bool? direct_only = false) {
 	try {
-		TestApp app = new TestApp (Tracker.Sparql.Connection.get());
+		Connection c;
+
+		// Test async
+		print ("Getting connection async (direct=%s)\n", direct_only ? "yes" : "no");
+		if (direct_only) {
+			c = yield Connection.get_direct_async ();
+		} else {
+			c = yield Connection.get_async ();
+		}
+
+		print ("Got it %p\n", c);
+
+		// Quite this loo because we start another one in app.run ()
+		loop.quit ();
+
+		print ("Creating app with connection\n");
+		TestApp app = new TestApp (c);
+
+		print ("Running app\n");
 		res = app.run();
-	} catch (Tracker.Sparql.Error e) {
-		warning ("Couldn't perform test: %s", e.message);
+	} catch (GLib.IOError e1) {
+		warning ("Couldn't perform test: %s", e1.message);
+	} catch (Tracker.Sparql.Error e2) {
+		warning ("Couldn't perform test: %s", e2.message);
 	}
+}
+
+int
+main( string[] args )
+{
+	print ("Starting...\n");
+	loop = new MainLoop (null, false);
+
+	// Test non-direct first
+	get_connection.begin (false);
+
+	loop.run ();
+
+	// Test direct first
+	get_connection.begin (true);
+
+	loop.run ();
 
 	return res;
 }
diff --git a/tests/tracker-steroids/tracker-test.c b/tests/tracker-steroids/tracker-test.c
index 720819f..eec0f32 100644
--- a/tests/tracker-steroids/tracker-test.c
+++ b/tests/tracker-steroids/tracker-test.c
@@ -615,7 +615,7 @@ main (gint argc, gchar **argv)
 	/* do not require prior installation */
 	g_setenv ("TRACKER_SPARQL_MODULE_PATH", "../../src/libtracker-bus/.libs", TRUE);
 
-	connection = tracker_sparql_connection_get (NULL);
+	connection = tracker_sparql_connection_get (NULL, NULL);
 
 	insert_test_data ();
 



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