[tracker/wip/fts4: 15/21] squash me, some changes that might be better put together with the initial changeset



commit febae68d6aba78ffe119395cf279acf4530c19a6
Author: Carlos Garnacho <carlos lanedo com>
Date:   Tue Jan 22 11:28:09 2013 +0100

    squash me, some changes that might be better put together with the initial changeset

 src/libtracker-data/tracker-data-manager.c        |   59 ++++++++------------
 src/libtracker-data/tracker-db-interface-sqlite.c |   28 +++++++++-
 src/libtracker-fts/tracker-fts.c                  |   54 +++++++++++++++----
 src/libtracker-fts/tracker-fts.h                  |    2 +-
 4 files changed, 94 insertions(+), 49 deletions(-)
---
diff --git a/src/libtracker-data/tracker-data-manager.c b/src/libtracker-data/tracker-data-manager.c
index 1a9824e..2e29e79 100644
--- a/src/libtracker-data/tracker-data-manager.c
+++ b/src/libtracker-data/tracker-data-manager.c
@@ -3583,50 +3583,39 @@ load_ontologies_gvdb (GError **error)
 }
 
 #if HAVE_TRACKER_FTS
-static gint
-compare_fts_property_ids (gconstpointer a,
-                          gconstpointer b)
-{
-	TrackerProperty *pa, *pb;
-
-	pa = (TrackerProperty *) a;
-	pb = (TrackerProperty *) b;
-
-	return tracker_property_get_id (pa) - tracker_property_get_id (pb);
-}
-
-static const gchar **
+static GHashTable *
 ontology_get_fts_properties (gboolean only_new)
 {
 	TrackerProperty **properties;
-	GList *fts_props = NULL, *l;
-	const gchar **prop_names;
+	GHashTable *hashtable;
 	guint i, len;
 
 	properties = tracker_ontologies_get_properties (&len);
+	hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
+					   (GDestroyNotify) g_list_free);
 
 	for (i = 0; i < len; i++) {
-		if (only_new && !tracker_property_get_is_new (properties[i])) {
-			continue;
-		}
+		const gchar *name, *table_name;
+		GList *list;
 
-		if (tracker_property_get_fulltext_indexed (properties[i])) {
-			/* Sort them by ID */
-			fts_props =
-				g_list_insert_sorted (fts_props, properties[i],
-						      (GCompareFunc) compare_fts_property_ids);
+		if (!tracker_property_get_fulltext_indexed (properties[i]) ||
+		    (only_new && !tracker_property_get_is_new (properties[i]))) {
+			continue;
 		}
-	}
 
-	prop_names = g_new0 (const gchar *, g_list_length (fts_props) + 1);
+		table_name = tracker_property_get_table_name (properties[i]);
+		name = tracker_property_get_name (properties[i]);
+		list = g_hash_table_lookup (hashtable, table_name);
 
-	for (l = fts_props, i = 0; l; l = l->next, i++) {
-		prop_names[i] = tracker_property_get_name (l->data);
+		if (!list) {
+			list = g_list_prepend (NULL, (gpointer) name);
+			g_hash_table_insert (hashtable, (gpointer) table_name, list);
+		} else {
+			list = g_list_append (list, (gpointer) name);
+		}
 	}
 
-	g_list_free (fts_props);
-
-	return prop_names;
+	return hashtable;
 }
 #endif
 
@@ -3808,7 +3797,7 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
 
 	if (is_first_time_index && !read_only) {
 #if HAVE_TRACKER_FTS
-		const gchar **fts_props;
+		GHashTable *fts_props;
 #endif
 
 		sorted = get_ontologies (test_schemas != NULL, ontologies_dir);
@@ -3899,15 +3888,15 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
 			return FALSE;
 		}
 
+		tracker_data_ontology_import_into_db (FALSE,
+		                                      &internal_error);
+
 #if HAVE_TRACKER_FTS
 		fts_props = ontology_get_fts_properties (FALSE);
 		tracker_db_interface_sqlite_fts_init (iface, fts_props, TRUE);
-		g_free (fts_props);
+		g_hash_table_destroy (fts_props);
 #endif
 
