[tracker/wip/carlosg/update-perf: 12/18] core: Keep statement to query resource IDs




commit 2c45f079d6f3143c737fad8f98d75df2ee7ac984
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sat Aug 20 15:49:49 2022 +0200

    core: Keep statement to query resource IDs
    
    This is quite a hot path during updates of already existing resources,
    it makes sense to keep the statement around, instead of resorting to
    cache lookups.

 src/libtracker-sparql/core/tracker-data-update.c | 45 +++++++++++++++++++-----
 1 file changed, 36 insertions(+), 9 deletions(-)
---
diff --git a/src/libtracker-sparql/core/tracker-data-update.c 
b/src/libtracker-sparql/core/tracker-data-update.c
index 570038b63..b1ec9df0e 100644
--- a/src/libtracker-sparql/core/tracker-data-update.c
+++ b/src/libtracker-sparql/core/tracker-data-update.c
@@ -93,6 +93,7 @@ struct _TrackerDataUpdateBuffer {
        GPtrArray *graphs;
        /* Statement to insert in Resource table */
        TrackerDBStatement *insert_resource;
+       TrackerDBStatement *query_resource;
 
        /* Array of TrackerDataPropertyEntry */
        GArray *properties;
@@ -665,6 +666,7 @@ tracker_data_finalize (GObject *object)
        g_clear_pointer (&data->update_buffer.new_resources, g_hash_table_unref);
        g_clear_pointer (&data->update_buffer.resource_cache, g_hash_table_unref);
        g_clear_object (&data->update_buffer.insert_resource);
+       g_clear_object (&data->update_buffer.query_resource);
        tracker_db_statement_mru_finish (&data->update_buffer.stmt_mru);
 
        g_clear_pointer (&data->insert_callbacks, g_ptr_array_unref);
@@ -884,23 +886,48 @@ query_resource_id (TrackerData  *data,
                    GError      **error)
 {
        TrackerDBInterface *iface;
-       TrackerRowid *value, id;
+       TrackerDBStatement *stmt;
+       TrackerRowid *value, id = 0;
+       GError *inner_error = NULL;
+       GArray *res = NULL;
 
        value = g_hash_table_lookup (data->update_buffer.resource_cache, uri);
+       if (value)
+               return *value;
 
-       if (value == NULL) {
+       stmt = data->update_buffer.query_resource;
+       if (!stmt) {
                iface = tracker_data_manager_get_writable_db_interface (data->manager);
-               id = tracker_data_query_resource_id (data->manager, iface, uri, error);
+               stmt = data->update_buffer.query_resource =
+                       tracker_db_interface_create_statement (iface,
+                                                              TRACKER_DB_STATEMENT_CACHE_TYPE_NONE,
+                                                              &inner_error,
+                                                              "SELECT ID FROM Resource WHERE Uri = ?");
+       }
 
-               if (id != 0) {
-                       g_hash_table_insert (data->update_buffer.resource_cache, g_strdup (uri),
-                                            tracker_rowid_copy (&id));
-               }
+       if (stmt) {
+               tracker_db_statement_bind_text (stmt, 0, uri);
+               res = tracker_db_statement_get_values (stmt,
+                                                      TRACKER_PROPERTY_TYPE_INTEGER,
+                                                      &inner_error);
+       }
 
-               return id;
+       if (G_UNLIKELY (inner_error)) {
+               g_propagate_prefixed_error (error,
+                                           inner_error,
+                                           "Querying resource ID:");
+               return 0;
+       }
+
+       if (res && res->len == 1) {
+               id = g_value_get_int64 (&g_array_index (res, GValue, 0));
+               g_hash_table_insert (data->update_buffer.resource_cache, g_strdup (uri),
+                                    tracker_rowid_copy (&id));
        }
 
-       return *value;
+       g_clear_pointer (&res, g_array_unref);
+
+       return id;
 }
 
 static gboolean


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