[tracker/wip/carlosg/portal: 14/34] libtracker-data: Implement DEFAULT/ALL in CONSTRAINT declaration



commit 7fe8ba952ba01b0befa5f2acfed50a7b9be7b4cd
Author: Carlos Garnacho <carlosg gnome org>
Date:   Fri Jan 24 12:14:59 2020 +0100

    libtracker-data: Implement DEFAULT/ALL in CONSTRAINT declaration
    
    Make it easy to include all graphs (no constraints as per this clause)
    or the default unnamed graph (as it's otherwise non referenceable here).

 src/libtracker-data/tracker-sparql-grammar.h |  7 +++--
 src/libtracker-data/tracker-sparql.c         | 42 +++++++++++++++++++++-------
 2 files changed, 36 insertions(+), 13 deletions(-)
---
diff --git a/src/libtracker-data/tracker-sparql-grammar.h b/src/libtracker-data/tracker-sparql-grammar.h
index 997c0f0f4..f104951a4 100644
--- a/src/libtracker-data/tracker-sparql-grammar.h
+++ b/src/libtracker-data/tracker-sparql-grammar.h
@@ -1501,14 +1501,15 @@ static const TrackerGrammarRule rule_SelectQuery[] = { R(SelectClause), GTE0(hel
  */
 static const TrackerGrammarRule rule_PrefixDecl[] = { L(PREFIX), T(PNAME_NS), T(IRIREF), NIL };
 
-/* ConstraintDecl ::= 'CONSTRAINT' ( 'GRAPH' | 'SERVICE' ) ( IRIREF (',' IRIREF)* )?
+/* ConstraintDecl ::= 'CONSTRAINT' ( 'GRAPH' | 'SERVICE' ) ( ( IRIREF | 'DEFAULT' | 'ALL' ) ( ',' ( IRIREF | 
'DEFAULT' | 'ALL' ) )* )?
  *
  * TRACKER EXTENSION
  */
 static const TrackerGrammarRule helper_ConstraintDecl_or_1[] = { L(GRAPH), L(SERVICE), NIL };
-static const TrackerGrammarRule helper_ConstraintDecl_seq_1[] = { L(COMMA), T(IRIREF), NIL };
+static const TrackerGrammarRule helper_ConstraintDecl_or_2[] = { T(IRIREF), L(DEFAULT), L(ALL), NIL };
+static const TrackerGrammarRule helper_ConstraintDecl_seq_1[] = { L(COMMA), OR(helper_ConstraintDecl_or_2), 
NIL };
 static const TrackerGrammarRule helper_ConstraintDecl_gte0_1[] = { S(helper_ConstraintDecl_seq_1), NIL };
-static const TrackerGrammarRule helper_ConstraintDecl_opt_1[] = { T(IRIREF), 
GTE0(helper_ConstraintDecl_gte0_1), NIL };
+static const TrackerGrammarRule helper_ConstraintDecl_opt_1[] = { OR(helper_ConstraintDecl_or_2), 
GTE0(helper_ConstraintDecl_gte0_1), NIL };
 static const TrackerGrammarRule rule_ConstraintDecl[] = { L(CONSTRAINT), OR(helper_ConstraintDecl_or_1), 
OPT(helper_ConstraintDecl_opt_1), NIL };
 
 /* BaseDecl ::= 'BASE' IRIREF
diff --git a/src/libtracker-data/tracker-sparql.c b/src/libtracker-data/tracker-sparql.c
index e24a5dd90..55f98153f 100644
--- a/src/libtracker-data/tracker-sparql.c
+++ b/src/libtracker-data/tracker-sparql.c
@@ -146,6 +146,7 @@ struct _TrackerSparql
                GPtrArray *graphs;
                GPtrArray *services;
                GHashTable *filtered_graphs;
+               gboolean filter_unnamed_graph;
        } policy;
 
        struct {
@@ -691,7 +692,7 @@ _append_union_graph_with_clause (TrackerSparql *sparql,
        _append_string_printf (sparql, "\"unionGraph_%s\"(ID, %s graph) AS (",
                               table_name, properties);
 
-       if (g_hash_table_size (graphs) > 0) {
+       if (!sparql->policy.filter_unnamed_graph) {
                _append_string_printf (sparql,
                                       "SELECT ID, %s 0 AS graph FROM \"main\".\"%s\" ",
                                       properties, table_name);
@@ -2696,8 +2697,10 @@ translate_ConstraintDecl (TrackerSparql  *sparql,
                           GError        **error)
 {
        GPtrArray **previous_set, *set;
+       gboolean graph = FALSE;
+       gboolean filter_unnamed_graph = TRUE;
 
-       /* ConstraintDecl ::= 'CONSTRAINT' ( 'GRAPH' | 'SERVICE' ) ( IRIREF (',' IRIREF)* )?
+       /* ConstraintDecl ::= 'CONSTRAINT' ( 'GRAPH' | 'SERVICE' ) ( ( IRIREF | 'DEFAULT' | 'ALL' ) ( ',' ( 
IRIREF | 'DEFAULT' | 'ALL' ) )* )?
         *
         * TRACKER EXTENSION
         */
@@ -2705,6 +2708,7 @@ translate_ConstraintDecl (TrackerSparql  *sparql,
 
        if (_accept (sparql, RULE_TYPE_LITERAL, LITERAL_GRAPH)) {
                previous_set = &sparql->policy.graphs;
+               graph = TRUE;
        } else if (_accept (sparql, RULE_TYPE_LITERAL, LITERAL_SERVICE)) {
                previous_set = &sparql->policy.services;
        } else {
@@ -2713,23 +2717,41 @@ translate_ConstraintDecl (TrackerSparql  *sparql,
 
        set = g_ptr_array_new_with_free_func (g_free);
 
-       while (_accept (sparql, RULE_TYPE_TERMINAL, TERMINAL_TYPE_IRIREF)) {
+       do {
                gchar *elem;
 
-               elem = _dup_last_string (sparql);
-               g_ptr_array_add (set, elem);
-
-               if (!_accept (sparql, RULE_TYPE_LITERAL, LITERAL_COMMA))
+               if (_accept (sparql, RULE_TYPE_TERMINAL, TERMINAL_TYPE_IRIREF)) {
+                       if (set) {
+                               elem = _dup_last_string (sparql);
+                               g_ptr_array_add (set, elem);
+                       }
+               } else if (_accept (sparql, RULE_TYPE_LITERAL, LITERAL_DEFAULT)) {
+                       if (graph)
+                               filter_unnamed_graph = FALSE;
+               } else if (_accept (sparql, RULE_TYPE_LITERAL, LITERAL_ALL)) {
+                       if (graph)
+                               filter_unnamed_graph = FALSE;
+                       g_clear_pointer (&set, g_ptr_array_unref);
+               } else {
                        break;
-       }
+               }
+       } while (_accept (sparql, RULE_TYPE_LITERAL, LITERAL_COMMA));
 
        if (*previous_set) {
-               intersect_set (*previous_set, set);
-               g_ptr_array_unref (set);
+               if (set) {
+                       intersect_set (*previous_set, set);
+                       g_ptr_array_unref (set);
+               }
        } else {
                *previous_set = set;
        }
 
+       if (graph) {
+               sparql->policy.filter_unnamed_graph |= filter_unnamed_graph;
+               g_clear_pointer (&sparql->policy.filtered_graphs,
+                                g_hash_table_unref);
+       }
+
        return TRUE;
 }
 


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