Coverage for src/romfarmer/metadata/transformation.py: 95%
66 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-05 02:19 -0700
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-05 02:19 -0700
1"""
2ROM Transformation Tracking
4Tracks the transformation chain from source ROMs (Redump, No-Intro) to final
5processed files (XISO, CHD, CSO, etc.). This enables smart scraping by
6maintaining hash relationships between original and processed files.
7"""
9from datetime import datetime
11from sqlalchemy import (
12 JSON,
13 BigInteger,
14 Boolean,
15 Column,
16 DateTime,
17 Float,
18 ForeignKey,
19 Index,
20 Integer,
21 String,
22 UniqueConstraint,
23)
24from sqlalchemy.orm import relationship
26from .database import Base
29class ROMTransformation(Base):
30 """
31 Tracks the transformation from source ROM to final processed file.
33 This allows us to query ScreenScraper with source hashes (what they know)
34 even when we have processed files (XISO, CHD, etc.) that have different hashes.
36 Example:
37 Source: Halo 3 (USA).iso (MD5: abc123, from Redump)
38 Tool: extract-xiso v2.5.0
39 Final: Halo 3 (USA).xiso (MD5: xyz789)
41 When scraping the XISO file, we can look up xyz789 in this table,
42 find the source hash abc123, and query ScreenScraper with that.
43 """
45 __tablename__ = "rom_transformations"
47 id = Column(Integer, primary_key=True)
49 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
50 # Source File (what ScreenScraper knows about)
51 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
53 source_md5 = Column(String(32), index=True, nullable=False)
54 source_sha1 = Column(String(40))
55 source_sha256 = Column(String(64))
56 source_crc32 = Column(String(8))
57 source_file_size = Column(BigInteger)
58 source_file_name = Column(String(512))
60 # Source format and DAT info
61 source_format = Column(String(32)) # "redump-iso", "nointro-zip", etc.
62 source_dat = Column(String(128)) # "Redump - Microsoft Xbox 360", "No-Intro - NES"
63 source_verified = Column(Boolean, default=False) # From official DAT?
65 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
66 # Transformation Metadata
67 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
69 transformation_tool = Column(String(64), nullable=False) # "extract-xiso", "maxcso", "chdman"
70 transformation_version = Column(String(32)) # "v2.5.0"
71 transformation_params = Column(JSON) # {"compression": 9, "level": "max"}
72 transformation_date = Column(DateTime, default=datetime.utcnow)
73 transformation_host = Column(String(128)) # For debugging/tracking
75 # Performance metrics
76 transformation_duration_seconds = Column(Float) # How long it took
78 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
79 # Final File (what you actually have)
80 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
82 final_md5 = Column(
83 String(32), index=True, nullable=False
84 ) # Not unique - multiple sources can → same final
85 final_sha1 = Column(String(40))
86 final_sha256 = Column(String(64))
87 final_crc32 = Column(String(8))
88 final_file_size = Column(BigInteger)
89 final_file_name = Column(String(512))
90 final_format = Column(String(32)) # "xiso", "cso", "chd", "rvz"
92 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
93 # Verification & Community
94 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
96 verified = Column(Boolean, default=False) # Has this been verified?
97 verification_count = Column(Integer, default=0) # How many users confirmed
98 community_reported = Column(Boolean, default=False) # From community DB?
100 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
101 # Relationships
102 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
104 game_id = Column(Integer, ForeignKey("scraped_games.id"))
105 game = relationship("ScrapedGame", back_populates="transformations")
107 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
108 # Indexes for fast lookup
109 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
111 __table_args__ = (
112 # Unique constraint: same source → same final (prevent duplicates)
113 UniqueConstraint("source_md5", "final_md5", name="uq_source_final"),
114 # Fast lookup by source hash (for generating transformations)
115 Index("idx_source_hash", "source_md5", "source_format"),
116 # Fast lookup by final hash (for finding source when scraping)
117 Index("idx_final_hash", "final_md5", "final_format"),
118 # Fast lookup by tool/version (for analysis)
119 Index("idx_tool_version", "transformation_tool", "transformation_version"),
120 # Fast lookup for verified transformations
121 Index("idx_verified", "verified", "source_verified"),
122 )
124 def __repr__(self):
125 return (
126 f"<ROMTransformation("
127 f"source={self.source_md5[:8]}..., "
128 f"tool={self.transformation_tool}, "
129 f"final={self.final_md5[:8]}..."
130 f")>"
131 )
134class HashCache(Base):
135 """
136 Cache calculated hashes to avoid recalculation.
138 Only needed for files NOT in DAT files. Most files (95%+) will have
139 hashes instantly available from Redump/No-Intro DATs.
140 """
142 __tablename__ = "hash_cache"
144 id = Column(Integer, primary_key=True)
146 # File identity (cache key)
147 file_path = Column(String(1024), nullable=False)
148 file_size = Column(BigInteger, nullable=False)
149 file_mtime = Column(DateTime, nullable=False) # Modification time
151 # Cached hashes
152 md5 = Column(String(32), index=True)
153 sha1 = Column(String(40))
154 sha256 = Column(String(64))
155 crc32 = Column(String(8))
157 # Cache metadata
158 calculated_at = Column(DateTime, default=datetime.utcnow)
159 calculation_time_seconds = Column(Float)
161 # Indexes for fast lookups
162 __table_args__ = (
163 # Lookup by file identity
164 Index("idx_cache_file_identity", "file_path", "file_size", "file_mtime"),
165 # Reverse lookup by hash
166 Index("idx_cache_hash_lookup", "md5", "sha1"),
167 )
169 def __repr__(self):
170 return (
171 f"<HashCache("
172 f"file={self.file_path[-50:]}, "
173 f"md5={self.md5[:8] if self.md5 else 'None'}..."
174 f")>"
175 )
178class ZipContentCache(Base):
179 """
180 Cache mapping ZIP file identity → contained file MD5.
182 For torrentzipped archives (Myrient, etc.), we can read the CRC32 from
183 the ZIP header instantly without decompression. This table caches the
184 expensive MD5 calculation of the contained file, keyed by the fast-to-read
185 ZIP metadata.
187 This provides ~69x speedup for large disc-based systems:
188 - Without cache: ~40 min (calculate MD5 for 2000+ GB of ISOs)
189 - With cache: ~35 sec (read ZIP headers + DB lookup)
191 Example:
192 ZIP: "007 - Nightfire (USA).zip" (1.2GB)
193 Content CRC32: 0c702336 (instant to read from header)
194 Content MD5: 9fa391e006b4103160f7eb9059d641cf (slow to calculate)
196 First run: Calculate MD5, store in cache
197 Future runs: Read CRC32 → lookup MD5 → instant!
198 """
200 __tablename__ = "zip_content_cache"
202 id = Column(Integer, primary_key=True)
204 # ZIP file identity (for cache invalidation if file changes)
205 zip_path = Column(String(1024), nullable=False)
206 zip_size = Column(BigInteger, nullable=False)
208 # Content identity from ZIP header (cache key - instant to read)
209 content_filename = Column(String(512), nullable=False)
210 content_crc32 = Column(String(8), nullable=False) # Hex string, e.g., "0c702336"
211 content_size = Column(BigInteger, nullable=False) # Uncompressed size
213 # Cached hash of contained file (expensive to calculate)
214 content_md5 = Column(String(32), nullable=False, index=True)
216 # Cache metadata
217 calculated_at = Column(DateTime, default=datetime.utcnow)
218 calculation_time_seconds = Column(Float)
220 # Indexes for fast lookups
221 __table_args__ = (
222 # Primary lookup: CRC32 + size (unique for torrentzipped files)
223 Index("idx_zip_content_lookup", "content_crc32", "content_size"),
224 # Secondary: by zip path for cache management
225 Index("idx_zip_path", "zip_path"),
226 # Unique constraint: same CRC32 + size should have same MD5
227 UniqueConstraint("content_crc32", "content_size", name="uq_content_identity"),
228 )
230 def __repr__(self):
231 return f"<ZipContentCache(crc32={self.content_crc32}, md5={self.content_md5[:8]}...)>"