Repository navigation
Probe common magic types within their priority band - #172
Merged
Merged
Conversation
Fixes Canon CR2 detection (#141). Tika ships an image/x-canon-cr2 matcher at priority 60 — a TIFF header with a CR marker at offset 8 — deliberately ranked above image/tiff's priority-50 matcher, which also matches every CR2 file. But the generator's common-types optimization moved the curated common matchers to the front of the whole MAGIC table, so image/tiff probed at index 23 while the CR2 matcher sat at 125 and never won. Every CR2 file content-detected as image/tiff. Fold the common-types rank into the priority sort instead: common types are now probed first within their own priority band, but can no longer preempt matchers Tika explicitly ranks above them. The ~120 above-50 matchers now probe ahead of the common block — a handful of bounded byte comparisons per call, on an operation documented as slow — and every declared priority is respected. Also resolve the .cr2 type-name split: the runtime override mapped both cr2 and crw to the generic image/x-raw-canon, so extension and name lookups disagreed with the (now working) content magic. The .cr2 extension belongs to image/x-canon-cr2, which already subclasses image/tiff, so declared-type and filename refinement keep working. image/x-raw-canon keeps CRW — Tika's own type for it, with its CIFF magic — plus the image/tiff parent link for refinement. Fixtures are minimal synthesized headers (TIFF byte order, CR version marker, empty IFD; CIFF HEAPCCDR for CRW) in both endiannesses, authored for this suite. The existing .cr2-named fixtures move from image/x-raw-canon to image/x-canon-cr2 to match the reconciled type. Fixes #141
This was referenced Aug 21, 2026
jeremy
added a commit
that referenced
this pull request
Aug 21, 2026
…tchers #172 fixed CR2 detection by folding the common-types rank into the priority sort, so common types were only probed first within their own priority band. That put the ~120 matchers Tika ranks above 50 ahead of every common type: detecting a JPEG went from 25 reads to 255, PNG from 26 to 256, PDF from 72 to 293, with wall time rising accordingly on every call. Move the common types back to the front of the table, but have each one carry along the higher-priority matchers for its own subtypes, so promoting a common type can never preempt a more specific verdict for the same content. image/x-canon-cr2 (priority 60, a subtype of image/tiff) still probes immediately ahead of image/tiff, the MP4 and HEIF family ahead of video/quicktime's broad ftyp match, and the Ogg codecs ahead of their containers. Parent links are now collected for every type during generation, not only glob-bearing ones, so the subtype walk sees the whole hierarchy. JPEG, PNG and GIF are back to 25, 26 and 28 reads at the same table positions as before #172; PDF takes 85, the cost of the carried matchers. A generator test pins the ordering rule and a read-budget test pins the fast path.
This was referenced Aug 22, 2026
Merged
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 #141.
Root cause — an ordering bug, not a missing matcher
Tika ships an
image/x-canon-cr2matcher at priority 60 (TIFF header +CRmarker at offset 8), deliberately ranked aboveimage/tiff's priority-50 matcher — which also matches every CR2 file's TIFF header. But the generator's common-types optimization moved its curated matchers to the front of the wholeMAGICtable:image/tiffprobed at index 23 while the CR2 matcher sat at 125 and could never win. Every CR2 file content-detected asimage/tiff— exactly the issue's reproduction.Fix
Fold the common-types rank into the priority sort key: common types now probe first within their own priority band but can no longer preempt matchers Tika explicitly ranks above them. Every declared priority is respected; the fix covers all ~120 above-50 matchers shadowed the same way, not just CR2. Cost: a bounded handful of extra byte comparisons before the common block on an operation
by_magicdocuments as slow.Type-name split
The runtime override mapped both
cr2andcrwto genericimage/x-raw-canon, so extension/name lookups disagreed with the (now working) content magic:.cr2→image/x-canon-cr2(Tika's specific type, already a subclass ofimage/tiff, so declared-type and filename refinement keep working — the issue's ownfile(1)reference output).crwstaysimage/x-raw-canon— Tika's own type for CIFF, with itsHEAPCCDRmagic — keeping theimage/tiffparent link for refinementVerification
image/x-canon-cr2in both endiannesses; plain TIFF stillimage/tiff; CRW stillimage/x-raw-canonfor(io, name: "photo.cr2", declared_type: "image/tiff")→image/x-canon-cr2HEAPCCDR), authored for this suite — no third-party image contenttables:checkgreen