Conversation
ForkPDFLayoutTextStripper sorts each article's text positions with TextPositionComparator, which is not transitive (PDFBOX-5308). TimSort rejects such comparators, so List.sort threw IllegalArgumentException after having already reordered part of the list, and writePage only logged it, leaving the page in an arbitrary order. Fall back to IterativeMergeSort, which does not check the comparator contract, the way PDFTextStripper already does. The ERROR log becomes a DEBUG one, since the page is no longer degraded. Fixes spring-projects#2479 Signed-off-by: zxuhan7 <zxuhan7@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2479
Problem
TextPositionComparatoris not transitive (PDFBOX-5308) and TimSort rejects such comparators, soForkPDFLayoutTextStrippercaught the failure and logged it:ArrayList.sortreorders the backing array in place before giving up, so the page was written from a half sorted list. The extracted text was silently in the wrong order, with a stack trace logged for every affected page.Fix
Fall back to a merge sort, which does not check the comparator contract, the way
PDFTextStripperalready does:IterativeMergeSortis public API of thepdfboxartifact this module already depends on. Thetry/catchinwritePageis gone and theERRORlog becomes aDEBUGone, since the page is no longer degraded.PDFLayoutTextStripperByAreainherits the fix throughsuper.writePage().Test
ForkPDFLayoutTextStripperTestslays out a staircase of 32 glyphs that makes the comparator intransitive, 32 being the smallest size for which TimSort checks the contract.IllegalArgumentException: Comparison method violates its general contract!./mvnw -pl document-readers/spring-ai-pdf-document-reader clean package: 43 tests, 0 checkstyle violationsNotes
#2479 suggests two other options.
IterativeMergeSort. It is a non-adaptive bottom-up merge sort:toArray()plusclone(), then ⌈log₂n⌉ full passes with no early exit. TimSort instead detects pre-existing runs, and text positions arrive in content-stream order, which is usually close to reading order.PDFTextStripper. PDFBOX-5308 has been open since 2021, which suggests it is not a bugfix-sized change.Sorting a 2000-glyph page 1000 times on JDK 17.0.19, a rough loop benchmark rather than JMH:
List.sortIterativeMergeSortAlways merge sorting pays that on every page, to avoid a second sort on the rare page that actually trips the comparator. The fallback instead leaves the fast path untouched and keeps the fork behaving exactly like upstream
PDFTextStripper, which matters since the same PDF can be read through either.