naruto-lgtm commented on code in PR #1582:
URL: https://github.com/apache/poi/pull/1582#discussion_r4651370799


##########
poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java:
##########
@@ -713,6 +718,7 @@ protected void read(POIXMLFactory factory, Map<PackagePart, 
POIXMLDocumentPart>
 
         PackageRelationshipCollection rels = packagePart.getRelationships();
         List<POIXMLDocumentPart> readLater = new ArrayList<>();
+        childRelationships = rels;

Review Comment:
   Done, that's much better. read() now records the part it is about to create 
and its relationship right before createDocumentPart, and 
getPackageRelationship returns that. No map at all. I kept the part next to the 
relationship so the "does this match" check is an identity comparison instead 
of resolving the target a second time.



##########
poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java:
##########
@@ -769,11 +775,42 @@ protected void read(POIXMLFactory factory, 
Map<PackagePart, POIXMLDocumentPart>
             }
         }
 
+        childRelationships = null;

Review Comment:
   Both fields are cleared in a finally around createDocumentPart now, so the 
chart-skip continue and any rethrow clear them too. With a single relationship 
instead of a map there's also nothing large left behind.



##########
poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java:
##########
@@ -769,11 +775,42 @@ protected void read(POIXMLFactory factory, 
Map<PackagePart, POIXMLDocumentPart>
             }
         }
 
+        childRelationships = null;
+        childRelationshipsByTarget = null;
+
         for (POIXMLDocumentPart childPart : readLater) {
             childPart.read(factory, context);
         }
     }
 
+    /**
+     * Returns the relationship of this part to a child part, while {@link 
#read} creates the child parts.
+     * {@link POIXMLFactory#getPackageRelationship} is called for each new 
child part, so the relationships
+     * are indexed by their target once instead of being searched for each 
child part.
+     *
+     * @param part the child part
+     * @return the first relationship that targets the part (ignoring case), 
or {@code null} if there is none
+     *  or the child parts aren't being read
+     */
+    /* package */ PackageRelationship findChildRelationship(PackagePart part) {
+        if (childRelationships == null) {
+            return null;
+        }
+        if (childRelationshipsByTarget == null) {
+            childRelationshipsByTarget = new HashMap<>();

Review Comment:
   Gone with the map. The relationship comes straight out of read()'s loop, 
which only reaches createDocumentPart for internal targets, so external and 
hyperlink relationships are never resolved or stored.



##########
poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java:
##########
@@ -769,11 +775,42 @@ protected void read(POIXMLFactory factory, 
Map<PackagePart, POIXMLDocumentPart>
             }
         }
 
+        childRelationships = null;
+        childRelationshipsByTarget = null;
+
         for (POIXMLDocumentPart childPart : readLater) {
             childPart.read(factory, context);
         }
     }
 
