[tracker/wip/carlosg/update-perf: 19/61] libtracker-sparql: Use tracker_db_statement_get_values()




commit e7884a322f999ed77a0f88e43b3db9641ee27edb
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun May 22 14:31:15 2022 +0200

    libtracker-sparql: Use tracker_db_statement_get_values()
    
    Use it in all places where it makes sense along the update machinery.
    This reduces the overhead of creating many short lived cursor objects
    along updates.

 src/libtracker-sparql/core/tracker-data-query.c  |  54 ++++++------
 src/libtracker-sparql/core/tracker-data-update.c | 107 +++++++----------------
 2 files changed, 59 insertions(+), 102 deletions(-)
---
diff --git a/src/libtracker-sparql/core/tracker-data-query.c b/src/libtracker-sparql/core/tracker-data-query.c
index 09f41961e..bc7413364 100644
--- a/src/libtracker-sparql/core/tracker-data-query.c
+++ b/src/libtracker-sparql/core/tracker-data-query.c
@@ -39,12 +39,14 @@ tracker_data_query_rdf_type (TrackerDataManager  *manager,
                              TrackerRowid         id,
                              GError             **error)
 {
-       TrackerDBCursor *cursor = NULL;
        TrackerDBInterface *iface;
        TrackerDBStatement *stmt;
+       GArray *classes = NULL;
        GPtrArray *ret = NULL;
        GError *inner_error = NULL;
        TrackerOntologies *ontologies;
+       const gchar *class_uri;
+       guint i;
 
        iface = tracker_data_manager_get_writable_db_interface (manager);
        ontologies = tracker_data_manager_get_ontologies (manager);
@@ -57,22 +59,19 @@ tracker_data_query_rdf_type (TrackerDataManager  *manager,
 
        if (stmt) {
                tracker_db_statement_bind_int (stmt, 0, id);
-               cursor = tracker_db_statement_start_cursor (stmt, &inner_error);
+               classes = tracker_db_statement_get_values (stmt,
+                                                          TRACKER_PROPERTY_TYPE_STRING,
+                                                          &inner_error);
                g_object_unref (stmt);
        }
 
-       if (cursor) {
+       if (classes) {
+               ret = g_ptr_array_sized_new (classes->len);
 
-               /* Query is usually a rather small result, but let's try to
-                * avoid reallocs in gptrarray.c as much as possible (this
-                * function is called fairly often) */
-
-               ret = g_ptr_array_sized_new (20);
-               while (tracker_db_cursor_iter_next (cursor, NULL, &inner_error)) {
-                       const gchar *class_uri;
+               for (i = 0; i < classes->len; i++) {
                        TrackerClass *cl;
 
-                       class_uri = tracker_db_cursor_get_string (cursor, 0, NULL);
+                       class_uri = g_value_get_string (&g_array_index (classes, GValue, i));
                        cl = tracker_ontologies_get_class_by_uri (ontologies, class_uri);
                        if (!cl) {
                                g_critical ("Unknown class %s", class_uri);
@@ -80,7 +79,8 @@ tracker_data_query_rdf_type (TrackerDataManager  *manager,
                        }
                        g_ptr_array_add (ret, cl);
                }
-               g_object_unref (cursor);
+
+               g_array_unref (classes);
        }
 
        if (G_UNLIKELY (inner_error)) {
@@ -99,9 +99,9 @@ tracker_data_query_resource_urn (TrackerDataManager  *manager,
                                  TrackerDBInterface  *iface,
                                  TrackerRowid         id)
 {
-       TrackerDBCursor *cursor = NULL;
        TrackerDBStatement *stmt;
        gchar *uri = NULL;
+       GArray *res = NULL;
 
        g_return_val_if_fail (id != 0, NULL);
 
@@ -111,16 +111,15 @@ tracker_data_query_resource_urn (TrackerDataManager  *manager,
                return NULL;
 
        tracker_db_statement_bind_int (stmt, 0, id);
-       cursor = tracker_db_statement_start_cursor (stmt, NULL);
+       res = tracker_db_statement_get_values (stmt,
+                                              TRACKER_PROPERTY_TYPE_STRING,
+                                              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));
+       if (res && res->len == 1)
+               uri = g_value_dup_string (&g_array_index (res, GValue, 0));
 
-       g_object_unref (cursor);
+       g_clear_pointer (&res, g_array_unref);
 
        return uri;
 }
@@ -131,10 +130,10 @@ tracker_data_query_resource_id (TrackerDataManager  *manager,
                                 const gchar         *uri,
                                 GError             **error)
 {
-       TrackerDBCursor *cursor = NULL;
        TrackerDBStatement *stmt;
        GError *inner_error = NULL;
        TrackerRowid id = 0;
+       GArray *res = NULL;
 
        g_return_val_if_fail (uri != NULL, 0);
 
@@ -143,17 +142,16 @@ tracker_data_query_resource_id (TrackerDataManager  *manager,
 
        if (stmt) {
                tracker_db_statement_bind_text (stmt, 0, uri);
-               cursor = tracker_db_statement_start_cursor (stmt, &inner_error);
+               res = tracker_db_statement_get_values (stmt,
+                                                      TRACKER_PROPERTY_TYPE_INTEGER,
+                                                      &inner_error);
                g_object_unref (stmt);
        }
 
-       if (cursor) {
-               if (tracker_db_cursor_iter_next (cursor, NULL, &inner_error)) {
-                       id = tracker_db_cursor_get_int (cursor, 0);
-               }
+       if (res && res->len == 1)
+               id = g_value_get_int64 (&g_array_index (res, GValue, 0));
 
-               g_object_unref (cursor);
-       }
+       g_clear_pointer (&res, g_array_unref);
 
        if (G_UNLIKELY (inner_error)) {
                g_propagate_prefixed_error (error,
diff --git a/src/libtracker-sparql/core/tracker-data-update.c 
b/src/libtracker-sparql/core/tracker-data-update.c
index 6830e1ebe..4446784ee 100644
--- a/src/libtracker-sparql/core/tracker-data-update.c
+++ b/src/libtracker-sparql/core/tracker-data-update.c
@@ -404,13 +404,13 @@ static gboolean
 tracker_data_update_initialize_modseq (TrackerData  *data,
                                        GError      **error)
 {
-       TrackerDBCursor    *cursor = NULL;
        TrackerDBInterface *temp_iface;
        TrackerDBStatement *stmt;
-       TrackerOntologies  *ontologies;
-       TrackerProperty    *property;
-       GError             *inner_error = NULL;
-       gint                max_modseq = 0;
+       TrackerOntologies *ontologies;
+       TrackerProperty *property;
+       GArray *res = NULL;
+       GError *inner_error = NULL;
+       gint max_modseq = 0;
 
        /* Is it already initialized? */
        if (data->transaction_modseq != 0)
@@ -426,17 +426,16 @@ tracker_data_update_initialize_modseq (TrackerData  *data,
                                                       tracker_property_get_id (property));
 
        if (stmt) {
-               cursor = tracker_db_statement_start_cursor (stmt, &inner_error);
+               res = tracker_db_statement_get_values (stmt,
+                                                      TRACKER_PROPERTY_TYPE_INTEGER,
+                                                      &inner_error);
                g_object_unref (stmt);
        }
 
-       if (cursor) {
-               if (tracker_db_cursor_iter_next (cursor, NULL, &inner_error)) {
-                       max_modseq = tracker_db_cursor_get_int (cursor, 0);
-                       data->transaction_modseq = max_modseq + 1;
-               }
-
-               g_object_unref (cursor);
+       if (res) {
+               max_modseq = g_value_get_int64 (&g_array_index (res, GValue, 0));
+               data->transaction_modseq = max_modseq + 1;
+               g_array_unref (res);
        }
 
        if (G_UNLIKELY (inner_error)) {
@@ -1552,7 +1551,6 @@ get_property_values (TrackerData      *data,
                      TrackerProperty  *property,
                      GError          **error)
 {
-       gboolean multiple_values;
        const gchar *database;
        GArray *old_values;
 
@@ -1560,83 +1558,40 @@ get_property_values (TrackerData      *data,
        if (old_values != NULL)
                return old_values;
 
-       multiple_values = tracker_property_get_multiple_values (property);
-
-       old_values = g_array_sized_new (FALSE, TRUE, sizeof (GValue), multiple_values ? 4 : 1);
-       g_array_set_clear_func (old_values, (GDestroyNotify) g_value_unset);
-       g_hash_table_insert (data->resource_buffer->predicates, g_object_ref (property), old_values);
-
        database = data->resource_buffer->graph->graph ?
                data->resource_buffer->graph->graph : "main";
 
        if (!data->resource_buffer->create) {
                TrackerDBInterface *iface;
                TrackerDBStatement *stmt;
-               TrackerDBCursor    *cursor = NULL;
                const gchar        *table_name;
                const gchar        *field_name;
-               GError             *inner_error = NULL;
 
                table_name = tracker_property_get_table_name (property);
                field_name = tracker_property_get_name (property);
 
                iface = tracker_data_manager_get_writable_db_interface (data->manager);
 
-               stmt = tracker_db_interface_create_vstatement (iface, TRACKER_DB_STATEMENT_CACHE_TYPE_SELECT, 
&inner_error,
+               stmt = tracker_db_interface_create_vstatement (iface, TRACKER_DB_STATEMENT_CACHE_TYPE_SELECT, 
error,
                                                               "SELECT \"%s\" FROM \"%s\".\"%s\" WHERE ID = 
?",
                                                               field_name, database, table_name);
-
                if (stmt) {
                        tracker_db_statement_bind_int (stmt, 0, data->resource_buffer->id);
-                       cursor = tracker_db_statement_start_cursor (stmt, &inner_error);
+                       old_values = tracker_db_statement_get_values (stmt,
+                                                                     tracker_property_get_data_type 
(property),
+                                                                     error);
                        g_object_unref (stmt);
                }
 
-               if (cursor) {
-                       while (tracker_db_cursor_iter_next (cursor, NULL, &inner_error)) {
-                               GValue gvalue = { 0 };
-
-                               tracker_db_cursor_get_value (cursor, 0, &gvalue);
-
-                               if (G_VALUE_TYPE (&gvalue)) {
-                                       if (tracker_property_get_data_type (property) == 
TRACKER_PROPERTY_TYPE_DATETIME) {
-                                               GDateTime *datetime;
-
-                                               if (G_VALUE_TYPE (&gvalue) == G_TYPE_INT64) {
-                                                       datetime = g_date_time_new_from_unix_utc 
(g_value_get_int64 (&gvalue));
-                                                       g_value_unset (&gvalue);
-                                                       g_value_init (&gvalue, G_TYPE_DATE_TIME);
-                                                       g_value_take_boxed (&gvalue, datetime);
-                                               } else {
-                                                       datetime = tracker_date_new_from_iso8601 
(g_value_get_string (&gvalue),
-                                                                                                 
&inner_error);
-                                                       g_value_unset (&gvalue);
-
-                                                       if (inner_error) {
-                                                               g_propagate_prefixed_error (error,
-                                                                                           inner_error,
-                                                                                           "Error in date 
conversion:");
-                                                               return NULL;
-                                                       }
-
-                                                       g_value_init (&gvalue, G_TYPE_DATE_TIME);
-                                                       g_value_take_boxed (&gvalue, datetime);
-                                               }
-                                       }
-
-                                       g_array_append_val (old_values, gvalue);
-                               }
-                       }
-
-                       g_object_unref (cursor);
-               }
-
-               if (inner_error) {
-                       g_propagate_error (error, inner_error);
+               if (!old_values)
                        return NULL;
-               }
        }
 
+       if (!old_values)
+               old_values = g_array_new (FALSE, FALSE, sizeof (GValue));
+
+       g_hash_table_insert (data->resource_buffer->predicates, g_object_ref (property), old_values);
+
        return old_values;
 }
 
@@ -1980,13 +1935,13 @@ cache_delete_resource_type_full (TrackerData   *data,
 {
        TrackerDBInterface *iface;
        TrackerDBStatement *stmt;
-       TrackerDBCursor    *cursor = NULL;
        TrackerProperty   **properties, *prop;
        gboolean            found;
        guint               i, p, n_props;
        GError             *inner_error = NULL;
        TrackerOntologies  *ontologies;
        const gchar        *database;
+       GArray *res = NULL;
        GValue gvalue = G_VALUE_INIT;
 
        iface = tracker_data_manager_get_writable_db_interface (data->manager);
@@ -2037,21 +1992,25 @@ cache_delete_resource_type_full (TrackerData   *data,
                if (stmt) {
                        tracker_db_statement_bind_int (stmt, 0, data->resource_buffer->id);
                        tracker_db_statement_bind_text (stmt, 1, tracker_class_get_uri (class));
-                       cursor = tracker_db_statement_start_cursor (stmt, &inner_error);
+                       res = tracker_db_statement_get_values (stmt,
+                                                              TRACKER_PROPERTY_TYPE_STRING,
+                                                              &inner_error);
                        g_object_unref (stmt);
                }
 
-               if (cursor) {
-                       while (tracker_db_cursor_iter_next (cursor, NULL, &inner_error)) {
+               if (res) {
+                       for (i = 0; i < res->len; i++) {
                                const gchar *class_uri;
 
-                               class_uri = tracker_db_cursor_get_string (cursor, 0, NULL);
+                               class_uri = g_value_get_string (&g_array_index (res, GValue, i));
                                if (!cache_delete_resource_type_full (data, 
tracker_ontologies_get_class_by_uri (ontologies, class_uri),
-                                                                     FALSE, error))
+                                                                     FALSE, error)) {
+                                       g_array_unref (res);
                                        return FALSE;
+                               }
                        }
 
-                       g_object_unref (cursor);
+                       g_array_unref (res);
                }
 
                if (inner_error) {


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