[Libosinfo] [RFC PATCH 4/5] osinfo_loader: replace some xpath queries with direct data access

Giuseppe Scrivano gscrivan at redhat.com
Tue Jun 3 14:08:11 UTC 2014


Signed-off-by: Giuseppe Scrivano <gscrivan at redhat.com>
---
 osinfo/osinfo_loader.c | 133 ++++++++++++-------------------------------------
 1 file changed, 32 insertions(+), 101 deletions(-)

diff --git a/osinfo/osinfo_loader.c b/osinfo/osinfo_loader.c
index 6b1d3e1..2a7d748 100644
--- a/osinfo/osinfo_loader.c
+++ b/osinfo/osinfo_loader.c
@@ -530,20 +530,25 @@ static void osinfo_loader_device_link(OsinfoLoader *loader,
 static void osinfo_loader_product_relshp(OsinfoLoader *loader,
                                          OsinfoProduct *product,
                                          OsinfoProductRelationship relshp,
-                                         const gchar *xpath,
+                                         const gchar *name,
                                          xmlXPathContextPtr ctxt,
                                          xmlNodePtr root,
                                          GError **err)
 {
     xmlNodePtr *related = NULL;
-    xmlXPathCompExprPtr comp = osinfo_loader_get_comp_xpath(loader, xpath);
-    int nrelated = osinfo_loader_nodeset(comp, ctxt, &related, err);
-    int i;
+    xmlNodePtr it;
+
     if (error_is_set(err))
         return;
 
-    for (i = 0 ; i < nrelated ; i++) {
-        gchar *id = (gchar *)xmlGetProp(related[i], BAD_CAST "id");
+    for(it = root->children; it; it = it->next) {
+        if (it->type != XML_ELEMENT_NODE)
+            continue;
+
+        if (!xmlStrEqual(it->name, BAD_CAST name))
+            continue;
+
+        gchar *id = (gchar *)xmlGetProp(it, BAD_CAST "id");
         if (!id) {
             OSINFO_ERROR(err, _("Missing product upgrades id property"));
             goto cleanup;
@@ -586,7 +591,7 @@ static void osinfo_loader_product(OsinfoLoader *loader,
 
     osinfo_loader_product_relshp(loader, product,
                                  OSINFO_PRODUCT_RELATIONSHIP_DERIVES_FROM,
-                                 "./derives-from",
+                                 "derives-from",
                                  ctxt,
                                  root,
                                  err);
@@ -595,7 +600,7 @@ static void osinfo_loader_product(OsinfoLoader *loader,
 
     osinfo_loader_product_relshp(loader, product,
                                  OSINFO_PRODUCT_RELATIONSHIP_CLONES,
-                                 "./clones",
+                                 "clones",
                                  ctxt,
                                  root,
                                  err);
@@ -604,7 +609,7 @@ static void osinfo_loader_product(OsinfoLoader *loader,
 
     osinfo_loader_product_relshp(loader, product,
                                  OSINFO_PRODUCT_RELATIONSHIP_UPGRADES,
-                                 "./upgrades",
+                                 "upgrades",
                                  ctxt,
                                  root,
                                  err);
@@ -1449,117 +1454,43 @@ static void osinfo_loader_root(OsinfoLoader *loader,
      * After loop, return success if no error
      * If there was an error, clean up lib data acquired so far
      */
-    xmlNodePtr *oss = NULL;
-    xmlNodePtr *devices = NULL;
-    xmlNodePtr *platforms = NULL;
-    xmlNodePtr *deployments = NULL;
-    xmlNodePtr *installScripts = NULL;
-    xmlNodePtr *dataMaps = NULL;
-    int i;
-    int ndeployment;
-    int nos;
-    int ndevice;
-    int nplatform;
-    int ninstallScript;
-    int ndataMaps;
-    xmlXPathCompExprPtr comp;
+    xmlNodePtr it;
 
     if (!xmlStrEqual(root->name, BAD_CAST "libosinfo")) {
         OSINFO_ERROR(err, _("Incorrect root element"));
         return;
     }
 
-    comp = osinfo_loader_get_comp_xpath(loader, "./device");
-    ndevice = osinfo_loader_nodeset(comp, ctxt, &devices, err);
-    if (error_is_set(err))
-        goto cleanup;
+    for(it = root->children; it; it = it->next) {
+        if (it->type != XML_ELEMENT_NODE)
+            continue;
 
-    for (i = 0 ; i < ndevice ; i++) {
         xmlNodePtr saved = ctxt->node;
-        ctxt->node = devices[i];
-        osinfo_loader_device(loader, ctxt, devices[i], err);
-        ctxt->node = saved;
-        if (error_is_set(err))
-            goto cleanup;
-    }
-
-    comp = osinfo_loader_get_comp_xpath(loader, "./platform");
-    nplatform = osinfo_loader_nodeset(comp, ctxt, &platforms, err);
-    if (error_is_set(err))
-        goto cleanup;
+        ctxt->node = it;
 
-    for (i = 0 ; i < nplatform ; i++) {
-        xmlNodePtr saved = ctxt->node;
-        ctxt->node = platforms[i];
-        osinfo_loader_platform(loader, ctxt, platforms[i], err);
-        ctxt->node = saved;
-        if (error_is_set(err))
-            goto cleanup;
-    }
+        if (xmlStrEqual(it->name, BAD_CAST "device"))
+            osinfo_loader_device(loader, ctxt, it, err);
 
-    comp = osinfo_loader_get_comp_xpath(loader, "./os");
-    nos = osinfo_loader_nodeset(comp, ctxt, &oss, err);
-    if (error_is_set(err))
-        goto cleanup;
+        else if (xmlStrEqual(it->name, BAD_CAST "platform"))
+            osinfo_loader_platform(loader, ctxt, it, err);
 
-    for (i = 0 ; i < nos ; i++) {
-        xmlNodePtr saved = ctxt->node;
-        ctxt->node = oss[i];
-        osinfo_loader_os(loader, ctxt, oss[i], err);
-        ctxt->node = saved;
-        if (error_is_set(err))
-            goto cleanup;
-    }
+        else if (xmlStrEqual(it->name, BAD_CAST "os"))
+            osinfo_loader_os(loader, ctxt, it, err);
 
-    comp = osinfo_loader_get_comp_xpath(loader, "./deployment");
-    ndeployment = osinfo_loader_nodeset(comp, ctxt, &deployments, err);
-    if (error_is_set(err))
-        goto cleanup;
+        else if (xmlStrEqual(it->name, BAD_CAST "deployment"))
+            osinfo_loader_deployment(loader, ctxt, it, err);
 
-    for (i = 0 ; i < ndeployment ; i++) {
-        xmlNodePtr saved = ctxt->node;
-        ctxt->node = deployments[i];
-        osinfo_loader_deployment(loader, ctxt, deployments[i], err);
-        ctxt->node = saved;
-        if (error_is_set(err))
-            goto cleanup;
-    }
+        else if (xmlStrEqual(it->name, BAD_CAST "install-script"))
+            osinfo_loader_install_script(loader, ctxt, it, err);
 
-    comp = osinfo_loader_get_comp_xpath(loader, "./install-script");
-    ninstallScript = osinfo_loader_nodeset(comp, ctxt, &installScripts, err);
-    if (error_is_set(err))
-        goto cleanup;
+        else if (xmlStrEqual(it->name, BAD_CAST "datamap"))
+            osinfo_loader_datamap(loader, ctxt, it, err);
 
-    for (i = 0 ; i < ninstallScript ; i++) {
-        xmlNodePtr saved = ctxt->node;
-        ctxt->node = installScripts[i];
-        osinfo_loader_install_script(loader, ctxt, installScripts[i], err);
         ctxt->node = saved;
-        if (error_is_set(err))
-            goto cleanup;
-    }
-
-    comp = osinfo_loader_get_comp_xpath(loader, "./datamap");
-    ndataMaps = osinfo_loader_nodeset(comp, ctxt, &dataMaps, err);
-    if (error_is_set(err))
-        goto cleanup;
 
-    for (i = 0 ; i < ndataMaps ; i++) {
-        xmlNodePtr saved = ctxt->node;
-        ctxt->node = dataMaps[i];
-        osinfo_loader_datamap(loader, ctxt, dataMaps[i], err);
-        ctxt->node = saved;
         if (error_is_set(err))
-            goto cleanup;
+            return;
     }
-
- cleanup:
-    g_free(dataMaps);
-    g_free(installScripts);
-    g_free(deployments);
-    g_free(platforms);
-    g_free(oss);
-    g_free(devices);
 }
 
 static void
-- 
1.9.3




More information about the Libosinfo mailing list