Coverage for src/romfarmer/cache/models.py: 0%

53 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-09-01 23:08 -0700

1""" 

2ROM Cache Database Models 

3 

4SQLAlchemy models for tracking cached ROM transformations, 

5including both single-file (ROMCache) and folder-based (TreeCache) outputs. 

6""" 

7 

8from datetime import datetime 

9 

10from sqlalchemy import ( 

11 BigInteger, 

12 Column, 

13 DateTime, 

14 Index, 

15 Integer, 

16 String, 

17 Text, 

18 UniqueConstraint, 

19) 

20 

21from ..metadata.database import Base 

22 

23 

24class ROMCache(Base): 

25 """ 

26 Cached ROM transformation entry. 

27 

28 Stores transformed ROM files (CHD, RVZ, 7z, etc.) keyed by source hash 

29 and transformation parameters. This enables: 

30 

31 1. Build acceleration: Skip re-processing files we've already built 

32 2. Disk savings: Hardlink outputs to cached files instead of copying 

33 3. Cross-build sharing: Same CHD works for .eng and .all builds 

34 

35 Cache Key = (source_md5, format, params_hash) 

36 

37 Example: 

38 source_md5='abc123', format='chd', params_hash='lzma_default' 

39 → cache/chd/ab/abc123_lzma_default.chd 

40 """ 

41 

42 __tablename__ = "rom_cache" 

43 

44 id = Column(Integer, primary_key=True) 

45 

46 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

47 # Cache Key (unique identifier for this cached file) 

48 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

49 

50 source_md5 = Column(String(32), nullable=False, index=True) 

51 format = Column(String(16), nullable=False) # 'chd', 'rvz', '7z', 'cso', 'xiso' 

52 params_hash = Column(String(32), nullable=False) # Hash of compression params 

53 

54 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

55 # Compression/Transformation Details 

56 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

57 

58 compression_params = Column(Text) # JSON: {"codec": "lzma", "level": 9} 

59 tool_name = Column(String(64)) # 'chdman', '7z', 'dolphin-tool' 

60 tool_version = Column(String(64)) # 'mame0262', '24.09' 

61 

62 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

63 # Cached File Info 

64 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

65 

66 cache_path = Column(String(512), nullable=False) # Relative: 'chd/ab/abc123_lzma.chd' 

67 final_md5 = Column(String(32)) # For integrity verification 

68 final_size = Column(BigInteger) # File size in bytes 

69 

70 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

71 # Source Info (for debugging/display) 

72 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

73 

74 source_filename = Column(String(512)) # Original filename for display 

75 source_size = Column(BigInteger) # Original file size 

76 

77 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

78 # ZIP Identity (for pre-extraction cache lookup) 

79 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

80 # Torrentzipped archives have stable CRC32 values that can be 

81 # read from ZIP headers without extraction. This enables fast 

82 # cache lookups before extraction: read ZIP header → lookup cache. 

83 

84 source_zip_crc32 = Column(String(8)) # CRC32 from ZIP header, e.g., "2578c3f9" 

85 source_zip_content_size = Column(BigInteger) # Uncompressed size from ZIP header 

86 

87 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

88 # Timestamps 

89 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

90 

91 created_date = Column(DateTime, default=datetime.utcnow) 

92 last_used = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) 

93 

94 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

95 # Constraints and Indexes 

96 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

97 

98 __table_args__ = ( 

99 # Unique cache key 

100 UniqueConstraint("source_md5", "format", "params_hash", name="uq_cache_key"), 

101 # Index for cache lookups 

102 Index("ix_cache_lookup", "source_md5", "format", "params_hash"), 

103 # Index for filename-based pre-check (skip extraction) 

104 Index("ix_cache_filename", "source_filename", "source_size", "format", "params_hash"), 

105 # Index for ZIP identity lookup (pre-extraction cache check) 

106 Index( 

107 "ix_cache_zip_identity", 

108 "source_zip_crc32", 

109 "source_zip_content_size", 

110 "format", 

111 "params_hash", 

112 ), 

113 # Index for finding unused entries (for pruning) 

114 Index("ix_cache_last_used", "last_used"), 

115 ) 

