[tracker/tracker-0.6] Add a SetProfile DBus call to the indexer to switch DB pragma profiles on runtime.



commit 0554aa8622f9579bab81955193718af7a2d0f2db
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Jun 3 12:54:52 2009 +0200

    Add a SetProfile DBus call to the indexer to switch DB pragma profiles on runtime.
    
    tracker_db_manager_init() was added a new parameter for the profile name, it
    will default to "Safe" if profile_name is NULL or doesn't exist. Callers of
    this function have been modified to pass a NULL profile_name.
---
 data/dbus/tracker-indexer.xml          |    5 ++
 src/libtracker-db/tracker-db-manager.c |   33 ++++++-----
 src/libtracker-db/tracker-db-manager.h |    3 +-
 src/tracker-indexer/tracker-indexer.c  |  102 ++++++++++++++++++++++++++++----
 src/tracker-indexer/tracker-indexer.h  |    4 +
 src/tracker-indexer/tracker-main.c     |    2 +-
 src/tracker-utils/tracker-processes.c  |    2 +-
 src/trackerd/tracker-main.c            |    4 +-
 8 files changed, 124 insertions(+), 31 deletions(-)

diff --git a/data/dbus/tracker-indexer.xml b/data/dbus/tracker-indexer.xml
index e084b84..ce6b1d6 100644
--- a/data/dbus/tracker-indexer.xml
+++ b/data/dbus/tracker-indexer.xml
@@ -79,6 +79,11 @@
       <arg type="s" name="backup_file" direction="in" />
     </method>
 
+    <method name="SetProfile">
+      <annotation name="org.freedesktop.DBus.GLib.Async" value="true"/>
+      <arg type="s" name="profile_name" direction="in" />
+    </method>
+
     <!-- Signals --> 
     <signal name="Status">
       <arg type="d" name="elapsed_time" />
diff --git a/src/libtracker-db/tracker-db-manager.c b/src/libtracker-db/tracker-db-manager.c
index 8771294..1fe426c 100644
--- a/src/libtracker-db/tracker-db-manager.c
+++ b/src/libtracker-db/tracker-db-manager.c
@@ -237,13 +237,12 @@ location_to_directory (TrackerDBLocation location)
 }
 
 static gboolean