-		tracker_data_ontology_import_into_db (FALSE,
-		                                      &internal_error);
-
 		if (internal_error) {
 			g_propagate_error (error, internal_error);
 
diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c b/src/libtracker-data/tracker-db-interface-sqlite.c
index ed27b55..16a4202 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.c
+++ b/src/libtracker-data/tracker-db-interface-sqlite.c
@@ -1029,9 +1029,29 @@ tracker_db_interface_sqlite_fts_update_text (TrackerDBInterface  *db_interface,
 	GString *query;
 	gint i;
 
-	if (create) {
-		query = g_string_new ("INSERT INTO fts (docid");
+	if (!create) {
+		stmt = tracker_db_interface_create_statement (db_interface,
+							      TRACKER_DB_STATEMENT_CACHE_TYPE_UPDATE,
+							      &error,
+							      "DELETE FROM fts WHERE docid=?");
+		tracker_db_statement_bind_int (stmt, 0, id);
+
+		tracker_db_statement_execute (stmt, &error);
+		g_object_unref (stmt);
+
+		if (error) {
+			g_warning ("Could not update FTS text: %s", error->message);
+			g_error_free (error);
+			return FALSE;
+		}
+	}
+
+//	if (create)
+	{
+		query = g_string_new ("INSERT INTO fts (docid, \"nfo:fileName\", \"nco:title\", \"nfo:genre\", \"nmm:genre\", \"mtp:creator\", \"nco:nameFamily\", \"nco:nameGiven\", \"nco:nameAdditional\", \"nco:hobby\", \"nco:phoneNumber\", \"nao:prefLabel\", \"nao:description\", \"nco:fullname\", \"nco:nickname\", \"nco:contactGroupName\", \"nfo:tableOfContents\", \"ncal:comment\", \"ncal:location\", \"ncal:summary\", \"ncal:contact\", \"ncal:description\", \"nmo:messageSubject\", \"nmm:albumTitle\", \"nmm:artistName\", \"nco:department\", \"nco:role\", \"nco:note\", \"nie:keyword\", \"nco:imID\", \"nco:imNickname\", \"nco:emailAddress\", \"nmm:category\", \"nfo:fontFamily\", \"nco:region\", \"nco:country\", \"nco:extendedAddress\", \"nco:streetAddress\", \"nco:postalcode\", \"nco:locality\", \"nco:county\", \"nco:district\", \"nco:pobox\", \"nie:title\", \"nie:subject\", \"nie:plainTextContent\", \"nie:description\", \"nie:comment\", \"nid3:title\", \"nid3:albumTitle\", \"nid3:contentT
 ype\")"
+				      " SELECT rowid, \"nfo:fileName\", \"nco:title\", \"nfo:genre\", \"nmm:genre\", \"mtp:creator\", \"nco:nameFamily\", \"nco:nameGiven\", \"nco:nameAdditional\", \"nco:hobby\", \"nco:phoneNumber\", \"nao:prefLabel\", \"nao:description\", \"nco:fullname\", \"nco:nickname\", \"nco:contactGroupName\", \"nfo:tableOfContents\", \"ncal:comment\", \"ncal:location\", \"ncal:summary\", \"ncal:contact\", \"ncal:description\", \"nmo:messageSubject\", \"nmm:albumTitle\", \"nmm:artistName\", \"nco:department\", \"nco:role\", \"nco:note\", \"nie:keyword\", \"nco:imID\", \"nco:imNickname\", \"nco:emailAddress\", \"nmm:category\", \"nfo:fontFamily\", \"nco:region\", \"nco:country\", \"nco:extendedAddress\", \"nco:streetAddress\", \"nco:postalcode\", \"nco:locality\", \"nco:county\", \"nco:district\", \"nco:pobox\", \"nie:title\", \"nie:subject\", \"nie:plainTextContent\", \"nie:description\", \"nie:comment\", \"nid3:title\", \"nid3:albumTitle\", \"nid3:contentType\" FROM fts_view WHE
 RE rowid=?");
 
+#if 0
 		for (i = 0; properties[i] != NULL; i++) {
 			g_string_append_printf (query, ", \"%s\"", properties[i]);
 		}
@@ -1054,6 +1074,7 @@ tracker_db_interface_sqlite_fts_update_text (TrackerDBInterface  *db_interface,
 		}
 
 		g_string_append (query, " WHERE docid = ?");
+#endif
 	}
 
 	stmt = tracker_db_interface_create_statement (db_interface,
@@ -1070,6 +1091,8 @@ tracker_db_interface_sqlite_fts_update_text (TrackerDBInterface  *db_interface,
 		return FALSE;
 	}
 
+	tracker_db_statement_bind_int (stmt, 0, id);
+#if 0
 	if (create) {
 		tracker_db_statement_bind_int (stmt, 0, id);
 
@@ -1083,6 +1106,7 @@ tracker_db_interface_sqlite_fts_update_text (TrackerDBInterface  *db_interface,
 
 		tracker_db_statement_bind_int (stmt, i, id);
 	}
+#endif
 
 	tracker_db_statement_execute (stmt, &error);
 	g_object_unref (stmt);
diff --git a/src/libtracker-fts/tracker-fts.c b/src/libtracker-fts/tracker-fts.c
index ed45b8e..ac96155 100644
--- a/src/libtracker-fts/tracker-fts.c
+++ b/src/libtracker-fts/tracker-fts.c
@@ -227,25 +227,57 @@ tracker_fts_init_db (sqlite3 *db) {
 }
 
 gboolean
-tracker_fts_create_table (sqlite3  *db,
-			  gchar    *table_name,
-			  gchar   **column_names)
+tracker_fts_create_table (sqlite3    *db,
+			  gchar      *table_name,
+			  GHashTable *tables)
 {
-	GString *str;
-	gint i, rc;
-
-	str = g_string_new ("CREATE VIRTUAL TABLE ");
-	g_string_append_printf (str, "%s USING fts4(", table_name);
+	GString *str, *from, *fts;
+	GHashTableIter iter;
+	gchar *index_table;
+	GList *columns;
+	gint rc;
+
+	/* Create view on tables/columns marked as FTS-indexed */
+	g_hash_table_iter_init (&iter, tables);
+	str = g_string_new ("CREATE VIEW fts_view AS SELECT Resource.ID as rowid ");
+	from = g_string_new ("FROM Resource ");
+
+	fts = g_string_new ("CREATE VIRTUAL TABLE ");
+	g_string_append_printf (fts, "%s USING fts4(content=\"fts_view\", ",
+				table_name);
+
+	while (g_hash_table_iter_next (&iter, (gpointer *) &index_table,
+				       (gpointer *) &columns)) {
+		while (columns) {
+			g_string_append_printf (str, ",\"%s\".\"%s\" AS \"%s\" ",
+						index_table,
+						(gchar *) columns->data,
+						(gchar *) columns->data);
+			g_string_append_printf (fts, "\"%s\", ",
+						(gchar *) columns->data);
+
+			columns = columns->next;
+		}
 
-	for (i = 0; column_names[i]; i++) {
-		g_string_append_printf (str, "\"%s\", ", column_names[i]);
+		g_string_append_printf (from, "LEFT OUTER JOIN \"%s\" ON "
+					" Resource.ID = \"%s\".ID ",
+					index_table, index_table);
 	}
 
-	g_string_append (str, " tokenize=TrackerTokenizer)");
+	g_string_append (str, from->str);
+	g_string_free (from, TRUE);
 
 	rc = sqlite3_exec(db, str->str, NULL, 0, NULL);
 	g_string_free (str, TRUE);
 
+	if (rc != SQLITE_OK) {
+		return FALSE;
+	}
+
+	g_string_append (fts, "tokenize=TrackerTokenizer)");
+	rc = sqlite3_exec(db, fts->str, NULL, 0, NULL);
+	g_string_free (fts, TRUE);
+
 	return (rc == SQLITE_OK);
 }
 
diff --git a/src/libtracker-fts/tracker-fts.h b/src/libtracker-fts/tracker-fts.h
index 487a6ff..d2ab90f 100644
--- a/src/libtracker-fts/tracker-fts.h
+++ b/src/libtracker-fts/tracker-fts.h
@@ -31,7 +31,7 @@ gboolean    tracker_fts_init             (void);
 gboolean    tracker_fts_init_db          (sqlite3    *db);
 gboolean    tracker_fts_create_table     (sqlite3    *db,
 					  gchar      *table_name,
-					  gchar     **column_names);
+					  GHashTable *tables);
 gboolean    tracker_fts_alter_table      (sqlite3    *db,
 					  gchar      *table_name,
 					  gchar     **added_columns,



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