[tracker/wip/carlosg/data-update-error-propagation: 11/11] libtracker-data: Propagate errors from tracker_data_query_resource_id




commit 932a59780afa39c90643253482d267f398226143
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Feb 21 19:49:10 2021 +0100

    libtracker-data: Propagate errors from tracker_data_query_resource_id

 src/libtracker-data/tracker-data-query.c  | 23 ++++++-----
 src/libtracker-data/tracker-data-query.h  |  3 +-
 src/libtracker-data/tracker-data-update.c | 66 ++++++++++++++++++++++++-------
 src/libtracker-data/tracker-sparql.c      |  3 +-
 4 files changed, 69 insertions(+), 26 deletions(-)
---
diff --git a/src/libtracker-data/tracker-data-query.c b/src/libtracker-data/tracker-data-query.c
index 38c8ec96f..de7ea1f67 100644
--- a/src/libtracker-data/tracker-data-query.c
+++ b/src/libtracker-data/tracker-data-query.c
@@ -95,37 +95,40 @@ tracker_data_query_rdf_type (TrackerDataManager  *manager,
 }
 
 gint
-tracker_data_query_resource_id (TrackerDataManager *manager,
-                                TrackerDBInterface *iface,
-                                const gchar        *uri)
+tracker_data_query_resource_id (TrackerDataManager  *manager,
+                                TrackerDBInterface  *iface,
+                                const gchar         *uri,
+                                GError             **error)
 {
        TrackerDBCursor *cursor = NULL;
        TrackerDBStatement *stmt;
-       GError *error = NULL;
+       GError *inner_error = NULL;
        gint id = 0;
 
        g_return_val_if_fail (uri != NULL, 0);
 
-       stmt = tracker_db_interface_create_statement (iface, TRACKER_DB_STATEMENT_CACHE_TYPE_SELECT, &error,
+       stmt = tracker_db_interface_create_statement (iface, TRACKER_DB_STATEMENT_CACHE_TYPE_SELECT, 
&inner_error,
                                                      "SELECT ID FROM Resource WHERE Uri = ?");
 
        if (stmt) {
                tracker_db_statement_bind_text (stmt, 0, uri);
-               cursor = tracker_db_statement_start_cursor (stmt, &error);
+               cursor = tracker_db_statement_start_cursor (stmt, &inner_error);
                g_object_unref (stmt);
        }
 
        if (cursor) {
-               if (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
+               if (tracker_db_cursor_iter_next (cursor, NULL, &inner_error)) {
                        id = tracker_db_cursor_get_int (cursor, 0);
                }
 
                g_object_unref (cursor);
        }
 
-       if (G_UNLIKELY (error)) {
-               g_critical ("Could not query resource ID: %s\n", error->message);
-               g_error_free (error);
+       if (G_UNLIKELY (inner_error)) {
+               g_propagate_prefixed_error (error,
+                                           inner_error,
+                                           "Querying resource ID:");
+               return 0;
        }
 
        return id;
diff --git a/src/libtracker-data/tracker-data-query.h b/src/libtracker-data/tracker-data-query.h
index 8321843cc..cec8caa3e 100644
--- a/src/libtracker-data/tracker-data-query.h
+++ b/src/libtracker-data/tracker-data-query.h
@@ -36,7 +36,8 @@ G_BEGIN_DECLS
 
 gint                 tracker_data_query_resource_id   (TrackerDataManager  *manager,
                                                        TrackerDBInterface  *iface,
-                                                       const gchar         *uri);
+                                                       const gchar         *uri,
+                                                       GError             **error);
 gchar               *tracker_data_query_unused_uuid   (TrackerDataManager  *manager,
                                                        TrackerDBInterface  *iface);
 
diff --git a/src/libtracker-data/tracker-data-update.c b/src/libtracker-data/tracker-data-update.c
index a5900ee7e..150792adc 100644
--- a/src/libtracker-data/tracker-data-update.c
+++ b/src/libtracker-data/tracker-data-update.c
@@ -666,8 +666,9 @@ cache_delete_value (TrackerData *data,
 }
 
 static gint
-query_resource_id (TrackerData *data,
-                   const gchar *uri)
+query_resource_id (TrackerData  *data,
+                   const gchar  *uri,
+                   GError      **error)
 {
        TrackerDBInterface *iface;
        gint id;
@@ -676,7 +677,7 @@ query_resource_id (TrackerData *data,
        iface = tracker_data_manager_get_writable_db_interface (data->manager);
 
        if (id == 0) {
-               id = tracker_data_query_resource_id (data->manager, iface, uri);
+               id = tracker_data_query_resource_id (data->manager, iface, uri, error);
 
                if (id) {
                        g_hash_table_insert (data->update_buffer.resource_cache, g_strdup (uri), 
GINT_TO_POINTER (id));
@@ -722,14 +723,14 @@ tracker_data_update_ensure_resource (TrackerData  *data,
                if (g_error_matches (inner_error,
                                     TRACKER_DB_INTERFACE_ERROR,
                                     TRACKER_DB_CONSTRAINT)) {
-                       id = query_resource_id (data, uri);
+                       g_clear_error (&inner_error);
+                       id = query_resource_id (data, uri, &inner_error);
 
                        if (id != 0) {
                                if (create)
                                        *create = FALSE;
 
                                g_hash_table_insert (data->update_buffer.resource_cache, g_strdup (uri), 
GINT_TO_POINTER (id));
-                               g_error_free (inner_error);
                                return id;
                        }
                }
@@ -2375,7 +2376,7 @@ tracker_data_delete_statement (TrackerData  *data,
        g_return_if_fail (object != NULL);
        g_return_if_fail (data->in_transaction);
 
-       subject_id = query_resource_id (data, subject);
+       subject_id = query_resource_id (data, subject, error);
 
        if (subject_id == 0) {
                /* subject not in database */
@@ -2510,7 +2511,7 @@ tracker_data_delete_all (TrackerData  *data,
        g_return_val_if_fail (predicate != NULL, FALSE);
        g_return_val_if_fail (data->in_transaction, FALSE);
 
-       subject_id = query_resource_id (data, subject);
+       subject_id = query_resource_id (data, subject, error);
 
        if (subject_id == 0) {
                /* subject not in database */
@@ -2688,8 +2689,21 @@ tracker_data_insert_statement_with_uri (TrackerData  *data,
                        return;
                }
 
-               final_prop_id = (prop_id != 0) ? prop_id : tracker_data_query_resource_id (data->manager, 
iface, predicate);
-               object_id = query_resource_id (data, object_str);
+               final_prop_id = prop_id;
+
+               if (final_prop_id == 0) {
+                       final_prop_id = tracker_data_query_resource_id (data->manager,
+                                                                       iface,
+                                                                       predicate,
+                                                                       error);
+               }
+
+               if (final_prop_id == 0)
+                       return;
+
+               object_id = query_resource_id (data, object_str, error);
+               if (object_id == 0)
+                       return;
 
                change = TRUE;
        } else {
@@ -2702,8 +2716,21 @@ tracker_data_insert_statement_with_uri (TrackerData  *data,
                }
 
                if (change) {
-                       final_prop_id = (prop_id != 0) ? prop_id : tracker_data_query_resource_id 
(data->manager, iface, predicate);
-                       object_id = query_resource_id (data, object_str);
+                       final_prop_id = prop_id;
+
+                       if (final_prop_id == 0) {
+                               final_prop_id = tracker_data_query_resource_id (data->manager,
+                                                                               iface,
+                                                                               predicate,
+                                                                               error);
+                       }
+
+                       if (final_prop_id == 0)
+                               return;
+
+                       object_id = query_resource_id (data, object_str, error);
+                       if (object_id == 0)
+                               return;
 
                        tracker_data_dispatch_insert_statement_callbacks (data,
                                                                          final_prop_id,
@@ -2765,9 +2792,17 @@ tracker_data_insert_statement_with_string (TrackerData  *data,
        }
 
        if (change) {
-               pred_id = (pred_id != 0) ? pred_id : tracker_data_query_resource_id (data->manager, iface, 
predicate);
-               object_str = g_bytes_get_data (object, NULL);
+               if (pred_id == 0) {
+                       pred_id = tracker_data_query_resource_id (data->manager,
+                                                                 iface,
+                                                                 predicate,
+                                                                 error);
+               }
 
+               if (pred_id == 0)
+                       return;
+
+               object_str = g_bytes_get_data (object, NULL);
                tracker_data_dispatch_insert_statement_callbacks (data,
                                                                  pred_id,
                                                                  0, /* Always a literal */
@@ -3137,7 +3172,10 @@ tracker_data_delete_graph (TrackerData  *data,
        TrackerDBStatement *stmt;
        gint id;
 
-       id = query_resource_id (data, uri);
+       id = query_resource_id (data, uri, error);
+       if (id == 0)
+               return FALSE;
+
        iface = tracker_data_manager_get_writable_db_interface (data->manager);
        stmt = tracker_db_interface_create_statement (iface, TRACKER_DB_STATEMENT_CACHE_TYPE_UPDATE, error,
                                                      "DELETE FROM Graph WHERE ID = ?");
diff --git a/src/libtracker-data/tracker-sparql.c b/src/libtracker-data/tracker-sparql.c
index 9b0f5701f..ac1d125f7 100644
--- a/src/libtracker-data/tracker-sparql.c
+++ b/src/libtracker-data/tracker-sparql.c
@@ -4503,7 +4503,8 @@ check_idempotent_delete (TrackerSparql     *sparql,
                subject_str = _extract_node_string (subject, sparql);
                iface = tracker_data_manager_get_writable_db_interface (sparql->data_manager);
                skip = tracker_data_query_resource_id (sparql->data_manager,
-                                                      iface, subject_str) == 0;
+                                                      iface, subject_str,
+                                                      NULL) == 0;
                g_free (subject_str);
 
                if (!skip)


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