[tracker/direct-access: 56/121] libtracker-sparql: Add Connection.get and Connection.get_direct



commit cde981901a225a2727edfb2d5302a93019e6b292
Author: Jürg Billeter <j bitron ch>
Date:   Thu Jul 15 16:19:28 2010 +0200

    libtracker-sparql: Add Connection.get and Connection.get_direct

 src/libtracker-sparql/Makefile.am                |    1 -
 src/libtracker-sparql/tracker-connection.vala    |    8 +
 src/libtracker-sparql/tracker-plugin-loader.vala |  160 ++++++++++++++--------
 src/libtracker-sparql/tracker-query.vala         |   27 ----
 4 files changed, 109 insertions(+), 87 deletions(-)
---
diff --git a/src/libtracker-sparql/Makefile.am b/src/libtracker-sparql/Makefile.am
index 959e723..534b0fa 100644
--- a/src/libtracker-sparql/Makefile.am
+++ b/src/libtracker-sparql/Makefile.am
@@ -18,7 +18,6 @@ libtracker_sparql_la_VALASOURCES = 			\
 	tracker-connection.vala					\
 	tracker-cursor.vala						\
 	tracker-plugin-loader.vala				\
-	tracker-query.vala						\
 	tracker-utils.vala
 
 libtracker_sparql_ TRACKER_API_VERSION@_la_SOURCES = 	\
