[virt-tools-list] [PATCH 38/47] Move filter matching code out of entity into filter object

Daniel P. Berrange berrange at redhat.com
Wed Aug 25 19:37:33 UTC 2010


To eliminate a circular dependency, move the filter matching
code out of the OsinfoEntity object and into OsinfoFilter
object.

* osinfo/osinfo_entity.c, osinfo/osinfo_entity.h,
  osinfo/osinfo_filter.c, osinfo/osinfo_filter.h,
  osinfo/osinfo_hypervisor.c, osinfo/osinfo_list.c,
  osinfo/osinfo_os.c: Move filter matching code into
  the OsinfoFilter object APIs
---
 osinfo/osinfo_entity.c     |   99 ++--------------------------------------
 osinfo/osinfo_entity.h     |   10 ++---
 osinfo/osinfo_filter.c     |  108 ++++++++++++++++++++++++++++++++-----------
 osinfo/osinfo_filter.h     |   17 +------
 osinfo/osinfo_hypervisor.c |    2 +-
 osinfo/osinfo_list.c       |    2 +-
 osinfo/osinfo_os.c         |    4 +-
 7 files changed, 94 insertions(+), 148 deletions(-)

diff --git a/osinfo/osinfo_entity.c b/osinfo/osinfo_entity.c
index a938bce..e1b5b38 100644
--- a/osinfo/osinfo_entity.c
+++ b/osinfo/osinfo_entity.c
@@ -121,7 +121,7 @@ osinfo_entity_init (OsinfoEntity *self)
 					       osinfo_entity_param_values_free);
 }
 
-void osinfo_entity_add_param(OsinfoEntity *self, gchar *key, gchar *value)
+void osinfo_entity_add_param(OsinfoEntity *self, const gchar *key, const gchar *value)
 {
     g_return_if_fail(OSINFO_IS_ENTITY(self));
     g_return_if_fail(key != NULL);
@@ -143,7 +143,7 @@ void osinfo_entity_add_param(OsinfoEntity *self, gchar *key, gchar *value)
     g_hash_table_insert(self->priv->params, g_strdup(key), values);
 }
 
-void osinfo_entity_clear_param(OsinfoEntity *self, gchar *key)
+void osinfo_entity_clear_param(OsinfoEntity *self, const gchar *key)
 {
     g_hash_table_remove(self->priv->params, key);
 }
@@ -164,7 +164,7 @@ GList *osinfo_entity_get_param_keys(OsinfoEntity *self)
     return g_hash_table_get_keys(self->priv->params);
 }
 
-gchar *osinfo_entity_get_param_value(OsinfoEntity *self, gchar *key)
+gchar *osinfo_entity_get_param_value(OsinfoEntity *self, const gchar *key)
 {
     g_return_val_if_fail(OSINFO_IS_ENTITY(self), NULL);
     g_return_val_if_fail(key != NULL, NULL);
@@ -178,7 +178,7 @@ gchar *osinfo_entity_get_param_value(OsinfoEntity *self, gchar *key)
     return NULL;
 }
 
-GList *osinfo_entity_get_param_value_list(OsinfoEntity *self, gchar *key)
+GList *osinfo_entity_get_param_value_list(OsinfoEntity *self, const gchar *key)
 {
     g_return_val_if_fail(OSINFO_IS_ENTITY(self), NULL);
     g_return_val_if_fail(key != NULL, NULL);
@@ -187,94 +187,3 @@ GList *osinfo_entity_get_param_value_list(OsinfoEntity *self, gchar *key)
 }
 
 
