[tracker] tracker-extract: Use safer method to insert tags in GIF module



commit bf0d77f786b50305a444331488ae4d851750c157
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Mar 14 12:45:16 2016 +0100

    tracker-extract: Use safer method to insert tags in GIF module
    
    The current way of inserting the nao:hasTag relationship on the
    extracted file involves one join operation per tag being inserted.
    This has performance implications, plus we can feasibly hit the
    sqlite limit of 64 tables in joins.
    
    Instead insert the tags in separate inserts, that will be as fast
    as it gets, plus there's no limit in the number of tags.

 src/tracker-extract/tracker-extract-gif.c |   64 +++++++++++++----------------
 1 files changed, 29 insertions(+), 35 deletions(-)
---
diff --git a/src/tracker-extract/tracker-extract-gif.c b/src/tracker-extract/tracker-extract-gif.c
index 10f87dc..7883f64 100644
--- a/src/tracker-extract/tracker-extract-gif.c
+++ b/src/tracker-extract/tracker-extract-gif.c
@@ -105,10 +105,10 @@ static inline void print_gif_error()
 static void
 read_metadata (TrackerSparqlBuilder *preupdate,
                TrackerSparqlBuilder *metadata,
-               GString              *where,
                GifFileType          *gifFile,
                const gchar          *uri,
-               const gchar          *graph)
+               const gchar          *graph,
+               const gchar          *urn)
 {
        GifRecordType RecordType;
        int frameheight;
@@ -395,7 +395,7 @@ read_metadata (TrackerSparqlBuilder *preupdate,
                tracker_sparql_builder_object_unvalidated (metadata, xd->metering_mode);
        }
 
-       keywords = g_ptr_array_new ();
+       keywords = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
 
        if (xd->keywords) {
                tracker_keywords_parse (keywords, xd->keywords);
@@ -419,31 +419,21 @@ read_metadata (TrackerSparqlBuilder *preupdate,
         }
 
        for (i = 0; i < keywords->len; i++) {
-               gchar *p, *escaped, *var;
+               gchar *escaped, *subject;
+               const gchar *p;
 
                p = g_ptr_array_index (keywords, i);
                escaped = tracker_sparql_escape_string (p);
-               var = g_strdup_printf ("tag%d", i + 1);
+               subject = g_strdup_printf ("_:tag%d", i + 1);
 
                /* ensure tag with specified label exists */
-               tracker_sparql_builder_append (preupdate, "INSERT { ");
-
-               if (graph) {
-                       tracker_sparql_builder_append (preupdate, "GRAPH <");
-                       tracker_sparql_builder_append (preupdate, graph);
-                       tracker_sparql_builder_append (preupdate, "> { ");
-               }
-
-               tracker_sparql_builder_append (preupdate,
-                                              "_:tag a nao:Tag ; nao:prefLabel \"");
-               tracker_sparql_builder_append (preupdate, escaped);
-               tracker_sparql_builder_append (preupdate, "\"");
-
-               if (graph) {
-                       tracker_sparql_builder_append (preupdate, " } ");
-               }
-
-               tracker_sparql_builder_append (preupdate, " }\n");
+               tracker_sparql_builder_insert_open (preupdate, graph);
+               tracker_sparql_builder_subject (preupdate, subject);
+               tracker_sparql_builder_predicate (preupdate, "a");
+               tracker_sparql_builder_object (preupdate, "nao:Tag");
+               tracker_sparql_builder_predicate (preupdate, "nao:prefLabel");
+               tracker_sparql_builder_object_unvalidated (preupdate, escaped);
+               tracker_sparql_builder_insert_close (preupdate);
                tracker_sparql_builder_append (preupdate,
                                               "WHERE { FILTER (NOT EXISTS { "
                                               "?tag a nao:Tag ; nao:prefLabel \"");
@@ -452,14 +442,21 @@ read_metadata (TrackerSparqlBuilder *preupdate,
                                               "\" }) }\n");
 
                /* associate file with tag */
-               tracker_sparql_builder_predicate (metadata, "nao:hasTag");
-               tracker_sparql_builder_object_variable (metadata, var);
-
-               g_string_append_printf (where, "?%s a nao:Tag ; nao:prefLabel \"%s\" .\n", var, escaped);
+               tracker_sparql_builder_insert_open (preupdate, graph);
+               tracker_sparql_builder_subject_iri (preupdate, urn);
+               tracker_sparql_builder_predicate (preupdate, "nao:hasTag");
+               tracker_sparql_builder_object (preupdate, "?tag");
+               tracker_sparql_builder_insert_close (preupdate);
+               tracker_sparql_builder_where_open (preupdate);
+               tracker_sparql_builder_subject (preupdate, "?tag");
+               tracker_sparql_builder_predicate (preupdate, "a");
+               tracker_sparql_builder_object (preupdate, "nao:Tag");
+               tracker_sparql_builder_predicate (preupdate, "nao:prefLabel");
+               tracker_sparql_builder_object_unvalidated (preupdate, escaped);
+               tracker_sparql_builder_where_close (preupdate);
 
-               g_free (var);
+               g_free (subject);
                g_free (escaped);
-               g_free (p);
        }
        g_ptr_array_free (keywords, TRUE);
 
@@ -627,8 +624,7 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
        TrackerSparqlBuilder *preupdate, *metadata;
        goffset size;
        GifFileType *gifFile = NULL;
-       GString *where;
-       const gchar *graph;
+       const gchar *graph, *urn;
        gchar *filename, *uri;
        GFile *file;
        int fd;
@@ -639,6 +635,7 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
        preupdate = tracker_extract_info_get_preupdate_builder (info);
        metadata = tracker_extract_info_get_metadata_builder (info);
        graph = tracker_extract_info_get_graph (info);
+       urn = tracker_extract_info_get_urn (info);
 
        file = tracker_extract_info_get_file (info);
        filename = g_file_get_path (file);
@@ -677,12 +674,9 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
        tracker_sparql_builder_object (metadata, "nfo:Image");
        tracker_sparql_builder_object (metadata, "nmm:Photo");
 
-       where = g_string_new ("");
        uri = g_file_get_uri (file);
 
-       read_metadata (preupdate, metadata, where, gifFile, uri, graph);
-       tracker_extract_info_set_where_clause (info, where->str);
-       g_string_free (where, TRUE);
+       read_metadata (preupdate, metadata, gifFile, uri, graph, urn);
 
        g_free (uri);
 


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