[tracker/wip/carlosg/batches-and-resources: 5/7] libtracker-sparql: Add tracker_sparql_connection_update_resource() call




commit 78649b06021427add9d3cbe0fbdc7648398e2b8f
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Nov 18 02:05:06 2020 +0100

    libtracker-sparql: Add tracker_sparql_connection_update_resource() call
    
    And an async variant. This takes a TrackerResource and pushes it directly
    to the DB.

 .../libtracker-sparql-sections.txt                 |   3 +
 src/libtracker-sparql/direct/tracker-direct.c      | 132 +++++++++++++++++++++
 src/libtracker-sparql/tracker-connection.c         |  96 +++++++++++++++
 src/libtracker-sparql/tracker-connection.h         |  18 +++
 src/libtracker-sparql/tracker-private.h            |  15 +++
 5 files changed, 264 insertions(+)
---
diff --git a/docs/reference/libtracker-sparql/libtracker-sparql-sections.txt 
b/docs/reference/libtracker-sparql/libtracker-sparql-sections.txt
index 495859130..39c373881 100644
--- a/docs/reference/libtracker-sparql/libtracker-sparql-sections.txt
+++ b/docs/reference/libtracker-sparql/libtracker-sparql-sections.txt
@@ -118,6 +118,9 @@ tracker_sparql_connection_update_array_finish
 tracker_sparql_connection_update_blank
 tracker_sparql_connection_update_blank_async
 tracker_sparql_connection_update_blank_finish
+tracker_sparql_connection_update_resource
+tracker_sparql_connection_update_resource_async
+tracker_sparql_connection_update_resource_finish
 tracker_sparql_connection_get_namespace_manager
 tracker_sparql_connection_create_notifier
 tracker_sparql_connection_close
diff --git a/src/libtracker-sparql/direct/tracker-direct.c b/src/libtracker-sparql/direct/tracker-direct.c
index 9e97f6a57..bd5790895 100644
--- a/src/libtracker-sparql/direct/tracker-direct.c
+++ b/src/libtracker-sparql/direct/tracker-direct.c
@@ -54,6 +54,11 @@ struct _TrackerDirectConnectionPrivate
        guint closing     : 1;
 };
 
+typedef struct {
+       gchar *graph;
+       TrackerResource *resource;
+} UpdateResource;
+
 enum {
        PROP_0,
        PROP_FLAGS,
@@ -68,6 +73,7 @@ typedef enum {
        TASK_TYPE_QUERY,
        TASK_TYPE_UPDATE,
        TASK_TYPE_UPDATE_BLANK,
+       TASK_TYPE_UPDATE_RESOURCE,
        TASK_TYPE_RELEASE_MEMORY,
 } TaskType;
 
@@ -143,6 +149,39 @@ cleanup_timeout_cb (gpointer user_data)
        return G_SOURCE_CONTINUE;
 }
 