-static gboolean osinfo_entity_matcher(OsinfoFilter *self,
-				      const gchar *propName,
-				      GList *propValues,
-				      gpointer data)
-{
-    OsinfoEntity *entity = data;
-    GList *values = g_hash_table_lookup(entity->priv->params, propName);
-
-    if (propValues && !values)
-        return FALSE;
-
-    while (propValues) {
-        const gchar *propValue = propValues->data;
-	gboolean found = FALSE;
-	GList *tmp = values;
-	while (tmp) {
-	    const gchar *testValue = tmp->data;
-            if (g_strcmp0(propValue, testValue) == 0) {
-                found = TRUE;
-                break;
-            }
-
-	    tmp = tmp->next;
-        }
-        if (!found)
-	    return FALSE;
-
-        propValues = propValues->next;
-    }
-
-    return TRUE;
-}
-
-
-static gboolean osinfo_entity_relation_matcher(OsinfoFilter *self,
-					       OsinfoOsRelationship relshp,
-					       GList *relOses,
-					       gpointer data)
-{
-    OsinfoOs *os = data;
-    OsinfoOsList *oslist = osinfo_os_get_related(os, relshp);
-    gboolean ret = TRUE;
-
-    if (relOses && osinfo_list_get_length(OSINFO_LIST(oslist)) == 0) {
-        ret = FALSE;
-	goto cleanup;
-    }
-
-    while (relOses) {
-        OsinfoOs *currOs = relOses->data;
-        int i;
-	gboolean found = FALSE;
-	for (i = 0 ; i < osinfo_list_get_length(OSINFO_LIST(oslist)) ; i++) {
-	    OsinfoOs *testOs = OSINFO_OS(osinfo_list_get_nth(OSINFO_LIST(oslist), i));
-            if (testOs == currOs) {
-                found = TRUE;
-                break;
-            }
-        }
-        if (!found) {
-	    ret = FALSE;
-	    goto cleanup;
-	}
-
-	relOses = relOses->next;
-    }
-
- cleanup:
-    g_object_unref(oslist);
-    return ret;
-}
-
-
-gboolean osinfo_entity_matches_filter(OsinfoEntity *self, OsinfoFilter *filter)
-{
-    g_return_val_if_fail(OSINFO_IS_ENTITY(self), FALSE);
-    g_return_val_if_fail(OSINFO_IS_FILTER(filter), FALSE);
-
-    if (!osinfo_filter_matches_constraints(filter,
-					   osinfo_entity_matcher,
-					   self))
-        return FALSE;
-
-    if (OSINFO_IS_OS(self) &&
-	!osinfo_filter_matches_relation_constraints(filter,
-						    osinfo_entity_relation_matcher,
-						    self))
-        return FALSE;
-
-    return TRUE;
-}
diff --git a/osinfo/osinfo_entity.h b/osinfo/osinfo_entity.h
index cf3dc61..ad78324 100644
--- a/osinfo/osinfo_entity.h
+++ b/osinfo/osinfo_entity.h
@@ -49,11 +49,9 @@ GType osinfo_entity_get_type(void);
 
 gchar *osinfo_entity_get_id(OsinfoEntity *self);
 GList *osinfo_entity_get_param_keys(OsinfoEntity *self);
-gchar *osinfo_entity_get_param_value(OsinfoEntity *self, gchar *key);
-GList *osinfo_entity_get_param_value_list(OsinfoEntity *self, gchar *key);
-void osinfo_entity_add_param(OsinfoEntity *self, gchar *key, gchar *value);
-void osinfo_entity_clear_param(OsinfoEntity *self, gchar *key);
-
-gboolean osinfo_entity_matches_filter(OsinfoEntity *self, OsinfoFilter *filter);
+gchar *osinfo_entity_get_param_value(OsinfoEntity *self, const gchar *key);
+GList *osinfo_entity_get_param_value_list(OsinfoEntity *self, const gchar *key);
+void osinfo_entity_add_param(OsinfoEntity *self, const gchar *key, const gchar *value);
+void osinfo_entity_clear_param(OsinfoEntity *self, const gchar *key);
 
 #endif /* __OSINFO_ENTITY_H__ */
