Clover coverage report -
Coverage timestamp: Mon May 5 2008 11:56:20 GMT-05:00
file stats: LOC: 104   Methods: 1
NCLOC: 73   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MigrateOldData.java 0% 0% 0% 0%
coverage
 1    package org.proteomecommons.tranche.util;
 2   
 3    import java.io.BufferedOutputStream;
 4    import java.io.File;
 5    import java.io.FileOutputStream;
 6    import org.apache.tools.tar.TarEntry;
 7    import org.apache.tools.tar.TarOutputStream;
 8    import org.proteomecommons.tranche.flatfile.DataBlockUtil;
 9    import org.proteomecommons.tranche.flatfile.DataDirectoryConfiguration;
 10   
 11    /**
 12    * A class designed to help migrate data from the single-file data structure used by old Tranche servers to the new block data structure.
 13    * @author Jayson Falkner - jfalkner@umich.edu
 14    */
 15    public class MigrateOldData {
 16   
 17  0 public static void main(String[] args) throws Exception {
 18    // check args
 19  0 if (args.length < 3) {
 20  0 System.out.println("Usage: <zip dir> <old data dir> <new data dir> ");
 21  0 return;
 22    }
 23    // take a starting directory
 24  0 File zipDirectory = new File(args[0]);
 25  0 File oldDataDirectory = new File(args[1]);
 26  0 System.out.println("ZIP Dir: "+zipDirectory);
 27  0 System.out.println("OLD Data Dir: "+oldDataDirectory);
 28   
 29    // make a data block util for the new format
 30  0 DataBlockUtil dbu = new DataBlockUtil();
 31   
 32    // use a set of directories for the new
 33  0 for (int i=2;i<args.length;i++) {
 34  0 DataDirectoryConfiguration ddc = new DataDirectoryConfiguration(args[i], Long.MAX_VALUE);
 35  0 dbu.add(ddc);
 36  0 System.out.println("Loading DDC: "+args[i]);
 37    }
 38   
 39    // iterate over the directories
 40  0 File[] firstLevel = oldDataDirectory.listFiles();
 41  0 for (int i=0;i<firstLevel.length;i++) {
 42    // for each top level directory, make a zip file
 43  0 File zipFile = new File(zipDirectory, firstLevel[i].getName()+".tar");
 44  0 FileOutputStream fos = new FileOutputStream(zipFile);
 45  0 BufferedOutputStream bos = new BufferedOutputStream(fos);
 46  0 TarOutputStream tos = new TarOutputStream(bos);
 47    // set to allow long file names (GNU style!)
 48  0 tos.setLongFileMode(2);
 49   
 50  0 File[] secondLevel = firstLevel[i].listFiles();
 51  0 for (int j=0;j<secondLevel.length;j++) {
 52    // handle all of the actual files
 53  0 File[] dirs = secondLevel[j].listFiles();
 54  0 for (int k=0;k<dirs.length;k++) {
 55    // make the big hash
 56  0 BigHash hash = BigHash.createHashFromString(dirs[k].getName());
 57    // handle all of the data/meta-data pairs
 58  0 File data = new File(dirs[k], "data");
 59  0 if (data.exists()) {
 60    // get the bytes
 61  0 byte[] bytes = IOUtil.getBytes(data);
 62    // add to the dbu
 63  0 dbu.addData(hash, bytes);
 64   
 65    // add to the zip
 66  0 String name = firstLevel[i].getName()+ "/" + secondLevel[j].getName() + "/" + dirs[k].getName()+"/data";
 67  0 System.out.println("Adding: "+name);
 68  0 TarEntry te = new TarEntry(name);
 69  0 te.setSize(bytes.length);
 70  0 tos.putNextEntry(te);
 71  0 tos.write(bytes);
 72  0 tos.closeEntry();
 73    }
 74    // handle all of the data/meta-data pairs
 75  0 File metaData = new File(dirs[k], "metaData");
 76  0 if (metaData.exists()) {
 77    // get the bytes
 78  0 byte[] bytes = IOUtil.getBytes(metaData);
 79    // add to the dbu
 80  0 dbu.addMetaData(hash, bytes);
 81   
 82    // add to the zip
 83  0 String name = firstLevel[i].getName()+ "/" + secondLevel[j].getName() + "/" + dirs[k].getName()+"/metaData";
 84  0 System.out.println("Adding: "+name);
 85  0 TarEntry ze = new TarEntry(name);
 86  0 ze.setSize(bytes.length);
 87  0 tos.putNextEntry(ze);
 88  0 tos.write(bytes);
 89  0 tos.closeEntry();
 90    }
 91    }
 92    }
 93   
 94  0 tos.flush();
 95  0 tos.finish();
 96  0 bos.flush();
 97  0 fos.flush();
 98  0 tos.close();
 99  0 bos.close();
 100  0 fos.close();
 101  0 System.out.println("Finished "+i+" out of "+firstLevel.length+": "+zipFile);
 102    }
 103    }
 104    }