+gboolean
+update_resource (TrackerData      *data,
+                 const gchar      *graph,
+                 TrackerResource  *resource,
+                 GError          **error)
+{
+       GError *inner_error = NULL;
+
+       tracker_data_begin_transaction (data, &inner_error);
+       if (inner_error)
+               goto error;
+
+       tracker_data_update_resource (data,
+                                     graph,
+                                     resource,
+                                     NULL,
+                                     &inner_error);
+
+       if (inner_error) {
+               tracker_data_rollback_transaction (data);
+               goto error;
+       }
+
+       tracker_data_commit_transaction (data, &inner_error);
+       if (inner_error)
+               goto error;
+
+       return TRUE;
+
+error:
+       g_propagate_error (error, inner_error);
+       return FALSE;
+}
 
 static void
 update_thread_func (gpointer data,
@@ -175,6 +214,11 @@ update_thread_func (gpointer data,
                retval = tracker_data_update_sparql_blank (tracker_data, task_data->data, &error);
                destroy_notify = (GDestroyNotify) g_variant_unref;
                break;
+       case TASK_TYPE_UPDATE_RESOURCE: {
+               UpdateResource *data = task_data->data;
+               update_resource (tracker_data, data->graph, data->resource, &error);
+               break;
+       }
        case TASK_TYPE_RELEASE_MEMORY:
                tracker_data_manager_release_memory (priv->data_manager);
                update_timestamp = FALSE;
@@ -1027,6 +1071,91 @@ tracker_direct_connection_close_finish (TrackerSparqlConnection  *connection,
        return g_task_propagate_boolean (G_TASK (res), error);
 }
 
+static UpdateResource *
+update_resource_data_new (const gchar     *graph,
+                          TrackerResource *resource)
+{
+       UpdateResource *data;
+
+       data = g_new0 (UpdateResource, 1);
+       data->graph = g_strdup (graph);
+       data->resource = g_object_ref (resource);
+
+       return data;
+}
+
+static void
+update_resource_data_free (UpdateResource *data)
+{
+       g_free (data->graph);
+       g_object_unref (data->resource);
+       g_free (data);
+}
+
+static gboolean
+tracker_direct_connection_update_resource (TrackerSparqlConnection  *self,
+                                           const gchar              *graph,
+                                           TrackerResource          *resource,
+                                           GCancellable             *cancellable,
+                                           GError                  **error)
+{
+       TrackerDirectConnectionPrivate *priv;
+       TrackerDirectConnection *conn;
+       TrackerData *data;
+       GError *inner_error = NULL;
+
+       conn = TRACKER_DIRECT_CONNECTION (self);
+       priv = tracker_direct_connection_get_instance_private (conn);
+
+       g_mutex_lock (&priv->mutex);
+       data = tracker_data_manager_get_data (priv->data_manager);
+       update_resource (data, graph, resource, &inner_error);
+       tracker_direct_connection_update_timestamp (conn);
+       g_mutex_unlock (&priv->mutex);
+
+       if (inner_error) {
+               g_propagate_error (error, inner_error);
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+static void
+tracker_direct_connection_update_resource_async (TrackerSparqlConnection *self,
+                                                 const gchar             *graph,
+                                                 TrackerResource         *resource,
+                                                 GCancellable            *cancellable,
+                                                 GAsyncReadyCallback      callback,
+                                                 gpointer                 user_data)
+{
+       TrackerDirectConnectionPrivate *priv;
+       TrackerDirectConnection *conn;
+       TaskData *task_data;
+       GTask *task;
+
+       conn = TRACKER_DIRECT_CONNECTION (self);
+       priv = tracker_direct_connection_get_instance_private (conn);
+
+       task_data = task_data_query_new (TASK_TYPE_UPDATE_RESOURCE,
+                                        update_resource_data_new (graph, resource),
+                                        (GDestroyNotify) update_resource_data_free);
+
+       task = g_task_new (self, cancellable, callback, user_data);
+       g_task_set_task_data (task, task_data,
+                             (GDestroyNotify) task_data_free);
+
+       g_thread_pool_push (priv->update_thread, task, NULL);
+}
+
+static gboolean
+tracker_direct_connection_update_resource_finish (TrackerSparqlConnection  *connection,
+                                                  GAsyncResult             *res,
+                                                  GError                  **error)
+{
+       return g_task_propagate_boolean (G_TASK (res), error);
+}
+
 static void
 tracker_direct_connection_class_init (TrackerDirectConnectionClass *klass)
 {
@@ -1057,6 +1186,9 @@ tracker_direct_connection_class_init (TrackerDirectConnectionClass *klass)
        sparql_connection_class->close = tracker_direct_connection_close;
        sparql_connection_class->close_async = tracker_direct_connection_close_async;
        sparql_connection_class->close_finish = tracker_direct_connection_close_finish;
+       sparql_connection_class->update_resource = tracker_direct_connection_update_resource;
+       sparql_connection_class->update_resource_async = tracker_direct_connection_update_resource_async;
+       sparql_connection_class->update_resource_finish = tracker_direct_connection_update_resource_finish;
 
        props[PROP_FLAGS] =
                g_param_spec_flags ("flags",
diff --git a/src/libtracker-sparql/tracker-connection.c b/src/libtracker-sparql/tracker-connection.c
index 8df18b895..10cb9a425 100644
--- a/src/libtracker-sparql/tracker-connection.c
+++ b/src/libtracker-sparql/tracker-connection.c
@@ -537,6 +537,102 @@ tracker_sparql_connection_update_blank_finish (TrackerSparqlConnection  *connect
                                                                                error);
 }
 
+/**
+ * tracker_sparql_connection_update_resource:
+ * @connection: a #TrackerSparqlConnection
+ * @graph: (nullable): RDF graph where the resource should be inserted/updated, or %NULL for the default 
graph
+ * @resource: a #TrackerResource
+ * @cancellable: (nullable): a #GCancellable, or %NULL
+ * @error: pointer to a #GError, or %NULL
+ *
+ * Inserts a resource as described by @resource, on the graph described by @graph.
+ * This operation blocks until done.
+ *
+ * Returns: #TRUE if there were no errors.
+ *
+ * Since: 3.1
+ **/
+gboolean
+tracker_sparql_connection_update_resource (TrackerSparqlConnection  *connection,
+                                           const gchar              *graph,
+                                           TrackerResource          *resource,
+                                           GCancellable             *cancellable,
+                                           GError                  **error)
+{
+       g_return_val_if_fail (TRACKER_IS_SPARQL_CONNECTION (connection), FALSE);
+       g_return_val_if_fail (TRACKER_IS_RESOURCE (resource), FALSE);
+       g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
+       g_return_val_if_fail (!error || !*error, FALSE);
+
+       return TRACKER_SPARQL_CONNECTION_GET_CLASS (connection)->update_resource (connection,
+                                                                                 graph,
+                                                                                 resource,
+                                                                                 cancellable,
+                                                                                 error);
+}
+
+/**
+ * tracker_sparql_connection_update_resource_async:
+ * @connection: a #TrackerSparqlConnection
+ * @graph: (nullable): RDF graph where the resource should be inserted/updated, or %NULL for the default 
graph
+ * @resource: a #TrackerResource
+ * @cancellable: (nullable): a #GCancellable, or %NULL
+ * @callback: the #GAsyncReadyCallback called when the operation completes
+ * @user_data: data passed to @callback
+ *
+ * Inserts a resource as described by @resource, on the graph described by @graph.
+ * This operation is executed asynchronously, when finished @callback will be
+ * executed.
+ *
+ * Since: 3.1
+ **/
+void
+tracker_sparql_connection_update_resource_async (TrackerSparqlConnection *connection,
+                                                 const gchar             *graph,
+                                                 TrackerResource         *resource,
+                                                 GCancellable            *cancellable,
+                                                 GAsyncReadyCallback      callback,
+                                                 gpointer                 user_data)
+{
+       g_return_if_fail (TRACKER_IS_SPARQL_CONNECTION (connection));
+       g_return_if_fail (TRACKER_IS_RESOURCE (resource));
+       g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+       g_return_if_fail (callback != NULL);
+
+       TRACKER_SPARQL_CONNECTION_GET_CLASS (connection)->update_resource_async (connection,
+                                                                                graph,
+                                                                                resource,
+                                                                                cancellable,
+                                                                                callback,
+                                                                                user_data);
+}
+
+/**
+ * tracker_sparql_connection_update_resource_finish:
+ * @connection: a #TrackerSparqlConnection
+ * @res: a #GAsyncResult with the result of the operation
+ * @error: pointer to a #GError, or %NULL
+ *
+ * Finishes a tracker_sparql_connection_update_resource_async() operation.
+ *
+ * Returns: #TRUE if there were no errors.
+ *
+ * Since: 3.1
+ **/
+gboolean
+tracker_sparql_connection_update_resource_finish (TrackerSparqlConnection  *connection,
+                                                  GAsyncResult             *res,
+                                                  GError                  **error)
+{
+       g_return_val_if_fail (TRACKER_IS_SPARQL_CONNECTION (connection), FALSE);
+       g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
+       g_return_val_if_fail (!error || !*error, FALSE);
+
+       return TRACKER_SPARQL_CONNECTION_GET_CLASS (connection)->update_resource_finish (connection,
+                                                                                        res,
+                                                                                        error);
+}
+
 /**
  * tracker_sparql_connection_get_namespace_manager:
  * @connection: a #TrackerSparqlConnection
diff --git a/src/libtracker-sparql/tracker-connection.h b/src/libtracker-sparql/tracker-connection.h
index ee92fde22..0a08782f3 100644
--- a/src/libtracker-sparql/tracker-connection.h
+++ b/src/libtracker-sparql/tracker-connection.h
@@ -25,6 +25,7 @@
 
 #include <libtracker-sparql/tracker-error.h>
 #include <libtracker-sparql/tracker-notifier.h>
+#include <libtracker-sparql/tracker-resource.h>
 #include <libtracker-sparql/tracker-version.h>
 #include <gio/gio.h>
 
@@ -136,6 +137,23 @@ TRACKER_AVAILABLE_IN_ALL
 gboolean tracker_sparql_connection_update_array_finish (TrackerSparqlConnection  *connection,
                                                         GAsyncResult             *res,
                                                         GError                  **error);
+TRACKER_AVAILABLE_IN_3_1
+gboolean tracker_sparql_connection_update_resource (TrackerSparqlConnection  *connection,
+                                                    const gchar              *graph,
+                                                    TrackerResource          *resource,
+                                                    GCancellable             *cancellable,
+                                                    GError                  **error);
+TRACKER_AVAILABLE_IN_3_1
+void tracker_sparql_connection_update_resource_async (TrackerSparqlConnection *connection,
+                                                      const gchar             *graph,
+                                                      TrackerResource         *resource,
+                                                      GCancellable            *cancellable,
+                                                      GAsyncReadyCallback      callback,
+                                                      gpointer                 user_data);
+TRACKER_AVAILABLE_IN_3_1
+gboolean tracker_sparql_connection_update_resource_finish (TrackerSparqlConnection  *connection,
+                                                           GAsyncResult             *res,
+                                                           GError                  **error);
 TRACKER_AVAILABLE_IN_ALL
 GVariant * tracker_sparql_connection_update_blank (TrackerSparqlConnection  *connection,
                                                    const gchar              *sparql,
diff --git a/src/libtracker-sparql/tracker-private.h b/src/libtracker-sparql/tracker-private.h
index 7b96a855b..87662361b 100644
--- a/src/libtracker-sparql/tracker-private.h
+++ b/src/libtracker-sparql/tracker-private.h
@@ -90,6 +90,21 @@ struct _TrackerSparqlConnectionClass
         gboolean (* close_finish) (TrackerSparqlConnection  *connection,
                                    GAsyncResult             *res,
                                    GError                  **error);
+
+       gboolean (* update_resource) (TrackerSparqlConnection  *connection,
+                                     const gchar              *graph,
+                                     TrackerResource          *resource,
+                                     GCancellable             *cancellable,
+                                     GError                  **error);
+       void (* update_resource_async) (TrackerSparqlConnection *connection,
+                                       const gchar             *graph,
+                                       TrackerResource         *resource,
+                                       GCancellable            *cancellable,
+                                       GAsyncReadyCallback      callback,
+                                       gpointer                 user_data);
+       gboolean (* update_resource_finish) (TrackerSparqlConnection  *connection,
+                                            GAsyncResult             *res,
+                                            GError                  **error);
 };
 
 typedef struct _TrackerSparqlCursorClass TrackerSparqlCursorClass;


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