tracker r2167 - in branches/indexer-split: . data src/libtracker-common src/tracker-indexer



Author: ifrade
Date: Wed Aug 27 12:27:22 2008
New Revision: 2167
URL: http://svn.gnome.org/viewvc/tracker?rev=2167&view=rev

Log:
Update of metadata when the file is updated in the indexer

Modified:
   branches/indexer-split/ChangeLog
   branches/indexer-split/data/sqlite-stored-procs.sql
   branches/indexer-split/src/libtracker-common/tracker-ontology.c
   branches/indexer-split/src/libtracker-common/tracker-ontology.h
   branches/indexer-split/src/tracker-indexer/tracker-indexer-db.c
   branches/indexer-split/src/tracker-indexer/tracker-indexer-db.h
   branches/indexer-split/src/tracker-indexer/tracker-indexer.c

Modified: branches/indexer-split/data/sqlite-stored-procs.sql
==============================================================================
--- branches/indexer-split/data/sqlite-stored-procs.sql	(original)
+++ branches/indexer-split/data/sqlite-stored-procs.sql	Wed Aug 27 12:27:22 2008
@@ -105,6 +105,10 @@
 GetMetadata SELECT MetaDataDisplay FROM ServiceMetaData WHERE ServiceID = ? AND MetaDataID = ?;
 GetMetadataNumeric SELECT MetaDataValue FROM ServiceNumericMetaData WHERE ServiceID = ? AND MetaDataID = ?;
 
+GetMetadataIDValueKeyword SELECT MetadataID, MetadataValue FROM ServiceKeywordMetadata WHERE ServiceID = ? ORDER BY MetadataID
+GetMetadataIDValue SELECT MetadataID, MetadataValue FROM ServiceMetadata WHERE ServiceID = ? ORDER BY MetadataID
+GetMetadataIDValueNumeric SELECT MetadataID, MetadataValue FROM ServiceNumericMetadata WHERE ServiceID = ? ORDER BY MetadataID
+
 SetMetadataKeyword INSERT INTO ServiceKeywordMetaData (ServiceID, MetaDataID, MetaDataValue) VALUES (?,?,?);
 SetMetadata INSERT INTO ServiceMetaData (ServiceID, MetaDataID, MetaDataValue, MetaDataDisplay) VALUES (?,?,?,?);
 SetMetadataNumeric INSERT INTO ServiceNumericMetaData (ServiceID, MetaDataID, MetaDataValue) VALUES (?,?,?);

Modified: branches/indexer-split/src/libtracker-common/tracker-ontology.c
==============================================================================
--- branches/indexer-split/src/libtracker-common/tracker-ontology.c	(original)
+++ branches/indexer-split/src/libtracker-common/tracker-ontology.c	Wed Aug 27 12:27:22 2008
@@ -696,6 +696,27 @@
 	return ontology_hash_lookup_by_str (metadata_table, name);
 }
 
+TrackerField *  
+tracker_ontology_get_field_def_by_id (gint id)
+{
+	GList *values = NULL, *iter;
+
+	/* TODO Create a hashtable with id -> field def. More efficient */
+
+	values = g_hash_table_get_values (metadata_table);
+	
+	for (iter = values; iter != NULL; iter = iter->next) {
+		TrackerField *def;
+
+		def = (TrackerField *) iter->data;
+		if (atoi(tracker_field_get_id (def)) == id) {
+			return def;
+		}
+	}
+
+	return NULL;
+}
+
 const gchar *
 tracker_ontology_get_field_id (const gchar *name)
 {

Modified: branches/indexer-split/src/libtracker-common/tracker-ontology.h
==============================================================================
--- branches/indexer-split/src/libtracker-common/tracker-ontology.h	(original)
+++ branches/indexer-split/src/libtracker-common/tracker-ontology.h	Wed Aug 27 12:27:22 2008
@@ -71,6 +71,7 @@
 								  const gchar    *parent);
 TrackerField *     tracker_ontology_get_field_def                (const gchar    *name);
 const gchar *      tracker_ontology_get_field_id                 (const gchar    *name);
+TrackerField *  tracker_ontology_get_field_def_by_id          (gint            id);
 
 G_END_DECLS
 

Modified: branches/indexer-split/src/tracker-indexer/tracker-indexer-db.c
==============================================================================
--- branches/indexer-split/src/tracker-indexer/tracker-indexer-db.c	(original)
+++ branches/indexer-split/src/tracker-indexer/tracker-indexer-db.c	Wed Aug 27 12:27:22 2008
@@ -317,6 +317,101 @@
 	return g_string_free (result, FALSE);
 }
 
