[tracker/wip/resource: 8/11] libtracker-sparql: Loads of work on TrackerResource



commit 7d531e59718475afa762c749a42eac639120f081
Author: Sam Thursfield <ssssam gmail com>
Date:   Sun Mar 27 00:16:16 2016 +0000

    libtracker-sparql: Loads of work on TrackerResource

 src/libtracker-sparql/tracker-resource.c |  191 +++++++++++++++++++++++++++---
 src/libtracker-sparql/tracker-resource.h |   35 ++++--
 2 files changed, 201 insertions(+), 25 deletions(-)
---
diff --git a/src/libtracker-sparql/tracker-resource.c b/src/libtracker-sparql/tracker-resource.c
index 1790b4f..90a9d69 100644
--- a/src/libtracker-sparql/tracker-resource.c
+++ b/src/libtracker-sparql/tracker-resource.c
@@ -27,7 +27,7 @@
 typedef struct {
        char *identifier;
        GHashTable *properties;
-       GHashTable *properties_overwrite;
+       GHashTable *overwrite;
 } TrackerResourcePrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (TrackerResource, tracker_resource, G_TYPE_OBJECT);
@@ -46,6 +46,24 @@ G_DEFINE_TYPE_WITH_PRIVATE (TrackerResource, tracker_resource, G_TYPE_OBJECT);
  * </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:
  *
@@ -86,7 +104,7 @@ tracker_resource_init (TrackerResource *resource)
            (GDestroyNotify) free_value);
 
        /* TRUE for any property where we should delete any existing values. */
-       priv->properties_overwrite = g_hash_table_new_full (
+       priv->overwrite = g_hash_table_new_full (
            g_str_hash,
            g_str_equal,
            g_free,
@@ -100,7 +118,7 @@ finalize (GObject *object)
 
        priv = GET_PRIVATE (TRACKER_RESOURCE (object));
 
-       g_hash_table_unref (priv->properties_overwrite);
+       g_hash_table_unref (priv->overwrite);
        g_hash_table_unref (priv->properties);
 
        (G_OBJECT_CLASS (tracker_resource_parent_class)->finalize) (object);
@@ -122,32 +140,157 @@ tracker_resource_new (const char *identifier)
        return g_object_new (TRACKER_TYPE_RESOURCE, NULL);
 }
 
-GValue *
-tracker_resource_get_property (TrackerResource *self,
-                               const char *property)
+/* 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;
 
-       return g_hash_table_lookup (priv->properties, property);
-}
+       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_set_property (TrackerResource *self,
-                               const char *property,
-                               GValue *value)
+tracker_resource_add_property_gvalue (TrackerResource *self,
+                                      const char *property_uri,
+                                      GValue *value)
 {
        TrackerResourcePrivate *priv = GET_PRIVATE (self);
-       GValue *our_value;
+       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);
 
-       /* Note that this should generate a DELETE; INSERT in the SPARQL
-        * query: we're assming the property has only one value.
-        */
-       g_hash_table_replace (priv->properties, g_strdup (property), 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)
@@ -156,3 +299,19 @@ tracker_resource_identifier_compare_func (TrackerResource *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
index 9d48f20..1318bc9 100644
--- a/src/libtracker-sparql/tracker-resource.h
+++ b/src/libtracker-sparql/tracker-resource.h
@@ -24,6 +24,8 @@
 
 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)
 
@@ -32,18 +34,33 @@ struct _TrackerResourceClass
        GObjectClass parent_class;
 };
 
-TrackerResource *tracker_resource_new             (const char *identifier);
+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);
 
-GValue          *tracker_resource_get_property    (TrackerResource *self,
-                                                   const char *property);
+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);
 
-void             tracker_resource_set_property    (TrackerResource *self,
-                                                   const char *property,
-                                                   GValue *value);
+gint tracker_resource_identifier_compare_func (TrackerResource *resource, const char *identifier);
 
-void             tracker_resource_append_property (TrackerResource *self,
-                                                   const char *property,
-                                                   GValue *value);
 G_END_DECLS
 
 #endif /* __LIBTRACKER_RESOURCE_H__ */


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