[tracker/wip/carlosg/resources-as-cursors: 3/18] libtracker-sparql: Add private TrackerResource iterator helper
- From: Carlos Garnacho <carlosg src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker/wip/carlosg/resources-as-cursors: 3/18] libtracker-sparql: Add private TrackerResource iterator helper
- Date: Mon, 11 Jul 2022 18:57:49 +0000 (UTC)
commit 2717dcb043c9a1f3c66bdf9925a92e687d1ae5e2
Author: Carlos Garnacho <carlosg gnome org>
Date: Sat Jul 2 19:31:18 2022 +0200
libtracker-sparql: Add private TrackerResource iterator helper
This is a simple heap struct to iterate through the properties and
values defined in a TrackerResource without requiring memory allocations.
This helper does not handle recursivity, if TrackerResource values are
found, these should be iterated separately.
src/libtracker-sparql/tracker-private.h | 14 ++++++++++
src/libtracker-sparql/tracker-resource.c | 47 ++++++++++++++++++++++++++++++++
2 files changed, 61 insertions(+)
---
diff --git a/src/libtracker-sparql/tracker-private.h b/src/libtracker-sparql/tracker-private.h
index 0e309e48e..8e3027486 100644
--- a/src/libtracker-sparql/tracker-private.h
+++ b/src/libtracker-sparql/tracker-private.h
@@ -304,6 +304,14 @@ struct _TrackerNamespaceManager {
GObject parent;
};
+typedef struct {
+ GHashTableIter prop_iter;
+ TrackerResource *cur_resource;
+ const gchar *cur_prop;
+ GPtrArray *cur_values;
+ guint idx;
+} TrackerResourceIterator;
+
gboolean
tracker_sparql_connection_lookup_dbus_service (TrackerSparqlConnection *connection,
const gchar *dbus_name,
@@ -316,4 +324,10 @@ GError * _translate_internal_error (GError *error);
void tracker_namespace_manager_seal (TrackerNamespaceManager *namespaces);
+void tracker_resource_iterator_init (TrackerResourceIterator *iter,
+ TrackerResource *resource);
+gboolean tracker_resource_iterator_next (TrackerResourceIterator *iter,
+ const gchar **property,
+ const GValue **value);
+
#endif /* __TRACKER_PRIVATE_H__ */
diff --git a/src/libtracker-sparql/tracker-resource.c b/src/libtracker-sparql/tracker-resource.c
index de509fc78..4022091d6 100644
--- a/src/libtracker-sparql/tracker-resource.c
+++ b/src/libtracker-sparql/tracker-resource.c
@@ -2016,3 +2016,50 @@ tracker_resource_get_property_overwrite (TrackerResource *resource,
return g_hash_table_contains (priv->overwrite, property_uri);
}
+
+void
+tracker_resource_iterator_init (TrackerResourceIterator *iter,
+ TrackerResource *resource)
+{
+ TrackerResourcePrivate *priv = GET_PRIVATE (resource);
+
+ bzero (iter, sizeof (TrackerResourceIterator));
+ g_hash_table_iter_init (&iter->prop_iter, priv->properties);
+}
+
+gboolean
+tracker_resource_iterator_next (TrackerResourceIterator *iter,
+ const gchar **property,
+ const GValue **value)
+{
+ gpointer key, val;
+
+ if (iter->cur_values && iter->cur_prop) {
+ iter->idx++;
+
+ if (iter->idx < iter->cur_values->len) {
+ *property = iter->cur_prop;
+ *value = g_ptr_array_index (iter->cur_values, iter->idx);
+ return TRUE;
+ } else {
+ iter->cur_values = NULL;
+ iter->cur_prop = NULL;
+ }
+ }
+
+ if (!g_hash_table_iter_next (&iter->prop_iter, &key, &val))
+ return FALSE;
+
+ if (G_VALUE_HOLDS (val, G_TYPE_PTR_ARRAY)) {
+ iter->cur_prop = key;
+ iter->cur_values = g_value_get_boxed (val);
+ iter->idx = 0;
+ *property = iter->cur_prop;
+ *value = g_ptr_array_index (iter->cur_values, iter->idx);
+ return TRUE;
+ }
+
+ *property = key;
+ *value = val;
+ return TRUE;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]