[tracker/wip/carlosg/bus-connection-methods: 3/4] libtracker-sparql: Implement TrackerBatch for bus connection




commit da0713e8e157e446b1bfcc3c336106bf6fd2e479
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Mar 24 12:16:14 2021 +0100

    libtracker-sparql: Implement TrackerBatch for bus connection
    
    This ATM uses the UpdateArray DBus method underneath, thus resources
    are converted to SPARQL update strings before sending. It could be
    argued that variant serialization is more compact than SPARQL
    updates, but I am not sure if the difference in payload is the bigger
    factor to take into account.

 src/libtracker-sparql/bus/meson.build            |  1 +
 src/libtracker-sparql/bus/tracker-bus-batch.vala | 63 ++++++++++++++++++++++++
 src/libtracker-sparql/bus/tracker-bus.vala       |  7 +++
 3 files changed, 71 insertions(+)
---
diff --git a/src/libtracker-sparql/bus/meson.build b/src/libtracker-sparql/bus/meson.build
index 54629e09a..53d93c057 100644
--- a/src/libtracker-sparql/bus/meson.build
+++ b/src/libtracker-sparql/bus/meson.build
@@ -1,6 +1,7 @@
 libtracker_bus = static_library('tracker-bus',
     'tracker-bus.vala',
     'tracker-namespace.vala',
+    'tracker-bus-batch.vala',
     'tracker-bus-fd-cursor.vala',
     'tracker-bus-statement.vala',
     '../../libtracker-common/libtracker-common.vapi',
diff --git a/src/libtracker-sparql/bus/tracker-bus-batch.vala 
b/src/libtracker-sparql/bus/tracker-bus-batch.vala
new file mode 100644
index 000000000..1993775b3
--- /dev/null
+++ b/src/libtracker-sparql/bus/tracker-bus-batch.vala
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2021, Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ *
+ * Author: Carlos Garnacho <carlosg gnome org>
+ */
+
+public class Tracker.Bus.Batch : Tracker.Batch {
+       private DBusConnection bus;
+       private string dbus_name;
+       private string object_path;
+       private string[] updates;
+
+       public Batch (DBusConnection bus, string dbus_name, string object_path) {
+               Object ();
+               this.bus = bus;
+               this.dbus_name = dbus_name;
+               this.object_path = object_path;
+       }
+
+       public override void add_sparql (string sparql) {
+               updates += sparql;
+       }
+
+       public override void add_resource (string? graph, Resource resource) {
+               var namespaces = this.connection.get_namespace_manager ();
+               var sparql = resource.print_sparql_update (namespaces, graph);
+               updates += sparql;
+       }
+
+       public override bool execute (GLib.Cancellable? cancellable) throws Sparql.Error, GLib.Error, 
GLib.IOError, GLib.DBusError {
+               // use separate main context for sync operation
+               var context = new MainContext ();
+               var loop = new MainLoop (context, false);
+               context.push_thread_default ();
+               AsyncResult async_res = null;
+               execute_async.begin (cancellable, (o, res) => {
+                       async_res = res;
+                       loop.quit ();
+               });
+               loop.run ();
+               context.pop_thread_default ();
+               return execute_async.end (async_res);
+       }
+
+       public async override bool execute_async (GLib.Cancellable? cancellable) throws Sparql.Error, 
GLib.Error, GLib.IOError, GLib.DBusError {
+               return yield Tracker.Bus.Connection.perform_update_array (bus, dbus_name, object_path, 
updates, cancellable);
+       }
+}
diff --git a/src/libtracker-sparql/bus/tracker-bus.vala b/src/libtracker-sparql/bus/tracker-bus.vala
index 5ba41d4e3..65ce9cff5 100644
--- a/src/libtracker-sparql/bus/tracker-bus.vala
+++ b/src/libtracker-sparql/bus/tracker-bus.vala
@@ -369,4 +369,11 @@ public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
        public override Tracker.NamespaceManager? get_namespace_manager () {
                return namespaces;
        }
+
+       public override Tracker.Batch? create_batch () {
+               var batch = (Tracker.Batch) Object.new (typeof (Tracker.Bus.Batch),
+                                                       "connection", this,
+                                                       null);
+               return batch;
+       }
 }


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