[tracker/wip/carlosg/tracker-3.0-api-breaks: 17/63] libtracker-sparql: Change TrackerNotifier to subscribe to Endpoint events



commit 30bcc0a6ed28ae80df859946a51a322a90210f65
Author: Carlos Garnacho <carlosg gnome org>
Date:   Fri Dec 20 15:22:27 2019 +0100

    libtracker-sparql: Change TrackerNotifier to subscribe to Endpoint events
    
    We so far settle on a sa{ii} signature for the endpoint's GraphUpdated
    signal. The DBus name of the emitter itself holds the endpoint name, the
    first string argument holds the graph, and the array holds pairs of
    event hints (created/updated/deleted) and resource IDs.
    
    This has some notable differences with the old GraphUpdated signature:
    - It used to notify insertions and deletions for all properties. That much
      granularity made the size of the passed variant data considerably bigger
      to little gain, as subcribers (TrackerNotifier first) do not track
      individual property changes except for rdf:type, which determines the
      existence of a resource. All other property changes are simply "updates".
    
      The new DBus signature takes this simplification closer to the root,
      providing high level hints about what happened per resource. This reduces
      a whole array of 4 integers to just 2 integers (no array) per resource.
      This should slim down the message size by a fair amount.
    
    - The old signal used to be all about rdf:types, it was the first argument
      in the signature precisely to allow arg0 filtering. With resources being
      able to be multiple things at the same time, and being able to be
      different things across different graphs, this split proves confusing.
      A single property change may be emitted multiple times, and might even
      result in different actions depending on the rdf:type it's notified
      through (eg. deleting the rdf:type itself).
    
      The new signal signature handles resources globally within the graph.
      When they gain the first rdf:type that must be notified they will be
      created, and after deleting the last such rdf:type will delete the
      resource. Everything in between are updates.

 src/libtracker-sparql/tracker-endpoint-dbus.c |   2 +-
 src/libtracker-sparql/tracker-notifier.c      | 127 +++-----------------------
 2 files changed, 16 insertions(+), 113 deletions(-)
---
diff --git a/src/libtracker-sparql/tracker-endpoint-dbus.c b/src/libtracker-sparql/tracker-endpoint-dbus.c
index 060e89665..00307a9c0 100644
--- a/src/libtracker-sparql/tracker-endpoint-dbus.c
+++ b/src/libtracker-sparql/tracker-endpoint-dbus.c
@@ -47,7 +47,7 @@ static const gchar introspection_xml[] =
        "      <arg type='aaa{ss}' name='result' direction='out' />"
        "    </method>"
        "    <signal name='GraphUpdate'>"
-       "      <arg type='a(ii)' name='updates' />"
+       "      <arg type='sa(ii)' name='updates' />"
        "    </signal>"
        "  </interface>"
        "</node>";
diff --git a/src/libtracker-sparql/tracker-notifier.c b/src/libtracker-sparql/tracker-notifier.c
index 46d5d4685..f4b5e738b 100644
--- a/src/libtracker-sparql/tracker-notifier.c
+++ b/src/libtracker-sparql/tracker-notifier.c
@@ -79,7 +79,6 @@ struct _TrackerNotifierPrivate {
        TrackerSparqlConnection *connection;
        GDBusConnection *dbus_connection;
        TrackerNotifierFlags flags;
-       GHashTable *cached_ids; /* gchar -> gint64* */
        gchar **expanded_classes;
        gchar **classes;
        guint graph_updated_signal_id;
@@ -121,47 +120,6 @@ G_DEFINE_TYPE_WITH_CODE (TrackerNotifier, tracker_notifier, G_TYPE_OBJECT,
                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
                                                 tracker_notifier_initable_iface_init))
 
-static void
-tracker_notifier_cache_id (TrackerNotifier *notifier,
-                           const gchar     *urn)
-{
-       TrackerSparqlCursor *cursor;
-       TrackerNotifierPrivate *priv;
-       gchar *sparql;
-
-       priv = tracker_notifier_get_instance_private (notifier);
-       sparql = g_strdup_printf ("SELECT tracker:id(%s) {}", urn);
-       cursor = tracker_sparql_connection_query (priv->connection, sparql,
-                                                 NULL, NULL);
-       g_free (sparql);
-
-       if (!cursor)
-               return;
-
-       if (tracker_sparql_cursor_next (cursor, NULL, NULL)) {
-               gint64 id = tracker_sparql_cursor_get_integer (cursor, 0);
-               gint64 *ptr = g_memdup (&id, sizeof (gint64));
-
-               g_hash_table_insert (priv->cached_ids, g_strdup (urn), ptr);
-       }
-
-       g_object_unref (cursor);
-}
-
-static gboolean
-tracker_notifier_id_matches (TrackerNotifier *notifier,
-                             gint64           id,
-                             const gchar     *urn)
-{
-       TrackerNotifierPrivate *priv;
-       gint64 *cached_id;
-
-       priv = tracker_notifier_get_instance_private (notifier);
-       cached_id = g_hash_table_lookup (priv->cached_ids, urn);
-
-       return (cached_id && *cached_id == id);
-}
-
 static TrackerNotifierEvent *
 tracker_notifier_event_new (gint64       id,
                             const gchar *rdf_type)
@@ -268,45 +226,14 @@ tracker_notifier_event_cache_push_event (TrackerNotifierEventCache *cache,
 }
 
 static void
