[tracker/avoid-iri-cache-miss: 1/2] libtracker-miner: Try to fix new nfo:FileDataObjects without nfo:belongsToContainer



commit 2378881e3b94e45193cf0558b562bb667ad89cff
Author: Aleksander Morgado <aleksander lanedo com>
Date:   Mon Sep 6 14:48:54 2010 +0200

    libtracker-miner: Try to fix new nfo:FileDataObjects without nfo:belongsToContainer

 src/libtracker-miner/tracker-miner-fs.c |   87 ++++++++++++++++++-------------
 1 files changed, 51 insertions(+), 36 deletions(-)
---
diff --git a/src/libtracker-miner/tracker-miner-fs.c b/src/libtracker-miner/tracker-miner-fs.c
index 7dc211d..8d9cd68 100644
--- a/src/libtracker-miner/tracker-miner-fs.c
+++ b/src/libtracker-miner/tracker-miner-fs.c
@@ -112,7 +112,7 @@ struct _TrackerMinerFSPrivate {
 	GHashTable     *items_ignore_next_update;
 
 	GQuark          quark_ignore_file;
-	GQuark          quark_force_cache_regeneration;
+	GQuark          quark_on_file_created;
 
 	GList          *config_directories;
 
@@ -560,8 +560,8 @@ tracker_miner_fs_init (TrackerMinerFS *object)
 	                  object);
 
 	priv->quark_ignore_file = g_quark_from_static_string ("tracker-ignore-file");
-	priv->quark_force_cache_regeneration =
-		g_quark_from_static_string ("tracker-force-cache-regeneration");
+	priv->quark_on_file_created =
+		g_quark_from_static_string ("tracker-on-file-created");
 
 	priv->iri_cache = g_hash_table_new_full (g_file_hash,
 	                                         (GEqualFunc) g_file_equal,
@@ -1367,35 +1367,46 @@ ensure_iri_cache (TrackerMinerFS *fs,
 
 static const gchar *
 iri_cache_lookup (TrackerMinerFS *fs,
-                  GFile          *file)
-{
-	gpointer value;
-	const gchar *iri;
-
-	if (!g_hash_table_lookup_extended (fs->private->iri_cache, file, NULL, &value)) {
-		/* Item doesn't exist in cache */
+                  GFile          *file,
+                  gboolean        force_direct_iri_query)
+{
+	gpointer in_cache_value;
+	gboolean in_cache;
+	gchar *query_iri;
+
+	/* Look for item in IRI cache */
+	in_cache = g_hash_table_lookup_extended (fs->private->iri_cache,
+	                                         file,
+	                                         NULL,
+	                                         &in_cache_value);
+
+	/* Item found with a proper value. If value is NULL, we need
+	 * to do a direct IRI query as it was a cache miss (item was added
+	 * after the last iri cache update) */
+	if (in_cache && in_cache_value)
+		return (const gchar *) in_cache_value;
+
+	/* Item doesn't exist in cache. If we don't need to force iri query,
+	 * just return. */
+	if (!in_cache && !force_direct_iri_query)
 		return NULL;
-	}
 
-	iri = value;
-
-	if (!iri) {
-		gchar *query_iri;
-
-		/* Cache miss, this item was added after the last
-		 * iri cache update, so query it independently
-		 */
-		if (item_query_exists (fs, file, &query_iri, NULL)) {
-			g_hash_table_insert (fs->private->iri_cache,
-			                     g_object_ref (file), query_iri);
-			iri = query_iri;
-		} else {
-			g_hash_table_remove (fs->private->iri_cache, file);
-			iri = NULL;
-		}
+	/* Independent direct IRI query */
+	if (item_query_exists (fs, file, &query_iri, NULL)) {
+		/* Replace! as we may already have an item in the cache with
+		 * NULL value! */
+		g_hash_table_replace (fs->private->iri_cache,
+		                      g_object_ref (file),
+		                      query_iri);
+		/* Set iri to return */
+		return query_iri;
 	}
 
-	return iri;
+	/* Not in store, remove item from cache if any */
+	if (in_cache)
+		g_hash_table_remove (fs->private->iri_cache, file);
+
+	return NULL;
 }
 
 static void
@@ -1530,6 +1541,7 @@ item_add_or_update (TrackerMinerFS *fs,
 	GFile *parent;
 	const gchar *urn;
 	const gchar *parent_urn = NULL;
+	gboolean just_created;
 
 	priv = fs->private;
 	retval = TRUE;
@@ -1538,11 +1550,15 @@ item_add_or_update (TrackerMinerFS *fs,
 	sparql = tracker_sparql_builder_new_update ();
 	g_object_ref (file);
 
+	/* Was the item just created? */
+	just_created = (g_object_steal_qdata (G_OBJECT (file),
+	                                      fs->private->quark_on_file_created) != NULL ?
+	                TRUE : FALSE);
+
 	parent = g_file_get_parent (file);
 
 	if (parent) {
-		if (g_object_steal_qdata (G_OBJECT (file), fs->private->quark_force_cache_regeneration) ||
-		    !fs->private->current_iri_cache_parent ||
+		if (!fs->private->current_iri_cache_parent ||
 		    !g_file_equal (parent, fs->private->current_iri_cache_parent)) {
 			/* Cache the URN for the new current parent, processing
 			 * order guarantees that all contents for a folder are
@@ -1571,7 +1587,11 @@ item_add_or_update (TrackerMinerFS *fs,
 		g_object_unref (parent);
 	}
 
-	urn = iri_cache_lookup (fs, file);
+	/* If the item was just created, force a direct URN query if not found
+	 * in the cache. This is to handle situations where an application inserted
+	 * items in the store after we updated the cache, or without a proper
+	 * nfo:belongsToContainer */
+	urn = iri_cache_lookup (fs, file, just_created);
 
 	data = process_data_new (TRACKER_MINER (fs), file, urn, parent_urn, cancellable, sparql);
 	priv->processing_pool = g_list_prepend (priv->processing_pool, data);
@@ -2783,13 +2803,8 @@ monitor_item_created_cb (TrackerMonitor *monitor,
 		    should_recurse_for_directory (fs, file)) {
 			tracker_miner_fs_directory_add_internal (fs, file);
 		} else {
-			/* On new item events, force a cache regeneration.
-			 * This is done to avoid issues when other applications
-			 * insert resources in the store, then we get the created
-			 * events, and we assume the previous cache was still
-			 * valid. */
 			g_object_set_qdata (G_OBJECT (file),
-			                    fs->private->quark_force_cache_regeneration,
+			                    fs->private->quark_on_file_created,
 			                    GINT_TO_POINTER (TRUE));
 
 			g_queue_push_tail (fs->private->items_created,



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