-load_pragma_file (gboolean safe)
+load_pragma_file (const gchar *profile_name)
 {
 	GKeyFile      *key_file = NULL;
 	GError        *error = NULL;
 	GStrv          keys;
 	gchar	      *pragma_file;
-	const gchar   *group;
 	gint           i;
 
 	if (pragmas) {
@@ -258,9 +257,13 @@ load_pragma_file (gboolean safe)
 	key_file = g_key_file_new ();
 	pragma_file = g_build_filename (config_dir, "sqlite-db.pragmas", NULL);
 
-	g_message ("Loading pragma file:'%s' using %s values", 
+	if (!profile_name) {
+		profile_name = "Safe";
+	}
+
+	g_message ("Loading pragma file:'%s' using profile '%s'",
 		   pragma_file,
-		   safe ? "safe" : "fast");
+		   profile_name);
 
 	if (!g_key_file_load_from_file (key_file, pragma_file, G_KEY_FILE_NONE, &error)) {
 		g_message ("  Couldn't load pragma file, %s", 
@@ -272,22 +275,21 @@ load_pragma_file (gboolean safe)
 
 		g_message ("  Trying to re-create file with defaults"); 
 
-		save_pragma_file_defaults (safe);
-		return load_pragma_file (safe);
+		save_pragma_file_defaults (TRUE);
+		return load_pragma_file (NULL);
 	}
 
-	if (safe) {
-		group = "Safe";
-	} else {
-		group = "Fast";
+	if (!g_key_file_has_group (key_file, profile_name)) {
+		g_warning ("  Profile '%s' does not exist, stepping back to 'Safe'", profile_name);
+		profile_name = "Safe";
 	}
 
-	keys = g_key_file_get_keys (key_file, group, NULL, NULL);
+	keys = g_key_file_get_keys (key_file, profile_name, NULL, NULL);
 
 	for (i = 0; keys[i]; i++) {
 		gchar *value;
 
-		value = g_key_file_get_string (key_file, group, keys[i], NULL);
+		value = g_key_file_get_string (key_file, profile_name, keys[i], NULL);
 		g_hash_table_insert (pragmas, g_strdup (keys[i]), value);
 
 		g_message ("  Adding pragma '%s' with value '%s'", 
@@ -2142,7 +2144,8 @@ tracker_db_manager_ensure_locale (void)
 gboolean
 tracker_db_manager_init (TrackerDBManagerFlags	flags,
 			 gboolean	       *first_time,
-			 gboolean	        shared_cache)
+			 gboolean	        shared_cache,
+			 const gchar           *profile_name)
 {
 	GType		    etype;
 	TrackerDBVersion    version;
@@ -2283,8 +2286,8 @@ tracker_db_manager_init (TrackerDBManagerFlags	flags,
 	load_prepared_queries ();
 
 	/* Get pragma details */
-	load_pragma_file (TRUE);
-	
+	load_pragma_file (profile_name);
+
 	/* Should we reindex? If so, just remove all databases files,
 	 * NOT the paths, note, that these paths are also used for
 	 * other things like the nfs lock file.
diff --git a/src/libtracker-db/tracker-db-manager.h b/src/libtracker-db/tracker-db-manager.h
index 3a0961d..c54d01f 100644
--- a/src/libtracker-db/tracker-db-manager.h
+++ b/src/libtracker-db/tracker-db-manager.h
@@ -66,7 +66,8 @@ GType	     tracker_db_get_type			    (void) G_GNUC_CONST;
 
 gboolean     tracker_db_manager_init			    (TrackerDBManagerFlags  flags,
 							     gboolean		   *first_time,
-							     gboolean 		    shared_cache);
+							     gboolean 		    shared_cache,
+							     const gchar           *profile_name);
 void	     tracker_db_manager_shutdown		    (void);
 
 void	     tracker_db_manager_remove_all		    (void);
diff --git a/src/tracker-indexer/tracker-indexer.c b/src/tracker-indexer/tracker-indexer.c
index 7d4bb9b..98b6422 100644
--- a/src/tracker-indexer/tracker-indexer.c
+++ b/src/tracker-indexer/tracker-indexer.c
@@ -1025,6 +1025,26 @@ tracker_indexer_load_modules (TrackerIndexer *indexer)
 }
 
 static void
+set_up_databases (TrackerIndexer *indexer)
+{
+	TrackerIndexerPrivate *priv;
+
+	priv = indexer->private;
+
+	/* Set up databases, these pointers are mostly used to
+	 * start/stop transactions, since TrackerDBManager treats
+	 * interfaces as singletons, it's safe to just ask it
+	 * again for an interface.
+	 */
+	priv->cache = tracker_db_manager_get_db_interface (TRACKER_DB_CACHE);
+	priv->common = tracker_db_manager_get_db_interface (TRACKER_DB_COMMON);
+	priv->file_metadata = tracker_db_manager_get_db_interface (TRACKER_DB_FILE_METADATA);
+	priv->file_contents = tracker_db_manager_get_db_interface (TRACKER_DB_FILE_CONTENTS);
+	priv->email_metadata = tracker_db_manager_get_db_interface (TRACKER_DB_EMAIL_METADATA);
+	priv->email_contents = tracker_db_manager_get_db_interface (TRACKER_DB_EMAIL_CONTENTS);
+}
+
+static void
 tracker_indexer_init (TrackerIndexer *indexer)
 {
 	TrackerIndexerPrivate *priv;
@@ -1088,17 +1108,7 @@ tracker_indexer_init (TrackerIndexer *indexer)
 	g_signal_connect (priv->email_index, "error-received",
 			  G_CALLBACK (index_error_received_cb), indexer);
 
-	/* Set up databases, these pointers are mostly used to
-	 * start/stop transactions, since TrackerDBManager treats
-	 * interfaces as singletons, it's safe to just ask it
-	 * again for an interface.
-	 */
-	priv->cache = tracker_db_manager_get_db_interface (TRACKER_DB_CACHE);
-	priv->common = tracker_db_manager_get_db_interface (TRACKER_DB_COMMON);
-	priv->file_metadata = tracker_db_manager_get_db_interface (TRACKER_DB_FILE_METADATA);
-	priv->file_contents = tracker_db_manager_get_db_interface (TRACKER_DB_FILE_CONTENTS);
-	priv->email_metadata = tracker_db_manager_get_db_interface (TRACKER_DB_EMAIL_METADATA);
-	priv->email_contents = tracker_db_manager_get_db_interface (TRACKER_DB_EMAIL_CONTENTS);
+	set_up_databases (indexer);
 
 	/* Set up volume monitor */
 	priv->volume_monitor = g_volume_monitor_get ();
@@ -3650,3 +3660,73 @@ tracker_indexer_shutdown (TrackerIndexer	 *indexer,
 	dbus_g_method_return (context);
 	tracker_dbus_request_success (request_id);
 }
+
+static gboolean
+set_profile (TrackerIndexer *indexer,
+	     const gchar    *profile_name)
+{
+	TrackerDBManagerFlags flags = 0;
+	TrackerIndexerPrivate *priv;
+	gboolean return_val = TRUE;
+
+	priv = indexer->private;
+
+	if (tracker_config_get_low_memory_mode (priv->config)) {
+		flags |= TRACKER_DB_MANAGER_LOW_MEMORY_MODE;
+	}
+
+	/* Pause the indexer, this also flushes data to DBs */
+	tracker_indexer_set_running (indexer, FALSE);
+
+	/* Reinitialize DB Manager with new profile */
+	tracker_db_manager_shutdown ();
+
+	if (!tracker_db_manager_init (flags, NULL, FALSE, profile_name)) {
+		g_critical ("Could not restart DB manager, trying again with defaults");
+
+		if (!tracker_db_manager_init (flags, NULL, FALSE, NULL)) {
+			g_critical ("  Not even defaults worked, bailing out.");
+			g_assert_not_reached ();
+		}
+
+		return_val = FALSE;
+	}
+
+	/* Restore database pointers */
+	set_up_databases (indexer);
+
+	/* Restart the indexer */
+	tracker_indexer_set_running (indexer, TRUE);
+
+	return return_val;
+}
+
+void
+tracker_indexer_set_profile (TrackerIndexer         *indexer,
+			     const gchar            *profile_name,
+			     DBusGMethodInvocation  *context,
+			     GError                **error)
+{
+	guint request_id;
+
+	tracker_dbus_async_return_if_fail (TRACKER_IS_INDEXER (indexer), context);
+
+	request_id = tracker_dbus_get_next_request_id ();
+	tracker_dbus_request_new (request_id,
+				  "DBus request to switch to profile '%s'", profile_name);
+
+	if (!set_profile (indexer, profile_name)) {
+		GError *actual_error = NULL;
+
+		tracker_dbus_request_failed (request_id,
+					     &actual_error,
+					     "Could not set profile '%s'",
+					     profile_name);
+
+		dbus_g_method_return_error (context, actual_error);
+		g_error_free (actual_error);
+	} else {
+		dbus_g_method_return (context);
+		tracker_dbus_request_success (request_id);
+	}
+}
diff --git a/src/tracker-indexer/tracker-indexer.h b/src/tracker-indexer/tracker-indexer.h
index 5e6f5e3..172ca0b 100644
--- a/src/tracker-indexer/tracker-indexer.h
+++ b/src/tracker-indexer/tracker-indexer.h
@@ -158,6 +158,10 @@ void            tracker_indexer_restore_backup      (TrackerIndexer         *ind
 void            tracker_indexer_shutdown            (TrackerIndexer         *indexer,
 						     DBusGMethodInvocation  *context,
 						     GError                **error);
+void            tracker_indexer_set_profile         (TrackerIndexer         *indexer,
+						     const gchar            *profile_name,
+						     DBusGMethodInvocation  *context,
+						     GError                **error);
 
 G_END_DECLS
 
diff --git a/src/tracker-indexer/tracker-main.c b/src/tracker-indexer/tracker-main.c
index e22dbaa..aad1b15 100644
--- a/src/tracker-indexer/tracker-main.c
+++ b/src/tracker-indexer/tracker-main.c
@@ -363,7 +363,7 @@ main (gint argc, gchar *argv[])
 		flags |= TRACKER_DB_MANAGER_LOW_MEMORY_MODE;
 	}
 
-	if (!tracker_db_manager_init (flags, NULL, FALSE)) {
+	if (!tracker_db_manager_init (flags, NULL, FALSE, NULL)) {
 		return EXIT_FAILURE;
         }
 
diff --git a/src/tracker-utils/tracker-processes.c b/src/tracker-utils/tracker-processes.c
index dae5140..1e4b844 100644
--- a/src/tracker-utils/tracker-processes.c
+++ b/src/tracker-utils/tracker-processes.c
@@ -247,7 +247,7 @@ main (int argc, char **argv)
 		g_log_set_default_handler (log_handler, NULL);
 
 		/* Clean up */
-		if (!tracker_db_manager_init (TRACKER_DB_MANAGER_REMOVE_ALL, NULL, FALSE)) {
+		if (!tracker_db_manager_init (TRACKER_DB_MANAGER_REMOVE_ALL, NULL, FALSE, NULL)) {
 			return EXIT_FAILURE;
 		}
 
diff --git a/src/trackerd/tracker-main.c b/src/trackerd/tracker-main.c
index 9ea14c2..4c46c3a 100644
--- a/src/trackerd/tracker-main.c
+++ b/src/trackerd/tracker-main.c
@@ -598,7 +598,7 @@ backup_user_metadata (TrackerConfig *config, TrackerLanguage *language)
 	/*
 	 *  Init the DB stack to get the user metadata
 	 */
-	if (!tracker_db_manager_init (0, &is_first_time_index, TRUE)) {
+	if (!tracker_db_manager_init (0, &is_first_time_index, TRUE, NULL)) {
 		return;
 	}
 
@@ -1102,7 +1102,7 @@ main (gint argc, gchar *argv[])
 		flags |= TRACKER_DB_MANAGER_LOW_MEMORY_MODE;
 	}
 
-	if (!tracker_db_manager_init (flags, &is_first_time_index, TRUE)) {
+	if (!tracker_db_manager_init (flags, &is_first_time_index, TRUE, NULL)) {
 		return EXIT_FAILURE;
 	}
 



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