import com.alibaba.fastjson2.*; import io.minio.*; import java.io.*; import java.nio.file.*; import java.sql.Connection; import java.time.Instant; import java.util.*; import java.util.concurrent.*; /** Read-only database/MinIO audit; never synthesizes or deletes audio. */ public class CorpusStorageAudit { public static void main(String[] args)throws Exception{ CorpusDatabase.init(); CorpusProduction.plan=JSON.parseObject(Files.readString(Path.of("tools/corpus/production_plan.json"))); for(Object v:CorpusProduction.plan.getJSONArray("voices")){JSONObject voice=(JSONObject)v;CorpusProduction.voices.put(voice.getString("vcn"),voice);} CorpusProduction.loadAndValidate(); Map planned=new HashMap<>();for(JSONObject j:CorpusProduction.jobs)planned.put(j.getString("id"),j); JSONArray rows; try(Connection c=CorpusDatabase.connect()){ rows=CorpusDatabase.query(c,"SELECT c.id,c.text,c.voice,c.language,c.audio_file_path,c.file_size,t.audio_file_path AS task_audio,t.status FROM tts_corpus_info c JOIN tts_synthesize_task t ON t.id=c.id AND t.remark=c.remark WHERE c.remark=?",CorpusProduction.BATCH); if(rows.size()!=CorpusDatabase.count(c,"SELECT COUNT(*) FROM tts_corpus_info WHERE remark=?",CorpusProduction.BATCH))throw new IOException("Task/corpus join mismatch"); } MinioClient minio=CorpusDatabase.minio();String bucket=CorpusDatabase.property("minio.bucketName"); ExecutorService pool=Executors.newFixedThreadPool(8); CompletionService completed=new ExecutorCompletionService<>(pool); try{ for(Object o:rows){JSONObject row=(JSONObject)o;completed.submit(()->{ String id=row.getString("id"),voice=row.getString("voice"),object="tts/"+CorpusProduction.BATCH+"/"+voice+"/"+id+".wav"; JSONObject j=planned.get(id);if(j==null)throw new IOException("Unexpected corpus id "+id); for(String k:List.of("text","voice","language"))if(!j.getString(k).equals(row.getString(k)))throw new IOException("Database/plan mismatch "+id); if(!object.equals(row.getString("audio_file_path"))||!object.equals(row.getString("task_audio"))||!"2".equals(row.getString("status")))throw new IOException("Database is not pointing at the expected MinIO object "+id); JSONObject receipt=JSON.parseObject(Files.readString(CorpusProduction.ROOT.resolve("receipts").resolve(id+".json"))); if(!"IMPORTED".equals(receipt.getString("state"))||!object.equals(receipt.getString("object_path")))throw new IOException("Receipt not imported "+id); Path backup=CorpusProduction.audioRoot().resolve(voice).resolve(id+".wav"); byte[] local=Files.readAllBytes(backup);String sha=CorpusDatabase.sha(local); if(local.length!=row.getLongValue("file_size")||!sha.equals(receipt.getString("sha256")))throw new IOException("E backup size/hash mismatch "+id); CorpusProduction.checkAudio(local); try(InputStream in=minio.getObject(GetObjectArgs.builder().bucket(bucket).object(object).build())){if(!sha.equals(CorpusDatabase.sha(in.readAllBytes())))throw new IOException("MinIO hash mismatch "+id);} catch(Exception remote){throw new IOException("MinIO content verification failed for "+id);} JSONObject result=new JSONObject();result.put("id",id);result.put("object_path",object);result.put("backup_file",backup.toString());result.put("sha256",sha);return result; });} List verified=new ArrayList<>(); for(int n=1;n<=rows.size();n++){verified.add(completed.take().get());if(n%1000==0)System.out.println("STORAGE_VERIFIED "+n+"/"+rows.size());} Path manifest=CorpusProduction.ROOT.resolve("minio-backup-verified.jsonl"); try(BufferedWriter out=Files.newBufferedWriter(manifest)){for(JSONObject v:verified){out.write(v.toJSONString());out.newLine();}} // Change only local checkpoint metadata; database paths already refer to MinIO. for(JSONObject v:verified){Path p=CorpusProduction.ROOT.resolve("receipts").resolve(v.getString("id")+".json");JSONObject r=JSON.parseObject(Files.readString(p));r.remove("audio_file");r.put("storage_provider","minio");r.put("backup_file",v.getString("backup_file"));CorpusProduction.save(p,r);} JSONObject audit=new JSONObject();audit.put("count",rows.size());audit.put("primary","minio");audit.put("database_paths_verified",true);audit.put("remote_sha256_verified",true);audit.put("e_backup_sha256_verified",true);audit.put("checked_at",Instant.now().toString());audit.put("manifest_sha256",CorpusDatabase.sha(Files.readAllBytes(manifest))); CorpusProduction.save(CorpusProduction.ROOT.resolve("minio-backup-audit.json"),audit); System.out.println("STORAGE_AUDIT_OK "+audit); }finally{pool.shutdownNow();} System.exit(0); } }