+static void
+result_set_to_metadata (TrackerDBResultSet *result_set,
+			TrackerMetadata    *metadata,
+			gboolean            numeric,
+			gboolean            only_embedded)
+{
+	gboolean      valid = TRUE;
+	TrackerField *field_def;
+	gint          metadata_id;
+	gchar        *value;
+	gint          numeric_value;
+
+	while (valid) {
+		
+		if (numeric) {
+			tracker_db_result_set_get (result_set, 0, &metadata_id, 1, &numeric_value, -1);
+			value = g_strdup_printf ("%d", numeric_value);
+		} else {
+			tracker_db_result_set_get (result_set, 0, &metadata_id, 1, &value, -1);
+		}
+		
+		field_def = tracker_ontology_get_field_def_by_id (metadata_id);
+		if (!field_def) {
+			g_critical ("Field id %d in database but not in tracker-ontology",
+				    metadata_id);
+			g_free (value);
+			return;
+		}
+		
+		if (tracker_field_get_embedded (field_def) || !only_embedded) {
+
+			if (tracker_field_get_multiple_values (field_def)) {
+			
+				GList *new_values = NULL;
+				const GList *old_values = NULL; 
+				old_values = tracker_metadata_lookup_multiple_values (metadata,
+										      tracker_field_get_name (field_def));
+				if (old_values) {
+					new_values = g_list_copy ((GList *)old_values);
+				}
+				
+				new_values = g_list_prepend (new_values, g_strdup (value));
+				tracker_metadata_insert_multiple_values (metadata, 
+									 tracker_field_get_name (field_def),
+									 new_values);
+				
+			} else {
+				tracker_metadata_insert (metadata, tracker_field_get_name (field_def), g_strdup (value));
+			}
+		}
+
+		g_free (value);
+		valid = tracker_db_result_set_iter_next (result_set);
+	}
+}
+
+TrackerMetadata *
+tracker_db_get_all_metadata (TrackerService *service,
+			     guint32         service_id,
+			     gboolean        only_embedded)
+{
+	TrackerDBInterface *iface;
+	TrackerDBResultSet *result_set = NULL;
+	gchar              *service_id_str;
+	TrackerMetadata    *metadata;
+
+	metadata = tracker_metadata_new ();
+
+	service_id_str = g_strdup_printf ("%d", service_id);
+	iface = tracker_db_manager_get_db_interface_by_type (tracker_service_get_name (service),
+							     TRACKER_DB_CONTENT_TYPE_METADATA);
+
+	result_set = tracker_db_interface_execute_procedure (iface, NULL, "GetMetadataIDValue", service_id_str, NULL);
+	if (result_set) {
+	        result_set_to_metadata (result_set, metadata, FALSE, only_embedded);
+		g_object_unref (result_set);
+	}
+
+	result_set = tracker_db_interface_execute_procedure (iface, NULL, "GetMetadataIDValueKeyword", service_id_str, NULL);
+	if (result_set) {
+	        result_set_to_metadata (result_set, metadata, FALSE, only_embedded);
+		g_object_unref (result_set);
+	}
+
+	result_set = tracker_db_interface_execute_procedure (iface, NULL, "GetMetadataIDValueNumeric", service_id_str, NULL);
+	if (result_set) {
+	        result_set_to_metadata (result_set, metadata, TRUE, only_embedded);
+		g_object_unref (result_set);
+	}
+
+	g_free (service_id_str);
+
+	return metadata;
+}
+
 void
 tracker_db_delete_service (TrackerService *service,
 			   guint32         service_id)

Modified: branches/indexer-split/src/tracker-indexer/tracker-indexer-db.h
==============================================================================
--- branches/indexer-split/src/tracker-indexer/tracker-indexer-db.h	(original)
+++ branches/indexer-split/src/tracker-indexer/tracker-indexer-db.h	Wed Aug 27 12:27:22 2008
@@ -27,66 +27,74 @@
 #include "tracker-metadata.h"
 
 G_BEGIN_DECLS
-guint32  tracker_db_get_new_service_id    (TrackerDBInterface *iface);
-void     tracker_db_increment_stats       (TrackerDBInterface *iface,
-					   TrackerService     *service);
-void     tracker_db_decrement_stats       (TrackerDBInterface *iface,
-					   TrackerService     *service);
+guint32          tracker_db_get_new_service_id    (TrackerDBInterface *iface);
+void             tracker_db_increment_stats       (TrackerDBInterface *iface,
+						   TrackerService     *service);
+void             tracker_db_decrement_stats       (TrackerDBInterface *iface,
+						   TrackerService     *service);
+
 /* Using path */
