[tracker/wip/carlosg/update-perf: 27/65] libtracker-sparql: Use API to deal with TrackerResources that are blank nodes




commit 9c401ed7cee091598b10d269f16ec1c11d9e51cd
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Jul 20 14:03:31 2022 +0200

    libtracker-sparql: Use API to deal with TrackerResources that are blank nodes
    
    Avoid getting a blank node label for every blank node resource, but respecting
    those blank node labels that were created under the hood (e.g. user calling
    tracker_resource_get_identifier() on a blank node, and using the obtained string
    elsewhere).
    
    Since we already have a hashtable to mark visited TrackerResources, use it also
    as a fast path to get the blank node ROWID for a resource object.

 src/libtracker-sparql/core/tracker-data-update.c | 33 +++++++++++++++++-------
 1 file changed, 23 insertions(+), 10 deletions(-)
---
diff --git a/src/libtracker-sparql/core/tracker-data-update.c 
b/src/libtracker-sparql/core/tracker-data-update.c
index 152a2fd4d..14e524489 100644
--- a/src/libtracker-sparql/core/tracker-data-update.c
+++ b/src/libtracker-sparql/core/tracker-data-update.c
@@ -1619,15 +1619,30 @@ get_bnode_id (GHashTable       *bnodes,
 
 static TrackerRowid
 get_bnode_for_resource (GHashTable       *bnodes,
+                        GHashTable       *visited,
                         TrackerData      *data,
                         TrackerResource  *resource,
                         GError          **error)
 {
        const gchar *identifier;
+       TrackerRowid *bnode_id;
+
+       bnode_id = g_hash_table_lookup (visited, resource);
+       if (bnode_id)
+               return *bnode_id;
 
-       identifier = tracker_resource_get_identifier (resource);
+       identifier = tracker_resource_get_identifier_internal (resource);
 
-       return get_bnode_id (bnodes, data, identifier, error);
+       if (identifier) {
+               /* If the resource has a blank node identifier string already,
+                * then it has been likely referenced in other user SPARQL.
+                * Make sure this blank node label is cached for future
+                * references.
+                */
+               return get_bnode_id (bnodes, data, identifier, error);
+       } else {
+               return tracker_data_generate_bnode (data, error);
+       }
 }
 
 static gboolean
@@ -2993,13 +3008,11 @@ resource_maybe_reset_property (TrackerData      *data,
                                GError          **error)
 {
        GError *inner_error = NULL;
-       const gchar *subject_name;
 
        /* If the subject is a blank node, this is a whole new insertion.
         * We don't need deleting anything then.
         */
-       subject_name = tracker_resource_get_identifier (resource);
-       if (g_str_has_prefix (subject_name, "_:"))
+       if (tracker_resource_is_blank_node (resource))
                return TRUE;
 
        if (!tracker_data_delete_all (data,
@@ -3131,15 +3144,15 @@ update_resource_single (TrackerData      *data,
        gchar *graph_uri = NULL;
        gboolean is_bnode = FALSE;
 
-       subject_str = tracker_resource_get_identifier (resource);
-       if (!subject_str || g_str_has_prefix (subject_str, "_:")) {
+       if (tracker_resource_is_blank_node (resource)) {
                is_bnode = TRUE;
-               subject = get_bnode_for_resource (bnodes, data, resource, error);
+               subject = get_bnode_for_resource (bnodes, visited, data, resource, error);
                if (!subject)
                        return FALSE;
        } else {
                gchar *subject_uri;
 
+               subject_str = tracker_resource_get_identifier (resource);
                tracker_data_manager_expand_prefix (data->manager,
                                                    subject_str, NULL, NULL,
                                                    &subject_uri);
@@ -3152,7 +3165,7 @@ update_resource_single (TrackerData      *data,
        if (g_hash_table_lookup (visited, resource))
                goto out;
 
-       g_hash_table_add (visited, resource);
+       g_hash_table_insert (visited, resource, tracker_rowid_copy (&subject));
 
        properties = tracker_resource_get_properties (resource);
 
@@ -3242,7 +3255,7 @@ tracker_data_update_resource (TrackerData      *data,
        GHashTable *visited;
        gboolean retval;
 
-       visited = g_hash_table_new (NULL, NULL);
+       visited = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) tracker_rowid_free);
 
        if (bnodes)
                g_hash_table_ref (bnodes);


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