[tracker/wip/carlosg/anonymous-nodes: 32/46] libtracker-data: Get resource URIs after failures




commit 7942c4ea1d5cacf07697180281eaa5d007cfe54b
Author: Carlos Garnacho <carlosg gnome org>
Date:   Thu Sep 23 22:46:08 2021 +0200

    libtracker-data: Get resource URIs after failures
    
    This avoids having to propagate the resource URI during insertion.
    Also make those errors somewhat more legible.

 src/libtracker-data/tracker-data-query.c  | 31 +++++++++++++++++++++++++++++++
 src/libtracker-data/tracker-data-query.h  |  3 +++
 src/libtracker-data/tracker-data-update.c | 28 ++++++++++++++++++++++++----
 3 files changed, 58 insertions(+), 4 deletions(-)
---
diff --git a/src/libtracker-data/tracker-data-query.c b/src/libtracker-data/tracker-data-query.c
index ea3d56a0c..5fc1f885b 100644
--- a/src/libtracker-data/tracker-data-query.c
+++ b/src/libtracker-data/tracker-data-query.c
@@ -94,6 +94,37 @@ tracker_data_query_rdf_type (TrackerDataManager  *manager,
        return ret;
 }
 
+gchar *
+tracker_data_query_resource_urn (TrackerDataManager  *manager,
+                                 TrackerDBInterface  *iface,
+                                 gint64               id)
+{
+       TrackerDBCursor *cursor = NULL;
+       TrackerDBStatement *stmt;
+       gchar *uri = NULL;
+
+       g_return_val_if_fail (id != 0, NULL);
+
+       stmt = tracker_db_interface_create_statement (iface, TRACKER_DB_STATEMENT_CACHE_TYPE_SELECT, NULL,
+                                                     "SELECT Uri FROM Resource WHERE ID = ?");
+       if (!stmt)
+               return NULL;
+
+       tracker_db_statement_bind_int (stmt, 0, id);
+       cursor = tracker_db_statement_start_cursor (stmt, NULL);
+       g_object_unref (stmt);
+
+       if (!cursor)
+               return NULL;
+
+       if (tracker_db_cursor_iter_next (cursor, NULL, NULL))
+               uri = g_strdup (tracker_db_cursor_get_string (cursor, 0, NULL));
+
+       g_object_unref (cursor);
+
+       return uri;
+}
+
 gint
 tracker_data_query_resource_id (TrackerDataManager  *manager,
                                 TrackerDBInterface  *iface,
diff --git a/src/libtracker-data/tracker-data-query.h b/src/libtracker-data/tracker-data-query.h
index a5b17ae13..d999c2ff1 100644
--- a/src/libtracker-data/tracker-data-query.h
+++ b/src/libtracker-data/tracker-data-query.h
@@ -34,6 +34,9 @@ G_BEGIN_DECLS
 #error "only <libtracker-data/tracker-data.h> must be included directly."
 #endif
 
+gchar *              tracker_data_query_resource_urn  (TrackerDataManager  *manager,
+                                                       TrackerDBInterface  *iface,
+                                                       gint64               id);
 gint                 tracker_data_query_resource_id   (TrackerDataManager  *manager,
                                                        TrackerDBInterface  *iface,
                                                        const gchar         *uri,
diff --git a/src/libtracker-data/tracker-data-update.c b/src/libtracker-data/tracker-data-update.c
index 2df0d1490..a3c833ea4 100644
--- a/src/libtracker-data/tracker-data-update.c
+++ b/src/libtracker-data/tracker-data-update.c
@@ -1574,11 +1574,22 @@ get_old_property_values (TrackerData      *data,
        old_values = g_hash_table_lookup (data->resource_buffer->predicates, property);
        if (old_values == NULL) {
                if (!check_property_domain (data, property)) {
+                       TrackerDBInterface *iface;
+                       gchar *resource;
+
+                       iface = tracker_data_manager_get_writable_db_interface (data->manager);
+                       resource = tracker_data_query_resource_urn (data->manager,
+                                                                   iface,
+                                                                   data->resource_buffer->id);
+
                        g_set_error (error, TRACKER_SPARQL_ERROR, TRACKER_SPARQL_ERROR_CONSTRAINT,
-                                    "Subject `%s' is not in domain `%s' of property `%s'",
-                                    data->resource_buffer->subject,
+                                    "%s %s is not is not a %s, cannot have property `%s'",
+                                    resource ? "Subject" : "Blank node",
+                                    resource ? resource : "",
                                     tracker_class_get_name (tracker_property_get_domain (property)),
                                     tracker_property_get_name (property));
+                       g_free (resource);
+
                        return NULL;
                }
 
@@ -1778,6 +1789,8 @@ cache_insert_metadata_decomposed (TrackerData      *data,
                /* value already inserted */
        } else if (!multiple_values && old_values->len > 1) {
                /* trying to add second value to single valued property */
+               TrackerDBInterface *iface;
+               gchar *resource;
                GValue old_value = { 0 };
                GValue new_value = { 0 };
                GValue *v;
@@ -1799,14 +1812,21 @@ cache_insert_metadata_decomposed (TrackerData      *data,
                        new_value_str = tracker_utf8_truncate (g_value_get_string (&new_value), 255);
                }
 
+               iface = tracker_data_manager_get_writable_db_interface (data->manager);
+               resource = tracker_data_query_resource_urn (data->manager,
+                                                           iface,
+                                                           data->resource_buffer->id);
+
                g_set_error (error, TRACKER_SPARQL_ERROR, TRACKER_SPARQL_ERROR_CONSTRAINT,
-                            "Unable to insert multiple values for subject `%s' and single valued property 
`%s' "
+                            "Unable to insert multiple values on single valued property `%s' for %s %s "
                             "(old_value: '%s', new value: '%s')",
-                            data->resource_buffer->subject,
                             field_name,
+                            resource ? "resource" : "blank node",
+                            resource ? resource : "",
                             old_value_str ? old_value_str : "<untransformable>",
                             new_value_str ? new_value_str : "<untransformable>");
 
+               g_free (resource);
                g_free (old_value_str);
                g_free (new_value_str);
                g_value_unset (&old_value);


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