| 1 8 package org.ozoneDB.core.storage.gammaStore; 9 10 import java.io.ByteArrayInputStream ; 11 import java.io.ByteArrayOutputStream ; 12 import java.io.IOException ; 13 import java.io.ObjectInputStream ; 14 import java.util.Iterator ; 15 import java.util.LinkedHashMap ; 16 import java.util.Map ; 17 import java.util.Set ; 18 import org.ozoneDB.OzoneInternalException; 19 20 24 public class DataFileManager { 25 26 private static final String DATAFILEPREFIX = ""; 27 private static final String DATAFILEPOSTFIX = ".datafile"; 28 29 38 private static final int NAMECONVERTRADIX = 16; 39 40 private LinkedHashMap dataFiles = new LinkedHashMap () { 41 42 public boolean removeEldestEntry(Map.Entry entry) { 43 if (size() > getMaxOpenDataFiles()) { 44 Storage dataFile = (Storage) entry.getValue(); 45 try { 46 dataFile.close(); 47 } catch (IOException e) { 48 throw new OzoneInternalException(e); 49 } 50 } 51 return false; 52 } 53 54 }; 55 56 private int maxOpenDataFiles; 57 58 private StorageFactory dataFileFactory; 59 60 private ObjectStreamClasses objectStreamClasses; 61 62 public DataFileManager() { 63 } 64 65 public void setObjectStreamClasses(ObjectStreamClasses objectStreamClasses) { 66 this.objectStreamClasses = objectStreamClasses; 67 } 68 69 public ObjectStreamClasses getObjectStreamClasses() { 70 return objectStreamClasses; 71 } 72 73 private Map getDataFiles() { 74 return dataFiles; 75 } 76 77 private static String dataFileIdToStorageName(int dataFileId) { 78 return DATAFILEPREFIX + Integer.toString(dataFileId, NAMECONVERTRADIX) + DATAFILEPOSTFIX; 79 } 80 81 private Storage getDataFile(int dataFileId) { 82 Storage result = (Storage) getDataFiles().get(new Integer (dataFileId)); 83 if (result == null) { 84 try { 85 result = dataFileFactory.createStorage(dataFileIdToStorageName(dataFileId)); 86 } catch (IOException e) { 87 throw new OzoneInternalException(e); 88 } 89 dataFiles.put(new Integer (dataFileId), result); 90 } 91 return result; 92 } 93 94 public GammaContainer getContainer(ContainerLocation containerLocation) throws ClassNotFoundException { 95 try { 96 Storage dataFile = getDataFile(containerLocation.getDataFileId()); 97 dataFile.seek(containerLocation.getPosition()); 98 long id = dataFile.readLong(); 99 int size = dataFile.readInt(); 100 byte[] buf = new byte[size]; 101 dataFile.readFully(buf); 102 GammaObjectInputStream in = new GammaObjectInputStream(new ByteArrayInputStream (buf), getObjectStreamClasses()); 103 GammaContainer result = (GammaContainer) in.readObject(); 104 return result; 105 } catch (IOException e) { 106 throw new OzoneInternalException(e); 107 } 108 } 109 110 public void saveContainer(ContainerLocation containerLocation, GammaContainer container, int freeSize) { 111 try { 112 Storage dataFile = getDataFile(containerLocation.getDataFileId()); 113 dataFile.seek(containerLocation.getPosition()); 114 dataFile.writeLong(container.getObjectId().value()); 115 dataFile.writeInt(freeSize); 116 ByteArrayOutputStream buf = new ByteArrayOutputStream (); 117 GammaObjectOutputStream out = new GammaObjectOutputStream(buf, getObjectStreamClasses()); 118 out.writeObject(container); 119 dataFile.write(buf.toByteArray()); 120 } catch (IOException e) { 121 throw new OzoneInternalException(e); 122 } 123 } 124 125 public int getMaxOpenDataFiles() { 126 return maxOpenDataFiles; 127 } 128 129 public void setMaxOpenDataFiles(int maxOpenDataFiles) { 130 this.maxOpenDataFiles = maxOpenDataFiles; 131 if (getDataFiles().size() > getMaxOpenDataFiles()) { 132 Iterator i = getDataFiles().values().iterator(); 133 for (int cnt = 0; cnt < getDataFiles().size(); cnt++) { 134 i.next(); 135 } 136 while (i.hasNext()) { 137 Storage dataFile = (Storage) i.next(); 138 try { 139 dataFile.close(); 140 } catch (IOException e) { 141 throw new OzoneInternalException(e); 142 } 143 i.remove(); 144 } 145 } 146 } 147 148 } 149 | Popular Tags |