""" Smart Hash Capture with 3-tier strategy. This module provides intelligent hash capture that avoids expensive calculation when possible: Tier 1: DAT file lookup (0.001s - 95% of files) Tier 2: Hash cache (0.1s - 4% of files) Tier 3: Calculate (120s - 1% of files) Expected speedup: 20x-50x for source hash capture. """ import hashlib import time from dataclasses import dataclass from datetime import datetime from pathlib import Path from rich.console import Console from rich.progress import Progress, SpinnerColumn, TextColumn from sqlalchemy.orm import Session from .dat_manager import DATManager from .transformation import HashCache console = Console() @dataclass class SourceHashInfo: """Complete hash information for a source file.""" md5: str | None = None sha1: str | None = None sha256: str | None = None crc32: str | None = None size: int | None = None # Metadata from_dat: bool = False # Was this from a DAT file? dat_name: str | None = None # Which DAT? from_cache: bool = False # Was this from cache? calculated: bool = False # Was this calculated? calculation_time: float = 0.0 # Seconds def __repr__(self): source = "DAT" if self.from_dat else "Cache" if self.from_cache else "Calculated" return f"" class SmartHashCapture: """ Intelligent hash capture with 3-tier fallback strategy. This class tries to avoid expensive hash calculation by: 1. Looking up in DAT files (instant) 2. Looking up in hash cache (fast) 3. Calculating only when necessary (slow) Example: capture = SmartHashCapture(dat_manager, db_session) # Try to get hashes for a file hashes = capture.get_source_hashes( Path("/path/to/roms/Halo 3 (USA).iso"), system="xbox360" ) if hashes.from_dat: print("Found in DAT! Instant!") elif hashes.from_cache: print("Found in cache! Fast!") else: print(f"Had to calculate (took {hashes.calculation_time:.1f}s)") """ def __init__(self, dat_manager: DATManager | None, session: Session): """ Initialize smart hash capture. Args: dat_manager: DAT manager for Tier 1 lookups (optional) session: Database session for cache operations """ self.dat_manager = dat_manager self.session = session # Statistics self.stats = { "dat_hits": 0, "cache_hits": 0, "calculated": 0, "total_calculation_time": 0.0, } def get_source_hashes( self, file_path: Path, system: str | None = None, force_calculate: bool = False ) -> SourceHashInfo: """ Get hashes for a source file using 3-tier strategy. Args: file_path: Path to file to hash system: System name (e.g., "xbox360") for DAT lookup force_calculate: Skip DAT/cache and force calculation Returns: SourceHashInfo with hashes and metadata """ if not file_path.exists(): raise FileNotFoundError(f"File not found: {file_path}") file_size = file_path.stat().st_size # Tier 1: Try DAT lookup if not force_calculate and self.dat_manager and system: dat_entry = self.dat_manager.lookup_by_filename(file_path.name, system) if dat_entry: self.stats["dat_hits"] += 1 return SourceHashInfo( md5=dat_entry.md5, sha1=dat_entry.sha1, sha256=dat_entry.sha256, crc32=dat_entry.crc32, size=dat_entry.size, from_dat=True, dat_name=dat_entry.dat_name, ) # Tier 2: Try cache lookup if not force_calculate: cached = self._lookup_cache(file_path, file_size) if cached: self.stats["cache_hits"] += 1 return cached # Tier 3: Calculate calculated = self._calculate_hashes(file_path, file_size) self.stats["calculated"] += 1 self.stats["total_calculation_time"] += calculated.calculation_time # Store in cache for future lookups self._store_cache(file_path, file_size, calculated) return calculated def _lookup_cache(self, file_path: Path, file_size: int) -> SourceHashInfo | None: """ Look up hashes in cache. Cache is valid if file path, size, and mtime match. """ file_mtime = datetime.fromtimestamp(file_path.stat().st_mtime) cached = ( self.session.query(HashCache) .filter_by( file_path=str(file_path), file_size=file_size, file_mtime=file_mtime, ) .first() ) if cached: return SourceHashInfo( md5=cached.md5, sha1=cached.sha1, sha256=cached.sha256, crc32=cached.crc32, size=file_size, from_cache=True, ) return None def _calculate_hashes(self, file_path: Path, file_size: int) -> SourceHashInfo: """ Calculate all hashes for a file. This is the slow path - only used for files not in DAT or cache. Shows progress for large files (>100MB). """ start_time = time.time() # Initialize hashers md5 = hashlib.md5() sha1 = hashlib.sha1() sha256 = hashlib.sha256() crc32 = 0 # Read file in chunks chunk_size = 8192 * 1024 # 8MB chunks show_progress = file_size > 100 * 1024 * 1024 # Show for files >100MB if show_progress: with Progress( SpinnerColumn(), TextColumn("[progress.description]{task.description}"), console=console, ) as progress: progress.add_task(f"Calculating hashes for {file_path.name}...", total=None) with open(file_path, "rb") as f: while chunk := f.read(chunk_size): md5.update(chunk) sha1.update(chunk) sha256.update(chunk) crc32 = self._update_crc32(crc32, chunk) else: with open(file_path, "rb") as f: while chunk := f.read(chunk_size): md5.update(chunk) sha1.update(chunk) sha256.update(chunk) crc32 = self._update_crc32(crc32, chunk) calculation_time = time.time() - start_time return SourceHashInfo( md5=md5.hexdigest(), sha1=sha1.hexdigest(), sha256=sha256.hexdigest(), crc32=f"{crc32:08x}", size=file_size, calculated=True, calculation_time=calculation_time, ) def _update_crc32(self, crc: int, data: bytes) -> int: """Update CRC32 checksum.""" import zlib return zlib.crc32(data, crc) & 0xFFFFFFFF def _store_cache(self, file_path: Path, file_size: int, hashes: SourceHashInfo): """Store calculated hashes in cache.""" file_mtime = datetime.fromtimestamp(file_path.stat().st_mtime) # Check if cache entry exists cached = self.session.query(HashCache).filter_by(file_path=str(file_path)).first() if cached: # Update existing cached.file_size = file_size cached.file_mtime = file_mtime cached.md5 = hashes.md5 cached.sha1 = hashes.sha1 cached.sha256 = hashes.sha256 cached.crc32 = hashes.crc32 cached.calculation_time_seconds = hashes.calculation_time else: # Create new cached = HashCache( file_path=str(file_path), file_size=file_size, file_mtime=file_mtime, md5=hashes.md5, sha1=hashes.sha1, sha256=hashes.sha256, crc32=hashes.crc32, calculation_time_seconds=hashes.calculation_time, ) self.session.add(cached) self.session.commit() def get_stats(self) -> dict: """Get statistics on cache performance.""" total = sum([self.stats["dat_hits"], self.stats["cache_hits"], self.stats["calculated"]]) if total == 0: return { "total_lookups": 0, "dat_hit_rate": 0.0, "cache_hit_rate": 0.0, "calculation_rate": 0.0, "avg_calculation_time": 0.0, } return { "total_lookups": total, "dat_hits": self.stats["dat_hits"], "cache_hits": self.stats["cache_hits"], "calculated": self.stats["calculated"], "dat_hit_rate": self.stats["dat_hits"] / total * 100, "cache_hit_rate": self.stats["cache_hits"] / total * 100, "calculation_rate": self.stats["calculated"] / total * 100, "avg_calculation_time": ( self.stats["total_calculation_time"] / self.stats["calculated"] if self.stats["calculated"] > 0 else 0.0 ), "total_calculation_time": self.stats["total_calculation_time"], } def print_stats(self): """Print cache performance statistics.""" stats = self.get_stats() if stats["total_lookups"] == 0: console.print("[yellow]No hash lookups performed yet[/yellow]") return console.print("\n[bold cyan]Hash Capture Performance:[/bold cyan]") console.print(f" Total Lookups: {stats['total_lookups']}") console.print(f" DAT Hits: {stats['dat_hits']} ({stats['dat_hit_rate']:.1f}%)") console.print(f" Cache Hits: {stats['cache_hits']} ({stats['cache_hit_rate']:.1f}%)") console.print(f" Calculated: {stats['calculated']} ({stats['calculation_rate']:.1f}%)") if stats["calculated"] > 0: console.print(f" Avg Calculation Time: {stats['avg_calculation_time']:.1f}s") console.print(f" Total Calculation Time: {stats['total_calculation_time']:.1f}s") # Show estimated speedup if stats["dat_hit_rate"] > 0: speedup = 100 / stats["calculation_rate"] if stats["calculation_rate"] > 0 else 0 console.print(f" [green]Estimated Speedup: {speedup:.1f}x[/green]")