Skip to content
Snippets Groups Projects
Commit f01dd10a authored by Julian Bouzas's avatar Julian Bouzas
Browse files

metadata: refactor API to quickly find a specific value

parent bef0b178
No related branches found
No related tags found
No related merge requests found
...@@ -293,8 +293,6 @@ struct metadata_iterator_data ...@@ -293,8 +293,6 @@ struct metadata_iterator_data
WpMetadata *metadata; WpMetadata *metadata;
const struct item *item; const struct item *item;
guint32 subject; guint32 subject;
gchar *key;
gchar *type;
}; };
static void static void
...@@ -316,9 +314,7 @@ metadata_iterator_next (WpIterator *it, GValue *item) ...@@ -316,9 +314,7 @@ metadata_iterator_next (WpIterator *it, GValue *item)
while (pw_array_check (&priv->metadata, it_data->item)) { while (pw_array_check (&priv->metadata, it_data->item)) {
if ((it_data->subject == PW_ID_ANY || if ((it_data->subject == PW_ID_ANY ||
it_data->subject == it_data->item->subject) && it_data->subject == it_data->item->subject)) {
(!it_data->key || !g_strcmp0 (it_data->key, it_data->item->key)) &&
(!it_data->type || !g_strcmp0 (it_data->type, it_data->item->type))) {
g_value_init (item, G_TYPE_POINTER); g_value_init (item, G_TYPE_POINTER);
g_value_set_pointer (item, (gpointer) it_data->item); g_value_set_pointer (item, (gpointer) it_data->item);
it_data->item++; it_data->item++;
...@@ -340,9 +336,7 @@ metadata_iterator_fold (WpIterator *it, WpIteratorFoldFunc func, GValue *ret, ...@@ -340,9 +336,7 @@ metadata_iterator_fold (WpIterator *it, WpIteratorFoldFunc func, GValue *ret,
pw_array_for_each (i, &priv->metadata) { pw_array_for_each (i, &priv->metadata) {
if ((it_data->subject == PW_ID_ANY || if ((it_data->subject == PW_ID_ANY ||
it_data->subject == it_data->item->subject) && it_data->subject == it_data->item->subject)) {
(!it_data->key || !g_strcmp0 (it_data->key, it_data->item->key)) &&
(!it_data->type || !g_strcmp0 (it_data->type, it_data->item->type))) {
g_auto (GValue) item = G_VALUE_INIT; g_auto (GValue) item = G_VALUE_INIT;
g_value_init (&item, G_TYPE_POINTER); g_value_init (&item, G_TYPE_POINTER);
g_value_set_pointer (&item, (gpointer) i); g_value_set_pointer (&item, (gpointer) i);
...@@ -358,8 +352,6 @@ metadata_iterator_finalize (WpIterator *it) ...@@ -358,8 +352,6 @@ metadata_iterator_finalize (WpIterator *it)
{ {
struct metadata_iterator_data *it_data = wp_iterator_get_user_data (it); struct metadata_iterator_data *it_data = wp_iterator_get_user_data (it);
g_object_unref (it_data->metadata); g_object_unref (it_data->metadata);
g_free (it_data->key);
g_free (it_data->type);
} }
static const WpIteratorMethods metadata_iterator_methods = { static const WpIteratorMethods metadata_iterator_methods = {
...@@ -371,13 +363,11 @@ static const WpIteratorMethods metadata_iterator_methods = { ...@@ -371,13 +363,11 @@ static const WpIteratorMethods metadata_iterator_methods = {
}; };
/** /**
* wp_metadata_find: * wp_metadata_iterate:
* @self: a metadata object * @self: a metadata object
* @subject: the metadata subject id, or %PW_ID_ANY * @subject: the metadata subject id, or %PW_ID_ANY
* @key: (nullable): the metadata key to find, or %NULL
* @type: (nullable): the metadata type to find, or %NULL
* *
* Find metadata that matches the given @subject, @key and @type. If no * Iterates over metadata items that matches the given @subject. If no
* constraints are specified, the returned iterator iterates over all the * constraints are specified, the returned iterator iterates over all the
* stored metadata. * stored metadata.
* *
...@@ -390,8 +380,7 @@ static const WpIteratorMethods metadata_iterator_methods = { ...@@ -390,8 +380,7 @@ static const WpIteratorMethods metadata_iterator_methods = {
* this iterator. * this iterator.
*/ */
WpIterator * WpIterator *
wp_metadata_find (WpMetadata * self, guint32 subject, wp_metadata_iterate (WpMetadata * self, guint32 subject)
const gchar * key, const gchar * type)
{ {
WpMetadataPrivate *priv; WpMetadataPrivate *priv;
g_autoptr (WpIterator) it = NULL; g_autoptr (WpIterator) it = NULL;
...@@ -406,8 +395,6 @@ wp_metadata_find (WpMetadata * self, guint32 subject, ...@@ -406,8 +395,6 @@ wp_metadata_find (WpMetadata * self, guint32 subject,
it_data->metadata = g_object_ref (self); it_data->metadata = g_object_ref (self);
it_data->item = pw_array_first (&priv->metadata); it_data->item = pw_array_first (&priv->metadata);
it_data->subject = subject; it_data->subject = subject;
it_data->key = g_strdup (key);
it_data->type = g_strdup (type);
return g_steal_pointer (&it); return g_steal_pointer (&it);
} }
...@@ -438,6 +425,36 @@ wp_metadata_iterator_item_extract (const GValue * item, guint32 * subject, ...@@ -438,6 +425,36 @@ wp_metadata_iterator_item_extract (const GValue * item, guint32 * subject,
*value = i->value; *value = i->value;
} }
/**
* wp_metadata_find:
* @self: a metadata object
* @subject: the metadata subject id
* @key: the metadata key name
* @type: (out)(optional): the metadata type name
*
* Finds the metadata value given its @subject and &key.
*
* Returns: the metadata string value, or NULL if not found.
*/
const gchar *
wp_metadata_find (WpMetadata * self, guint32 subject, const gchar * key,
const gchar ** type)
{
g_autoptr (WpIterator) it = NULL;
g_auto (GValue) val = G_VALUE_INIT;
it = wp_metadata_iterate (self, subject);
for (; wp_iterator_next (it, &val); g_value_unset (&val)) {
const gchar *k = NULL, *t = NULL, *v = NULL;
wp_metadata_iterator_item_extract (&val, NULL, &k, &t, &v);
if (g_strcmp0 (k, key) == 0) {
if (type)
*type = t;
return v;
}
}
return NULL;
}
/** /**
* wp_metadata_set: * wp_metadata_set:
* @self: the metadata object * @self: the metadata object
......
...@@ -39,13 +39,16 @@ struct _WpMetadataClass ...@@ -39,13 +39,16 @@ struct _WpMetadataClass
}; };
WP_API WP_API
WpIterator * wp_metadata_find (WpMetadata * self, guint32 subject, WpIterator * wp_metadata_iterate (WpMetadata * self, guint32 subject);
const gchar * key, const gchar * type);
WP_API WP_API
void wp_metadata_iterator_item_extract (const GValue * item, guint32 * subject, void wp_metadata_iterator_item_extract (const GValue * item, guint32 * subject,
const gchar ** key, const gchar ** type, const gchar ** value); const gchar ** key, const gchar ** type, const gchar ** value);
WP_API
const gchar * wp_metadata_find (WpMetadata * self, guint32 subject,
const gchar * key, const gchar ** type);
WP_API WP_API
void wp_metadata_set (WpMetadata * self, guint32 subject, void wp_metadata_set (WpMetadata * self, guint32 subject,
const gchar * key, const gchar * type, const gchar * value); const gchar * key, const gchar * type, const gchar * value);
......
...@@ -161,8 +161,7 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data) ...@@ -161,8 +161,7 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data)
/* verify properties are set before export */ /* verify properties are set before export */
{ {
g_autoptr (WpIterator) iter = g_autoptr (WpIterator) iter = wp_metadata_iterate (metadata, PW_ID_ANY);
wp_metadata_find (metadata, PW_ID_ANY, NULL, NULL);
g_auto (GValue) val = G_VALUE_INIT; g_auto (GValue) val = G_VALUE_INIT;
guint subject = -1; guint subject = -1;
const gchar *key = NULL, *type = NULL, *value = NULL; const gchar *key = NULL, *type = NULL, *value = NULL;
...@@ -201,7 +200,7 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data) ...@@ -201,7 +200,7 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data)
/* test round 1: verify the values on the proxy */ /* test round 1: verify the values on the proxy */
{ {
g_autoptr (WpIterator) iter = g_autoptr (WpIterator) iter =
wp_metadata_find (fixture->proxy_metadata, PW_ID_ANY, NULL, NULL); wp_metadata_iterate (fixture->proxy_metadata, PW_ID_ANY);
g_auto (GValue) val = G_VALUE_INIT; g_auto (GValue) val = G_VALUE_INIT;
guint subject = -1; guint subject = -1;
const gchar *key = NULL, *type = NULL, *value = NULL; const gchar *key = NULL, *type = NULL, *value = NULL;
...@@ -243,7 +242,7 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data) ...@@ -243,7 +242,7 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data)
/* test round 2: verify the value change on both sides */ /* test round 2: verify the value change on both sides */
{ {
g_autoptr (WpIterator) iter = g_autoptr (WpIterator) iter =
wp_metadata_find (fixture->proxy_metadata, PW_ID_ANY, NULL, NULL); wp_metadata_iterate (fixture->proxy_metadata, PW_ID_ANY);
g_auto (GValue) val = G_VALUE_INIT; g_auto (GValue) val = G_VALUE_INIT;
guint subject = -1; guint subject = -1;
const gchar *key = NULL, *type = NULL, *value = NULL; const gchar *key = NULL, *type = NULL, *value = NULL;
...@@ -275,8 +274,7 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data) ...@@ -275,8 +274,7 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data)
g_assert_false (wp_iterator_next (iter, &val)); g_assert_false (wp_iterator_next (iter, &val));
} }
{ {
g_autoptr (WpIterator) iter = g_autoptr (WpIterator) iter = wp_metadata_iterate (metadata, PW_ID_ANY);
wp_metadata_find (metadata, PW_ID_ANY, NULL, NULL);
g_auto (GValue) val = G_VALUE_INIT; g_auto (GValue) val = G_VALUE_INIT;
guint subject = -1; guint subject = -1;
const gchar *key = NULL, *type = NULL, *value = NULL; const gchar *key = NULL, *type = NULL, *value = NULL;
...@@ -320,7 +318,7 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data) ...@@ -320,7 +318,7 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data)
/* test round 3: verify the value change on both sides */ /* test round 3: verify the value change on both sides */
{ {
g_autoptr (WpIterator) iter = g_autoptr (WpIterator) iter =
wp_metadata_find (fixture->proxy_metadata, PW_ID_ANY, NULL, NULL); wp_metadata_iterate (fixture->proxy_metadata, PW_ID_ANY);
g_auto (GValue) val = G_VALUE_INIT; g_auto (GValue) val = G_VALUE_INIT;
guint subject = -1; guint subject = -1;
const gchar *key = NULL, *type = NULL, *value = NULL; const gchar *key = NULL, *type = NULL, *value = NULL;
...@@ -360,8 +358,7 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data) ...@@ -360,8 +358,7 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data)
g_assert_false (wp_iterator_next (iter, &val)); g_assert_false (wp_iterator_next (iter, &val));
} }
{ {
g_autoptr (WpIterator) iter = g_autoptr (WpIterator) iter = wp_metadata_iterate (metadata, PW_ID_ANY);
wp_metadata_find (metadata, PW_ID_ANY, NULL, NULL);
g_auto (GValue) val = G_VALUE_INIT; g_auto (GValue) val = G_VALUE_INIT;
guint subject = -1; guint subject = -1;
const gchar *key = NULL, *type = NULL, *value = NULL; const gchar *key = NULL, *type = NULL, *value = NULL;
...@@ -403,8 +400,7 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data) ...@@ -403,8 +400,7 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data)
/* find with constraints */ /* find with constraints */
{ {
g_autoptr (WpIterator) iter = g_autoptr (WpIterator) iter = wp_metadata_iterate (metadata, 0);
wp_metadata_find (metadata, 0, NULL, NULL);
g_auto (GValue) val = G_VALUE_INIT; g_auto (GValue) val = G_VALUE_INIT;
guint subject = -1; guint subject = -1;
const gchar *key = NULL, *type = NULL, *value = NULL; const gchar *key = NULL, *type = NULL, *value = NULL;
...@@ -436,21 +432,10 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data) ...@@ -436,21 +432,10 @@ test_metadata_basic (TestFixture *fixture, gconstpointer data)
g_assert_false (wp_iterator_next (iter, &val)); g_assert_false (wp_iterator_next (iter, &val));
} }
{ {
g_autoptr (WpIterator) iter = const gchar *value = NULL, *type = NULL;
wp_metadata_find (metadata, 0, "3rd.key", NULL); value = wp_metadata_find (metadata, 0, "3rd.key", &type);
g_auto (GValue) val = G_VALUE_INIT;
guint subject = -1;
const gchar *key = NULL, *type = NULL, *value = NULL;
g_assert_true (wp_iterator_next (iter, &val));
wp_metadata_iterator_item_extract (&val, &subject, &key, &type, &value);
g_assert_cmpint (subject, ==, 0);
g_assert_cmpstr (key, ==, "3rd.key");
g_assert_cmpstr (type, ==, "string"); g_assert_cmpstr (type, ==, "string");
g_assert_cmpstr (value, ==, "3rd.value"); g_assert_cmpstr (value, ==, "3rd.value");
g_value_unset (&val);
g_assert_false (wp_iterator_next (iter, &val));
} }
/* destroy impl metadata */ /* destroy impl metadata */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment