[tracker/wip/carlosg/update-perf: 39/65] core: Untangle class insert and update queries




commit fa2447fd29180a30d983f3bc085038feedcaa2ee
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Aug 14 12:46:37 2022 +0200

    core: Untangle class insert and update queries
    
    The SQL update construction of both is mixed, which hinders readability.
    Untangle these so the generated INSERT/UPDATE can be followed more
    easily.

 src/libtracker-sparql/core/tracker-data-update.c | 153 +++++++++++------------
 1 file changed, 72 insertions(+), 81 deletions(-)
---
diff --git a/src/libtracker-sparql/core/tracker-data-update.c 
b/src/libtracker-sparql/core/tracker-data-update.c
index 00cfa3b48..9b5551486 100644
--- a/src/libtracker-sparql/core/tracker-data-update.c
+++ b/src/libtracker-sparql/core/tracker-data-update.c
@@ -968,10 +968,6 @@ tracker_data_resource_buffer_flush (TrackerData                      *data,
                                }
                        }
                } else {
-                       GString *sql, *values_sql;
-                       GHashTable *visited_properties;
-                       gint n;
-
                        if (table->delete_row) {
                                /* remove row from class table */
                                stmt = tracker_db_interface_create_vstatement (iface, 
TRACKER_DB_STATEMENT_CACHE_TYPE_UPDATE, &actual_error,
@@ -988,109 +984,104 @@ tracker_data_resource_buffer_flush (TrackerData                      *data,
                                        g_propagate_error (error, actual_error);
                                        return;
                                }
+                       } else {
+                               GHashTable *visited_properties;
+                               GString *sql;
+                               gint n;
 
-                               continue;
-                       }
+                               sql = g_string_new (NULL);
+                               visited_properties = g_hash_table_new (NULL, NULL);
+                               param = 1;
 
-                       if (table->insert) {
-                               sql = g_string_new ("INSERT INTO ");
-                               values_sql = g_string_new ("VALUES (?");
-                       } else {
-                               sql = g_string_new ("UPDATE ");
-                               values_sql = NULL;
-                       }
+                               if (table->insert) {
+                                       GString *values_sql;
 
-                       g_string_append_printf (sql, "\"%s\".\"%s\"",
-                                               database, table_name);
+                                       g_string_append_printf (sql,
+                                                               "INSERT INTO \"%s\".\"%s\" (ID",
+                                                               database, table_name);
+                                       values_sql = g_string_new ("VALUES (?1");
 
-                       if (table->insert) {
-                               g_string_append (sql, " (ID");
-                       } else {
-                               g_string_append (sql, " SET ");
-                       }
+                                       for (n = table->properties->len - 1; n >= 0; n--) {
+                                               property = &g_array_index (table->properties, 
TrackerDataUpdateBufferProperty, n);
+                                               property_name = tracker_property_get_name 
(property->property);
 
-                       visited_properties = g_hash_table_new (NULL, NULL);
+                                               if (g_hash_table_contains (visited_properties, 
property->property))
+                                                       continue;
 
-                       for (n = table->properties->len - 1; n >= 0; n--) {
-                               property = &g_array_index (table->properties, 
TrackerDataUpdateBufferProperty, n);
-                               property_name = tracker_property_get_name (property->property);
+                                               g_string_append_printf (sql, ", \"%s\"", property_name);
+                                               g_string_append_printf (values_sql, ", ?%d", ++param);
+                                               g_hash_table_add (visited_properties, property->property);
+                                       }
 
-                               if (g_hash_table_contains (visited_properties, property->property))
-                                       continue;
+                                       g_string_append (sql, ")");
+                                       g_string_append (values_sql, ")");
 
-                               if (table->insert) {
-                                       g_string_append_printf (sql, ", \"%s\"", property_name);
-                                       g_string_append (values_sql, ", ?");
+                                       stmt = tracker_db_interface_create_vstatement (iface, 
TRACKER_DB_STATEMENT_CACHE_TYPE_UPDATE, &actual_error,
+                                                                                      "%s %s", sql->str, 
values_sql->str);
+                                       g_string_free (sql, TRUE);
+                                       g_string_free (values_sql, TRUE);
                                } else {
-                                       if (n < (int) table->properties->len - 1) {
-                                               g_string_append (sql, ", ");
-                                       }
-                                       g_string_append_printf (sql, "\"%s\" = ?", property_name);
-                               }
+                                       g_string_append_printf (sql,
+                                                               "UPDATE \"%s\".\"%s\" SET ",
+                                                               database, table_name);
 
-                               g_hash_table_add (visited_properties, property->property);
-                       }
+                                       for (n = table->properties->len - 1; n >= 0; n--) {
+                                               property = &g_array_index (table->properties, 
TrackerDataUpdateBufferProperty, n);
+                                               property_name = tracker_property_get_name 
(property->property);
 
-                       g_hash_table_unref (visited_properties);
+                                               if (g_hash_table_contains (visited_properties, 
property->property))
+                                                       continue;
 
-                       if (table->insert) {
-                               g_string_append (sql, ")");
-                               g_string_append (values_sql, ")");
+                                               if (n < (int) table->properties->len - 1) {
+                                                       g_string_append (sql, ", ");
+                                               }
 
-                               stmt = tracker_db_interface_create_vstatement (iface, 
TRACKER_DB_STATEMENT_CACHE_TYPE_UPDATE, &actual_error,
-                                                                              "%s %s", sql->str, 
values_sql->str);
-                               g_string_free (sql, TRUE);
-                               g_string_free (values_sql, TRUE);
-                       } else {
-                               g_string_append (sql, " WHERE ID = ?");
+                                               g_string_append_printf (sql, "\"%s\" = ?%d", property_name, 
++param);
+                                               g_hash_table_add (visited_properties, property->property);
+                                       }
 
-                               stmt = tracker_db_interface_create_statement (iface, 
TRACKER_DB_STATEMENT_CACHE_TYPE_UPDATE, &actual_error,
-                                                                             sql->str);
-                               g_string_free (sql, TRUE);
-                       }
+                                       g_string_append (sql, " WHERE ID = ?1");
 
-                       if (actual_error) {
-                               g_propagate_error (error, actual_error);
-                               return;
-                       }
+                                       stmt = tracker_db_interface_create_statement (iface, 
TRACKER_DB_STATEMENT_CACHE_TYPE_UPDATE, &actual_error,
+                                                                                     sql->str);
+                                       g_string_free (sql, TRUE);
+                               }
+
+                               if (actual_error) {
+                                       g_propagate_error (error, actual_error);
+                                       return;
+                               }
 
-                       if (table->insert) {
                                tracker_db_statement_bind_int (stmt, 0, resource->id);
                                param = 1;
-                       } else {
-                               param = 0;
-                       }
 
-                       visited_properties = g_hash_table_new (NULL, NULL);
+                               g_hash_table_remove_all (visited_properties);
 
-                       for (n = table->properties->len - 1; n >= 0; n--) {
-                               property = &g_array_index (table->properties, 
TrackerDataUpdateBufferProperty, n);
+                               for (n = table->properties->len - 1; n >= 0; n--) {
+                                       property = &g_array_index (table->properties, 
TrackerDataUpdateBufferProperty, n);
 
-                               if (g_hash_table_contains (visited_properties, property->property))
-                                       continue;
+                                       if (g_hash_table_contains (visited_properties, property->property))
+                                               continue;
 
-                               if (property->delete_value) {
-                                       /* just set value to NULL for single value properties */
-                                       tracker_db_statement_bind_null (stmt, param++);
-                               } else {
-                                       statement_bind_gvalue (stmt, &param, &property->value);
-                               }
-
-                               g_hash_table_add (visited_properties, property->property);
-                       }
+                                       if (property->delete_value) {
+                                               /* just set value to NULL for single value properties */
+                                               tracker_db_statement_bind_null (stmt, param++);
+                                       } else {
+                                               statement_bind_gvalue (stmt, &param, &property->value);
+                                       }
 
-                       g_hash_table_unref (visited_properties);
+                                       g_hash_table_add (visited_properties, property->property);
+                               }
 
-                       if (!table->insert) {
-                               tracker_db_statement_bind_int (stmt, param++, resource->id);
-                       }
+                               g_hash_table_unref (visited_properties);
 
-                       tracker_db_statement_execute (stmt, &actual_error);
-                       g_object_unref (stmt);
+                               tracker_db_statement_execute (stmt, &actual_error);
+                               g_object_unref (stmt);
 
-                       if (actual_error) {
-                               g_propagate_error (error, actual_error);
-                               return;
+                               if (actual_error) {
+                                       g_propagate_error (error, actual_error);
+                                       return;
+                               }
                        }
                }
        }


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