Changeset 18954 in josm


Ignore:
Timestamp:
2024-01-23T15:17:43+01:00 (10 months ago)
Author:
GerdP
Message:

fix #23427 load session performance strongly depends on order of layers
The patch

  • removes the duplicated code regarding XPath and calls existing method OsmDataSessionImporter.extractFileName() instead
  • improves that method to first try to find a "file" node given the current element before falling back to the possibly very slow XPath evaluate call (not sure if the XPath part is still needed, it is never used in my example).
Location:
trunk/src/org/openstreetmap/josm/io/session
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/session/GpxTracksSessionImporter.java

    r18466 r18954  
    66import java.io.IOException;
    77import java.io.InputStream;
    8 
    9 import javax.xml.xpath.XPath;
    10 import javax.xml.xpath.XPathConstants;
    11 import javax.xml.xpath.XPathExpression;
    12 import javax.xml.xpath.XPathExpressionException;
    13 import javax.xml.xpath.XPathFactory;
    148
    159import org.openstreetmap.josm.gui.io.importexport.GpxImporter;
     
    2115import org.openstreetmap.josm.io.IllegalDataException;
    2216import org.openstreetmap.josm.tools.Logging;
    23 import org.openstreetmap.josm.tools.Utils;
    2417import org.w3c.dom.Element;
    2518import org.w3c.dom.Node;
     
    3932            throw new IllegalDataException(tr("Version ''{0}'' of meta data for gpx track layer is not supported. Expected: 0.1", version));
    4033        }
    41         try {
    42             XPathFactory xPathFactory = XPathFactory.newInstance();
    43             XPath xpath = xPathFactory.newXPath();
    44             XPathExpression fileExp = xpath.compile("file/text()");
    45             String fileStr = (String) fileExp.evaluate(elem, XPathConstants.STRING);
    46             if (Utils.isEmpty(fileStr)) {
    47                 throw new IllegalDataException(tr("File name expected for layer no. {0}", support.getLayerIndex()));
     34        String fileStr = OsmDataSessionImporter.extractFileName(elem, support);
     35
     36        try (InputStream in = support.getInputStream(fileStr)) {
     37            GpxImporter.GpxImporterData importData;
     38
     39            if (NMEAImporter.FILE_FILTER.acceptName(fileStr)) {
     40                importData = NMEAImporter.loadLayers(in, support.getFile(fileStr), support.getLayerName());
     41            } else if (RtkLibImporter.FILE_FILTER.acceptName(fileStr)) {
     42                importData = RtkLibImporter.loadLayers(in, support.getFile(fileStr), support.getLayerName());
     43            } else {
     44                importData = GpxImporter.loadLayers(in, support.getFile(fileStr), support.getLayerName(), progressMonitor);
     45            }
     46            if (importData.getGpxLayer() != null && importData.getGpxLayer().data != null) {
     47                importData.getGpxLayer().data.fromSession = true;
     48            }
     49            NodeList markerNodes = elem.getElementsByTagName("markerLayer");
     50            if (markerNodes.getLength() > 0 && markerNodes.item(0).getNodeType() == Node.ELEMENT_NODE) {
     51                Element markerEl = (Element) markerNodes.item(0);
     52                try {
     53                    int index = Integer.parseInt(markerEl.getAttribute("index"));
     54                    support.addSubLayer(index, importData.getMarkerLayer(), markerEl);
     55                } catch (NumberFormatException ex) {
     56                    Logging.warn(ex);
     57                }
    4858            }
    4959
    50             try (InputStream in = support.getInputStream(fileStr)) {
    51                 GpxImporter.GpxImporterData importData;
    52 
    53                 if (NMEAImporter.FILE_FILTER.acceptName(fileStr)) {
    54                     importData = NMEAImporter.loadLayers(in, support.getFile(fileStr), support.getLayerName());
    55                 } else if (RtkLibImporter.FILE_FILTER.acceptName(fileStr)) {
    56                     importData = RtkLibImporter.loadLayers(in, support.getFile(fileStr), support.getLayerName());
    57                 } else {
    58                     importData = GpxImporter.loadLayers(in, support.getFile(fileStr), support.getLayerName(), progressMonitor);
    59                 }
    60                 if (importData.getGpxLayer() != null && importData.getGpxLayer().data != null) {
    61                     importData.getGpxLayer().data.fromSession = true;
    62                 }
    63                 NodeList markerNodes = elem.getElementsByTagName("markerLayer");
    64                 if (markerNodes.getLength() > 0 && markerNodes.item(0).getNodeType() == Node.ELEMENT_NODE) {
    65                     Element markerEl = (Element) markerNodes.item(0);
    66                     try {
    67                         int index = Integer.parseInt(markerEl.getAttribute("index"));
    68                         support.addSubLayer(index, importData.getMarkerLayer(), markerEl);
    69                     } catch (NumberFormatException ex) {
    70                         Logging.warn(ex);
    71                     }
    72                 }
    73 
    74                 support.addPostLayersTask(importData.getPostLayerTask());
    75                 return getLayer(importData);
    76             }
    77 
    78         } catch (XPathExpressionException e) {
    79             throw new IllegalDataException(e);
     60            support.addPostLayersTask(importData.getPostLayerTask());
     61            return getLayer(importData);
    8062        }
    8163    }
  • trunk/src/org/openstreetmap/josm/io/session/MarkerSessionImporter.java

    r18466 r18954  
    77import java.io.InputStream;
    88
    9 import javax.xml.xpath.XPath;
    10 import javax.xml.xpath.XPathConstants;
    11 import javax.xml.xpath.XPathExpression;
    12 import javax.xml.xpath.XPathExpressionException;
    13 import javax.xml.xpath.XPathFactory;
    14 
    159import org.openstreetmap.josm.gui.io.importexport.GpxImporter;
    1610import org.openstreetmap.josm.gui.layer.Layer;
     
    1913import org.openstreetmap.josm.io.IllegalDataException;
    2014import org.openstreetmap.josm.io.session.SessionReader.ImportSupport;
    21 import org.openstreetmap.josm.tools.Utils;
    2215import org.w3c.dom.Element;
    2316
     
    3427            throw new IllegalDataException(tr("Version ''{0}'' of meta data for marker layer is not supported. Expected: 0.1", version));
    3528        }
    36         try {
    37             XPathFactory xPathFactory = XPathFactory.newInstance();
    38             XPath xpath = xPathFactory.newXPath();
    39             XPathExpression fileExp = xpath.compile("file/text()");
    40             String fileStr = (String) fileExp.evaluate(elem, XPathConstants.STRING);
    41             if (Utils.isEmpty(fileStr)) {
    42                 throw new IllegalDataException(tr("File name expected for layer no. {0}", support.getLayerIndex()));
    43             }
     29        String fileStr = OsmDataSessionImporter.extractFileName(elem, support);
    4430
    45             try (InputStream in = support.getInputStream(fileStr)) {
    46                 GpxImporter.GpxImporterData importData = GpxImporter.loadLayers(in, support.getFile(fileStr), support.getLayerName(),
    47                         progressMonitor);
     31        try (InputStream in = support.getInputStream(fileStr)) {
     32            GpxImporter.GpxImporterData importData = GpxImporter.loadLayers(in, support.getFile(fileStr), support.getLayerName(),
     33                    progressMonitor);
    4834
    49                 support.addPostLayersTask(importData.getPostLayerTask());
     35            support.addPostLayersTask(importData.getPostLayerTask());
    5036
    51                 importData.getGpxLayer().destroy();
    52                 return importData.getMarkerLayer();
    53             }
    54         } catch (XPathExpressionException e) {
    55             throw new IllegalDataException(e);
     37            importData.getGpxLayer().destroy();
     38            return importData.getMarkerLayer();
    5639        }
    5740    }
  • trunk/src/org/openstreetmap/josm/io/session/NoteSessionImporter.java

    r18208 r18954  
    77import java.io.InputStream;
    88
    9 import javax.xml.xpath.XPath;
    10 import javax.xml.xpath.XPathConstants;
    11 import javax.xml.xpath.XPathExpression;
    12 import javax.xml.xpath.XPathExpressionException;
    13 import javax.xml.xpath.XPathFactory;
    14 
    159import org.openstreetmap.josm.gui.io.importexport.NoteImporter;
    1610import org.openstreetmap.josm.gui.layer.Layer;
     
    1913import org.openstreetmap.josm.io.IllegalDataException;
    2014import org.openstreetmap.josm.io.session.SessionReader.ImportSupport;
    21 import org.openstreetmap.josm.tools.Utils;
    2215import org.w3c.dom.Element;
    2316import org.xml.sax.SAXException;
     
    3528            throw new IllegalDataException(tr("Version ''{0}'' of meta data for note layer is not supported. Expected: 0.1", version));
    3629        }
    37         try {
    38             XPathFactory xPathFactory = XPathFactory.newInstance();
    39             XPath xpath = xPathFactory.newXPath();
    40             XPathExpression fileExp = xpath.compile("file/text()");
    41             String fileStr = (String) fileExp.evaluate(elem, XPathConstants.STRING);
    42             if (Utils.isEmpty(fileStr)) {
    43                 throw new IllegalDataException(tr("File name expected for layer no. {0}", support.getLayerIndex()));
    44             }
    4530
    46             NoteImporter importer = new NoteImporter();
    47             try (InputStream in = support.getInputStream(fileStr)) {
    48                 return importer.loadLayer(in, support.getFile(fileStr), support.getLayerName(), progressMonitor);
    49             }
    50         } catch (XPathExpressionException | SAXException e) {
     31        String fileStr = OsmDataSessionImporter.extractFileName(elem, support);
     32
     33        NoteImporter importer = new NoteImporter();
     34        try (InputStream in = support.getInputStream(fileStr)) {
     35            return importer.loadLayer(in, support.getFile(fileStr), support.getLayerName(), progressMonitor);
     36        } catch (SAXException e) {
    5137            throw new IllegalDataException(e);
    5238        }
  • trunk/src/org/openstreetmap/josm/io/session/OsmDataSessionImporter.java

    r18807 r18954  
    2323import org.openstreetmap.josm.io.session.SessionReader.ImportSupport;
    2424import org.openstreetmap.josm.tools.Utils;
     25import org.w3c.dom.Attr;
    2526import org.w3c.dom.Element;
     27import org.w3c.dom.Node;
     28import org.w3c.dom.NodeList;
    2629
    2730/**
     
    3033 */
    3134public class OsmDataSessionImporter implements SessionLayerImporter {
     35
     36    private static Attr x2;
    3237
    3338    @Override
     
    6873    public static String extractFileName(Element elem, ImportSupport support) throws IllegalDataException {
    6974        try {
     75            // see #23427: try first to avoid possibly very slow XPath call
     76            NodeList x = elem.getElementsByTagName("file");
     77            if (x.getLength() > 0 && x.item(0).getNodeType() == Node.ELEMENT_NODE) {
     78                String fileStr = x.item(0).getTextContent();
     79                if (!Utils.isEmpty(fileStr))
     80                    return fileStr;
     81            }
    7082            XPathFactory xPathFactory = XPathFactory.newInstance();
    7183            XPath xpath = xPathFactory.newXPath();
Note: See TracChangeset for help on using the changeset viewer.