+    /**
+     * Returns the relationship of this part to a child part, while {@link 
#read} creates the child parts.
+     * {@link POIXMLFactory#getPackageRelationship} is called for each new 
child part, so the relationships
+     * are indexed by their target once instead of being searched for each 
child part.
+     *
+     * @param part the child part
+     * @return the first relationship that targets the part (ignoring case), 
or {@code null} if there is none
+     *  or the child parts aren't being read
+     */
+    /* package */ PackageRelationship findChildRelationship(PackagePart part) {
+        if (childRelationships == null) {
+            return null;
+        }
+        if (childRelationshipsByTarget == null) {
+            childRelationshipsByTarget = new HashMap<>();
+            for (PackageRelationship rel : childRelationships) {
+                try {
+                    
childRelationshipsByTarget.putIfAbsent(rel.getTargetURI().toASCIIString().toLowerCase(Locale.ROOT),
 rel);

Review Comment:
   Moot now, nothing resolves a target a second time, so there's no 
IllegalArgumentException to swallow and the fallback search keeps its old 
id-order-and-throw behaviour untouched.



##########
poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java:
##########
@@ -769,11 +775,42 @@ protected void read(POIXMLFactory factory, 
Map<PackagePart, POIXMLDocumentPart>
             }
         }
 
+        childRelationships = null;
+        childRelationshipsByTarget = null;
+
         for (POIXMLDocumentPart childPart : readLater) {
             childPart.read(factory, context);
         }
     }
 
+    /**
+     * Returns the relationship of this part to a child part, while {@link 
#read} creates the child parts.
+     * {@link POIXMLFactory#getPackageRelationship} is called for each new 
child part, so the relationships
+     * are indexed by their target once instead of being searched for each 
child part.
+     *
+     * @param part the child part
+     * @return the first relationship that targets the part (ignoring case), 
or {@code null} if there is none
+     *  or the child parts aren't being read
+     */
+    /* package */ PackageRelationship findChildRelationship(PackagePart part) {
+        if (childRelationships == null) {
+            return null;
+        }
+        if (childRelationshipsByTarget == null) {
+            childRelationshipsByTarget = new HashMap<>();
+            for (PackageRelationship rel : childRelationships) {
+                try {
+                    
childRelationshipsByTarget.putIfAbsent(rel.getTargetURI().toASCIIString().toLowerCase(Locale.ROOT),
 rel);
+                } catch (IllegalArgumentException e) {
+                    // the target can't be resolved, a part can't be found by 
it
+                }
+            }
+        }
+        PackageRelationship rel = 
childRelationshipsByTarget.get(part.getPartName().getName().toLowerCase(Locale.ROOT));
+        // only return a relationship the part still has
+        return (rel != null && rel == 
getPackagePart().getRelationship(rel.getId())) ? rel : null;

Review Comment:
   Dropped, along with the map and that wording.



##########
poi-ooxml/src/test/java/org/apache/poi/ooxml/TestPOIXMLDocument.java:
##########
@@ -293,6 +299,53 @@ void dontParseEmbeddedDocuments() throws IOException {
         }
     }
 
+    /**
+     * The relationship of each new child part was looked up by searching all 
relationships of
+     * the parent, which made loading quadratic in the number of child parts 
and relationships
+     */
+    @Test
+    void testManyChildPartsAndRelationships() throws IOException {
+        final int childParts = 900;
+        final String relNs = 
"http://schemas.openxmlformats.org/package/2006/relationships";;
+        UnsynchronizedByteArrayOutputStream bos = 
UnsynchronizedByteArrayOutputStream.builder().get();
+        try (ZipOutputStream zos = new ZipOutputStream(bos)) {
+            addEntry(zos, "[Content_Types].xml",
+                "<Types 
xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\";>"
+                + "<Default Extension=\"rels\" 
ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/>"
+                + "<Default Extension=\"xml\" 
ContentType=\"application/xml\"/></Types>");
+            addEntry(zos, "_rels/.rels", "<Relationships xmlns=\"" + relNs + 
"\">"
+                + "<Relationship Id=\"rId1\" Type=\"" + 
PackageRelationshipTypes.CORE_DOCUMENT + "\" Target=\"main.xml\"/>"
+                + "</Relationships>");
+            addEntry(zos, "main.xml", "<main/>");
+            StringBuilder rels = new StringBuilder("<Relationships xmlns=\"" + 
relNs + "\">");
+            for (int i = 0; i < childParts; i++) {
+                rels.append("<Relationship Id=\"rIdP").append(i)
+                    .append("\" Type=\"urn:child\" 
Target=\"child/p").append(i).append(".xml\"/>");
+                addEntry(zos, "child/p" + i + ".xml", "<p/>");
+            }
+            for (int i = 0; i < 50_000; i++) {
+                rels.append("<Relationship Id=\"rIdE").append(i)
+                    .append("\" Type=\"urn:link\" TargetMode=\"External\" 
Target=\"https://example.com/";).append(i).append("\"/>");
+            }
+            addEntry(zos, "_rels/main.xml.rels", 
rels.append("</Relationships>").toString());
+        }
+

Review Comment:
   Timeout is gone. Input is down to 50 child parts and 2,000 external 
relationships, and the test asserts the mapping rather than wall-clock time.



##########
poi-ooxml/src/test/java/org/apache/poi/ooxml/TestPOIXMLDocument.java:
##########
@@ -293,6 +299,53 @@ void dontParseEmbeddedDocuments() throws IOException {
         }
     }
 
+    /**
+     * The relationship of each new child part was looked up by searching all 
relationships of
+     * the parent, which made loading quadratic in the number of child parts 
and relationships
+     */
+    @Test
+    void testManyChildPartsAndRelationships() throws IOException {
+        final int childParts = 900;
+        final String relNs = 
"http://schemas.openxmlformats.org/package/2006/relationships";;
+        UnsynchronizedByteArrayOutputStream bos = 
UnsynchronizedByteArrayOutputStream.builder().get();
+        try (ZipOutputStream zos = new ZipOutputStream(bos)) {
+            addEntry(zos, "[Content_Types].xml",
+                "<Types 
xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\";>"
+                + "<Default Extension=\"rels\" 
ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/>"
+                + "<Default Extension=\"xml\" 
ContentType=\"application/xml\"/></Types>");
+            addEntry(zos, "_rels/.rels", "<Relationships xmlns=\"" + relNs + 
"\">"
+                + "<Relationship Id=\"rId1\" Type=\"" + 
PackageRelationshipTypes.CORE_DOCUMENT + "\" Target=\"main.xml\"/>"
+                + "</Relationships>");
+            addEntry(zos, "main.xml", "<main/>");
+            StringBuilder rels = new StringBuilder("<Relationships xmlns=\"" + 
relNs + "\">");
+            for (int i = 0; i < childParts; i++) {
+                rels.append("<Relationship Id=\"rIdP").append(i)
+                    .append("\" Type=\"urn:child\" 
Target=\"child/p").append(i).append(".xml\"/>");
+                addEntry(zos, "child/p" + i + ".xml", "<p/>");
+            }
+            for (int i = 0; i < 50_000; i++) {
+                rels.append("<Relationship Id=\"rIdE").append(i)
+                    .append("\" Type=\"urn:link\" TargetMode=\"External\" 
Target=\"https://example.com/";).append(i).append("\"/>");
+            }
+            addEntry(zos, "_rels/main.xml.rels", 
rels.append("</Relationships>").toString());
+        }
+
+        assertTimeoutPreemptively(Duration.ofSeconds(20), () -> {
+            try (OPCPackage pkg = OPCPackage.open(bos.toInputStream());
+                 OPCParser doc = new OPCParser(pkg)) {
+                doc.parse(new TestFactory());

Review Comment:
   Added. Each child relationship now carries its own type (urn:child:<i>), and 
the test asserts both the type getPackageRelationship returned for each part 
and that getRelationId(child) is rIdP<i>. I checked it by making 
getPackageRelationship return the parent's first relationship, which fails it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to