[tracker/wip/carlosg/sparql1.1: 93/113] libtracker-data: Honor FROM / FROM NAMED



commit 6316a19451fad7c8691d35b6e474aa898a73be54
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Jul 15 01:19:09 2019 +0200

    libtracker-data: Honor FROM / FROM NAMED

 src/libtracker-data/tracker-sparql.c | 110 +++++++++++++++++++++++++----------
 1 file changed, 80 insertions(+), 30 deletions(-)
---
diff --git a/src/libtracker-data/tracker-sparql.c b/src/libtracker-data/tracker-sparql.c
index 4700557b3..68d5f3102 100644
--- a/src/libtracker-data/tracker-sparql.c
+++ b/src/libtracker-data/tracker-sparql.c
@@ -1737,6 +1737,34 @@ convert_expression_to_string (TrackerSparql       *sparql,
        }
 }
 
+static void
+_append_graph_checks (TrackerSparql *sparql,
+                      const gchar   *column_name,
+                      GStrv          graphs,
+                      gint           len)
+{
+       gint i;
+
+       _append_string_printf (sparql,
+                              "WHERE (SELECT Uri FROM Resource WHERE ID = %s) ",
+                              column_name);
+
+       if (len == 1)
+               _append_string (sparql, "= ");
+       else
+               _append_string (sparql, "IN (");
+
+       for (i = 0; i < len; i++) {
+               _append_string_printf (sparql,
+                                      "%c \"%s\" ",
+                                      i == 0 ? ' ' : ',',
+                                      graphs[i]);
+       }
+
+       if (len > 1)
+               _append_string (sparql, ")");
+}
+
 static TrackerContext *
 _begin_triples_block (TrackerSparql *sparql)
 {
@@ -1809,8 +1837,25 @@ _end_triples_block (TrackerSparql  *sparql,
                if (table->predicate_variable) {
                        _append_string (sparql,
                                        "(SELECT subject AS ID, predicate, "
-                                       "object, object_type, graph FROM tracker_triples) ");
+                                       "object, object_type, graph FROM tracker_triples ");
                        g_clear_pointer (&sparql->union_views, g_hash_table_unref);
+
+                       if (table->graph) {
+                               _append_graph_checks (sparql, "graph",
+                                                     &table->graph, 1);
+                       } else if (sparql->anon_graphs->len > 0 &&
+                                  tracker_token_is_empty (&sparql->current_state.graph)) {
+                               _append_graph_checks (sparql, "graph",
+                                                     (GStrv) sparql->anon_graphs->pdata,
+                                                     sparql->anon_graphs->len);
+                       } else if (sparql->named_graphs->len > 0 &&
+                                  tracker_token_get_variable (&sparql->current_state.graph)) {
+                               _append_graph_checks (sparql, "graph",
+                                                     (GStrv) sparql->named_graphs->pdata,
+                                                     sparql->named_graphs->len);
+                       }
+
+                       _append_string (sparql, ") ");
                } else if (table->predicate_path) {
                        _append_string_printf (sparql, "\"%s\"", table->sql_db_tablename);
                } else {
@@ -1820,17 +1865,27 @@ _end_triples_block (TrackerSparql  *sparql,
                                                       table->graph,
                                                       table->sql_db_tablename);
                        } else {
+                               _append_string_printf (sparql,
+                                                      "(SELECT * FROM \"unionGraph_%s\" ",
+                                                      table->sql_db_tablename);
+
                                if (table->graph) {
-                                       _append_string_printf (sparql,
-                                                              "(SELECT * FROM \"unionGraph_%s\" "
-                                                              "WHERE graph = (SELECT ID FROM Resource WHERE 
Uri = \"%s\")) ",
-                                                              table->sql_db_tablename,
-                                                              table->graph);
-                               } else {
-                                       _append_string_printf (sparql, "\"unionGraph_%s\" ",
-                                                              table->sql_db_tablename);
+                                       _append_graph_checks (sparql, "graph",
+                                                             &table->graph, 1);
+                               } else if (sparql->anon_graphs->len > 0 &&
+                                          tracker_token_is_empty (&sparql->current_state.graph)) {
+                                       _append_graph_checks (sparql, "graph",
+                                                             (GStrv) sparql->anon_graphs->pdata,
+                                                             sparql->anon_graphs->len);
+                               } else if (sparql->named_graphs->len > 0 &&
+                                          tracker_token_get_variable (&sparql->current_state.graph)) {
+                                       _append_graph_checks (sparql, "graph",
+                                                             (GStrv) sparql->named_graphs->pdata,
+                                                             sparql->named_graphs->len);
                                }
 
+                               _append_string (sparql, ") ");
+
                                if (sparql->union_views)
                                        g_hash_table_add (sparql->union_views, g_strdup 
(table->sql_db_tablename));
                        }
@@ -2298,6 +2353,21 @@ translate_SelectQuery (TrackerSparql  *sparql,
                _call_rule (sparql, NAMED_RULE_DatasetClause, error);
        }
 
+       /* Optimize single graph case, so it always resorts to querying
+        * the specific database.
+        */
+       if (sparql->named_graphs->len + sparql->anon_graphs->len == 1) {
+               const gchar *graph = NULL;
+
+               if (sparql->named_graphs->len > 0)
+                       graph = g_ptr_array_index (sparql->named_graphs, 0);
+               else if (sparql->anon_graphs->len > 0)
+                       graph = g_ptr_array_index (sparql->anon_graphs, 0);
+
+               if (graph)
+                       tracker_token_literal_init (&sparql->current_state.graph, graph, -1);
+       }
+
        _call_rule (sparql, NAMED_RULE_WhereClause, error);
 
        if (!_check_undefined_variables (sparql, TRACKER_SELECT_CONTEXT (sparql->context), error))
@@ -3868,6 +3938,7 @@ translate_UsingClause (TrackerSparql  *sparql,
         */
        _expect (sparql, RULE_TYPE_LITERAL, LITERAL_USING);
 
+       // FIXME
        if (_accept (sparql, RULE_TYPE_LITERAL, LITERAL_NAMED)) {
        }
 
@@ -4228,27 +4299,6 @@ translate_GraphGraphPattern (TrackerSparql  *sparql,
                     sparql->current_state.prev_node, sparql);
        _call_rule (sparql, NAMED_RULE_GroupGraphPattern, error);
 
-       if (sparql->named_graphs->len > 0) {
-               gint i;
-
-               _append_string (sparql, "WHERE graph IN (");
-
-               for (i = 0; i < sparql->named_graphs->len; i++) {
-                       const gchar *graph_name;
-                       gint graph_id;
-
-                       if (i > 0)
-                               _append_string (sparql, ", ");
-
-                       graph_name = g_ptr_array_index (sparql->named_graphs, i);
-                       graph_id = tracker_data_manager_find_graph (sparql->data_manager,
-                                                                   graph_name);
-                       _append_string_printf (sparql, "%d", graph_id);
-               }
-
-               _append_string (sparql, ") ");
-       }
-
        tracker_token_unset (&sparql->current_state.graph);
        sparql->current_state.graph = old_graph;
 


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