[tracker/tracker-0.10] tracker-extract: Do not create duplicate tags
- From: Martyn James Russell <mr src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker/tracker-0.10] tracker-extract: Do not create duplicate tags
- Date: Thu, 24 Mar 2011 22:07:09 +0000 (UTC)
commit 6ca73cbf3110fbe00d6b166984c6a43c5bf917e0
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 d5fecb0..3875a11 100644
--- a/src/tracker-extract/tracker-extract-gif.c
+++ b/src/tracker-extract/tracker-extract-gif.c
@@ -93,6 +93,7 @@ read_metadata (TrackerSparqlBuilder *preupdate,
MergeData md = { 0 };
GifData gd = { 0 };
TrackerXmpData *xd = NULL;
+ GString *where = NULL;
do {
GifByteType *ExtData;
@@ -350,18 +351,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);
@@ -483,6 +501,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 8df263e..c8b6f7f 100644
--- a/src/tracker-extract/tracker-extract-jpeg.c
+++ b/src/tracker-extract/tracker-extract-jpeg.c
@@ -117,6 +117,7 @@ extract_jpeg (const gchar *uri,
gchar *comment = NULL;
GPtrArray *keywords;
guint i;
+ GString *where = NULL;
filename = g_filename_from_uri (uri, NULL, NULL);
@@ -361,18 +362,35 @@ extract_jpeg (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);
@@ -621,6 +639,11 @@ extract_jpeg (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 345f6b9..bf9df65 100644
--- a/src/tracker-extract/tracker-extract-pdf.c
+++ b/src/tracker-extract/tracker-extract-pdf.c
@@ -290,6 +290,7 @@ extract_pdf (const gchar *uri,
guint n_bytes;
GPtrArray *keywords;
guint i;
+ GString *where = NULL;
g_type_init ();
@@ -566,18 +567,35 @@ extract_pdf (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);
@@ -597,6 +615,11 @@ extract_pdf (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 b96c72e..4e5880a 100644
--- a/src/tracker-extract/tracker-extract-png.c
+++ b/src/tracker-extract/tracker-extract-png.c
@@ -107,6 +107,7 @@ read_metadata (TrackerSparqlBuilder *preupdate,
gint i;
gint found;
GPtrArray *keywords;
+ GString *where = NULL;
info_ptrs[0] = info_ptr;
info_ptrs[1] = end_ptr;
@@ -484,22 +485,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 128301a..fbc1ed9 100644
--- a/src/tracker-extract/tracker-extract-tiff.c
+++ b/src/tracker-extract/tracker-extract-tiff.c
@@ -264,6 +264,7 @@ extract_tiff (const gchar *uri,
glong exif_offset;
GPtrArray *keywords;
guint i;
+ GString *where = NULL;
#ifdef HAVE_LIBIPTCDATA
gchar *iptc_offset;
@@ -536,18 +537,35 @@ extract_tiff (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);
@@ -709,6 +727,11 @@ extract_tiff (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]