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




commit 98599e73ae1a8b11cad3ee158666222f28485898
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 1afa003c9..8504da173 100644
--- a/src/libtracker-sparql/core/tracker-data-update.c
+++ b/src/libtracker-sparql/core/tracker-data-update.c
@@ -1621,15 +1621,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
@@ -2995,13 +3010,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,
@@ -3133,15 +3146,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);
@@ -3154,7 +3167,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);
 
@@ -3244,7 +3257,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]