[tracker/wip/carlosg/deserialize-api: 10/17] libtracker-sparql: Implement RDF deserialization on D-Bus connections




commit 04abacdb65a365db01b9ad0799772dec98410b41
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Jun 26 16:47:56 2022 +0200

    libtracker-sparql: Implement RDF deserialization on D-Bus connections
    
    Implement this through a new "Deserialize" D-Bus request, implemented by
    the D-Bus endpoint, and used by the bus TrackerConnection.

 src/libtracker-sparql/bus/tracker-bus.vala    | 30 ++++++++++++
 src/libtracker-sparql/tracker-endpoint-dbus.c | 69 +++++++++++++++++++++++++++
 src/libtracker-sparql/tracker-sparql.vapi     |  6 +++
 3 files changed, 105 insertions(+)
---
diff --git a/src/libtracker-sparql/bus/tracker-bus.vala b/src/libtracker-sparql/bus/tracker-bus.vala
index b4897859d..93f4b9f05 100644
--- a/src/libtracker-sparql/bus/tracker-bus.vala
+++ b/src/libtracker-sparql/bus/tracker-bus.vala
@@ -411,4 +411,34 @@ public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
        public async override GLib.InputStream serialize_async (SerializeFlags flags, RdfFormat format, 
string sparql, GLib.Cancellable? cancellable = null) throws Sparql.Error, GLib.Error, GLib.IOError, 
GLib.DBusError {
                return yield perform_serialize (bus, dbus_name, object_path, flags, format, sparql, null, 
cancellable);
        }
+
+       public static async bool perform_deserialize (DBusConnection bus, string dbus_name, string 
object_path, DeserializeFlags flags, RdfFormat format, string? default_graph, GLib.InputStream istream, 
VariantBuilder? arguments, Cancellable? cancellable) throws GLib.IOError, GLib.Error {
+               UnixInputStream input;
+               UnixOutputStream output;
+               pipe (out input, out output);
+
+               var message = new DBusMessage.method_call (dbus_name, object_path, ENDPOINT_IFACE, 
"Deserialize");
+               var fd_list = new UnixFDList ();
+               DBusMessage? reply = null;
+               message.set_body (new Variant ("(hiisa{sv})", fd_list.append (input.fd), flags, format, 
default_graph != null ? default_graph : "", arguments));
+               message.set_unix_fd_list (fd_list);
+
+               bus.send_message_with_reply.begin (message, DBusSendMessageFlags.NONE, int.MAX, null, 
cancellable, (o, res) => {
+                       reply = bus.send_message_with_reply.end(res);
+                       perform_deserialize.callback();
+               });
+
+               output.splice_async.begin (istream, OutputStreamSpliceFlags.CLOSE_SOURCE | 
OutputStreamSpliceFlags.CLOSE_TARGET, Priority.DEFAULT, cancellable);
+
+               yield;
+
+               if (reply != null)
+                       handle_error_reply (reply);
+
+               return true;
+       }
+
+       public async override bool deserialize_async (DeserializeFlags flags, RdfFormat format, string? 
default_graph, GLib.InputStream istream, Cancellable? cancellable) throws GLib.IOError, GLib.Error {
+               return yield perform_deserialize (bus, dbus_name, object_path, flags, format, default_graph, 
istream, null, cancellable);
+       }
 }
diff --git a/src/libtracker-sparql/tracker-endpoint-dbus.c b/src/libtracker-sparql/tracker-endpoint-dbus.c
index c6735603d..e1b22cda9 100644
--- a/src/libtracker-sparql/tracker-endpoint-dbus.c
+++ b/src/libtracker-sparql/tracker-endpoint-dbus.c
@@ -78,6 +78,13 @@ static const gchar introspection_xml[] =
        "      <arg type='h' name='input_stream' direction='in' />"
        "      <arg type='aaa{ss}' name='result' direction='out' />"
        "    </method>"
+       "    <method name='Deserialize'>"
+       "      <arg type='h' name='input_stream' direction='in' />"
+       "      <arg type='i' name='flags' direction='in' />"
+       "      <arg type='i' name='format' direction='in' />"
+       "      <arg type='s' name='default_graph' direction='in' />"
+       "      <arg type='a{sv}' name='arguments' direction='in' />"
+       "    </method>"
        "    <signal name='GraphUpdated'>"
        "      <arg type='sa{ii}' name='updates' />"
        "    </signal>"