diff --git a/osinfo/osinfo_filter.c b/osinfo/osinfo_filter.c
index 61ac9f2..05cc39c 100644
--- a/osinfo/osinfo_filter.c
+++ b/osinfo/osinfo_filter.c
@@ -200,53 +200,105 @@ OsinfoOsList *osinfo_filter_get_relationship_constraint_value(OsinfoFilter *self
 
 struct osinfo_filter_match_args {
     OsinfoFilter *self;
-    osinfo_filter_match_func matcher;
-    gpointer data;
+    OsinfoEntity *entity;
     gboolean matched;
 };
 
 static void osinfo_filter_match_iterator(gpointer key, gpointer value, gpointer data)
 {
     struct osinfo_filter_match_args *args = data;
+    OsinfoEntity *entity = args->entity;
+    const gchar *propName = key;
+    GList *propValues = value;
 
-    if (!(args->matcher)(args->self, key, value, args->data))
+    GList *values = osinfo_entity_get_param_value_list(entity, propName);
+
+    if (propValues && !values) {
         args->matched = FALSE;
-}
+        return;
+    }
 
-gboolean osinfo_filter_matches_constraints(OsinfoFilter *self,
-					   osinfo_filter_match_func matcher,
-					   gpointer data)
-{
-    struct osinfo_filter_match_args args = { self, matcher, data, TRUE };
-    g_hash_table_foreach(self->priv->propertyConstraints,
-			 osinfo_filter_match_iterator,
-			 &args);
-    return args.matched;
-}
+    while (propValues) {
+        const gchar *propValue = propValues->data;
+	gboolean found = FALSE;
+	GList *tmp = values;
+	while (tmp) {
+	    const gchar *testValue = tmp->data;
+            if (g_strcmp0(propValue, testValue) == 0) {
+                found = TRUE;
+                break;
+            }
+
+	    tmp = tmp->next;
+        }
+        if (!found) {
+	    args->matched = FALSE;
+	    return;
+	}
 
+        propValues = propValues->next;
+    }
+}
 
-struct osinfo_filter_match_relation_args {
-    OsinfoFilter *self;
-    osinfo_filter_match_relation_func matcher;
-    gpointer data;
-    gboolean matched;
-};
 
 static void osinfo_filter_match_relation_iterator(gpointer key, gpointer value, gpointer data)
 {
     struct osinfo_filter_match_args *args = data;
+    OsinfoOs *os = OSINFO_OS(args->entity);
+    OsinfoOsRelationship relshp = GPOINTER_TO_INT(key);
+    GList *relOses = value;
+    OsinfoOsList *oslist = osinfo_os_get_related(os, relshp);
+    gboolean ret = TRUE;
+
+    if (relOses && osinfo_list_get_length(OSINFO_LIST(oslist)) == 0) {
+        ret = FALSE;
+	goto cleanup;
+    }
 
-    if (!(args->matcher)(args->self, key, value, args->data))
-        args->matched = FALSE;
+    while (relOses) {
+        OsinfoOs *currOs = relOses->data;
+        int i;
+	gboolean found = FALSE;
+	for (i = 0 ; i < osinfo_list_get_length(OSINFO_LIST(oslist)) ; i++) {
+	    OsinfoOs *testOs = OSINFO_OS(osinfo_list_get_nth(OSINFO_LIST(oslist), i));
+            if (testOs == currOs) {
+                found = TRUE;
+                break;
+            }
+        }
+        if (!found) {
+	    ret = FALSE;
+	    goto cleanup;
+	}
+
+	relOses = relOses->next;
+    }
+
+ cleanup:
+    g_object_unref(oslist);
+    args->matched = ret;
 }
 
-gboolean osinfo_filter_matches_relation_constraints(OsinfoFilter *self,
-						    osinfo_filter_match_relation_func matcher,
-						    gpointer data)
+gboolean osinfo_filter_matches(OsinfoFilter *self, OsinfoEntity *entity)
 {
-    struct osinfo_filter_match_relation_args args = { self, matcher, data, TRUE };
+    g_return_val_if_fail(OSINFO_IS_FILTER(self), FALSE);
+    g_return_val_if_fail(OSINFO_IS_ENTITY(entity), FALSE);
+
+    struct osinfo_filter_match_args args = { self, entity, TRUE };
     g_hash_table_foreach(self->priv->propertyConstraints,
-			 osinfo_filter_match_relation_iterator,
+			 osinfo_filter_match_iterator,
 			 &args);
-    return args.matched;
+
+    if (!args.matched)
+        return FALSE;
+
+    if (OSINFO_IS_OS(self)) {
+        g_hash_table_foreach(self->priv->relationshipConstraints,
+			     osinfo_filter_match_relation_iterator,
+			     &args);
+	if (!args.matched)
+	    return FALSE;
+    }
+
+    return TRUE;
 }
