[tracker/wip/carlosg/anonymous-nodes: 38/46] libtracker-data: Use bnode tokens where blank nodes are required




commit 5b3cc30b625b1b9936ed6a1d294de421d7e86e9c
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sat Sep 25 20:09:09 2021 +0200

    libtracker-data: Use bnode tokens where blank nodes are required
    
    This helps us dealing with these as ROWIDs, for the most part at
    least.

 src/libtracker-data/tracker-data-query.c | 34 ---------------
 src/libtracker-data/tracker-data-query.h |  3 --
 src/libtracker-data/tracker-sparql.c     | 74 +++++++++++++++++++++-----------
 3 files changed, 50 insertions(+), 61 deletions(-)
---
diff --git a/src/libtracker-data/tracker-data-query.c b/src/libtracker-data/tracker-data-query.c
index 24050aa7a..f4a191e6f 100644
--- a/src/libtracker-data/tracker-data-query.c
+++ b/src/libtracker-data/tracker-data-query.c
@@ -165,40 +165,6 @@ tracker_data_query_resource_id (TrackerDataManager  *manager,
        return id;
 }
 
-gchar *
-tracker_data_query_unused_uuid (TrackerDataManager *manager,
-                                TrackerDBInterface *iface)
-{
-       TrackerDBCursor *cursor = NULL;
-       TrackerDBStatement *stmt;
-       GError *error = NULL;
-       gchar *uuid = NULL;
-
-       stmt = tracker_db_interface_create_statement (iface, TRACKER_DB_STATEMENT_CACHE_TYPE_SELECT, &error,
-                                                     "SELECT SparqlBNODE()");
-
-       if (stmt) {
-               cursor = tracker_db_statement_start_cursor (stmt, &error);
-               g_object_unref (stmt);
-       }
-
-       if (cursor) {
-               if (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
-                       uuid = g_strdup (tracker_db_cursor_get_string (cursor, 0, NULL));
-               }
-
-               g_object_unref (cursor);
-       }
-
-       if (G_UNLIKELY (error)) {
-               g_critical ("Could not query resource ID: %s\n", error->message);
-               g_error_free (error);
-       }
-
-       return uuid;
-}
-
-
 TrackerDBCursor *
 tracker_data_query_sparql_cursor (TrackerDataManager  *manager,
                                   const gchar         *query,
diff --git a/src/libtracker-data/tracker-data-query.h b/src/libtracker-data/tracker-data-query.h
index 9700639f6..9d916e8dc 100644
--- a/src/libtracker-data/tracker-data-query.h
+++ b/src/libtracker-data/tracker-data-query.h
@@ -41,9 +41,6 @@ gint64               tracker_data_query_resource_id   (TrackerDataManager  *mana
                                                        TrackerDBInterface  *iface,
                                                        const gchar         *uri,
                                                        GError             **error);
-gchar               *tracker_data_query_unused_uuid   (TrackerDataManager  *manager,
-                                                       TrackerDBInterface  *iface);
-
 TrackerDBCursor     *tracker_data_query_sparql_cursor (TrackerDataManager  *manager,
                                                        const gchar         *query,
                                                        GError             **error);
diff --git a/src/libtracker-data/tracker-sparql.c b/src/libtracker-data/tracker-sparql.c
index f57b54bc5..ca4b81eb8 100644
--- a/src/libtracker-data/tracker-sparql.c
+++ b/src/libtracker-data/tracker-sparql.c
@@ -6976,9 +6976,10 @@ translate_TriplesNode (TrackerSparql  *sparql,
        return TRUE;
 }
 
-static void
-tracker_sparql_generate_bnode (TrackerSparql *sparql,
-                               TrackerToken  *token)
+static gboolean
+tracker_sparql_generate_bnode (TrackerSparql  *sparql,
+                               TrackerToken   *token,
+                               GError        **error)
 {
        if (sparql->current_state->type == TRACKER_SPARQL_TYPE_SELECT ||
            sparql->current_state->type == TRACKER_SPARQL_TYPE_CONSTRUCT) {
@@ -6987,14 +6988,17 @@ tracker_sparql_generate_bnode (TrackerSparql *sparql,
                var = tracker_select_context_add_generated_variable (TRACKER_SELECT_CONTEXT 
(sparql->context));
                tracker_token_variable_init (token, var);
        } else {
-               TrackerDBInterface *iface;
-               gchar *bnode_id;
+               gint64 bnode_id;
 
-               iface = tracker_data_manager_get_writable_db_interface (sparql->data_manager);
-               bnode_id = tracker_data_query_unused_uuid (sparql->data_manager, iface);
-               tracker_token_literal_init (token, bnode_id, -1);
-               g_free (bnode_id);
+               bnode_id = tracker_data_generate_bnode (tracker_data_manager_get_data (sparql->data_manager),
+                                                       error);
+               if (bnode_id == 0)
+                       return FALSE;
+
+               tracker_token_bnode_init (token, bnode_id);
        }
+
+       return TRUE;
 }
 
 static gboolean
@@ -7007,8 +7011,10 @@ translate_BlankNodePropertyList (TrackerSparql  *sparql,
         */
        _expect (sparql, RULE_TYPE_LITERAL, LITERAL_OPEN_BRACKET);
 
-       tracker_sparql_generate_bnode (sparql,
-                                      &sparql->current_state->subject);
+       if (!tracker_sparql_generate_bnode (sparql,
+                                           &sparql->current_state->subject,
+                                           error))
+               return FALSE;
 
        _call_rule (sparql, NAMED_RULE_PropertyListNotEmpty, error);
 
@@ -7101,7 +7107,8 @@ translate_Collection (TrackerSparql  *sparql,
                        g_array_set_size (elems, elems->len + 1);
                        cur = &g_array_index (elems, TrackerToken, 0);
 
-                       tracker_sparql_generate_bnode (sparql, cur);
+                       if (!tracker_sparql_generate_bnode (sparql, cur, error))
+                               goto error;
                }
 
                sparql->current_state->subject = *cur;
@@ -7135,7 +7142,8 @@ translate_Collection (TrackerSparql  *sparql,
                        g_array_set_size (elems, elems->len + 1);
                        cur = &g_array_index (elems, TrackerToken, elems->len - 1);
 
-                       tracker_sparql_generate_bnode (sparql, cur);
+                       if (!tracker_sparql_generate_bnode (sparql, cur, error))
+                               goto error;
 
                        sparql->current_state->object = *cur;
 
@@ -9398,7 +9406,7 @@ translate_BlankNode (TrackerSparql  *sparql,
                      GError        **error)
 {
        TrackerDBInterface *iface;
-       gchar *bnode_id;
+       gint64 bnode_id;
        TrackerVariable *var;
 
        /* BlankNode ::= BLANK_NODE_LABEL | ANON
@@ -9410,31 +9418,49 @@ translate_BlankNode (TrackerSparql  *sparql,
         if (sparql->current_state->type != TRACKER_SPARQL_TYPE_SELECT &&
            sparql->current_state->type != TRACKER_SPARQL_TYPE_CONSTRUCT) {
                if (_accept (sparql, RULE_TYPE_TERMINAL, TERMINAL_TYPE_ANON)) {
-                       bnode_id = tracker_data_query_unused_uuid (sparql->data_manager, iface);
-                       tracker_token_literal_init (sparql->current_state->token, bnode_id, -1);
-                       g_free (bnode_id);
+                       bnode_id = tracker_data_generate_bnode (tracker_data_manager_get_data 
(sparql->data_manager),
+                                                               error);
+                       if (bnode_id == 0)
+                               return FALSE;
+
+                       tracker_token_bnode_init (sparql->current_state->token, bnode_id);
                } else if (_accept (sparql, RULE_TYPE_TERMINAL, TERMINAL_TYPE_BLANK_NODE_LABEL)) {
                        gchar *str;
+                       gint64 *value;
 
                        str = _dup_last_string (sparql);
 
                        if (sparql->current_state->blank_node_map) {
-                               bnode_id = g_hash_table_lookup (sparql->current_state->blank_node_map, str);
-
-                               if (!bnode_id) {
-                                       bnode_id = tracker_data_query_unused_uuid (sparql->data_manager, 
iface);
+                               value = g_hash_table_lookup (sparql->current_state->blank_node_map, str);
+
+                               if (value) {
+                                       bnode_id = *value;
+                               } else {
+                                       bnode_id = tracker_data_generate_bnode (tracker_data_manager_get_data 
(sparql->data_manager),
+                                                                               error);
+                                       if (bnode_id == 0)
+                                               return FALSE;
+                                       value = g_new0 (gint64, 1);
+                                       *value = bnode_id;
                                        g_hash_table_insert (sparql->current_state->blank_node_map,
-                                                            g_strdup (str), bnode_id);
+                                                            g_strdup (str), value);
                                }
 
                                if (sparql->blank_nodes &&
                                    sparql->current_state->update_blank_nodes &&
                                    !g_hash_table_contains (sparql->current_state->update_blank_nodes, str)) {
+                                       gchar *urn;
+
+                                       urn = tracker_data_query_resource_urn (sparql->data_manager,
+                                                                              iface,
+                                                                              bnode_id);
+
                                        g_hash_table_add (sparql->current_state->update_blank_nodes, str);
-                                       g_variant_builder_add (sparql->blank_nodes, "{ss}", str, bnode_id);
+                                       g_variant_builder_add (sparql->blank_nodes, "{ss}", str, urn);
+                                       g_free (urn);
                                }
 
-                               tracker_token_literal_init (sparql->current_state->token, bnode_id, -1);
+                               tracker_token_bnode_init (sparql->current_state->token, bnode_id);
                        } else {
                                tracker_token_literal_init (sparql->current_state->token, str, -1);
                        }


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