[tracker/wip/sam/resource: 2/22] libtracker-sparql: Add TrackerResource class
- From: Sam Thursfield <sthursfield src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker/wip/sam/resource: 2/22] libtracker-sparql: Add TrackerResource class
- Date: Fri, 8 Apr 2016 17:17:44 +0000 (UTC)
commit 2a205426f86c4c17eca4e9d3389920587a717d37
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 | 3 +
src/libtracker-sparql/tracker-resource.c | 522 +++++++++++++++++++++++
src/libtracker-sparql/tracker-resource.h | 67 +++
src/libtracker-sparql/tracker-sparql.h | 1 +
src/libtracker-sparql/tracker-uri.h | 2 +
tests/libtracker-sparql/Makefile.am | 3 +
tests/libtracker-sparql/tracker-resource-test.c | 147 +++++++
8 files changed, 746 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..4423f3f 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 \
@@ -49,6 +51,7 @@ tracker-sparql-$(TRACKER_API_VERSION).vapi: tracker-sparql.vapi
libtracker_sparqlinclude_HEADERS = \
$(vala_header) \
tracker-ontologies.h \
+ tracker-resource.h \
tracker-sparql.h \
tracker-version.h
diff --git a/src/libtracker-sparql/tracker-resource.c b/src/libtracker-sparql/tracker-resource.c
new file mode 100644
index 0000000..0335386
--- /dev/null
+++ b/src/libtracker-sparql/tracker-resource.c
@@ -0,0 +1,522 @@
+/*
+ * 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-uri.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>
+ */
+
+static char *
+generate_blank_node_identifier (void)
+{
+ static gint64 counter = 0;
+
+ return g_strdup_printf("_:%" G_GINT64_FORMAT, counter);
+}
+
+/**
+ * TrackerResource:
+ *
+ * The <structname>TrackerResource</structname> object represents information
+ * about a given resource.
+ */
+
+enum {
+ PROP_0,
+
+ PROP_IDENTIFIER,
+};
+
+static void finalize (GObject *object);
+static void get_property (GObject *object,
+ guint param_id,
+ GValue *value,
+ GParamSpec *pspec);
+static void set_property (GObject *object,
+ guint param_id,
+ const GValue *value,
+ GParamSpec *pspec);
+
+
+static void
+tracker_resource_class_init (TrackerResourceClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = finalize;
+ object_class->get_property = get_property;
+ object_class->set_property = set_property;
+
+ g_object_class_install_property (object_class,
+ PROP_IDENTIFIER,
+ g_param_spec_string ("identifier",
+ "Identifier",
+ "Identifier",
+ NULL,
+ G_PARAM_READWRITE));
+}
+
+/* Destroy-notify function for the values stored in the hash table. */
+static void
+free_value (GValue *value)
+{
+ g_value_unset (value);
+ g_slice_free (GValue, 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);
+}
+
+static void
+get_property (GObject *object,
+ guint param_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ TrackerResourcePrivate *priv;
+
+ priv = GET_PRIVATE (TRACKER_RESOURCE (object));
+
+ switch (param_id) {
+ case PROP_IDENTIFIER:
+ g_value_set_string (value, priv->identifier);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+ break;
+ }
+}
+
+static void
+set_property (GObject *object,
+ guint param_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ TrackerResourcePrivate *priv;
+
+ priv = GET_PRIVATE (TRACKER_RESOURCE (object));
+
+ switch (param_id) {
+ case PROP_IDENTIFIER:
+ if (priv->identifier) {
+ g_free (priv->identifier);
+ }
+ if (g_value_get_string (value) == NULL) {
+ /* We take NULL to mean "this is a blank node", and generate a
+ * unique blank node identifier right away. This is easier than
+ * leaving it NULL and trying to generate a blank node ID at
+ * serialization time, and it means that the serialization
+ * output is stable when called multiple times.
+ */
+ priv->identifier = generate_blank_node_identifier ();
+ } else {
+ priv->identifier = g_value_dup_string (value);
+ }
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+ break;
+ }
+}
+
+/**
+ * tracker_resource_new:
+ * @identifier: A string containing a URI
+ *
+ * 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)
+{
+ TrackerResource *resource;
+ GValue value = G_VALUE_INIT;
+
+ resource = g_object_new (TRACKER_TYPE_RESOURCE, NULL);
+
+ /* The identifier may be NULL, so I don't think we can pass it in the va_list
+ * that g_object_new() accepts.
+ */
+ g_value_init (&value, G_TYPE_STRING);
+ g_value_set_string (&value, identifier);
+ g_object_set_property (G_OBJECT (resource), "identifier", &value);
+ g_value_unset (&value);
+
+ return resource;
+}
+
+/* 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.
+ */
+
+/**
+ * tracker_resource_set_gvalue:
+ * @self: the #TrackerResource
+ * @property_uri: a string identifying the property to set
+ * @value: an initialised #GValue
+ *
+ * State that the only value for the given property is 'value'. Any existing
+ * values for 'property' will be removed.
+ *
+ * When serialising to SPARQL, any properties that were set with this function
+ * will get a corresponding DELETE statement to remove any existing values in
+ * the database.
+ *
+ * You can pass any kind of GValue for @value, but serialization functions will
+ * normally only be able to serialize URIs/relationships and fundamental value
+ * types (string, int, etc.).
+ *
+ * Since: 1.10
+ */
+void
+tracker_resource_set_gvalue (TrackerResource *self,
+ const char *property_uri,
+ GValue *value)
+{
+ TrackerResourcePrivate *priv = GET_PRIVATE (self);
+ GValue *our_value;
+
+ our_value = g_slice_new0 (GValue);
+ g_value_init (our_value, G_VALUE_TYPE (value));
+ 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_new0 (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_double, double, G_TYPE_DOUBLE, g_value_set_double);
+SET_PROPERTY_FOR_GTYPE (tracker_resource_set_int, int, G_TYPE_INT, g_value_set_int);
+SET_PROPERTY_FOR_GTYPE (tracker_resource_set_int64, gint64, G_TYPE_INT64, g_value_set_int64);
+SET_PROPERTY_FOR_GTYPE (tracker_resource_set_relation, TrackerResource *, TRACKER_TYPE_RESOURCE,
g_value_set_object);
+SET_PROPERTY_FOR_GTYPE (tracker_resource_set_string, const char *, G_TYPE_STRING, g_value_set_string);
+SET_PROPERTY_FOR_GTYPE (tracker_resource_set_uri, const char *, TRACKER_TYPE_URI, g_value_set_string);
+
+/**
+ * tracker_resource_add_gvalue:
+ * @self: the #TrackerResource
+ * @property_uri: a string identifying the property to set
+ * @value: an initialised #GValue
+ *
+ * Add 'value' to the list of values for given property.
+ *
+ * You can pass any kind of GValue for @value, but serialization functions will
+ * normally only be able to serialize URIs/relationships and fundamental value
+ * types (string, int, etc.).
+ *
+ * Since: 1.10
+ */
+void
+tracker_resource_add_gvalue (TrackerResource *self,
+ const char *property_uri,
+ GValue *value)
+{
+ TrackerResourcePrivate *priv = GET_PRIVATE (self);
+ GValue *existing_value, *array_holder, *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_holder = existing_value;
+ } else {
+ array = g_ptr_array_new_with_free_func ((GDestroyNotify)free_value);
+ array_holder = g_slice_new0 (GValue);
+ g_value_init (array_holder, G_TYPE_PTR_ARRAY);
+ g_value_take_boxed (array_holder, array);
+
+ if (existing_value) {
+ /* The existing_value is owned by the hash table and will be freed
+ * when we overwrite it with array_holder, so we need to allocate a
+ * new value and give it to the ptrarray.
+ */
+ our_value = g_slice_new0 (GValue);
+ g_value_init (our_value, G_VALUE_TYPE (existing_value));
+ g_value_copy (existing_value, our_value);
+ g_ptr_array_add (array, our_value);
+ }
+ }
+
+ our_value = g_slice_new0 (GValue);
+ g_value_init (our_value, G_VALUE_TYPE (value));
+ g_value_copy (value, our_value);
+
+ g_ptr_array_add (array, our_value);
+
+ if (array_holder != existing_value) {
+ g_hash_table_insert (priv->properties, g_strdup (property_uri), array_holder);
+ }
+};
+
+#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_holder, *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_holder = existing_value; \
+ } else { \
+ array = g_ptr_array_new_with_free_func ( \
+ (GDestroyNotify)free_value); \
+ array_holder = g_slice_new0 (GValue); \
+ g_value_init (array_holder, G_TYPE_PTR_ARRAY); \
+ g_value_take_boxed (array_holder, array); \
+ \
+ if (existing_value) { \
+ /* existing_value is owned by hash table */ \
+ our_value = g_slice_new0 (GValue); \
+ g_value_init (our_value, G_VALUE_TYPE(existing_value)); \
+ g_value_copy (existing_value, our_value); \
+ g_ptr_array_add (array, our_value); \
+ } \
+ } \
+ \
+ our_value = g_slice_new0 (GValue); \
+ g_value_init (our_value, gtype); \
+ set_function (our_value, value); \
+ \
+ g_ptr_array_add (array, our_value); \
+ \
+ if (array_holder != existing_value) { \
+ g_hash_table_insert (priv->properties, \
+ g_strdup (property_uri), array_holder); \
+ } \
+ };
+
+ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_double, double, G_TYPE_DOUBLE, g_value_set_double);
+ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_int, int, G_TYPE_INT, g_value_set_int);
+ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_int64, gint64, G_TYPE_INT64, g_value_set_int64);
+ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_relation, TrackerResource *, TRACKER_TYPE_RESOURCE,
g_value_set_object);
+ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_string, const char *, G_TYPE_STRING, g_value_set_string);
+ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_uri, const char *, TRACKER_TYPE_URI, g_value_set_string);
+
+
+/**
+ * tracker_resource_get_values:
+ * @self: the #TrackerResource
+ * @property_uri: a string identifying the property to look up
+ *
+ * Returns the list of all known values of the given property.
+ *
+ * Returns: a #GList of #GValue instances, which must be freed by the caller.
+ *
+ * Since: 1.10
+ */
+GList *tracker_resource_get_values (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 NULL;
+ }
+
+ if (G_VALUE_HOLDS (value, G_TYPE_PTR_ARRAY)) {
+ GList *result = NULL;
+ GPtrArray *array;
+ int i;
+
+ array = g_value_get_boxed (value);
+
+ for (i = 0; i < array->len; i++) {
+ value = g_ptr_array_index (array, i);
+ result = g_list_prepend (result, value);
+ };
+
+ return g_list_reverse (result);
+ } else {
+ return g_list_append (NULL, value);
+ }
+}
+
+#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; \
+ }; \
+ \
+ if (G_VALUE_HOLDS (value, G_TYPE_PTR_ARRAY)) { \
+ GPtrArray *array; \
+ array = g_value_get_boxed (value); \
+ if (array->len == 0) { \
+ return no_value; \
+ } else { \
+ value = g_ptr_array_index (array, 0); \
+ } \
+ } \
+ \
+ return get_function (value); \
+ };
+
+GET_PROPERTY_FOR_GTYPE (tracker_resource_get_first_double, double, G_TYPE_DOUBLE, g_value_get_double, 0.0);
+GET_PROPERTY_FOR_GTYPE (tracker_resource_get_first_int, int, G_TYPE_INT, g_value_get_int, 0);
+GET_PROPERTY_FOR_GTYPE (tracker_resource_get_first_int64, gint64, G_TYPE_INT64, g_value_get_int64, 0);
+GET_PROPERTY_FOR_GTYPE (tracker_resource_get_first_relation, TrackerResource *, TRACKER_TYPE_RESOURCE,
g_value_get_object, NULL);
+GET_PROPERTY_FOR_GTYPE (tracker_resource_get_first_string, const char *, G_TYPE_STRING, g_value_get_string,
NULL);
+GET_PROPERTY_FOR_GTYPE (tracker_resource_get_first_uri, const char *, TRACKER_TYPE_URI, g_value_get_string,
NULL);
+
+/**
+ * tracker_resource_get_identifier:
+ * @self: A #TrackerResource
+ *
+ * Returns the identifier of a resource.
+ *
+ * If the identifier was set to NULL, the identifier returned will be a unique
+ * SPARQL blank node identifier, such as "_:123".
+ *
+ * Returns: a string owned by the resource
+ *
+ * Since: 1.10
+ */
+const char *
+tracker_resource_get_identifier (TrackerResource *self)
+{
+ TrackerResourcePrivate *priv = GET_PRIVATE (self);
+
+ return priv->identifier;
+}
+
+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;
+
+ string = g_string_new ("");
+
+ /* 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..ff43028
--- /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_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_gvalue (TrackerResource *self, const char *property_uri, GValue *value);
+void tracker_resource_set_double (TrackerResource *self, const char *property_uri, double value);
+void tracker_resource_set_int (TrackerResource *self, const char *property_uri, int value);
+void tracker_resource_set_int64 (TrackerResource *self, const char *property_uri, gint64 value);
+void tracker_resource_set_relation (TrackerResource *self, const char *property_uri, TrackerResource
*resource);
+void tracker_resource_set_string (TrackerResource *self, const char *property_uri, const char *value);
+void tracker_resource_set_uri (TrackerResource *self, const char *property_uri, const char *value);
+
+void tracker_resource_add_gvalue (TrackerResource *self, const char *property_uri, GValue *value);
+void tracker_resource_add_double (TrackerResource *self, const char *property_uri, double value);
+void tracker_resource_add_int (TrackerResource *self, const char *property_uri, int value);
+void tracker_resource_add_int64 (TrackerResource *self, const char *property_uri, gint64 value);
+void tracker_resource_add_relation (TrackerResource *self, const char *property_uri, TrackerResource
*resource);
+void tracker_resource_add_string (TrackerResource *self, const char *property_uri, const char *value);
+void tracker_resource_add_uri (TrackerResource *self, const char *property_uri, const char *value);
+
+GList *tracker_resource_get_values (TrackerResource *self, const char *property_uri);
+
+double tracker_resource_get_first_double (TrackerResource *self, const char *property_uri);
+int tracker_resource_get_first_int (TrackerResource *self, const char *property_uri);
+gint64 tracker_resource_get_first_int64 (TrackerResource *self, const char *property_uri);
+TrackerResource *tracker_resource_get_first_relation (TrackerResource *self, const char *property_uri);
+const char *tracker_resource_get_first_string (TrackerResource *self, const char *property_uri);
+const char *tracker_resource_get_first_uri (TrackerResource *self, const char *property_uri);
+
+const char *tracker_resource_get_identifier (TrackerResource *self);
+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 ee7eda7..4de6f1f 100644
--- a/src/libtracker-sparql/tracker-sparql.h
+++ b/src/libtracker-sparql/tracker-sparql.h
@@ -25,6 +25,7 @@
#include <libtracker-sparql/tracker-version.h>
#include <libtracker-sparql/tracker-uri.h>
#include <libtracker-sparql/tracker-ontologies.h>
+#include <libtracker-sparql/tracker-resource.h>
#include <libtracker-sparql/tracker-generated.h>
#undef __LIBTRACKER_SPARQL_INSIDE__
diff --git a/src/libtracker-sparql/tracker-uri.h b/src/libtracker-sparql/tracker-uri.h
index 9ea8116..4ffa1e6 100644
--- a/src/libtracker-sparql/tracker-uri.h
+++ b/src/libtracker-sparql/tracker-uri.h
@@ -28,6 +28,8 @@ G_BEGIN_DECLS
#error "only <libtracker-sparql/tracker-sparql.h> must be included directly."
#endif
+GType tracker_uri_get_type (void);
+
#define TRACKER_TYPE_URI tracker_uri_get_type()
/* The public functions in tracker-uri.c are prototyped in the generated Vala
diff --git a/tests/libtracker-sparql/Makefile.am b/tests/libtracker-sparql/Makefile.am
index 1f4cac0..e1dedc6 100644
--- a/tests/libtracker-sparql/Makefile.am
+++ b/tests/libtracker-sparql/Makefile.am
@@ -3,6 +3,7 @@ include $(top_srcdir)/Makefile.decl
noinst_PROGRAMS += $(test_programs)
test_programs = \
+ tracker-resource-test \
tracker-sparql-test \
tracker-gb-737023-test
@@ -18,6 +19,8 @@ LDADD = \
$(BUILD_LIBS) \
$(LIBTRACKER_SPARQL_LIBS)
+tracker_resource_test_SOURCES = tracker-resource-test.c
+
tracker_sparql_test_SOURCES = tracker-sparql-test.c
tracker_gb_737023_test_SOURCES = tracker-gb-737023.c
diff --git a/tests/libtracker-sparql/tracker-resource-test.c b/tests/libtracker-sparql/tracker-resource-test.c
new file mode 100644
index 0000000..92537b9
--- /dev/null
+++ b/tests/libtracker-sparql/tracker-resource-test.c
@@ -0,0 +1,147 @@
+/*
+ * Copyright (C) 2016, Sam Thursfield <sam afuera me uk>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "config.h"
+
+#include <locale.h>
+
+#include <libtracker-sparql/tracker-resource.h>
+
+static void
+test_resource_get_empty (void)
+{
+ TrackerResource *resource;
+
+ resource = tracker_resource_new ("http://example.com/resource");
+
+ g_assert (tracker_resource_get_values (resource, "http://example.com/0") == NULL);
+
+ g_assert (tracker_resource_get_first_double (resource, "http://example.com/0") == 0.0);
+ g_assert (tracker_resource_get_first_int (resource, "http://example.com/0") == 0);
+ g_assert (tracker_resource_get_first_int64 (resource, "http://example.com/0") == 0);
+ g_assert (tracker_resource_get_first_string (resource, "http://example.com/0") == NULL);
+ g_assert (tracker_resource_get_first_uri (resource, "http://example.com/0") == NULL);
+
+ g_object_unref (resource);
+}
+
+static void
+test_resource_get_set_simple (void)
+{
+ TrackerResource *resource;
+
+ resource = tracker_resource_new ("http://example.com/resource");
+
+ tracker_resource_set_double (resource, "http://example.com/1", 0.6);
+ tracker_resource_set_int (resource, "http://example.com/2", 60);
+ tracker_resource_set_int64 (resource, "http://example.com/3", 123456789);
+ tracker_resource_set_string (resource, "http://example.com/4", "Hello");
+ tracker_resource_set_uri (resource, "http://example.com/5", "http://example.com/");
+
+ g_assert (tracker_resource_get_first_double (resource, "http://example.com/1") == 0.6);
+ g_assert_cmpint (tracker_resource_get_first_int (resource, "http://example.com/2"), ==, 60);
+ g_assert (tracker_resource_get_first_int64 (resource, "http://example.com/3") == 123456789);
+ g_assert_cmpstr (tracker_resource_get_first_string (resource, "http://example.com/4"), ==, "Hello");
+ g_assert_cmpstr (tracker_resource_get_first_uri (resource, "http://example.com/5"), ==,
"http://example.com/");
+
+ g_object_unref (resource);
+}
+
+static void
+test_resource_get_set_gvalue (void)
+{
+ TrackerResource *resource;
+ GValue value = G_VALUE_INIT;
+ GList *list;
+
+ resource = tracker_resource_new ("http://example.com/resource");
+
+ g_value_init (&value, G_TYPE_STRING);
+ g_value_set_string (&value, "xyzzy");
+ tracker_resource_set_gvalue (resource, "http://example.com/0", &value);
+
+ list = tracker_resource_get_values (resource, "http://example.com/0");
+ g_assert_cmpint (g_list_length (list), ==, 1);
+
+ g_object_unref (resource);
+}
+
+#define RANDOM_GVALUE_TYPE (G_TYPE_HASH_TABLE)
+
+static void
+init_gvalue_with_random_type (GValue *value)
+{
+ /* Hash table is used here as an "unexpected" value type. The rationale
+ * is that while makes no sense to do this in practice, the code shouldn't
+ * crash or assert.
+ */
+ g_value_init (value, G_TYPE_HASH_TABLE);
+ g_value_take_boxed (value, g_hash_table_new (NULL, NULL));
+}
+
+static void
+test_resource_get_set_many (void)
+{
+ TrackerResource *resource;
+ GValue value = G_VALUE_INIT;
+ GList *list;
+
+ resource = tracker_resource_new ("http://example.com/resource");
+
+ /* All the add_* functions except for add_gvalue are generated using the
+ * same macro, so we only need to test one or two of them here.
+ */
+ tracker_resource_add_int (resource, "http://example.com/0", 60);
+ tracker_resource_add_string (resource, "http://example.com/0", "Hello");
+
+ init_gvalue_with_random_type (&value);
+ tracker_resource_add_gvalue (resource, "http://example.com/0", &value);
+ g_value_unset (&value);
+
+ list = tracker_resource_get_values (resource, "http://example.com/0");
+ g_assert_cmpint (g_list_length (list), ==, 3);
+
+ g_assert_cmpint (g_value_get_int (list->data), ==, 60);
+ g_assert_cmpstr (g_value_get_string (list->next->data), ==, "Hello");
+ g_assert (G_VALUE_HOLDS (list->next->next->data, RANDOM_GVALUE_TYPE));
+
+ g_list_free_full (list, (GDestroyNotify) g_value_unset);
+
+ g_object_unref (resource);
+}
+
+int
+main (int argc,
+ char **argv)
+{
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_message ("Testing Tracker resource abstraction");
+
+ g_test_add_func ("/libtracker-sparql/tracker-resource/get_empty",
+ test_resource_get_empty);
+ g_test_add_func ("/libtracker-sparql/tracker-resource/get_set_simple",
+ test_resource_get_set_simple);
+ g_test_add_func ("/libtracker-sparql/tracker-resource/get_set_gvalue",
+ test_resource_get_set_gvalue);
+ g_test_add_func ("/libtracker-sparql/tracker-resource/get_set_many",
+ test_resource_get_set_many);
+
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]