[tracker/collation: 9/15] libtracker-data: if locale changes, re-create indexes



commit 95917bab8c97f909fa0705a187825d56be13a520
Author: Aleksander Morgado <aleksander lanedo com>
Date:   Wed Aug 25 17:52:16 2010 +0200

    libtracker-data: if locale changes, re-create indexes

 src/libtracker-data/tracker-data-manager.c |   99 ++++++++++++++++++++++++----
 src/libtracker-data/tracker-db-manager.c   |   27 ++++++--
 src/libtracker-data/tracker-db-manager.h   |    3 +-
 3 files changed, 107 insertions(+), 22 deletions(-)
---
diff --git a/src/libtracker-data/tracker-data-manager.c b/src/libtracker-data/tracker-data-manager.c
index b097ead..b1ef80e 100644
--- a/src/libtracker-data/tracker-data-manager.c
+++ b/src/libtracker-data/tracker-data-manager.c
@@ -111,7 +111,8 @@ set_secondary_index_for_single_value_property (TrackerDBInterface *iface,
                                                const gchar        *second_field_name,
                                                gboolean            enabled)
 {
-	g_debug ("Dropping index:  DROP INDEX IF EXISTS \"%s_%s\"",
+	g_debug ("Dropping secondary index (single-value property):  "
+	         "DROP INDEX IF EXISTS \"%s_%s\"",
 	         service_name, field_name);
 
 	tracker_db_interface_execute_query (iface, NULL,
@@ -120,7 +121,8 @@ set_secondary_index_for_single_value_property (TrackerDBInterface *iface,
 	                                    field_name);
 
 	if (enabled) {
-		g_debug ("Creating index: CREATE INDEX \"%s_%s\" ON \"%s\" (\"%s\", \"%s\")",
+		g_debug ("Creating secondary index (single-value property): "
+		         "CREATE INDEX \"%s_%s\" ON \"%s\" (\"%s\", \"%s\")",
 		         service_name, field_name, service_name, field_name, second_field_name);
 
 		tracker_db_interface_execute_query (iface, NULL,
@@ -139,7 +141,8 @@ set_index_for_single_value_property (TrackerDBInterface *iface,
                                      const gchar        *field_name,
                                      gboolean            enabled)
 {
-	g_debug ("Dropping index: DROP INDEX IF EXISTS \"%s_%s\"",
+	g_debug ("Dropping index (single-value property): "
+	         "DROP INDEX IF EXISTS \"%s_%s\"",
 	         service_name, field_name);
 
 	tracker_db_interface_execute_query (iface, NULL,
@@ -148,7 +151,8 @@ set_index_for_single_value_property (TrackerDBInterface *iface,
 	                                    field_name);
 
 	if (enabled) {
-		g_debug ("Creating index: CREATE INDEX \"%s_%s\" ON \"%s\" (\"%s\")",
+		g_debug ("Creating index (single-value property): "
+		         "CREATE INDEX \"%s_%s\" ON \"%s\" (\"%s\")",
 		         service_name, field_name, service_name, field_name);
 
 		tracker_db_interface_execute_query (iface, NULL,
@@ -166,18 +170,46 @@ set_index_for_multi_value_property (TrackerDBInterface *iface,
                                     const gchar        *field_name,
                                     gboolean            enabled)
 {
+	g_debug ("Dropping index (multi-value property): "
+	         "DROP INDEX IF EXISTS \"%s_%s_ID_ID\"",
+	         service_name, field_name);
 	tracker_db_interface_execute_query (iface, NULL,
 	                                    "DROP INDEX IF EXISTS \"%s_%s_ID_ID\"",
 	                                    service_name,
 	                                    field_name);
 
+	/* Useful to have this here for the cases where we want to fully
+	 * re-create the indexes even without an ontology change (when locale
+	 * of the user changes) */
+	g_debug ("Dropping index (multi-value property): "
+	         "DROP INDEX IF EXISTS \"%s_%s_ID\"",
+	         service_name,
+	         field_name);
+	tracker_db_interface_execute_query (iface, NULL,
+	                                    "DROP INDEX IF EXISTS \"%s_%s_ID\"",
+	                                    service_name,
+	                                    field_name);
+
 	if (enabled) {
+		g_debug ("Creating index (multi-value property): "
+		         "CREATE INDEX \"%s_%s_ID\" ON \"%s_%s\" (ID)",
+		         service_name,
+		         field_name,
+		         service_name,
+		         field_name);
 		tracker_db_interface_execute_query (iface, NULL,
 		                                    "CREATE INDEX \"%s_%s_ID\" ON \"%s_%s\" (ID)",
 		                                    service_name,
 		                                    field_name,
 		                                    service_name,
 		                                    field_name);
+		g_debug ("Creating index (multi-value property): "
+		         "CREATE UNIQUE INDEX \"%s_%s_ID_ID\" ON \"%s_%s\" (\"%s\", ID)",
+		         service_name,
+		         field_name,
+		         service_name,
+		         field_name,
+		         field_name);
 		tracker_db_interface_execute_query (iface, NULL,
 		                                    "CREATE UNIQUE INDEX \"%s_%s_ID_ID\" ON \"%s_%s\" (\"%s\", ID)",
 		                                    service_name,
@@ -186,10 +218,13 @@ set_index_for_multi_value_property (TrackerDBInterface *iface,
 		                                    field_name,
 		                                    field_name);
 	} else {
-		tracker_db_interface_execute_query (iface, NULL,
-		                                    "DROP INDEX IF EXISTS \"%s_%s_ID\"",
-		                                    service_name,
-		                                    field_name);
+		g_debug ("Creating index (multi-value property): "
+		         "CREATE UNIQUE INDEX \"%s_%s_ID_ID\" ON \"%s_%s\" (ID, \"%s\")",
+		         service_name,
+		         field_name,
+		         service_name,
+		         field_name,
+		         field_name);
 		tracker_db_interface_execute_query (iface, NULL,
 		                                    "CREATE UNIQUE INDEX \"%s_%s_ID_ID\" ON \"%s_%s\" (ID, \"%s\")",
 		                                    service_name,
@@ -2677,9 +2712,44 @@ get_new_service_id (TrackerDBInterface *iface)
 	return ++max_service_id;
 }
 
+static void
+tracker_data_manager_recreate_indexes (void)
+{
+	TrackerDBInterface *iface;
+	TrackerProperty **properties;
+	guint n_properties;
+	guint i;
+
+	properties = tracker_ontologies_get_properties (&n_properties);
+	if (!properties) {
+		g_critical ("Couldn't get all properties to recreate indexes");
+		return;
+	}
+
+	iface = tracker_db_manager_get_db_interface ();
+
+	g_debug ("Starting index re-creation...");
+	for (i = 0; i < n_properties; i++) {
+		TrackerClass *class;
+		const gchar *service_name;
+		const gchar *field_name;
+
+		class = tracker_property_get_domain (properties [i]);
+		field_name = tracker_property_get_name (properties [i]);
+		service_name = tracker_class_get_name (class);
+		g_debug ("  Re-creating possible indexes in property "
+		         "(service: '%s', field: '%s')",
+		         service_name,
+		         field_name);
+
+		fix_indexed (properties [i]);
+	}
+	g_debug ("  Finished index re-creation...");
+}
+
 gboolean
 tracker_data_manager_init (TrackerDBManagerFlags  flags,
-                          const gchar          **test_schemas,
+                           const gchar          **test_schemas,
                            gboolean              *first_time,
                            gboolean               journal_check,
                            TrackerBusyCallback    busy_callback,
@@ -2861,11 +2931,6 @@ tracker_data_manager_init (TrackerDBManagerFlags  flags,
 		tracker_db_interface_sqlite_fts_init (iface, FALSE);
 	}
 
-	/* If LC_COLLATE changed, re-create indexes, before applying
-	 * ontology changes, as those will already use the new locale.
-	 */
-	tracker_db_manager_ensure_locale ();
-
 	if (check_ontology && !read_only) {
 		GList *to_reload = NULL;
 		GList *ontos = NULL;
@@ -3064,6 +3129,12 @@ tracker_data_manager_init (TrackerDBManagerFlags  flags,
 		g_list_free (ontos);
 	}
 
+	/* If locale changed, re-create indexes */
+	if (!read_only && tracker_db_manager_locale_changed ()) {
+		tracker_data_manager_recreate_indexes ();
+		tracker_db_manager_set_current_locale ();
+	}
+
 	initialized = TRUE;
 
 	g_free (ontologies_dir);
diff --git a/src/libtracker-data/tracker-db-manager.c b/src/libtracker-data/tracker-db-manager.c
index f0961a7..f8f4482 100644
--- a/src/libtracker-data/tracker-db-manager.c
+++ b/src/libtracker-data/tracker-db-manager.c
@@ -542,11 +542,12 @@ db_set_locale (const gchar *locale)
 	g_free (filename);
 }
 
-void
-tracker_db_manager_ensure_locale (void)
+gboolean
+tracker_db_manager_locale_changed (void)
 {
 	gchar *db_locale;
 	gchar *current_locale;
+	gboolean changed;
 
 	/* Get current collation locale */
 	current_locale = setlocale (LC_COLLATE, NULL);
@@ -558,17 +559,29 @@ tracker_db_manager_ensure_locale (void)
 	 * both to NULL is actually valid, they would default to
 	 * the unicode collation without locale-specific stuff. */
 	if (g_strcmp0 (db_locale, current_locale) != 0) {
-		g_message ("Switching collation from locale '%s' to locale '%s'...",
+		g_message ("Locale change detected from '%s' to '%s'...",
 		           db_locale, current_locale);
-
-		/* TODO here */
-
-		db_set_locale (current_locale);
+		changed = TRUE;
 	} else {
 		g_message ("Current and DB locales match: '%s'", db_locale);
+		changed = FALSE;
 	}
 
 	g_free (db_locale);
+	return changed;
+}
+
+void
+tracker_db_manager_set_current_locale (void)
+{
+	const gchar *current_locale;
+
+	/* Get current collation locale */
+	current_locale = setlocale (LC_COLLATE, NULL);
+
+	g_message ("Changing db locale to: '%s'", current_locale);
+
+	db_set_locale (current_locale);
 }
 
 static void
diff --git a/src/libtracker-data/tracker-db-manager.h b/src/libtracker-data/tracker-db-manager.h
index ea24ff1..7c041e8 100644
--- a/src/libtracker-data/tracker-db-manager.h
+++ b/src/libtracker-data/tracker-db-manager.h
@@ -70,7 +70,8 @@ guint64             tracker_db_manager_get_last_crawl_done    (void);
 void                tracker_db_manager_set_first_index_done   (gboolean done);
 void                tracker_db_manager_set_last_crawl_done    (gboolean done);
 
-void                tracker_db_manager_ensure_locale          (void);
+gboolean            tracker_db_manager_locale_changed         (void);
+void                tracker_db_manager_set_current_locale     (void);
 
 G_END_DECLS
 



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