[tracker/wip/carlosg/update-perf: 2/5] core: Avoid finding out resource insertion through GError




commit 7b2491bd1f975d8033388b1b343b5b7d8486b7cd
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sat Aug 20 16:41:34 2022 +0200

    core: Avoid finding out resource insertion through GError
    
    When ensuring that a resource IRI is known in the database, we optimize
    for newly added resources and try to insert them without further checks,
    only resorting to a query to fetch the resource ID if that insertion failed.
    
    But we figure it out after getting a GError, and clearing it. Instead, just
    check the return value, so that we don't create/free errors for every resource
    where that assumption fails.
    
    Also, we failed to cache the resource if the second querying step was taken,
    fix that to get another nice speed improvement.

 src/libtracker-sparql/core/tracker-data-update.c | 48 ++++++++++--------------
 1 file changed, 19 insertions(+), 29 deletions(-)
---
diff --git a/src/libtracker-sparql/core/tracker-data-update.c 
b/src/libtracker-sparql/core/tracker-data-update.c
index 1186d5f87..5d8e8b011 100644
--- a/src/libtracker-sparql/core/tracker-data-update.c
+++ b/src/libtracker-sparql/core/tracker-data-update.c
@@ -957,8 +957,7 @@ tracker_data_update_ensure_resource (TrackerData  *data,
        TrackerDBManagerFlags db_flags;
        TrackerDBInterface *iface;
        TrackerDBStatement *stmt = NULL;
-       GError *inner_error = NULL;
-       gchar *key;
+       gboolean inserted;
        TrackerRowid *value, id = 0;
        TrackerOntologies *ontologies;
        TrackerClass *class;
@@ -1002,37 +1001,28 @@ tracker_data_update_ensure_resource (TrackerData  *data,
                        return id;
        }
 
-       if (tracker_data_ensure_insert_resource_stmt (data, &inner_error)) {
-               stmt = data->update_buffer.insert_resource;
-               tracker_db_statement_bind_text (stmt, 0, uri);
-               tracker_db_statement_bind_int (stmt, 1, FALSE);
-               tracker_db_statement_execute (stmt, &inner_error);
-       }
-
-       if (inner_error) {
-               if (g_error_matches (inner_error,
-                                    TRACKER_DB_INTERFACE_ERROR,
-                                    TRACKER_DB_CONSTRAINT)) {
-                       g_clear_error (&inner_error);
-                       id = query_resource_id (data, uri, &inner_error);
-
-                       if (id != 0)
-                               return id;
-               }
+       if (!tracker_data_ensure_insert_resource_stmt (data, error))
+               return 0;
 
-               g_propagate_error (error, inner_error);
+       stmt = data->update_buffer.insert_resource;
+       tracker_db_statement_bind_text (stmt, 0, uri);
+       tracker_db_statement_bind_int (stmt, 1, FALSE);
+       inserted = tracker_db_statement_execute (stmt, NULL);
 
-               return 0;
+       if (inserted) {
+               iface = tracker_data_manager_get_writable_db_interface (data->manager);
+               id = tracker_db_interface_sqlite_get_last_insert_id (iface);
+               g_hash_table_add (data->update_buffer.new_resources,
+                                 tracker_rowid_copy (&id));
+       } else {
+               id = query_resource_id (data, uri, error);
        }
 
-       iface = tracker_data_manager_get_writable_db_interface (data->manager);
-       id = tracker_db_interface_sqlite_get_last_insert_id (iface);
-       key = g_strdup (uri);
-       g_hash_table_insert (data->update_buffer.resource_cache, key,
-                            tracker_rowid_copy (&id));
-
-       g_hash_table_add (data->update_buffer.new_resources,
-                         tracker_rowid_copy (&id));
+       if (id != 0) {
+               g_hash_table_insert (data->update_buffer.resource_cache,
+                                    g_strdup (uri),
+                                    tracker_rowid_copy (&id));
+       }
 
        return id;
 }


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