[tracker/wip/carlosg/remote-namespace-manager: 3/4] libtracker-sparql: Implement get_namespace_manager() in remote connections
- From: Carlos Garnacho <carlosg src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker/wip/carlosg/remote-namespace-manager: 3/4] libtracker-sparql: Implement get_namespace_manager() in remote connections
- Date: Sun, 1 May 2022 18:49:34 +0000 (UTC)
commit 55a4b654226d7791af8d47f5d8cf5a277442cfa0
Author: Carlos Garnacho <carlosg gnome org>
Date: Sun Apr 17 00:00:02 2022 +0200
libtracker-sparql: Implement get_namespace_manager() in remote connections
Add a TrackerNamespaceManager implementation that figures out the relevant
prefixes from a DESCRIBE query on an empty URI, since the produced Turtle will
contain the prefixes relevant to the connection, we can populate the
namespace manager from there.
src/libtracker-sparql/remote/meson.build | 1 +
.../remote/tracker-remote-namespaces.c | 194 +++++++++++++++++++++
.../remote/tracker-remote-namespaces.h | 42 +++++
src/libtracker-sparql/remote/tracker-remote.vala | 19 ++
src/libtracker-sparql/remote/tracker-remote.vapi | 5 +
src/libtracker-sparql/tracker-namespace-manager.c | 4 -
src/libtracker-sparql/tracker-private.h | 4 +
7 files changed, 265 insertions(+), 4 deletions(-)
---
diff --git a/src/libtracker-sparql/remote/meson.build b/src/libtracker-sparql/remote/meson.build
index 3d9e0c653..4f732e81c 100644
--- a/src/libtracker-sparql/remote/meson.build
+++ b/src/libtracker-sparql/remote/meson.build
@@ -1,6 +1,7 @@
remote_files = files(
'tracker-http.c',
'tracker-remote-statement.c',
+ 'tracker-remote-namespaces.c',
'tracker-remote.vapi',
'tracker-json-cursor.vala',
'tracker-xml-cursor.vala',
diff --git a/src/libtracker-sparql/remote/tracker-remote-namespaces.c
b/src/libtracker-sparql/remote/tracker-remote-namespaces.c
new file mode 100644
index 000000000..44603b8d1
--- /dev/null
+++ b/src/libtracker-sparql/remote/tracker-remote-namespaces.c
@@ -0,0 +1,194 @@
+/*
+ * 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>
+ */
+
+#include "config.h"
+
+#include "tracker-remote-namespaces.h"
+#include "tracker-private.h"
+
+#include <libtracker-sparql/tracker-deserializer.h>
+
+struct _TrackerRemoteNamespaceManager {
+ TrackerNamespaceManager parent_instance;
+ TrackerSparqlConnection *conn;
+};
+
+enum {
+ PROP_0,
+ PROP_CONNECTION,
+ N_PROPS
+};
+
+static GParamSpec *props[N_PROPS];
+
+static void
+tracker_remote_namespace_manager_async_initable_init (GAsyncInitableIface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (TrackerRemoteNamespaceManager,
+ tracker_remote_namespace_manager,
+ TRACKER_TYPE_NAMESPACE_MANAGER,
+ G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE,
+ tracker_remote_namespace_manager_async_initable_init))
+
+static void
+serialize_cb (GObject *source,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ GTask *task = user_data;
+ TrackerNamespaceManager *manager =
+ TRACKER_NAMESPACE_MANAGER (g_task_get_source_object (task));
+ TrackerRemoteNamespaceManager *remote_manager =
+ TRACKER_REMOTE_NAMESPACE_MANAGER (manager);
+ TrackerSparqlCursor *deserializer;
+ GInputStream *istream;
+ GError *error = NULL;
+
+ istream = tracker_sparql_connection_serialize_finish (remote_manager->conn,
+ res,
+ &error);
+ if (!istream) {
+ g_task_return_error (task, error);
+ g_object_unref (task);
+ return;
+ }
+
+ deserializer = tracker_deserializer_new (istream, manager, TRACKER_SERIALIZER_FORMAT_TTL);
+ g_object_unref (istream);
+
+ /* We don't care much about the content, just to have the
+ * prefixes parsed.
+ */
+ tracker_sparql_cursor_next (deserializer, NULL, NULL);
+ g_object_unref (deserializer);
+
+ g_task_return_boolean (task, TRUE);
+ g_object_unref (task);
+}
+
+static void
+tracker_remote_namespace_manager_init_async (GAsyncInitable *initable,
+ int io_priority,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ TrackerRemoteNamespaceManager *manager =
+ TRACKER_REMOTE_NAMESPACE_MANAGER (initable);
+ GTask *task;
+
+ task = g_task_new (manager, NULL, callback, user_data);
+
+ /* Make a query for an empty URI, we only want the namespaces */
+ tracker_sparql_connection_serialize_async (manager->conn,
+ TRACKER_SERIALIZE_FLAGS_NONE,
+ TRACKER_RDF_FORMAT_TURTLE,
+ "DESCRIBE <>",
+ NULL,
+ serialize_cb,
+ task);
+}
+
+static gboolean
+tracker_remote_namespace_manager_init_finish (GAsyncInitable *initable,
+ GAsyncResult *res,
+ GError **error)
+{
+ return g_task_propagate_boolean (G_TASK (res), error);
+}
+
+static void
+tracker_remote_namespace_manager_async_initable_init (GAsyncInitableIface *iface)
+{
+ iface->init_async = tracker_remote_namespace_manager_init_async;
+ iface->init_finish = tracker_remote_namespace_manager_init_finish;
+}
+
+static void
+tracker_remote_namespace_manager_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ TrackerRemoteNamespaceManager *manager =
+ TRACKER_REMOTE_NAMESPACE_MANAGER (object);
+
+ switch (prop_id) {
+ case PROP_CONNECTION:
+ manager->conn = g_value_get_object (value);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+tracker_remote_namespace_manager_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ TrackerRemoteNamespaceManager *manager =
+ TRACKER_REMOTE_NAMESPACE_MANAGER (object);
+
+ switch (prop_id) {
+ case PROP_CONNECTION:
+ g_value_set_object (value, manager->conn);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+tracker_remote_namespace_manager_class_init (TrackerRemoteNamespaceManagerClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->set_property = tracker_remote_namespace_manager_set_property;
+ object_class->get_property = tracker_remote_namespace_manager_get_property;
+
+ props[PROP_CONNECTION] =
+ g_param_spec_object ("connection",
+ "Connection",
+ "Connection",
+ TRACKER_TYPE_SPARQL_CONNECTION,
+ G_PARAM_READWRITE |
+ G_PARAM_CONSTRUCT_ONLY);
+
+ g_object_class_install_properties (object_class, N_PROPS, props);
+
+}
+
+static void
+tracker_remote_namespace_manager_init (TrackerRemoteNamespaceManager *manager)
+{
+}
+
+TrackerNamespaceManager *
+tracker_remote_namespace_manager_new (TrackerSparqlConnection *conn)
+{
+ return g_object_new (TRACKER_TYPE_REMOTE_NAMESPACE_MANAGER,
+ "connection", conn,
+ NULL);
+}
diff --git a/src/libtracker-sparql/remote/tracker-remote-namespaces.h
b/src/libtracker-sparql/remote/tracker-remote-namespaces.h
new file mode 100644
index 000000000..443691eff
--- /dev/null
+++ b/src/libtracker-sparql/remote/tracker-remote-namespaces.h
@@ -0,0 +1,42 @@
+/*
+ * 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>
+ */
+
+#ifndef __TRACKER_REMOTE_NAMESPACES_H__
+#define __TRACKER_REMOTE_NAMESPACES_H__
+
+#include <libtracker-sparql/tracker-sparql.h>
+
+#define TRACKER_TYPE_REMOTE_NAMESPACE_MANAGER (tracker_remote_namespace_manager_get_type ())
+G_DECLARE_FINAL_TYPE (TrackerRemoteNamespaceManager,
+ tracker_remote_namespace_manager,
+ TRACKER, REMOTE_NAMESPACE_MANAGER,
+ TrackerNamespaceManager)
+
+TrackerNamespaceManager * tracker_remote_namespace_manager_new (TrackerSparqlConnection *conn);
+
+void tracker_remote_namespace_manager_new_async (TrackerSparqlConnection *conn,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+
+TrackerNamespaceManager * tracker_remote_namespace_manager_new_finish (GAsyncResult *res,
+ GError **error);
+
+#endif /* __TRACKER_REMOTE_NAMESPACES_H__ */
diff --git a/src/libtracker-sparql/remote/tracker-remote.vala
b/src/libtracker-sparql/remote/tracker-remote.vala
index aef5325c6..88d131f8b 100644
--- a/src/libtracker-sparql/remote/tracker-remote.vala
+++ b/src/libtracker-sparql/remote/tracker-remote.vala
@@ -21,6 +21,7 @@
public class Tracker.Remote.Connection : Tracker.Sparql.Connection {
internal HttpClient _client;
internal string _base_uri;
+ internal NamespaceManager _namespaces;
public Connection (string base_uri) {
Object ();
@@ -83,4 +84,22 @@ public class Tracker.Remote.Connection : Tracker.Sparql.Connection {
SerializerFormat unused;
return yield _client.send_message_async (_base_uri, sparql, formats, cancellable, out unused);
}
+
+ public override Tracker.NamespaceManager? get_namespace_manager () {
+ if (_namespaces == null) {
+ _namespaces = new Tracker.Remote.NamespaceManager (this);
+ var loop = new MainLoop (null, false);
+ _namespaces.init_async.begin (GLib.Priority.DEFAULT, null, (o, res) => {
+ try {
+ _namespaces.init_async.end(res);
+ loop.quit();
+ } catch (Error e) {
+ warning ("Failed to initalized namespaces: " + e.message);
+ }
+ });
+ loop.run ();
+ }
+
+ return _namespaces;
+ }
}
diff --git a/src/libtracker-sparql/remote/tracker-remote.vapi
b/src/libtracker-sparql/remote/tracker-remote.vapi
index d266470e1..70bafc58e 100644
--- a/src/libtracker-sparql/remote/tracker-remote.vapi
+++ b/src/libtracker-sparql/remote/tracker-remote.vapi
@@ -17,5 +17,10 @@ namespace Tracker {
XML,
TTL,
TRIG,
+ }
+
+ [CCode (cheader_filename = "libtracker-sparql/remote/tracker-remote-namespaces.h")]
+ class Remote.NamespaceManager : Tracker.NamespaceManager, GLib.AsyncInitable {
+ public NamespaceManager (Sparql.Connection conn);
}
}
diff --git a/src/libtracker-sparql/tracker-namespace-manager.c
b/src/libtracker-sparql/tracker-namespace-manager.c
index 68935c9c6..377feab81 100644
--- a/src/libtracker-sparql/tracker-namespace-manager.c
+++ b/src/libtracker-sparql/tracker-namespace-manager.c
@@ -30,10 +30,6 @@
#define MAX_PREFIX_LENGTH 100
-struct _TrackerNamespaceManager {
- GObject parent;
-};
-
typedef struct {
GHashTable *prefix_to_namespace;
GHashTable *namespace_to_prefix;
diff --git a/src/libtracker-sparql/tracker-private.h b/src/libtracker-sparql/tracker-private.h
index 5eec82e7c..39b893757 100644
--- a/src/libtracker-sparql/tracker-private.h
+++ b/src/libtracker-sparql/tracker-private.h
@@ -289,6 +289,10 @@ struct _TrackerDeserializerClass {
goffset *column_no);
};
+struct _TrackerNamespaceManager {
+ GObject parent;
+};
+
gboolean
tracker_sparql_connection_lookup_dbus_service (TrackerSparqlConnection *connection,
const gchar *dbus_name,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]