-gboolean tracker_db_check_service         (TrackerService     *service,
-					   const gchar        *dirname,
-					   const gchar        *basename,
-					   guint32            *id,
-					   time_t             *mtime);
-guint    tracker_db_get_service_type      (const gchar        *dirname,
-					   const gchar        *basename);
+gboolean         tracker_db_check_service         (TrackerService     *service,
+						   const gchar        *dirname,
+						   const gchar        *basename,
+						   guint32            *id,
+						   time_t             *mtime);
+guint            tracker_db_get_service_type      (const gchar        *dirname,
+						   const gchar        *basename);
+
 
 /* Services  */
-gboolean tracker_db_create_service        (TrackerService     *service,
-					   guint32             id,
-					   const gchar        *dirname,
-					   const gchar        *basename,
-					   TrackerMetadata    *metadata);
-void     tracker_db_delete_service        (TrackerService     *service,
-					   guint32             id);
-
-void     tracker_db_move_service          (TrackerService     *service,
-					   const gchar        *from,
-					   const gchar        *to);
+gboolean         tracker_db_create_service        (TrackerService     *service,
+						   guint32             id,
+						   const gchar        *dirname,
+						   const gchar        *basename,
+						   TrackerMetadata    *metadata);
+void             tracker_db_delete_service        (TrackerService     *service,
+						   guint32             id);
+void             tracker_db_move_service          (TrackerService     *service,
+						   const gchar        *from,
+						   const gchar        *to);
+
 
 /* Metadata */
-void     tracker_db_set_metadata          (TrackerService     *service,
-					   guint32             id,
-					   TrackerField       *field,
-					   const gchar        *value,
-					   const gchar        *parsed_value);
-gchar   *tracker_db_get_parsed_metadata   (TrackerService     *service,
-					   guint32             id);
-gchar   *tracker_db_get_unparsed_metadata (TrackerService     *service,
-					   guint32             id);
-gchar  **tracker_db_get_property_values   (TrackerService     *service_def,
-					   guint32             id,
-					   TrackerField       *field_def);
-void     tracker_db_delete_all_metadata   (TrackerService     *service,
-					   guint32             id);
-void     tracker_db_delete_metadata       (TrackerService *service,
-					   guint32         id,
-					   TrackerField   *field,
-					   const gchar    *value);
+void             tracker_db_set_metadata          (TrackerService     *service,
+						   guint32             id,
+						   TrackerField       *field,
+						   const gchar        *value,
+						   const gchar        *parsed_value);
+TrackerMetadata *tracker_db_get_all_metadata      (TrackerService     *service,
+						   guint32             id,
+						   gboolean            only_embedded);
+gchar   *        tracker_db_get_parsed_metadata   (TrackerService     *service,
+						   guint32             id);
+gchar   *        tracker_db_get_unparsed_metadata (TrackerService     *service,
+						   guint32             id);
+gchar  **        tracker_db_get_property_values   (TrackerService     *service_def,
+						   guint32             id,
+						   TrackerField       *field_def);
+void             tracker_db_delete_all_metadata   (TrackerService     *service,
+						   guint32             id);
+void             tracker_db_delete_metadata       (TrackerService     *service,
+						   guint32             id,
+						   TrackerField       *field,
+						   const gchar        *value);
+
 /* Contents */
-void     tracker_db_set_text              (TrackerService     *service,
-					   guint32             id,
-					   const gchar        *text);
-gchar   *tracker_db_get_text              (TrackerService     *service,
-					   guint32             id);
-void     tracker_db_delete_text           (TrackerService     *service,
-					   guint32             id);
+void             tracker_db_set_text              (TrackerService     *service,
+						   guint32             id,
+						   const gchar        *text);
+gchar   *        tracker_db_get_text              (TrackerService     *service,
+						   guint32             id);
+void             tracker_db_delete_text           (TrackerService     *service,
+						   guint32             id);
+
 
 
 /* Events */
-void     tracker_db_create_event          (TrackerDBInterface *iface,
-					   guint32             service_id,
-					   const gchar        *type);
+void             tracker_db_create_event          (TrackerDBInterface *iface,
+						   guint32             service_id,
+						   const gchar        *type);
+
 
 
 

