""" ROM Cache Database Models SQLAlchemy models for tracking cached ROM transformations, including both single-file (ROMCache) and folder-based (TreeCache) outputs. """ from datetime import datetime from sqlalchemy import ( BigInteger, Column, DateTime, Index, Integer, String, Text, UniqueConstraint, ) from ..metadata.database import Base class ROMCache(Base): """ Cached ROM transformation entry. Stores transformed ROM files (CHD, RVZ, 7z, etc.) keyed by source hash and transformation parameters. This enables: 1. Build acceleration: Skip re-processing files we've already built 2. Disk savings: Hardlink outputs to cached files instead of copying 3. Cross-build sharing: Same CHD works for .eng and .all builds Cache Key = (source_md5, format, params_hash) Example: source_md5='abc123', format='chd', params_hash='lzma_default' → cache/chd/ab/abc123_lzma_default.chd """ __tablename__ = "rom_cache" id = Column(Integer, primary_key=True) # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # Cache Key (unique identifier for this cached file) # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ source_md5 = Column(String(32), nullable=False, index=True) format = Column(String(16), nullable=False) # 'chd', 'rvz', '7z', 'cso', 'xiso' params_hash = Column(String(32), nullable=False) # Hash of compression params # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # Compression/Transformation Details # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ compression_params = Column(Text) # JSON: {"codec": "lzma", "level": 9} tool_name = Column(String(64)) # 'chdman', '7z', 'dolphin-tool' tool_version = Column(String(64)) # 'mame0262', '24.09' # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # Cached File Info # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ cache_path = Column(String(512), nullable=False) # Relative: 'chd/ab/abc123_lzma.chd' final_md5 = Column(String(32)) # For integrity verification final_size = Column(BigInteger) # File size in bytes # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # Source Info (for debugging/display) # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ source_filename = Column(String(512)) # Original filename for display source_size = Column(BigInteger) # Original file size # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # ZIP Identity (for pre-extraction cache lookup) # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # Torrentzipped archives have stable CRC32 values that can be # read from ZIP headers without extraction. This enables fast # cache lookups before extraction: read ZIP header → lookup cache. source_zip_crc32 = Column(String(8)) # CRC32 from ZIP header, e.g., "2578c3f9" source_zip_content_size = Column(BigInteger) # Uncompressed size from ZIP header # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # Timestamps # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ created_date = Column(DateTime, default=datetime.utcnow) last_used = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # Constraints and Indexes # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ __table_args__ = ( # Unique cache key UniqueConstraint("source_md5", "format", "params_hash", name="uq_cache_key"), # Index for cache lookups Index("ix_cache_lookup", "source_md5", "format", "params_hash"), # Index for filename-based pre-check (skip extraction) Index("ix_cache_filename", "source_filename", "source_size", "format", "params_hash"), # Index for ZIP identity lookup (pre-extraction cache check) Index( "ix_cache_zip_identity", "source_zip_crc32", "source_zip_content_size", "format", "params_hash", ), # Index for finding unused entries (for pruning) Index("ix_cache_last_used", "last_used"), ) def __repr__(self) -> str: return ( f"" ) @property def cache_key(self) -> str: """Return the unique cache key.""" return f"{self.source_md5}_{self.format}_{self.params_hash}" class TreeCache(Base): """ Cached tree (folder) transformation entry. Stores folder-based outputs (PS3 JB folders, daphne, scummvm, etc.) keyed by source hash and transformation parameters. The tree_hash references a TreeManifest in the CAS that lists all individual files. This enables: 1. Build acceleration: Skip re-processing folder outputs 2. Disk savings: Hardlink individual files from CAS 3. Cross-build sharing: Same PS3 folder works for multiple builds 4. Deduplication: Shared files across games (firmware, DLLs) stored once Cache Key = (source_md5, format, params_hash) Tree Hash = SHA-256 of sorted (path, hash, size) tuples """ __tablename__ = "tree_cache" id = Column(Integer, primary_key=True) # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # Cache Key (matches ROMCache pattern) # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ source_md5 = Column(String(32), nullable=False, index=True) format = Column(String(32), nullable=False) # 'ps3-jb', 'daphne', 'scummvm', 'xbox360-god' params_hash = Column(String(32), nullable=False) # Hash of transform params # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # Tree Manifest Reference # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ tree_hash = Column(String(64), nullable=False, index=True) # SHA-256 of manifest total_files = Column(Integer) # Number of files in tree total_size = Column(BigInteger) # Total size in bytes folder_name = Column(String(512)) # Output folder name (e.g., "BLUS30455.ps3") # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # Transformation Details # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ platform = Column(String(32)) # 'ps3', 'daphne', 'scummvm', 'xbox360' tool_name = Column(String(64)) # 'ps3dec+7z', 'extract', etc. tool_version = Column(String(64)) transformation_params = Column(Text) # JSON params # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # Source Info # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ source_filename = Column(String(512)) source_size = Column(BigInteger) # ZIP identity for pre-extraction lookup source_zip_crc32 = Column(String(8)) source_zip_content_size = Column(BigInteger) # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # Timestamps # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ created_date = Column(DateTime, default=datetime.utcnow) last_used = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ # Constraints and Indexes # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ __table_args__ = ( UniqueConstraint("source_md5", "format", "params_hash", name="uq_tree_cache_key"), Index("ix_tree_cache_lookup", "source_md5", "format", "params_hash"), Index("ix_tree_cache_hash", "tree_hash"), Index("ix_tree_cache_filename", "source_filename", "source_size", "format", "params_hash"), Index( "ix_tree_cache_zip_identity", "source_zip_crc32", "source_zip_content_size", "format", "params_hash", ), Index("ix_tree_cache_last_used", "last_used"), ) def __repr__(self) -> str: return ( f"" ) @property def cache_key(self) -> str: """Return the unique cache key.""" return f"{self.source_md5}_{self.format}_{self.params_hash}"