[tracker/tags: 2/2] tracker-extract: Do not create duplicate tags



commit c5e31d8355a19aa5cdfe6c21ef0e6b69783a92d8
Author: Jürg Billeter <j bitron ch>
Date:   Thu Mar 24 12:49:44 2011 +0100

    tracker-extract: Do not create duplicate tags
    
    This appends the WHERE pattern as string to the SPARQL fragment
    returned by tracker-extract to avoid changes in the D-Bus API.
    
    Fixes NB#237176.

 src/libtracker-extract/tracker-xmp.c       |   39 +++++++++++++++++++++-----
 src/tracker-extract/tracker-extract-gif.c  |   39 +++++++++++++++++++++-----
 src/tracker-extract/tracker-extract-jpeg.c |   39 +++++++++++++++++++++-----
 src/tracker-extract/tracker-extract-pdf.c  |   41 +++++++++++++++++++++------
 src/tracker-extract/tracker-extract-png.c  |   39 +++++++++++++++++++++-----
 src/tracker-extract/tracker-extract-tiff.c |   39 +++++++++++++++++++++-----
 6 files changed, 187 insertions(+), 49 deletions(-)
---
diff --git a/src/libtracker-extract/tracker-xmp.c b/src/libtracker-extract/tracker-xmp.c
index 7be0a2d..ce364ca 100644
--- a/src/libtracker-extract/tracker-xmp.c
+++ b/src/libtracker-extract/tracker-xmp.c
@@ -662,6 +662,7 @@ tracker_xmp_apply (TrackerSparqlBuilder *preupdate,
 {
 	GPtrArray *keywords;
 	guint i;
+	GString *where = NULL;
 
 	g_return_val_if_fail (TRACKER_SPARQL_IS_BUILDER (metadata), FALSE);
 	g_return_val_if_fail (uri != NULL, FALSE);
@@ -682,18 +683,35 @@ tracker_xmp_apply (TrackerSparqlBuilder *preupdate,
 	}
 
 	for (i = 0; i < keywords->len; i++) {
-		gchar *p;
+		gchar *p, *escaped, *var;
 
 		p = g_ptr_array_index (keywords, i);
-
+		escaped = tracker_sparql_escape_string (p);
+		var = g_strdup_printf ("tag%d", i + 1);
+
+		/* ensure tag with specified label exists */
+		tracker_sparql_builder_append (preupdate,
+		                               "INSERT { _:tag a nao:Tag ; nao:prefLabel \"");
+		tracker_sparql_builder_append (preupdate, escaped);
+		tracker_sparql_builder_append (preupdate,
+		                               "\" }\nWHERE { FILTER (NOT EXISTS { "
+		                               "?tag a nao:Tag ; nao:prefLabel \"");
+		tracker_sparql_builder_append (preupdate, escaped);
+		tracker_sparql_builder_append (preupdate,
+		                               "\" }) }\n");
+
+		/* associate file with tag */
 		tracker_sparql_builder_predicate (metadata, "nao:hasTag");
+		tracker_sparql_builder_object_variable (metadata, var);
 
-		tracker_sparql_builder_object_blank_open (metadata);
-		tracker_sparql_builder_predicate (metadata, "a");
-		tracker_sparql_builder_object (metadata, "nao:Tag");
-		tracker_sparql_builder_predicate (metadata, "nao:prefLabel");
-		tracker_sparql_builder_object_unvalidated (metadata, p);
-		tracker_sparql_builder_object_blank_close (metadata);
+		if (where == NULL) {
+			where = g_string_new ("} } WHERE { {\n");
+		}
+
+		g_string_append_printf (where, "?%s a nao:Tag ; nao:prefLabel \"%s\" .\n", var, escaped);
+
+		g_free (var);
+		g_free (escaped);
 		g_free (p);
 	}
 	g_ptr_array_free (keywords, TRUE);
@@ -919,5 +937,10 @@ tracker_xmp_apply (TrackerSparqlBuilder *preupdate,
 
 	}
 
+	if (where != NULL) {
+		tracker_sparql_builder_append (metadata, where->str);
+		g_string_free (where, TRUE);
+	}
+
 	return TRUE;
 }
diff --git a/src/tracker-extract/tracker-extract-gif.c b/src/tracker-extract/tracker-extract-gif.c
index 58cb598..6671cfe 100644
--- a/src/tracker-extract/tracker-extract-gif.c
+++ b/src/tracker-extract/tracker-extract-gif.c
@@ -84,6 +84,7 @@ read_metadata (TrackerSparqlBuilder *preupdate,
 	MergeData md = { 0 };
 	GifData   gd = { 0 };
 	TrackerXmpData *xd = NULL;
+	GString *where = NULL;
 
 	do {
 		GifByteType *ExtData;
@@ -341,18 +342,35 @@ read_metadata (TrackerSparqlBuilder *preupdate,
 	}
 
 	for (i = 0; i < keywords->len; i++) {
-		gchar *p;
+		gchar *p, *escaped, *var;
 
 		p = g_ptr_array_index (keywords, i);
-
+		escaped = tracker_sparql_escape_string (p);
+		var = g_strdup_printf ("tag%d", i + 1);
+
+		/* ensure tag with specified label exists */
+		tracker_sparql_builder_append (preupdate,
+		                               "INSERT { _:tag a nao:Tag ; nao:prefLabel \"");
+		tracker_sparql_builder_append (preupdate, escaped);
+		tracker_sparql_builder_append (preupdate,
+		                               "\" }\nWHERE { FILTER (NOT EXISTS { "
+		                               "?tag a nao:Tag ; nao:prefLabel \"");
+		tracker_sparql_builder_append (preupdate, escaped);
+		tracker_sparql_builder_append (preupdate,
+		                               "\" }) }\n");
+
+		/* associate file with tag */
 		tracker_sparql_builder_predicate (metadata, "nao:hasTag");
+		tracker_sparql_builder_object_variable (metadata, var);
 
-		tracker_sparql_builder_object_blank_open (metadata);
-		tracker_sparql_builder_predicate (metadata, "a");
-		tracker_sparql_builder_object (metadata, "nao:Tag");
-		tracker_sparql_builder_predicate (metadata, "nao:prefLabel");
-		tracker_sparql_builder_object_unvalidated (metadata, p);
-		tracker_sparql_builder_object_blank_close (metadata);
+		if (where == NULL) {
+			where = g_string_new ("} } WHERE { {\n");
+		}
+
+		g_string_append_printf (where, "?%s a nao:Tag ; nao:prefLabel \"%s\" .\n", var, escaped);
+
+		g_free (var);
+		g_free (escaped);
 		g_free (p);
 	}
 	g_ptr_array_free (keywords, TRUE);
@@ -474,6 +492,11 @@ read_metadata (TrackerSparqlBuilder *preupdate,
 		g_free (gd.comment);
 	}
 
+	if (where != NULL) {
+		tracker_sparql_builder_append (metadata, where->str);
+		g_string_free (where, TRUE);
+	}
+
 	tracker_xmp_free (xd);
 }
 
diff --git a/src/tracker-extract/tracker-extract-jpeg.c b/src/tracker-extract/tracker-extract-jpeg.c
index b6fd791..589c438 100644
--- a/src/tracker-extract/tracker-extract-jpeg.c
+++ b/src/tracker-extract/tracker-extract-jpeg.c
@@ -110,6 +110,7 @@ tracker_extract_get_metadata (const gchar          *uri,
 	GPtrArray *keywords;
 	gboolean success = TRUE;
 	guint i;
+	GString *where = NULL;
 
 	filename = g_filename_from_uri (uri, NULL, NULL);
 
@@ -355,18 +356,35 @@ tracker_extract_get_metadata (const gchar          *uri,
 	}
 
 	for (i = 0; i < keywords->len; i++) {
-		gchar *p;
+		gchar *p, *escaped, *var;
 
 		p = g_ptr_array_index (keywords, i);
-
+		escaped = tracker_sparql_escape_string (p);
+		var = g_strdup_printf ("tag%d", i + 1);
+
+		/* ensure tag with specified label exists */
+		tracker_sparql_builder_append (preupdate,
+		                               "INSERT { _:tag a nao:Tag ; nao:prefLabel \"");
+		tracker_sparql_builder_append (preupdate, escaped);
+		tracker_sparql_builder_append (preupdate,
+		                               "\" }\nWHERE { FILTER (NOT EXISTS { "
+		                               "?tag a nao:Tag ; nao:prefLabel \"");
+		tracker_sparql_builder_append (preupdate, escaped);
+		tracker_sparql_builder_append (preupdate,
+		                               "\" }) }\n");
+
+		/* associate file with tag */
 		tracker_sparql_builder_predicate (metadata, "nao:hasTag");
+		tracker_sparql_builder_object_variable (metadata, var);
 
-		tracker_sparql_builder_object_blank_open (metadata);
-		tracker_sparql_builder_predicate (metadata, "a");
-		tracker_sparql_builder_object (metadata, "nao:Tag");
-		tracker_sparql_builder_predicate (metadata, "nao:prefLabel");
-		tracker_sparql_builder_object_unvalidated (metadata, p);
-		tracker_sparql_builder_object_blank_close (metadata);
+		if (where == NULL) {
+			where = g_string_new ("} } WHERE { {\n");
+		}
+
+		g_string_append_printf (where, "?%s a nao:Tag ; nao:prefLabel \"%s\" .\n", var, escaped);
+
+		g_free (var);
+		g_free (escaped);
 		g_free (p);
 	}
 	g_ptr_array_free (keywords, TRUE);
@@ -615,6 +633,11 @@ tracker_extract_get_metadata (const gchar          *uri,
 		tracker_sparql_builder_object_double (metadata, value);
 	}
 
+	if (where != NULL) {
+		tracker_sparql_builder_append (metadata, where->str);
+		g_string_free (where, TRUE);
+	}
+
 	jpeg_destroy_decompress (&cinfo);
 
 	tracker_exif_free (ed);
diff --git a/src/tracker-extract/tracker-extract-pdf.c b/src/tracker-extract/tracker-extract-pdf.c
index fdb6c9a..57a216f 100644
--- a/src/tracker-extract/tracker-extract-pdf.c
+++ b/src/tracker-extract/tracker-extract-pdf.c
@@ -283,6 +283,7 @@ tracker_extract_get_metadata (const gchar          *uri,
 	guint n_bytes;
 	GPtrArray *keywords;
 	guint i;
+	GString *where = NULL;
 
 	g_type_init ();
 
@@ -561,18 +562,35 @@ tracker_extract_get_metadata (const gchar          *uri,
 	}
 
 	for (i = 0; i < keywords->len; i++) {
-		gchar *p;
+		gchar *p, *escaped, *var;
+
+		p = g_ptr_array_index (keywords, i);
+		escaped = tracker_sparql_escape_string (p);
+		var = g_strdup_printf ("tag%d", i + 1);
+
+		/* ensure tag with specified label exists */
+		tracker_sparql_builder_append (preupdate,
+		                               "INSERT { _:tag a nao:Tag ; nao:prefLabel \"");
+		tracker_sparql_builder_append (preupdate, escaped);
+		tracker_sparql_builder_append (preupdate,
+		                               "\" }\nWHERE { FILTER (NOT EXISTS { "
+		                               "?tag a nao:Tag ; nao:prefLabel \"");
+		tracker_sparql_builder_append (preupdate, escaped);
+		tracker_sparql_builder_append (preupdate,
+		                               "\" }) }\n");
+
+		/* associate file with tag */
+		tracker_sparql_builder_predicate (metadata, "nao:hasTag");
+		tracker_sparql_builder_object_variable (metadata, var);
 
-		p = (gchar *) g_ptr_array_index (keywords, i);
+		if (where == NULL) {
+			where = g_string_new ("} } WHERE { {\n");
+		}
 
-		tracker_sparql_builder_predicate (metadata, "nao:hasTag");
+		g_string_append_printf (where, "?%s a nao:Tag ; nao:prefLabel \"%s\" .\n", var, escaped);
 
-		tracker_sparql_builder_object_blank_open (metadata);
-		tracker_sparql_builder_predicate (metadata, "a");
-		tracker_sparql_builder_object (metadata, "nao:Tag");
-		tracker_sparql_builder_predicate (metadata, "nao:prefLabel");
-		tracker_sparql_builder_object_unvalidated (metadata, p);
-		tracker_sparql_builder_object_blank_close (metadata);
+		g_free (var);
+		g_free (escaped);
 		g_free (p);
 	}
 	g_ptr_array_free (keywords, TRUE);
@@ -592,6 +610,11 @@ tracker_extract_get_metadata (const gchar          *uri,
 
 	read_outline (document, metadata);
 
+	if (where != NULL) {
+		tracker_sparql_builder_append (metadata, where->str);
+		g_string_free (where, TRUE);
+	}
+
 	g_free (xml);
 	g_free (pd.keywords);
 	g_free (pd.title);
diff --git a/src/tracker-extract/tracker-extract-png.c b/src/tracker-extract/tracker-extract-png.c
index 57b8776..801afc8 100644
--- a/src/tracker-extract/tracker-extract-png.c
+++ b/src/tracker-extract/tracker-extract-png.c
@@ -97,6 +97,7 @@ read_metadata (TrackerSparqlBuilder *preupdate,
 	gint i;
 	gint found;
 	GPtrArray *keywords;
+	GString *where = NULL;
 
 	info_ptrs[0] = info_ptr;
 	info_ptrs[1] = end_ptr;
@@ -474,22 +475,44 @@ read_metadata (TrackerSparqlBuilder *preupdate,
 	}
 
 	for (i = 0; i < keywords->len; i++) {
-		gchar *p;
+		gchar *p, *escaped, *var;
 
 		p = g_ptr_array_index (keywords, i);
-
+		escaped = tracker_sparql_escape_string (p);
+		var = g_strdup_printf ("tag%d", i + 1);
+
+		/* ensure tag with specified label exists */
+		tracker_sparql_builder_append (preupdate,
+		                               "INSERT { _:tag a nao:Tag ; nao:prefLabel \"");
+		tracker_sparql_builder_append (preupdate, escaped);
+		tracker_sparql_builder_append (preupdate,
+		                               "\" }\nWHERE { FILTER (NOT EXISTS { "
+		                               "?tag a nao:Tag ; nao:prefLabel \"");
+		tracker_sparql_builder_append (preupdate, escaped);
+		tracker_sparql_builder_append (preupdate,
+		                               "\" }) }\n");
+
+		/* associate file with tag */
 		tracker_sparql_builder_predicate (metadata, "nao:hasTag");
+		tracker_sparql_builder_object_variable (metadata, var);
 
-		tracker_sparql_builder_object_blank_open (metadata);
-		tracker_sparql_builder_predicate (metadata, "a");
-		tracker_sparql_builder_object (metadata, "nao:Tag");
-		tracker_sparql_builder_predicate (metadata, "nao:prefLabel");
-		tracker_sparql_builder_object_unvalidated (metadata, p);
-		tracker_sparql_builder_object_blank_close (metadata);
+		if (where == NULL) {
+			where = g_string_new ("} } WHERE { {\n");
+		}
+
+		g_string_append_printf (where, "?%s a nao:Tag ; nao:prefLabel \"%s\" .\n", var, escaped);
+
+		g_free (var);
+		g_free (escaped);
 		g_free (p);
 	}
 	g_ptr_array_free (keywords, TRUE);
 
+	if (where != NULL) {
+		tracker_sparql_builder_append (metadata, where->str);
+		g_string_free (where, TRUE);
+	}
+
 	tracker_exif_free (ed);
 	tracker_xmp_free (xd);
 	g_free (pd.creation_time);
diff --git a/src/tracker-extract/tracker-extract-tiff.c b/src/tracker-extract/tracker-extract-tiff.c
index d465a18..e6aeb6c 100644
--- a/src/tracker-extract/tracker-extract-tiff.c
+++ b/src/tracker-extract/tracker-extract-tiff.c
@@ -256,6 +256,7 @@ tracker_extract_get_metadata (const gchar          *uri,
 	glong exif_offset;
 	GPtrArray *keywords;
 	guint i;
+	GString *where = NULL;
 
 #ifdef HAVE_LIBIPTCDATA
 	gchar *iptc_offset;
@@ -528,18 +529,35 @@ tracker_extract_get_metadata (const gchar          *uri,
 	}
 
 	for (i = 0; i < keywords->len; i++) {
-		gchar *p;
+		gchar *p, *escaped, *var;
 
 		p = g_ptr_array_index (keywords, i);
-
+		escaped = tracker_sparql_escape_string (p);
+		var = g_strdup_printf ("tag%d", i + 1);
+
+		/* ensure tag with specified label exists */
+		tracker_sparql_builder_append (preupdate,
+		                               "INSERT { _:tag a nao:Tag ; nao:prefLabel \"");
+		tracker_sparql_builder_append (preupdate, escaped);
+		tracker_sparql_builder_append (preupdate,
+		                               "\" }\nWHERE { FILTER (NOT EXISTS { "
+		                               "?tag a nao:Tag ; nao:prefLabel \"");
+		tracker_sparql_builder_append (preupdate, escaped);
+		tracker_sparql_builder_append (preupdate,
+		                               "\" }) }\n");
+
+		/* associate file with tag */
 		tracker_sparql_builder_predicate (metadata, "nao:hasTag");
+		tracker_sparql_builder_object_variable (metadata, var);
 
-		tracker_sparql_builder_object_blank_open (metadata);
-		tracker_sparql_builder_predicate (metadata, "a");
-		tracker_sparql_builder_object (metadata, "nao:Tag");
-		tracker_sparql_builder_predicate (metadata, "nao:prefLabel");
-		tracker_sparql_builder_object_unvalidated (metadata, p);
-		tracker_sparql_builder_object_blank_close (metadata);
+		if (where == NULL) {
+			where = g_string_new ("} } WHERE { {\n");
+		}
+
+		g_string_append_printf (where, "?%s a nao:Tag ; nao:prefLabel \"%s\" .\n", var, escaped);
+
+		g_free (var);
+		g_free (escaped);
 		g_free (p);
 	}
 	g_ptr_array_free (keywords, TRUE);
@@ -701,6 +719,11 @@ tracker_extract_get_metadata (const gchar          *uri,
 		tracker_sparql_builder_object_double (metadata, value);
 	}
 
+	if (where != NULL) {
+		tracker_sparql_builder_append (metadata, where->str);
+		g_string_free (where, TRUE);
+	}
+
 	tiff_data_free (&td);
 	tracker_exif_free (ed);
 	tracker_xmp_free (xd);



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