[tracker/wip/sam/resource: 10/12] libtracker-sparql: Add TrackerResource class



commit 98c0deb834f96c1d9d56c06f28d39be427f9c0a7
Author: Sam Thursfield <ssssam gmail com>
Date:   Sat Mar 26 19:51:24 2016 +0000

    libtracker-sparql: Add TrackerResource class
    
    This provides a "resource-oriented" API for inserting and updating the
    database. Rather than having to generate SPARQL queries, you can use the
    TrackerResource abstraction to prepare information about a set of
    resources, then generate a SPARQL query automatically. TrackerResource
    can also serialize its data in JSON-LD format.

 src/libtracker-sparql-backend/Makefile.am |    2 +-
 src/libtracker-sparql/Makefile.am         |    2 +
 src/libtracker-sparql/tracker-resource.c  |  317 +++++++++++++++++++++++++++++
 src/libtracker-sparql/tracker-resource.h  |   67 ++++++
 src/libtracker-sparql/tracker-sparql.h    |    1 +
 5 files changed, 388 insertions(+), 1 deletions(-)
---
diff --git a/src/libtracker-sparql-backend/Makefile.am b/src/libtracker-sparql-backend/Makefile.am
index 27356a5..2f0b14d 100644
--- a/src/libtracker-sparql-backend/Makefile.am
+++ b/src/libtracker-sparql-backend/Makefile.am
@@ -25,5 +25,5 @@ libtracker_sparql_ TRACKER_API_VERSION@_la_LIBADD =    \
 
 libtracker_sparql_ TRACKER_API_VERSION@_la_LDFLAGS =   \
        -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) \
-       -export-symbols-regex '^tracker_sparql_(connection|cursor|builder|escape|error|value|get)_.*'
+       -export-symbols-regex 
'^(tracker_sparql_(connection|cursor|builder|escape|error|value|get)_.*|tracker_resource_*)'
 
diff --git a/src/libtracker-sparql/Makefile.am b/src/libtracker-sparql/Makefile.am
index 48ed4ad..94985e8 100644
--- a/src/libtracker-sparql/Makefile.am
+++ b/src/libtracker-sparql/Makefile.am
@@ -22,6 +22,8 @@ libtracker_sparql_la_SOURCES =   \
        tracker-builder.vala                           \
        tracker-connection.vala                        \
        tracker-cursor.vala                            \
+       tracker-resource.c                             \
+       tracker-resource.h                             \
        tracker-utils.vala                             \
        tracker-uri.c                                  \
        tracker-ontologies.h \