diff --git a/src/libtracker-sparql/tracker-connection.vala b/src/libtracker-sparql/tracker-connection.vala
index cdc1446..7bdd902 100644
--- a/src/libtracker-sparql/tracker-connection.vala
+++ b/src/libtracker-sparql/tracker-connection.vala
@@ -18,6 +18,14 @@
  */
 
 public abstract class Tracker.Sparql.Connection : Object {
+	public static Connection get () throws GLib.Error {
+		return new PluginLoader ();
+	}
+
+	public static Connection get_direct () throws GLib.Error {
+		return new PluginLoader (true /* direct_only */);
+	}
+
 	// Query
 	public abstract Cursor? query (string sparql, Cancellable? cancellable = null) throws GLib.Error;
 	public async abstract Cursor? query_async (string sparql, Cancellable? cancellable = null) throws GLib.Error;
diff --git a/src/libtracker-sparql/tracker-plugin-loader.vala b/src/libtracker-sparql/tracker-plugin-loader.vala
index 6d60afb..c1abaa7 100644
--- a/src/libtracker-sparql/tracker-plugin-loader.vala
+++ b/src/libtracker-sparql/tracker-plugin-loader.vala
@@ -17,29 +17,66 @@
  * Boston, MA  02110-1301, USA.
  */
 
-private class Tracker.Sparql.PluginLoader : Object {
+class Tracker.Sparql.PluginLoader : Connection {
 	static bool initialized = false;
 	static Tracker.Sparql.Connection direct = null;
 	static Tracker.Sparql.Connection bus = null;
 
 	private delegate Tracker.Sparql.Connection ModuleInitFunc ();
 
-	public PluginLoader ()
+	public PluginLoader (bool direct_only = false) throws Error
 	requires (!initialized) {
 		if (!Module.supported ()) {
 		    return;
 		}
 
-		if (!load_plugins ()) {
-			initialized = false;
-			return;
-		}
+		load_plugins (direct_only);
 
 		initialized = true;
 	}
 
+	public override Cursor? query (string sparql, Cancellable? cancellable = null) throws GLib.Error {
+		if (direct != null) {
+			return direct.query (sparql, cancellable);
+		} else {
+			return bus.query (sparql, cancellable);
+		}
+	}
+
+	public async override Cursor? query_async (string sparql, Cancellable? cancellable = null) throws GLib.Error {
+		if (direct != null) {
+			return yield direct.query_async (sparql, cancellable);
+		} else {
+			return yield bus.query_async (sparql, cancellable);
+		}
+	}
+
+	public override void update (string sparql, Cancellable? cancellable = null) throws GLib.Error {
+		bus.update (sparql, cancellable);
+	}
+
+	public async override void update_async (string sparql, int? priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws GLib.Error {
+		yield bus.update_async (sparql, priority, cancellable);
+	}
+
+	public override void update_commit (Cancellable? cancellable = null) throws GLib.Error {
+		bus.update_commit (cancellable);
+	}
+
+	public async override void update_commit_async (Cancellable? cancellable = null) throws GLib.Error {
+		yield bus.update_commit_async (cancellable);
+	}
+
+	public override void import (File file, Cancellable? cancellable = null) throws GLib.Error {
+		bus.import (file, cancellable);
+	}
+
+	public async override void import_async (File file, Cancellable? cancellable = null) throws GLib.Error {
+		yield bus.import_async (file, cancellable);
+	}
+
 	// Plugin loading functions
-	private bool load_plugins () {
+	private bool load_plugins (bool direct_only) throws GLib.Error {
 		string env_path = Environment.get_variable ("TRACKER_SPARQL_MODULE_PATH");
 		string path;
 		
@@ -57,79 +94,84 @@ private class Tracker.Sparql.PluginLoader : Object {
 
 		// First get direct library details
 		string direct_path = Module.build_path (dir_path, "tracker-direct-0.9");
-		direct = load_plugins_from_path (direct_path);
+		direct = load_plugins_from_path (direct_path, direct_only /* required */);
 
-		// Second get bus library details
-		string bus_path = Module.build_path (dir_path, "tracker-bus-0.9");
-		bus = load_plugins_from_path (bus_path);
+		if (!direct_only) {
+			// Second get bus library details
+			string bus_path = Module.build_path (dir_path, "tracker-bus-0.9");
+			bus = load_plugins_from_path (bus_path, true /* required */);
+		}
 
 		debug ("Finished searching for modules in folder '%s'", dir_path);
 
-		// FIXME: Finish this by checking for bus too!
-		return direct != null;
+		if (direct_only) {
+			return direct != null;
+		} else {
+			return bus != null;
+		}
 	}
 
-	private Tracker.Sparql.Connection? load_plugins_from_path (string path) {
-		File file = File.new_for_path (path);
-		assert (file != null);
+	private Tracker.Sparql.Connection? load_plugins_from_path (string path, bool required) throws GLib.Error {
+		try {
+			File file = File.new_for_path (path);
+			assert (file != null);
 
-		FileInfo info = null;
+			FileInfo info = null;
 
-		try {
 			string attributes = FILE_ATTRIBUTE_STANDARD_NAME + "," +
-			                    FILE_ATTRIBUTE_STANDARD_TYPE + "," +
-			                    FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE;
+				            FILE_ATTRIBUTE_STANDARD_TYPE + "," +
+				            FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE;
 
 			info = file.query_info (attributes,
-			                        FileQueryInfoFlags.NONE,
-			                        null);
-		} catch (Error e) {
-			warning ("Could not get GFileInfo for '%s'", path);
-			return null;
-		}
+				                FileQueryInfoFlags.NONE,
+				                null);
 
-		string content_type = info.get_content_type ();
-		string mime = g_content_type_get_mime_type (content_type);
-		string expected_mime = "application/x-sharedlib";
+			string content_type = info.get_content_type ();
+			string mime = g_content_type_get_mime_type (content_type);
+			string expected_mime = "application/x-sharedlib";
 		
-		if (mime != expected_mime) {
-			warning ("Could not load plugin, mime type was '%s', expected:'%s'", 
-			         mime,
-			         expected_mime);
-			return null;
-		}
-
-		Module module = Module.open (path, ModuleFlags.BIND_LOCAL);
-		if (module == null) {
-			warning ("Failed to load module from path '%s': %s",
-			         path,
-			         Module.error ());
-			return null;
-		}
+			if (mime != expected_mime) {
+				throw new IOError.FAILED ("Could not load plugin, mime type was '%s', expected:'%s'",
+				                          mime,
+				                          expected_mime);
+			}
 
-		void *function;
+			Module module = Module.open (path, ModuleFlags.BIND_LOCAL);
+			if (module == null) {
+				throw new IOError.FAILED ("Failed to load module from path '%s': %s",
+				                          path,
+				                          Module.error ());
+			}
 
-		if (!module.symbol ("module_init", out function)) {
-			warning ("Failed to find entry point function '%s' in '%s': %s",
-			         "module_init",
-			         path,
-			         Module.error ());
+			void *function;
 
-			return null;
-		}
+			if (!module.symbol ("module_init", out function)) {
+				throw new IOError.FAILED ("Failed to find entry point function '%s' in '%s': %s",
+				                          "module_init",
+				                          path,
+				                          Module.error ());
+			}
 
-		ModuleInitFunc module_init = (ModuleInitFunc) function;
-		assert (module_init != null);
+			ModuleInitFunc module_init = (ModuleInitFunc) function;
+			assert (module_init != null);
 
-		// We don't want our modules to ever unload
-		module.make_resident ();
+			// We don't want our modules to ever unload
+			module.make_resident ();
 
-		// Call module init function
-		Tracker.Sparql.Connection c = module_init ();
+			// Call module init function
+			Tracker.Sparql.Connection c = module_init ();
 
-		debug ("Loaded module source: '%s'", module.name ());
+			debug ("Loaded module source: '%s'", module.name ());
 
-		return c;
+			return c;
+		} catch (Error e) {
+			if (required) {
+				// plugin required => error is fatal
+				throw e;
+			} else {
+				return null;
+			}
+		}
 	}
 }
 



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