| 1 16 package org.outerj.daisy.blobstoreconvertor; 17 18 import java.io.File ; 19 import java.io.FileFilter ; 20 21 public class BlobStoreConvertor { 22 private File blobDir; 23 24 private static final int DIRECTORY_DEPTH = 4; 25 26 private static final int DIRECTORY_NAME_LENGTH = 2; 27 28 private static final int KEYLENGTH = 20; 29 30 public static void main(String [] args) { 31 if (args.length == 1) { 32 try { 33 new BlobStoreConvertor(args[0]).flatToHierarchical(); 34 } catch (Exception e) { 35 e.printStackTrace(); 36 System.exit(1); 37 } 38 } 39 } 40 41 public BlobStoreConvertor(String blobStorePath) throws Exception { 42 blobDir = new File (blobStorePath); 43 if (!blobDir.exists() || !blobDir.isDirectory()) 44 throw new Exception (blobStorePath + " does not exist or is not a directory"); 45 } 46 47 public void flatToHierarchical() throws Exception { 48 File [] files = blobDir.listFiles(new FlatBlobFilter()); 49 for (int i = 0; i < files.length; i++) { 50 String id = files[i].getName(); 51 System.out.println("Processing " + id); 52 String subdirName = ""; 53 int position = 0; 54 for (int j = 0; j < DIRECTORY_DEPTH; j++) 55 subdirName += id.substring(position, position += DIRECTORY_NAME_LENGTH) + File.separator; 56 57 File subdir = new File (blobDir, subdirName); 58 subdir.mkdirs(); 59 60 File newFile = new File (subdir, id.substring(position)); 61 if (!files[i].renameTo(newFile)) { 62 throw new Exception ("Could not rename file " + files[i].getName()); 63 } 64 } 65 System.out.println("Finished successfully."); 66 } 67 68 private class FlatBlobFilter implements FileFilter { 69 public boolean accept(File pathname) { 70 return pathname.isFile() && pathname.getName().length() == KEYLENGTH * 2; 71 } 72 } 73 } 74 | Popular Tags |