diff --git a/src/libtracker-sparql/tracker-resource.c b/src/libtracker-sparql/tracker-resource.c
new file mode 100644
index 0000000..90a9d69
--- /dev/null
+++ b/src/libtracker-sparql/tracker-resource.c
@@ -0,0 +1,317 @@
+/*
+ * Copyright (C) 2016, Sam Thursfield <sam afuera me uk>
+ *
+ * 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.
+ */
+
+#include <glib.h>
+
+#include <string.h>
+
+#include "config.h"
+#include <tracker-resource.h>
+
+typedef struct {
+       char *identifier;
+       GHashTable *properties;
+       GHashTable *overwrite;
+} TrackerResourcePrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (TrackerResource, tracker_resource, G_TYPE_OBJECT);
+#define GET_PRIVATE(object)  (tracker_resource_get_instance_private (object))
+
+/**
+ * SECTION: tracker-resource
+ * @short_description: Represenets a single Tracker source
+ * @title: TrackerResource
+ * @stability: Stable
+ * @include: tracker-resource.h
+ *
+ * <para>
+ * #TrackerResource keeps track of a set of properties for a given resource.
+ * The resulting data can be serialized in several ways.
+ * </para>
+ */
+
+/* TrackerUri is introduced so that the SPARQL generation can tell
+   what to emit -- a <uri> or "string".
+ */
+GType
+tracker_uri_get_type (void)
+{
+       static volatile gsize g_define_type_id__volatile = 0;
+       if (g_once_init_enter (&g_define_type_id__volatile)) {
+               GTypeInfo info = { 0, NULL, NULL, NULL, NULL, NULL, 0, 0, NULL, NULL, };
+               GType g_define_type_id = g_type_register_static (G_TYPE_STRING,
+                                                                g_intern_static_string ("TrackerUri"),
+                                                                &info,
+                                                                0);
+               g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
+       }
+       return g_define_type_id__volatile;
+}
+
+/**
+ * TrackerResource:
+ *
+ * The <structname>TrackerResource</structname> object represents information
+ * about a given resource.
+ */
+
+static void finalize (GObject *object);
+
+static void
+tracker_resource_class_init (TrackerResourceClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->finalize     = finalize;
+
+       g_type_class_add_private (object_class, sizeof (TrackerResourcePrivate));
+}
+
+/* Destroy-notify function for the values stored in the hash table. */
+static void
+free_value (GValue *value)
+{
+       g_value_unset (value);
+       g_free (value);
+}
+
+static void
+tracker_resource_init (TrackerResource *resource)
+{
+       TrackerResourcePrivate *priv = GET_PRIVATE(resource);
+
+       /* Values of properties */
+       priv->properties = g_hash_table_new_full (
+           g_str_hash,
+           g_str_equal,
+           g_free,
+           (GDestroyNotify) free_value);
+
+       /* TRUE for any property where we should delete any existing values. */
+       priv->overwrite = g_hash_table_new_full (
+           g_str_hash,
+           g_str_equal,
+           g_free,
+           NULL);
+}
+
+static void
+finalize (GObject *object)
+{
+       TrackerResourcePrivate *priv;
+
+       priv = GET_PRIVATE (TRACKER_RESOURCE (object));
+
+       g_hash_table_unref (priv->overwrite);
+       g_hash_table_unref (priv->properties);
+
+       (G_OBJECT_CLASS (tracker_resource_parent_class)->finalize) (object);
+}
+
+
+/**
+ * tracker_resource_new()
+ *
+ * Creates a TrackerResource instance.
+ *
+ * Returns: a newly created #TrackerResource. Free with g_object_unref() when done
+ *
+ * Since: 1.10
+ */
+TrackerResource *
+tracker_resource_new (const char *identifier)
+{
+       return g_object_new (TRACKER_TYPE_RESOURCE, NULL);
+}
+
+/* Difference between 'set' and 'add': when generating a SPARQL update, the
+ * setter will generate a corresponding DELETE, the adder will not. The setter
+ * will also overwrite existing values in the Resource object, while the adder
+ * will make a list.
+ */
+
+void
+tracker_resource_set_property_gvalue (TrackerResource *self,
+                                      const char *property_uri,
+                                      GValue *value)
+{
+       TrackerResourcePrivate *priv = GET_PRIVATE (self);
+       GValue *our_value;
+
+       our_value = g_slice_new (GValue);
+       g_value_copy (value, our_value);
+
+       g_hash_table_insert (priv->properties, g_strdup (property_uri), our_value);
+
+       g_hash_table_insert (priv->overwrite, g_strdup (property_uri), GINT_TO_POINTER (TRUE));
+};
+
+#define SET_PROPERTY_FOR_GTYPE(name, ctype, gtype, set_function)   \
+       void name (TrackerResource *self,                              \
+                  const char *property_uri,                           \
+                  ctype value)                                        \
+       {                                                              \
+               TrackerResourcePrivate *priv = GET_PRIVATE (self);         \
+               GValue *our_value;                                         \
+                                                                          \
+               our_value = g_slice_new (GValue);                          \
+               g_value_init (our_value, gtype);                           \
+               set_function (our_value, value);                           \
+                                                                          \
+               g_hash_table_insert (priv->properties,                    \
+                                     g_strdup (property_uri),             \
+                                     our_value);                          \
+                                                                          \
+               g_hash_table_insert (priv->overwrite,                     \
+                                     g_strdup (property_uri),             \
+                                     GINT_TO_POINTER (TRUE));             \
+       };
+
+SET_PROPERTY_FOR_GTYPE (tracker_resource_set_property_double, double, G_TYPE_DOUBLE, g_value_set_double);
+SET_PROPERTY_FOR_GTYPE (tracker_resource_set_property_int, int, G_TYPE_INT, g_value_set_int);
+SET_PROPERTY_FOR_GTYPE (tracker_resource_set_property_int64, gint64, G_TYPE_INT64, g_value_set_int64);
+SET_PROPERTY_FOR_GTYPE (tracker_resource_set_property_resource, TrackerResource *, TRACKER_TYPE_RESOURCE, 
g_value_set_object);
+SET_PROPERTY_FOR_GTYPE (tracker_resource_set_property_string, const char *, G_TYPE_STRING, 
g_value_set_string);
+SET_PROPERTY_FOR_GTYPE (tracker_resource_set_property_uri, const char *, TRACKER_TYPE_URI, 
g_value_set_string);
+
+void
+tracker_resource_add_property_gvalue (TrackerResource *self,
+                                      const char *property_uri,
+                                      GValue *value)
+{
+       TrackerResourcePrivate *priv = GET_PRIVATE (self);
+       GValue *existing_value, *array_value, *our_value;
+       GPtrArray *array;
+
+       existing_value = g_hash_table_lookup (priv->properties, property_uri);
+
+       if (existing_value && G_VALUE_HOLDS (existing_value, G_TYPE_PTR_ARRAY)) {
+               array = g_value_get_boxed (existing_value);
+               array_value = existing_value;
+       } else {
+               array = g_ptr_array_new_with_free_func ((GDestroyNotify)free_value);
+               g_value_set_boxed (array_value, array);
+
+               if (existing_value) {
+                       g_ptr_array_add (array, existing_value);
+               }
+       }
+
+       our_value = g_slice_new (GValue);
+       g_value_copy (value, our_value);
+
+       g_ptr_array_add (array, our_value);
+
+       if (array_value != existing_value) {
+               g_hash_table_insert (priv->properties, g_strdup (property_uri), array_value);
+       }
+};
+
+#define ADD_PROPERTY_FOR_GTYPE(name, ctype, gtype, set_function)        \
+       void name (TrackerResource *self,                                   \
+                  const char *property_uri,                                \
+                  ctype value)                                             \
+       {                                                                   \
+               TrackerResourcePrivate *priv = GET_PRIVATE (self);              \
+               GValue *existing_value, *array_value, *our_value;               \
+               GPtrArray *array;                                               \
+                                                                               \
+               existing_value = g_hash_table_lookup (priv->properties,         \
+                                                     property_uri);            \
+                                                                               \
+               if (existing_value && G_VALUE_HOLDS (existing_value,            \
+                                                    G_TYPE_PTR_ARRAY)) {       \
+                       array = g_value_get_boxed (existing_value);                 \
+                       array_value = existing_value;                               \
+               } else {                                                        \
+                       array = g_ptr_array_new_with_free_func (                    \
+                           (GDestroyNotify)free_value);                            \
+                       g_value_set_boxed (array_value, array);                     \
+                                                                                   \
+                       if (existing_value) {                                       \
+                               g_ptr_array_add (array, existing_value);                \
+                       }                                                           \
+               }                                                               \
+                                                                               \
+               our_value = g_slice_new (GValue);                               \
+               g_value_init (our_value, gtype);                                \
+               set_function (our_value, value);                                \
+                                                                               \
+               g_ptr_array_add (array, our_value);                             \
+                                                                               \
+               if (array_value != existing_value) {                            \
+                       g_hash_table_insert (priv->properties,                      \
+                                            g_strdup (property_uri), array_value); \
+               }                                                               \
+       };
+
+ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_property_double, double, G_TYPE_DOUBLE, g_value_set_double);
+ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_property_int, int, G_TYPE_INT, g_value_set_int);
+ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_property_int64, gint64, G_TYPE_INT64, g_value_set_int64);
+ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_property_resource, TrackerResource *, TRACKER_TYPE_RESOURCE, 
g_value_set_object);
+ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_property_string, const char *, G_TYPE_STRING, 
g_value_set_string);
+ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_property_uri, const char *, TRACKER_TYPE_URI, 
g_value_set_string);
+
+
+#define GET_PROPERTY_FOR_GTYPE(name, ctype, gtype, get_function, no_value)  \
+       ctype name (TrackerResource *self,                                      \
+                   const char *property_uri)                                   \
+       {                                                                       \
+               TrackerResourcePrivate *priv = GET_PRIVATE (self);                  \
+               GValue *value;                                                      \
+                                                                                   \
+               value = g_hash_table_lookup (priv->properties, property_uri);       \
+                                                                                   \
+               if (value == NULL)                                                  \
+                   return no_value;                                                \
+                                                                                   \
+               return get_function (value);                                        \
+       };
+
+GET_PROPERTY_FOR_GTYPE (tracker_resource_get_property_double, double, G_TYPE_DOUBLE, g_value_get_double, 
0.0);
+GET_PROPERTY_FOR_GTYPE (tracker_resource_get_property_int, int, G_TYPE_INT, g_value_get_int, 0);
+GET_PROPERTY_FOR_GTYPE (tracker_resource_get_property_int64, gint64, G_TYPE_INT64, g_value_get_int64, 0);
+GET_PROPERTY_FOR_GTYPE (tracker_resource_get_property_resource, TrackerResource *, TRACKER_TYPE_RESOURCE, 
g_value_get_object, NULL);
+GET_PROPERTY_FOR_GTYPE (tracker_resource_get_property_string, const char *, G_TYPE_STRING, 
g_value_get_string, NULL);
+GET_PROPERTY_FOR_GTYPE (tracker_resource_get_property_uri, const char *, TRACKER_TYPE_URI, 
g_value_get_string, NULL);
+
+gint
+tracker_resource_identifier_compare_func (TrackerResource *resource,
+                                          const char *identifier)
+{
+       TrackerResourcePrivate *priv = GET_PRIVATE (resource);
+
+       return strcmp (priv->identifier, identifier);
+}
+
+GString *
+tracker_resource_print_jsonld_string (TrackerResource *resource)
+{
+       GString *string;
+
+       /* How will this work then? */
+       /* Ignore formatting for now */
+       /*{
+               @context: {
+                       "foo": "prefix"
+               },
+               "nie:title": title
+       }*/
+       return string;
+}
diff --git a/src/libtracker-sparql/tracker-resource.h b/src/libtracker-sparql/tracker-resource.h
new file mode 100644
index 0000000..1318bc9
--- /dev/null
+++ b/src/libtracker-sparql/tracker-resource.h
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2016, Sam Thursfield <sam afuera me uk>
+ *
+ * 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.
+ */
+
+#ifndef __LIBTRACKER_RESOURCE_H__
+#define __LIBTRACKER_RESOURCE_H__
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define TRACKER_TYPE_URI tracker_uri_get_type()
+
+#define TRACKER_TYPE_RESOURCE tracker_resource_get_type()
+G_DECLARE_DERIVABLE_TYPE (TrackerResource, tracker_resource, TRACKER, RESOURCE, GObject)
+
+struct _TrackerResourceClass
+{
+       GObjectClass parent_class;
+};
+
+TrackerResource *tracker_resource_new (const char *identifier);
+
+void tracker_resource_set_property_gvalue (TrackerResource *self, const char *property_uri, GValue *value);
+void tracker_resource_set_property_double (TrackerResource *self, const char *property_uri, double value);
+void tracker_resource_set_property_int (TrackerResource *self, const char *property_uri, int value);
+void tracker_resource_set_property_int64 (TrackerResource *self, const char *property_uri, gint64 value);
+void tracker_resource_set_property_resource (TrackerResource *self, const char *property_uri, 
TrackerResource *resource);
+void tracker_resource_set_property_string (TrackerResource *self, const char *property_uri, const char 
*value);
+void tracker_resource_set_property_uri (TrackerResource *self, const char *property_uri, const char *value);
+
+void tracker_resource_add_property_gvalue (TrackerResource *self, const char *property_uri, GValue *value);
+void tracker_resource_add_property_double (TrackerResource *self, const char *property_uri, double value);
+void tracker_resource_add_property_int (TrackerResource *self, const char *property_uri, int value);
+void tracker_resource_add_property_int64 (TrackerResource *self, const char *property_uri, gint64 value);
+void tracker_resource_add_property_resource (TrackerResource *self, const char *property_uri, 
TrackerResource *resource);
+void tracker_resource_add_property_string (TrackerResource *self, const char *property_uri, const char 
*value);
+void tracker_resource_add_property_uri (TrackerResource *self, const char *property_uri, const char *value);
+
+double tracker_resource_get_property_double (TrackerResource *self, const char *property_uri);
+int tracker_resource_get_property_int (TrackerResource *self, const char *property_uri);
+gint64 tracker_resource_get_property_int64 (TrackerResource *self, const char *property_uri);
+TrackerResource *tracker_resource_get_property_resource (TrackerResource *self, const char *property_uri);
+const char *tracker_resource_get_property_string (TrackerResource *self, const char *property_uri);
+const char *tracker_resource_get_property_uri (TrackerResource *self, const char *property_uri);
+
+gint tracker_resource_identifier_compare_func (TrackerResource *resource, const char *identifier);
+
+G_END_DECLS
+
+#endif /* __LIBTRACKER_RESOURCE_H__ */
+
diff --git a/src/libtracker-sparql/tracker-sparql.h b/src/libtracker-sparql/tracker-sparql.h
index 8f042d4..8b4d9ae 100644
--- a/src/libtracker-sparql/tracker-sparql.h
+++ b/src/libtracker-sparql/tracker-sparql.h
@@ -24,6 +24,7 @@
 
 #include <libtracker-sparql/tracker-version.h>
 #include <libtracker-sparql/tracker-ontologies.h>
+#include <libtracker-sparql/tracker-resource.h>
 #include <libtracker-sparql/tracker-generated.h>
 
 #undef __LIBTRACKER_SPARQL_INSIDE__


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