diff --git a/osinfo/osinfo_filter.h b/osinfo/osinfo_filter.h
index 45d66b3..18b0be8 100644
--- a/osinfo/osinfo_filter.h
+++ b/osinfo/osinfo_filter.h
@@ -60,20 +60,7 @@ GList *osinfo_filter_get_constraint_keys(OsinfoFilter *self);
 GList *osinfo_filter_get_constraint_values(OsinfoFilter *self, gchar *propName);
 OsinfoOsList *osinfo_filter_get_relationship_constraint_value(OsinfoFilter *self, OsinfoOsRelationship relshp);
 
-typedef gboolean (*osinfo_filter_match_func)(OsinfoFilter *self,
-					     const gchar *propName,
-					     GList *propValues,
-					     gpointer data);
-typedef gboolean (*osinfo_filter_match_relation_func)(OsinfoFilter *self,
-						      OsinfoOsRelationship relshp,
-						      GList *relOses,
-						      gpointer data);
-
-gboolean osinfo_filter_matches_constraints(OsinfoFilter *self,
-					   osinfo_filter_match_func matcher,
-					   gpointer data);
-gboolean osinfo_filter_matches_relation_constraints(OsinfoFilter *self,
-						    osinfo_filter_match_relation_func matcher,
-						    gpointer data);
+gboolean osinfo_filter_matches(OsinfoFilter *self,
+			       OsinfoEntity *entity);
 
 #endif /* __OSINFO_FILTER_H__ */
diff --git a/osinfo/osinfo_hypervisor.c b/osinfo/osinfo_hypervisor.c
index 911edca..88fd87e 100644
--- a/osinfo/osinfo_hypervisor.c
+++ b/osinfo/osinfo_hypervisor.c
@@ -75,7 +75,7 @@ OsinfoDeviceList *osinfo_hypervisor_get_devices(OsinfoHypervisor *self, OsinfoFi
     while (tmp) {
         struct _OsinfoHypervisorDeviceLink *link = tmp->data;
 
-        if (osinfo_entity_matches_filter(OSINFO_ENTITY(link->dev), filter))
+        if (osinfo_filter_matches(filter, OSINFO_ENTITY(link->dev)))
 	    osinfo_list_add(OSINFO_LIST(newList), OSINFO_ENTITY(link->dev));
 
 	tmp = tmp->next;
diff --git a/osinfo/osinfo_list.c b/osinfo/osinfo_list.c
index e21eebb..cd9de4c 100644
--- a/osinfo/osinfo_list.c
+++ b/osinfo/osinfo_list.c
@@ -77,7 +77,7 @@ void osinfo_list_add_filtered(OsinfoList *self, OsinfoList *source, OsinfoFilter
     len = osinfo_list_get_length(source);
     for (i = 0; i < len; i++) {
         OsinfoEntity *entity = osinfo_list_get_nth(source, i);
-        if (osinfo_entity_matches_filter(entity, filter))
+        if (osinfo_filter_matches(filter, entity))
 	    osinfo_list_add(self, entity);
     }
 }
diff --git a/osinfo/osinfo_os.c b/osinfo/osinfo_os.c
index 421655b..7ccd997 100644
--- a/osinfo/osinfo_os.c
+++ b/osinfo/osinfo_os.c
@@ -129,7 +129,7 @@ OsinfoDevice *osinfo_os_get_preferred_device(OsinfoOs *self, OsinfoHypervisor *h
     while (tmp) {
         struct _OsinfoOsDeviceLink *link = tmp->data;
 
-        if (osinfo_entity_matches_filter(OSINFO_ENTITY(link->dev), filter)) {
+        if (osinfo_filter_matches(filter, OSINFO_ENTITY(link->dev))) {
 	    *driver = link->driver;
 	    return link->dev;
 	}
@@ -178,7 +178,7 @@ OsinfoDeviceList *osinfo_os_get_devices(OsinfoOs *self, OsinfoHypervisor *hv, Os
     while (tmp) {
         struct _OsinfoOsDeviceLink *link = tmp->data;
 
-        if (osinfo_entity_matches_filter(OSINFO_ENTITY(link->dev), filter))
+        if (osinfo_filter_matches(filter, OSINFO_ENTITY(link->dev)))
 	    osinfo_list_add(OSINFO_LIST(newList), OSINFO_ENTITY(link->dev));
 
 	tmp = tmp->next;
-- 
1.7.2.1




More information about the virt-tools-list mailing list