-handle_deletes (TrackerNotifier           *notifier,
-                TrackerNotifierEventCache *cache,
-                GVariantIter              *iter)
+handle_events (TrackerNotifier           *notifier,
+               TrackerNotifierEventCache *cache,
+               GVariantIter              *iter)
 {
-       gint32 graph, subject, predicate, object;
-
-       while (g_variant_iter_loop (iter, "(iiii)",
-                                   &graph, &subject, &predicate, &object)) {
-               TrackerNotifierEventType type;
-
-               if (tracker_notifier_id_matches (notifier, predicate, "rdf:type")) {
-                       type = TRACKER_NOTIFIER_EVENT_DELETE;
-               } else {
-                       type = TRACKER_NOTIFIER_EVENT_UPDATE;
-               }
-
-               tracker_notifier_event_cache_push_event (cache, subject, type);
-       }
-}
-
-static void
-handle_updates (TrackerNotifier           *notifier,
-                TrackerNotifierEventCache *cache,
-                GVariantIter              *iter)
-{
-       gint32 graph, subject, predicate, object;
-
-       while (g_variant_iter_loop (iter, "(iiii)",
-                                   &graph, &subject, &predicate, &object)) {
-               TrackerNotifierEventType type;
-
-               if (tracker_notifier_id_matches (notifier, predicate, "rdf:type")) {
-                       type = TRACKER_NOTIFIER_EVENT_CREATE;
-               } else {
-                       type = TRACKER_NOTIFIER_EVENT_UPDATE;
-               }
+       gint32 type, resource;
 
-               tracker_notifier_event_cache_push_event (cache, subject, type);
-       }
+       while (g_variant_iter_loop (iter, "(ii)", &type, &resource))
+               tracker_notifier_event_cache_push_event (cache, resource, type);
 }
 
 static GPtrArray *
@@ -473,28 +400,15 @@ graph_updated_cb (GDBusConnection *connection,
                   gpointer         user_data)
 {
        TrackerNotifier *notifier = user_data;
-       TrackerNotifierPrivate *priv;
        TrackerNotifierEventCache *cache;
-       GVariantIter *deletes, *updates;
-       const gchar *class;
+       GVariantIter *events;
+       const gchar *graph;
 
-       priv = tracker_notifier_get_instance_private (notifier);
-       g_variant_get (parameters, "(&sa(iiii)a(iiii))", &class, &deletes, &updates);
+       g_variant_get (parameters, "(&sa(ii))", &graph, &events);
 
-       if (!priv->has_arg0_filter && priv->expanded_classes &&
-           !g_strv_contains ((const gchar * const *) priv->expanded_classes, class)) {
-               /* This class is not listened for */
-               g_variant_iter_free (deletes);
-               g_variant_iter_free (updates);
-               return;
-       }
-
-       cache = tracker_notifier_event_cache_new (notifier, class);
-       handle_deletes (notifier, cache, deletes);
-       handle_updates (notifier, cache, updates);
-
-       g_variant_iter_free (deletes);
-       g_variant_iter_free (updates);
+       cache = tracker_notifier_event_cache_new (notifier, NULL);
+       handle_events (notifier, cache, events);
+       g_variant_iter_free (events);
 
        tracker_notifier_event_cache_flush_events (cache);
        tracker_notifier_event_cache_free (cache);
@@ -565,8 +479,6 @@ tracker_notifier_initable_init (GInitable     *initable,
        if (!expand_class_iris (notifier, cancellable, error))
                return FALSE;
 
-       tracker_notifier_cache_id (notifier, "rdf:type");
-
        priv->dbus_connection = tracker_sparql_connection_get_dbus_connection ();
        if (!priv->dbus_connection)
                priv->dbus_connection = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, error);
@@ -580,15 +492,13 @@ tracker_notifier_initable_init (GInitable     *initable,
 
        dbus_name = tracker_domain_ontology_get_domain (domain_ontology, "Tracker1");
 
-       priv->has_arg0_filter =
-               priv->expanded_classes && g_strv_length (priv->expanded_classes) == 1;
        priv->graph_updated_signal_id =
                g_dbus_connection_signal_subscribe (priv->dbus_connection,
                                                    dbus_name,
-                                                   TRACKER_DBUS_INTERFACE_RESOURCES,
+                                                   "org.freedesktop.Tracker1.Endpoint",
                                                    "GraphUpdated",
-                                                   TRACKER_DBUS_OBJECT_RESOURCES,
-                                                   priv->has_arg0_filter ? priv->expanded_classes[0] : NULL,
+                                                   "/org/freedesktop/Tracker1/Endpoint",
+                                                   NULL,
                                                    G_DBUS_SIGNAL_FLAGS_NONE,
                                                    graph_updated_cb,
                                                    initable, NULL);
@@ -665,7 +575,6 @@ tracker_notifier_finalize (GObject *object)
        if (priv->connection)
                g_object_unref (priv->connection);
 
-       g_hash_table_unref (priv->cached_ids);
        g_strfreev (priv->expanded_classes);
        g_strfreev (priv->classes);
 
@@ -732,12 +641,6 @@ tracker_notifier_class_init (TrackerNotifierClass *klass)
 static void
 tracker_notifier_init (TrackerNotifier *notifier)
 {
-       TrackerNotifierPrivate *priv;
-
-       priv = tracker_notifier_get_instance_private (notifier);
-       priv->cached_ids = g_hash_table_new_full (g_str_hash,
-                                                 g_str_equal,
-                                                 g_free, g_free);
 }
 
 /**


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