[tracker/wip/carlosg/deserializers: 1/4] libtracker-sparql: Add base deserializer object




commit acbd535972bb823221d1095180d68b4b134cbfe6
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Feb 13 11:21:44 2022 +0100

    libtracker-sparql: Add base deserializer object
    
    This object takes a GInputStream/TrackerNamespaceManager and is a
    TrackerSparqlCursor subclass in order to allow iterating across
    values. This gives a nice parallelism with serializers, which work
    the other way around.

 src/libtracker-sparql/meson.build            |   1 +
 src/libtracker-sparql/tracker-deserializer.c | 227 +++++++++++++++++++++++++++
 src/libtracker-sparql/tracker-deserializer.h |  48 ++++++
 src/libtracker-sparql/tracker-private.h      |   9 ++
 4 files changed, 285 insertions(+)
---
diff --git a/src/libtracker-sparql/meson.build b/src/libtracker-sparql/meson.build
index a55fb3826..b06898eae 100644
--- a/src/libtracker-sparql/meson.build
+++ b/src/libtracker-sparql/meson.build
@@ -22,6 +22,7 @@ libtracker_sparql_c_sources = files(
     'tracker-batch.c',
     'tracker-connection.c',
     'tracker-cursor.c',
+    'tracker-deserializer.c',
     'tracker-endpoint.c',
     'tracker-endpoint-dbus.c',
     'tracker-error.c',
diff --git a/src/libtracker-sparql/tracker-deserializer.c b/src/libtracker-sparql/tracker-deserializer.c
new file mode 100644
index 000000000..62daa2edb
--- /dev/null
+++ b/src/libtracker-sparql/tracker-deserializer.c
@@ -0,0 +1,227 @@
+/*
+ * Copyright (C) 2022, 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-deserializer.h"
+
+#include "tracker-private.h"
+
+enum {
+       PROP_0,
+       PROP_STREAM,
+       PROP_NAMESPACE_MANAGER,
+       N_PROPS
+};
+
+static GParamSpec *props[N_PROPS];
+
+typedef struct _TrackerDeserializerPrivate TrackerDeserializerPrivate;
+
+struct _TrackerDeserializerPrivate
+{
+       GInputStream *stream;
+       TrackerNamespaceManager *namespaces;
+};
+
+G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (TrackerDeserializer, tracker_deserializer,
+                                     TRACKER_TYPE_SPARQL_CURSOR)
+
+static void
+tracker_deserializer_finalize (GObject *object)
+{
+       TrackerDeserializer *deserializer = TRACKER_DESERIALIZER (object);
+       TrackerDeserializerPrivate *priv =
+               tracker_deserializer_get_instance_private (deserializer);
+
+       g_input_stream_close (priv->stream, NULL, NULL);
+       g_object_unref (priv->stream);
+       g_clear_object (&priv->namespaces);
+
+       G_OBJECT_CLASS (tracker_deserializer_parent_class)->finalize (object);
+}
+static void
+tracker_deserializer_set_property (GObject      *object,
+                                   guint         prop_id,
+                                   const GValue *value,
+                                   GParamSpec   *pspec)
+{
+       TrackerDeserializer *deserializer = TRACKER_DESERIALIZER (object);
+       TrackerDeserializerPrivate *priv =
+               tracker_deserializer_get_instance_private (deserializer);
+
+       switch (prop_id) {
+       case PROP_STREAM:
+               priv->stream = g_value_dup_object (value);
+               break;
+       case PROP_NAMESPACE_MANAGER:
+               priv->namespaces = g_value_dup_object (value);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
+}
+
+static void
+tracker_deserializer_get_property (GObject    *object,
+                                   guint       prop_id,
+                                   GValue     *value,
+                                   GParamSpec *pspec)
+{
+       TrackerDeserializer *deserializer = TRACKER_DESERIALIZER (object);
+       TrackerDeserializerPrivate *priv =
+               tracker_deserializer_get_instance_private (deserializer);
+
+       switch (prop_id) {
+       case PROP_STREAM:
+               g_value_set_object (value, priv->stream);
+               break;
+       case PROP_NAMESPACE_MANAGER:
+               g_value_set_object (value,
+                                   tracker_deserializer_get_namespaces (deserializer));
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
+}
+
+static gboolean
+tracker_deserializer_is_bound (TrackerSparqlCursor *cursor,
+                               gint                 column)
+{
+       return tracker_sparql_cursor_get_string (cursor, column, NULL) != NULL;
+}
+
+static void
+tracker_deserializer_class_init (TrackerDeserializerClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       TrackerSparqlCursorClass *cursor_class = TRACKER_SPARQL_CURSOR_CLASS (klass);
+
+       object_class->finalize = tracker_deserializer_finalize;
+       object_class->set_property = tracker_deserializer_set_property;
+       object_class->get_property = tracker_deserializer_get_property;
+
+       cursor_class->is_bound = tracker_deserializer_is_bound;
+
+       props[PROP_STREAM] =
+               g_param_spec_object ("stream",
+                                    "Stream",
+                                    "Stream",
+                                    G_TYPE_INPUT_STREAM,
+                                    G_PARAM_CONSTRUCT_ONLY |
+                                    G_PARAM_STATIC_STRINGS |
+                                    G_PARAM_READABLE |
+                                    G_PARAM_WRITABLE);
+       props[PROP_NAMESPACE_MANAGER] =
+               g_param_spec_object ("namespace-manager",
+                                    "Namespace Manager",
+                                    "Namespace Manager",
+                                    G_TYPE_INPUT_STREAM,
+                                    G_PARAM_CONSTRUCT_ONLY |
+                                    G_PARAM_STATIC_STRINGS |
+                                    G_PARAM_READABLE |
+                                    G_PARAM_WRITABLE);
+
+       g_object_class_install_properties (object_class, N_PROPS, props);
+}
+
+static void
+tracker_deserializer_init (TrackerDeserializer *deserializer)
+{
+}
+
+TrackerSparqlCursor *
+tracker_deserializer_new (GInputStream            *stream,
+                          TrackerNamespaceManager *namespaces,
+                          TrackerRdfFormat         format)
+{
+       g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
+
+       switch (format) {
+       default:
+               g_warn_if_reached ();
+               return NULL;
+       }
+
+}
+
+static TrackerRdfFormat
+pick_format_for_file (GFile *file)
+{
+       return TRACKER_RDF_FORMAT_TURTLE;
+}
+
+TrackerSparqlCursor *
+tracker_deserializer_new_for_file (GFile                    *file,
+                                   TrackerNamespaceManager  *namespaces,
+                                   GError                  **error)
+{
+       TrackerSparqlCursor *deserializer;
+       GInputStream *istream;
+       TrackerRdfFormat format;
+
+       g_return_val_if_fail (G_IS_FILE (file), NULL);
+       g_return_val_if_fail (!error || !*error, NULL);
+
+       istream = G_INPUT_STREAM (g_file_read (file, NULL, error));
+       if (!istream)
+               return NULL;
+
+       format = pick_format_for_file (file);
+       deserializer = tracker_deserializer_new (istream, namespaces, format);
+       g_object_unref (istream);
+
+       return TRACKER_SPARQL_CURSOR (deserializer);
+}
+
+gboolean
+tracker_deserializer_get_parser_location (TrackerDeserializer *deserializer,
+                                          goffset             *line_no,
+                                          goffset             *column_no)
+{
+       return TRACKER_DESERIALIZER_GET_CLASS (deserializer)->get_parser_location (deserializer,
+                                                                                  line_no,
+                                                                                  column_no);
+}
+
+GInputStream *
+tracker_deserializer_get_stream (TrackerDeserializer *deserializer)
+{
+       TrackerDeserializerPrivate *priv =
+               tracker_deserializer_get_instance_private (deserializer);
+
+       return priv->stream;
+}
+
+TrackerNamespaceManager *
+tracker_deserializer_get_namespaces (TrackerDeserializer *deserializer)
+{
+       TrackerDeserializerPrivate *priv =
+               tracker_deserializer_get_instance_private (deserializer);
+
+       if (!priv->namespaces)
+               priv->namespaces = tracker_namespace_manager_new ();
+
+       return priv->namespaces;
+}
diff --git a/src/libtracker-sparql/tracker-deserializer.h b/src/libtracker-sparql/tracker-deserializer.h
new file mode 100644
index 000000000..7e54f8961
--- /dev/null
+++ b/src/libtracker-sparql/tracker-deserializer.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2022, 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_DESERIALIZER_H
+#define TRACKER_DESERIALIZER_H
+
+#include <libtracker-sparql/tracker-sparql.h>
+
+#define TRACKER_TYPE_DESERIALIZER (tracker_deserializer_get_type())
+
+G_DECLARE_DERIVABLE_TYPE (TrackerDeserializer,
+                          tracker_deserializer,
+                          TRACKER, DESERIALIZER,
+                          TrackerSparqlCursor)
+
+TrackerSparqlCursor * tracker_deserializer_new (GInputStream            *stream,
+                                                TrackerNamespaceManager *manager,
+                                                TrackerRdfFormat         format);
+TrackerSparqlCursor * tracker_deserializer_new_for_file (GFile                    *file,
+                                                         TrackerNamespaceManager  *manager,
+                                                         GError                  **error);
+
+gboolean tracker_deserializer_get_parser_location (TrackerDeserializer *deserializer,
+                                                   goffset             *line_no,
+                                                   goffset             *column_no);
+GInputStream * tracker_deserializer_get_stream (TrackerDeserializer *deserializer);
+
+TrackerNamespaceManager * tracker_deserializer_get_namespaces (TrackerDeserializer *deserializer);
+
+#endif /* TRACKER_DESERIALIZER_H */
diff --git a/src/libtracker-sparql/tracker-private.h b/src/libtracker-sparql/tracker-private.h
index d91f5dae3..992ad269e 100644
--- a/src/libtracker-sparql/tracker-private.h
+++ b/src/libtracker-sparql/tracker-private.h
@@ -23,6 +23,7 @@
 #include <libtracker-sparql/tracker-version-generated.h>
 #include <libtracker-sparql/tracker-cursor.h>
 #include <libtracker-sparql/tracker-endpoint-dbus.h>
+#include <libtracker-sparql/tracker-deserializer.h>
 
 struct _TrackerSparqlConnectionClass
 {
@@ -279,6 +280,14 @@ struct _TrackerSerializerClass {
        GInputStreamClass parent_class;
 };
 
+struct _TrackerDeserializerClass {
+       TrackerSparqlCursorClass parent_class;
+
+       gboolean (* get_parser_location) (TrackerDeserializer *deserializer,
+                                         goffset             *line_no,
+                                         goffset             *column_no);
+};
+
 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]