116 

117 def __repr__(self) -> str: 

118 return ( 

119 f"<ROMCache(source_md5={self.source_md5[:8]}..., " 

120 f"format={self.format}, path={self.cache_path})>" 

121 ) 

122 

123 @property 

124 def cache_key(self) -> str: 

125 """Return the unique cache key.""" 

126 return f"{self.source_md5}_{self.format}_{self.params_hash}" 

127 

128 

129class TreeCache(Base): 

130 """ 

131 Cached tree (folder) transformation entry. 

132 

133 Stores folder-based outputs (PS3 JB folders, daphne, scummvm, etc.) 

134 keyed by source hash and transformation parameters. The tree_hash 

135 references a TreeManifest in the CAS that lists all individual files. 

136 

137 This enables: 

138 1. Build acceleration: Skip re-processing folder outputs 

139 2. Disk savings: Hardlink individual files from CAS 

140 3. Cross-build sharing: Same PS3 folder works for multiple builds 

141 4. Deduplication: Shared files across games (firmware, DLLs) stored once 

142 

143 Cache Key = (source_md5, format, params_hash) 

144 Tree Hash = SHA-256 of sorted (path, hash, size) tuples 

145 """ 

146 

147 __tablename__ = "tree_cache" 

148 

149 id = Column(Integer, primary_key=True) 

150 

151 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

152 # Cache Key (matches ROMCache pattern) 

153 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

154 

155 source_md5 = Column(String(32), nullable=False, index=True) 

156 format = Column(String(32), nullable=False) # 'ps3-jb', 'daphne', 'scummvm', 'xbox360-god' 

157 params_hash = Column(String(32), nullable=False) # Hash of transform params 

158 

159 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

160 # Tree Manifest Reference 

161 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

162 

163 tree_hash = Column(String(64), nullable=False, index=True) # SHA-256 of manifest 

164 total_files = Column(Integer) # Number of files in tree 

165 total_size = Column(BigInteger) # Total size in bytes 

166 folder_name = Column(String(512)) # Output folder name (e.g., "BLUS30455.ps3") 

167 

168 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

169 # Transformation Details 

170 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

171 

172 platform = Column(String(32)) # 'ps3', 'daphne', 'scummvm', 'xbox360' 

173 tool_name = Column(String(64)) # 'ps3dec+7z', 'extract', etc. 

174 tool_version = Column(String(64)) 

175 transformation_params = Column(Text) # JSON params 

176 

177 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

178 # Source Info 

179 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

180 

181 source_filename = Column(String(512)) 

182 source_size = Column(BigInteger) 

183 

184 # ZIP identity for pre-extraction lookup 

185 source_zip_crc32 = Column(String(8)) 

186 source_zip_content_size = Column(BigInteger) 

187 

188 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

189 # Timestamps 

190 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

191 

192 created_date = Column(DateTime, default=datetime.utcnow) 

193 last_used = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) 

194 

195 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

196 # Constraints and Indexes 

197 # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 

198 

199 __table_args__ = ( 

200 UniqueConstraint("source_md5", "format", "params_hash", name="uq_tree_cache_key"), 

201 Index("ix_tree_cache_lookup", "source_md5", "format", "params_hash"), 

202 Index("ix_tree_cache_hash", "tree_hash"), 

203 Index("ix_tree_cache_filename", "source_filename", "source_size", "format", "params_hash"), 

204 Index( 

205 "ix_tree_cache_zip_identity", 

206 "source_zip_crc32", 

207 "source_zip_content_size", 

208 "format", 

209 "params_hash", 

210 ), 

211 Index("ix_tree_cache_last_used", "last_used"), 

212 ) 

213 

214 def __repr__(self) -> str: 

215 return ( 

216 f"<TreeCache(source_md5={self.source_md5[:8]}..., " 

217 f"format={self.format}, tree_hash={self.tree_hash[:12]}..., " 

218 f"{self.total_files} files)>" 

219 ) 

220 

221 @property 

222 def cache_key(self) -> str: 

223 """Return the unique cache key.""" 

224 return f"{self.source_md5}_{self.format}_{self.params_hash}"