@@ -688,6 +695,25 @@ read_update_blank_cb (GObject      *object,
                                                      request);
 }
 
+static void
+deserialize_cb (GObject      *object,
+                GAsyncResult *res,
+                gpointer      user_data)
+{
+       UpdateRequest *request = user_data;
+       GError *error = NULL;
+
+       if (!tracker_sparql_connection_deserialize_finish (TRACKER_SPARQL_CONNECTION (object),
+                                                          res, &error)) {
+               g_dbus_method_invocation_return_gerror (request->invocation, error);
+               update_request_free (request);
+               return;
+       }
+
+       g_dbus_method_invocation_return_value (request->invocation, NULL);
+       update_request_free (request);
+}
+
 static TrackerSparqlStatement *
 create_statement (TrackerSparqlConnection  *conn,
                   const gchar              *query,
@@ -929,6 +955,49 @@ endpoint_dbus_iface_method_call (GDBusConnection       *connection,
                        request = update_request_new (endpoint_dbus, invocation, FALSE, fd);
                        update_request_read_next (request, read_update_blank_cb);
                }
+       } else if (g_strcmp0 (method_name, "Deserialize") == 0) {
+               TrackerDeserializeFlags flags;
+               TrackerRdfFormat format;
+               gchar *graph;
+
+               if (tracker_endpoint_dbus_forbid_operation (endpoint_dbus,
+                                                           invocation,
+                                                           TRACKER_OPERATION_TYPE_UPDATE)) {
+                       g_dbus_method_invocation_return_error (invocation,
+                                                              G_DBUS_ERROR,
+                                                              G_DBUS_ERROR_ACCESS_DENIED,
+                                                              "Operation not allowed");
+                       return;
+               }
+
+               g_variant_get (parameters, "(hiisa{sv})", &handle, &flags, &format, &graph, &arguments);
+
+               if (fd_list)
+                       fd = g_unix_fd_list_get (fd_list, handle, &error);
+
+               if (fd < 0) {
+                       g_dbus_method_invocation_return_error (invocation,
+                                                              G_DBUS_ERROR,
+                                                              G_DBUS_ERROR_INVALID_ARGS,
+                                                              "Did not get a file descriptor");
+               } else {
+                       TrackerSparqlConnection *conn;
+                       UpdateRequest *request;
+
+                       request = update_request_new (endpoint_dbus, invocation, FALSE, fd);
+                       conn = tracker_endpoint_get_sparql_connection (TRACKER_ENDPOINT (request->endpoint));
+
+                       tracker_sparql_connection_deserialize_async (conn,
+                                                                    flags,
+                                                                    format,
+                                                                    graph && *graph ? graph : NULL,
+                                                                    G_INPUT_STREAM (request->input_stream),
+                                                                    request->endpoint->cancellable,
+                                                                    deserialize_cb,
+                                                                    request);
+               }
+
+               g_free (graph);
        } else {
                g_dbus_method_invocation_return_error (invocation,
                                                       G_DBUS_ERROR,
diff --git a/src/libtracker-sparql/tracker-sparql.vapi b/src/libtracker-sparql/tracker-sparql.vapi
index ac246e2e1..94f4a48f0 100644
--- a/src/libtracker-sparql/tracker-sparql.vapi
+++ b/src/libtracker-sparql/tracker-sparql.vapi
@@ -64,6 +64,11 @@ namespace Tracker {
                NONE = 0,
        }
 
+       [CCode (cheader_filename = "libtracker-sparql/tracker-connection.h")]
+       public enum DeserializeFlags {
+               NONE = 0,
+       }
+
        namespace Sparql {
                [CCode (cheader_filename = "libtracker-sparql/tracker-sparql.h")]
                public static string escape_string (string literal);
@@ -114,6 +119,7 @@ namespace Tracker {
                 public async virtual bool close_async () throws GLib.IOError;
 
                 public async virtual GLib.InputStream serialize_async (SerializeFlags flags, RdfFormat 
format, string sparql, GLib.Cancellable? cancellable = null) throws Sparql.Error, GLib.Error, GLib.IOError, 
GLib.DBusError;
+                public async virtual bool deserialize_async (DeserializeFlags flags, RdfFormat format, 
string? default_graph, GLib.InputStream istream, GLib.Cancellable? cancellable = null) throws Sparql.Error, 
GLib.Error, GLib.IOError, GLib.DBusError;
                 public virtual void map_connection (string handle_name, Sparql.Connection 
service_connection);
        }
 


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