Modified: branches/indexer-split/src/tracker-indexer/tracker-indexer.c
==============================================================================
--- branches/indexer-split/src/tracker-indexer/tracker-indexer.c	(original)
+++ branches/indexer-split/src/tracker-indexer/tracker-indexer.c	Wed Aug 27 12:27:22 2008
@@ -148,6 +148,7 @@
 	TrackerLanguage *language;
 	TrackerConfig *config;
 	TrackerService *service;
+	gboolean add;
 	guint32 id;
 };
 
@@ -840,6 +841,7 @@
 	gchar **arr;
 	gint service_id;
 	gint i;
+	gint score;
 
 	parsed_value = tracker_parser_text_to_string (value,
 						      data->language,
@@ -853,6 +855,12 @@
 		return;
 	}
 
+	if (data->add) {
+		score = -1*tracker_field_get_weight (field);
+	} else {
+		score = tracker_field_get_weight (field);
+	}
+
 	arr = g_strsplit (parsed_value, " ", -1);
 	service_id = tracker_service_get_id (data->service);
 	index = tracker_db_index_manager_get_index_by_service_id (service_id);
@@ -861,11 +869,15 @@
 		tracker_db_index_add_word (index,
 					   arr[i],
 					   data->id,
-					   service_id,
-					   tracker_field_get_weight (field));
+					   tracker_service_get_id (data->service),
+					   score);
 	}
 
-	tracker_db_set_metadata (data->service, data->id, field, (gchar *) value, parsed_value);
+	if (data->add) {
+		tracker_db_set_metadata (data->service, data->id, field, (gchar *) value, parsed_value);
+	} else {
+		tracker_db_delete_metadata (data->service, data->id, field, (gchar *)value);
+	}
 
 	g_free (parsed_value);
 	g_strfreev (arr);
@@ -917,6 +929,7 @@
 	data.config = indexer->private->config;
 	data.service = service;
 	data.id = id;
+	data.add = TRUE;
 
 	tracker_metadata_foreach (metadata, index_metadata_foreach, &data);
 
@@ -924,6 +937,26 @@
 }
 
 static void
+unindex_metadata (TrackerIndexer  *indexer,
+		  guint32          id,
+		  TrackerService  *service,
+		  TrackerMetadata *metadata)
+{
+	MetadataForeachData data;
+
+	data.language = indexer->private->language;
+	data.config = indexer->private->config;
+	data.service = service;
+	data.id = id;
+	data.add = FALSE;
+
+	tracker_metadata_foreach (metadata, index_metadata_foreach, &data);
+
+	schedule_flush (indexer, FALSE);
+}
+
+
+static void
 send_text_to_index (TrackerIndexer *indexer,
 		    gint            service_id,
 		    gint            service_type,
@@ -1166,13 +1199,18 @@
 			g_free (text);
 		}
 	} else {
-		gchar *old_text;
-		gchar *new_text;
+		gchar           *old_text = NULL,  *new_text = NULL;
+		TrackerMetadata *old_metadata = NULL;
+		/* Update case */
+		g_debug ("Updating file '%s'", info->file->path);
 
-		/* TODO: Take the old metadata -> the new one,
-		 * calculate difference and add the words.
+		/* 
+		 * Using DB directly: get old (embedded) metadata, unindex, index the new metadata
 		 */
-		
+		old_metadata = tracker_db_get_all_metadata (service_def, id, TRUE);
+		unindex_metadata (indexer, id, service_def, old_metadata);
+		index_metadata (indexer, id, service_def, metadata);
+
 		/* Take the old text -> the new one, calculate
 		 * difference and add the words.
 		 */
@@ -1180,8 +1218,8 @@
 		new_text = tracker_indexer_module_file_get_text (info->module, info->file);
 		
 		if (old_text || new_text) {
-			GHashTable *old_words;
-			GHashTable *new_words;
+			GHashTable *old_words = NULL;
+			GHashTable *new_words = NULL;
 
 			/* Service has/had full text */
 			old_words = tracker_parser_text (NULL, 
@@ -1215,6 +1253,16 @@
 						 tracker_service_get_id (service_def), 
 						 new_words);
 
+			/* Remove old text and set new one in the db */
+			if (old_text) {
+				tracker_db_delete_text (service_def, id);
+				g_free (old_text);
+			}
+
+			if (new_text) {
+				tracker_db_set_text (service_def, id, new_text);
+				g_free (new_text);
+			}
 			g_hash_table_unref (old_words);
 			g_hash_table_unref (new_words);
 		}



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