[tracker/wip/carlosg/anonymous-nodes: 11/24] libtracker-data: Deal with resource IDs as int64




commit 12ef892703a97c907ff4e36811c687d7cade3f68
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sat Sep 25 11:42:53 2021 +0200

    libtracker-data: Deal with resource IDs as int64
    
    These are ROWIDs, thus are signed 64-bit wide integers. Handle them
    as such, lest there's some overflows.

 src/libtracker-data/tracker-data-query.c      |   8 +-
 src/libtracker-data/tracker-data-query.h      |   4 +-
 src/libtracker-data/tracker-data-update.c     | 111 ++++++++++++++------------
 src/libtracker-data/tracker-data-update.h     |  16 ++--
 src/libtracker-sparql/direct/tracker-direct.c |  16 ++--
 5 files changed, 82 insertions(+), 73 deletions(-)
---
diff --git a/src/libtracker-data/tracker-data-query.c b/src/libtracker-data/tracker-data-query.c
index 5fc1f885b..24050aa7a 100644
--- a/src/libtracker-data/tracker-data-query.c
+++ b/src/libtracker-data/tracker-data-query.c
@@ -36,7 +36,7 @@
 GPtrArray*
 tracker_data_query_rdf_type (TrackerDataManager  *manager,
                              const gchar         *graph,
-                             gint                 id,
+                             gint64               id,
                              GError             **error)
 {
        TrackerDBCursor *cursor = NULL;
@@ -125,7 +125,7 @@ tracker_data_query_resource_urn (TrackerDataManager  *manager,
        return uri;
 }
 
-gint
+gint64
 tracker_data_query_resource_id (TrackerDataManager  *manager,
                                 TrackerDBInterface  *iface,
                                 const gchar         *uri,
@@ -134,7 +134,7 @@ tracker_data_query_resource_id (TrackerDataManager  *manager,
        TrackerDBCursor *cursor = NULL;
        TrackerDBStatement *stmt;
        GError *inner_error = NULL;
-       gint id = 0;
+       gint64 id = 0;
 
        g_return_val_if_fail (uri != NULL, 0);
 
@@ -227,7 +227,7 @@ tracker_data_query_string_to_value (TrackerDataManager   *manager,
                                     GError              **error)
 {
        TrackerData *data;
-       gint object_id;
+       gint64 object_id;
        gchar *datetime_str;
        GDateTime *datetime;
 
diff --git a/src/libtracker-data/tracker-data-query.h b/src/libtracker-data/tracker-data-query.h
index d999c2ff1..9700639f6 100644
--- a/src/libtracker-data/tracker-data-query.h
+++ b/src/libtracker-data/tracker-data-query.h
@@ -37,7 +37,7 @@ G_BEGIN_DECLS
 gchar *              tracker_data_query_resource_urn  (TrackerDataManager  *manager,
                                                        TrackerDBInterface  *iface,
                                                        gint64               id);
-gint                 tracker_data_query_resource_id   (TrackerDataManager  *manager,
+gint64               tracker_data_query_resource_id   (TrackerDataManager  *manager,
                                                        TrackerDBInterface  *iface,
                                                        const gchar         *uri,
                                                        GError             **error);
@@ -50,7 +50,7 @@ TrackerDBCursor     *tracker_data_query_sparql_cursor (TrackerDataManager  *mana
 
 GPtrArray*           tracker_data_query_rdf_type      (TrackerDataManager  *manager,
                                                        const gchar         *graph,
-                                                       gint                 id,
+                                                       gint64               id,
                                                        GError             **error);
 
 gboolean             tracker_data_query_string_to_value (TrackerDataManager   *manager,
diff --git a/src/libtracker-data/tracker-data-update.c b/src/libtracker-data/tracker-data-update.c
index e1891e128..99d422d64 100644
--- a/src/libtracker-data/tracker-data-update.c
+++ b/src/libtracker-data/tracker-data-update.c
@@ -49,9 +49,9 @@ typedef struct _TrackerStatementDelegate TrackerStatementDelegate;
 typedef struct _TrackerCommitDelegate TrackerCommitDelegate;
 
 struct _TrackerDataUpdateBuffer {
-       /* string -> integer */
+       /* string -> ID */
        GHashTable *resource_cache;
-       /* set of string, key is same pointer than resource_cache, and owned there */
+       /* set of IDs, key is same pointer than resource_cache, and owned there */
        GHashTable *new_resources;
        /* string -> TrackerDataUpdateBufferGraph */
        GPtrArray *graphs;
@@ -59,7 +59,7 @@ struct _TrackerDataUpdateBuffer {
 
 struct _TrackerDataUpdateBufferGraph {
        gchar *graph;
-       gint id;
+       gint64 id;
 
        /* string -> TrackerDataUpdateBufferResource */
        GHashTable *resources;
@@ -70,7 +70,7 @@ struct _TrackerDataUpdateBufferGraph {
 struct _TrackerDataUpdateBufferResource {
        const TrackerDataUpdateBufferGraph *graph;
        const gchar *subject;
-       gint id;
+       gint64 id;
        gboolean create;
        gboolean modified;
        /* TrackerProperty -> GArray */
@@ -321,8 +321,8 @@ tracker_data_remove_insert_statement_callback (TrackerData             *data,
 
 void
 tracker_data_dispatch_insert_statement_callbacks (TrackerData *data,
-                                                  gint         predicate_id,
-                                                  gint         class_id)
+                                                  gint64       predicate_id,
+                                                  gint64       class_id)
 {
        if (data->insert_callbacks) {
                guint n;
@@ -382,8 +382,8 @@ tracker_data_remove_delete_statement_callback (TrackerData             *data,
 
 void
 tracker_data_dispatch_delete_statement_callbacks (TrackerData *data,
-                                                  gint         predicate_id,
-                                                  gint         class_id)
+                                                  gint64       predicate_id,
+                                                  gint64       class_id)
 {
        if (data->delete_callbacks) {
                guint n;
@@ -658,29 +658,33 @@ cache_delete_value (TrackerData  *data,
        g_array_append_val (table->properties, property);
 }
 
-static gint
+static gint64
 query_resource_id (TrackerData  *data,
                    const gchar  *uri,
                    GError      **error)
 {
        TrackerDBInterface *iface;
-       gint id;
+       gint64 *value, id;
 
-       id = GPOINTER_TO_INT (g_hash_table_lookup (data->update_buffer.resource_cache, uri));
-       iface = tracker_data_manager_get_writable_db_interface (data->manager);
+       value = g_hash_table_lookup (data->update_buffer.resource_cache, uri);
 
-       if (id == 0) {
+       if (value == NULL) {
+               iface = tracker_data_manager_get_writable_db_interface (data->manager);
                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));
+               if (id != 0) {
+                       value = g_new0 (gint64, 1);
+                       *value = id;
+                       g_hash_table_insert (data->update_buffer.resource_cache, g_strdup (uri), value);
                }
+
+               return id;
        }
 
-       return id;
+       return *value;
 }
 
-gint
+gint64
 tracker_data_update_ensure_resource (TrackerData  *data,
                                      const gchar  *uri,
                                      GError      **error)
@@ -689,12 +693,12 @@ tracker_data_update_ensure_resource (TrackerData  *data,
        TrackerDBStatement *stmt = NULL;
        GError *inner_error = NULL;
        gchar *key;
-       gint id;
+       gint64 *value, id;
 
-       id = GPOINTER_TO_INT (g_hash_table_lookup (data->update_buffer.resource_cache, uri));
+       value = g_hash_table_lookup (data->update_buffer.resource_cache, uri);
 
-       if (id != 0) {
-               return id;
+       if (value != NULL) {
+               return *value;
        }
 
        iface = tracker_data_manager_get_writable_db_interface (data->manager);
@@ -716,10 +720,8 @@ tracker_data_update_ensure_resource (TrackerData  *data,
                        g_clear_error (&inner_error);
                        id = query_resource_id (data, uri, &inner_error);
 
-                       if (id != 0) {
-                               g_hash_table_insert (data->update_buffer.resource_cache, g_strdup (uri), 
GINT_TO_POINTER (id));
+                       if (id != 0)
                                return id;
-                       }
                }
 
                g_propagate_error (error, inner_error);
@@ -729,8 +731,10 @@ tracker_data_update_ensure_resource (TrackerData  *data,
 
        id = tracker_db_interface_sqlite_get_last_insert_id (iface);
        key = g_strdup (uri);
-       g_hash_table_insert (data->update_buffer.resource_cache, key, GINT_TO_POINTER (id));
-       g_hash_table_add (data->update_buffer.new_resources, key);
+       value = g_new0 (gint64, 1);
+       *value = id;
+       g_hash_table_insert (data->update_buffer.resource_cache, key, value);
+       g_hash_table_add (data->update_buffer.new_resources, value);
 
        return id;
 }
@@ -1040,25 +1044,26 @@ tracker_data_resource_buffer_flush (TrackerData                      *data,
 
 static void
 tracker_data_update_refcount (TrackerData *data,
-                              gint         id,
+                              gint64       id,
                               gint         refcount)
 {
        const TrackerDataUpdateBufferGraph *graph;
        gint old_refcount;
+       gint64 *value;
 
        g_assert (data->resource_buffer != NULL);
        graph = data->resource_buffer->graph;
 
-       old_refcount = GPOINTER_TO_INT (g_hash_table_lookup (graph->refcounts,
-                                                            GINT_TO_POINTER (id)));
-       g_hash_table_insert (graph->refcounts,
-                            GINT_TO_POINTER (id),
+       old_refcount = GPOINTER_TO_INT (g_hash_table_lookup (graph->refcounts, &id));
+       value = g_new0 (gint64, 1);
+       *value = id;
+       g_hash_table_insert (graph->refcounts, value,
                             GINT_TO_POINTER (old_refcount + refcount));
 }
 
 static void
 tracker_data_resource_ref (TrackerData *data,
-                           gint         id,
+                           gint64       id,
                            gboolean     multivalued)
 {
        if (multivalued)
@@ -1069,7 +1074,7 @@ tracker_data_resource_ref (TrackerData *data,
 
 static void
 tracker_data_resource_unref (TrackerData *data,
-                             gint         id,
+                             gint64       id,
                              gboolean     multivalued)
 {
        if (multivalued)
@@ -1113,7 +1118,8 @@ tracker_data_flush_graph_refcounts (TrackerData                   *data,
        TrackerDBStatement *stmt;
        GHashTableIter iter;
        gpointer key, value;
-       gint id, refcount;
+       gint64 id;
+       gint refcount;
        GError *inner_error = NULL;
        const gchar *database;
        gchar *insert_query;
@@ -1133,7 +1139,7 @@ tracker_data_flush_graph_refcounts (TrackerData                   *data,
        g_hash_table_iter_init (&iter, graph->refcounts);
 
        while (g_hash_table_iter_next (&iter, &key, &value)) {
-               id = GPOINTER_TO_INT (key);
+               id = *(gint64*) key;
                refcount = GPOINTER_TO_INT (value);
 
                if (refcount > 0) {
@@ -1291,7 +1297,7 @@ cache_create_service_decomposed (TrackerData   *data,
        TrackerProperty    **domain_indexes;
        GValue              gvalue = { 0 };
        guint               i;
-        gint                class_id;
+        gint64              class_id;
        TrackerOntologies  *ontologies;
 
        /* also create instance of all super classes */
@@ -2107,7 +2113,8 @@ ensure_graph_buffer (TrackerDataUpdateBuffer  *buffer,
        }
 
        graph_buffer = g_slice_new0 (TrackerDataUpdateBufferGraph);
-       graph_buffer->refcounts = g_hash_table_new (NULL, NULL);
+       graph_buffer->refcounts =
+               g_hash_table_new_full (g_int64_hash, g_int64_equal, g_free, NULL);
        graph_buffer->graph = g_strdup (name);
        if (graph_buffer->graph) {
                graph_buffer->id = tracker_data_manager_find_graph (data->manager,
@@ -2158,7 +2165,7 @@ resource_buffer_switch (TrackerData  *data,
        if (data->resource_buffer == NULL) {
                TrackerDataUpdateBufferResource *resource_buffer;
                gchar *subject_dup;
-               gint resource_id;
+               gint64 resource_id;
                gboolean create;
                GPtrArray *rdf_types;
 
@@ -2215,7 +2222,7 @@ tracker_data_delete_statement (TrackerData      *data,
                                GError          **error)
 {
        TrackerClass       *class;
-       gint                subject_id = 0;
+       gint64              subject_id = 0;
        gboolean            change = FALSE;
        TrackerOntologies  *ontologies;
 
@@ -2251,7 +2258,7 @@ tracker_data_delete_statement (TrackerData      *data,
                                     "Class '%s' not found in the ontology", object_str);
                }
        } else {
-               gint pred_id;
+               gint64 pred_id;
 
                pred_id = tracker_property_get_id (predicate);
                data->has_persistent = TRUE;
@@ -2347,7 +2354,7 @@ tracker_data_delete_all (TrackerData  *data,
                          const gchar  *predicate,
                          GError      **error)
 {
-       gint subject_id = 0;
+       gint64 subject_id = 0;
        TrackerOntologies *ontologies;
        TrackerProperty *property;
        GArray *old_values;
@@ -2471,7 +2478,7 @@ tracker_data_insert_statement_with_uri (TrackerData      *data,
 {
        GError          *actual_error = NULL;
        TrackerClass    *class;
-       gint             prop_id = 0;
+       gint64           prop_id = 0;
        gboolean change = FALSE;
        TrackerOntologies *ontologies;
 
@@ -2544,7 +2551,7 @@ tracker_data_insert_statement_with_string (TrackerData      *data,
 {
        GError          *actual_error = NULL;
        gboolean         change;
-       gint             pred_id = 0;
+       gint64           pred_id = 0;
 
        g_return_if_fail (subject != NULL);
        g_return_if_fail (predicate != NULL);
@@ -2673,8 +2680,8 @@ tracker_data_begin_transaction (TrackerData  *data,
        data->has_persistent = FALSE;
 
        if (data->update_buffer.resource_cache == NULL) {
-               data->update_buffer.resource_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, 
NULL);
-               data->update_buffer.new_resources = g_hash_table_new (g_str_hash, g_str_equal);
+               data->update_buffer.resource_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, 
g_free);
+               data->update_buffer.new_resources = g_hash_table_new (g_int64_hash, g_int64_equal);
                /* used for normal transactions */
                data->update_buffer.graphs = g_ptr_array_new_with_free_func ((GDestroyNotify) 
graph_buffer_free);
        }
@@ -2924,14 +2931,14 @@ failed:
        g_free (ontology_uri);
 }
 
-gint
+gint64
 tracker_data_ensure_graph (TrackerData  *data,
                            const gchar  *uri,
                            GError      **error)
 {
        TrackerDBInterface *iface;
        TrackerDBStatement *stmt;
-       gint id;
+       gint64 id;
 
        id = tracker_data_update_ensure_resource (data, uri, error);
        if (id == 0)
@@ -2957,7 +2964,7 @@ tracker_data_delete_graph (TrackerData  *data,
 {
        TrackerDBInterface *iface;
        TrackerDBStatement *stmt;
-       gint id;
+       gint64 id;
 
        id = query_resource_id (data, uri, error);
        if (id == 0)
@@ -3205,7 +3212,7 @@ tracker_data_update_ensure_new_bnode (TrackerData         *data,
        TrackerDBStatement *stmt = NULL;
        GError *inner_error = NULL;
        gchar *uuid, *key;
-       gint id;
+       gint64 *value, id;
 
        iface = tracker_data_manager_get_writable_db_interface (data->manager);
 
@@ -3245,8 +3252,10 @@ tracker_data_update_ensure_new_bnode (TrackerData         *data,
 
        id = tracker_db_interface_sqlite_get_last_insert_id (iface);
        key = g_strdup (uuid);
-       g_hash_table_insert (data->update_buffer.resource_cache, key, GINT_TO_POINTER (id));
-       g_hash_table_add (data->update_buffer.new_resources, key);
+       value = g_new0 (gint64, 1);
+       *value = id;
+       g_hash_table_insert (data->update_buffer.resource_cache, key, value);
+       g_hash_table_add (data->update_buffer.new_resources, value);
 
        return uuid;
 }
diff --git a/src/libtracker-data/tracker-data-update.h b/src/libtracker-data/tracker-data-update.h
index fa21417d5..97428b5a6 100644
--- a/src/libtracker-data/tracker-data-update.h
+++ b/src/libtracker-data/tracker-data-update.h
@@ -45,11 +45,11 @@ typedef struct _TrackerDataClass TrackerDataClass;
 
 typedef struct _TrackerData TrackerDataUpdate;
 
-typedef void (*TrackerStatementCallback) (gint                  graph_id,
+typedef void (*TrackerStatementCallback) (gint64                graph_id,
                                           const gchar          *graph,
-                                          gint                  subject_id,
-                                          gint                  predicate_id,
-                                          gint                  object_id,
+                                          gint64                subject_id,
+                                          gint64                predicate_id,
+                                          gint64                object_id,
                                           GPtrArray            *rdf_types,
                                           gpointer              user_data);
 typedef void (*TrackerCommitCallback)    (gpointer              user_data);
@@ -98,7 +98,7 @@ void     tracker_data_load_turtle_file              (TrackerData               *
                                                      const gchar               *graph,
                                                      GError                   **error);
 
-gint     tracker_data_ensure_graph                  (TrackerData               *data,
+gint64   tracker_data_ensure_graph                  (TrackerData               *data,
                                                      const gchar               *name,
                                                      GError                   **error);
 gboolean tracker_data_delete_graph                  (TrackerData               *data,
@@ -137,9 +137,9 @@ gboolean tracker_data_update_resource (TrackerData      *data,
                                        GHashTable       *bnodes,
                                        GError          **error);
 
-gint tracker_data_update_ensure_resource (TrackerData  *data,
-                                          const gchar  *uri,
-                                          GError      **error);
+gint64 tracker_data_update_ensure_resource (TrackerData  *data,
+                                            const gchar  *uri,
+                                            GError      **error);
 gchar * tracker_data_update_ensure_new_bnode (TrackerData         *data,
                                               TrackerDBInterface  *iface,
                                               GError             **error);
diff --git a/src/libtracker-sparql/direct/tracker-direct.c b/src/libtracker-sparql/direct/tracker-direct.c
index de394a579..cf9f1167a 100644
--- a/src/libtracker-sparql/direct/tracker-direct.c
+++ b/src/libtracker-sparql/direct/tracker-direct.c
@@ -485,11 +485,11 @@ lookup_event_cache (TrackerNotifier *notifier,
  * (always the same one though), handle with care.
  */
 static void
-insert_statement_cb (gint         graph_id,
+insert_statement_cb (gint64       graph_id,
                      const gchar *graph,
-                     gint         subject_id,
-                     gint         predicate_id,
-                     gint         object_id,
+                     gint64       subject_id,
+                     gint64       predicate_id,
+                     gint64       object_id,
                      GPtrArray   *rdf_types,
                      gpointer     user_data)
 {
@@ -529,11 +529,11 @@ insert_statement_cb (gint         graph_id,
 }
 
 static void
-delete_statement_cb (gint         graph_id,
+delete_statement_cb (gint64       graph_id,
                      const gchar *graph,
-                     gint         subject_id,
-                     gint         predicate_id,
-                     gint         object_id,
+                     gint64       subject_id,
+                     gint64       predicate_id,
+                     gint64       object_id,
                      GPtrArray   *rdf_types,
                      gpointer     user_data)
 {


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