1 21 22 package org.apache.derby.impl.store.raw.data; 23 24 import org.apache.derby.iapi.reference.SQLState; 25 26 import org.apache.derby.iapi.services.context.ContextService; 27 import org.apache.derby.iapi.services.context.ContextManager; 28 import org.apache.derby.iapi.services.daemon.Serviceable; 29 import org.apache.derby.iapi.services.sanity.SanityManager; 30 import org.apache.derby.iapi.error.StandardException; 31 import org.apache.derby.iapi.store.access.FileResource; 32 import org.apache.derby.iapi.store.raw.Transaction; 33 import org.apache.derby.iapi.store.access.AccessFactoryGlobals; 34 import org.apache.derby.iapi.store.access.DatabaseInstant; 35 import org.apache.derby.iapi.store.raw.xact.RawTransaction; 36 37 import org.apache.derby.io.StorageFactory; 38 import org.apache.derby.io.WritableStorageFactory; 39 import org.apache.derby.io.StorageFile; 40 import org.apache.derby.io.StorageRandomAccessFile; 41 42 import java.io.InputStream ; 43 import java.io.OutputStream ; 44 import java.io.FileNotFoundException ; 45 import java.io.IOException ; 46 47 class RFResource implements FileResource { 48 49 protected final BaseDataFileFactory factory; 50 51 public RFResource(BaseDataFileFactory dataFactory) { 52 this.factory = dataFactory; 53 } 54 55 59 public long add(String name, InputStream source) 60 throws StandardException 61 { 62 OutputStream os = null; 63 64 if (factory.isReadOnly()) 65 { 66 throw StandardException.newException(SQLState.FILE_READ_ONLY); 67 } 68 69 long generationId = factory.getNextId(); 70 71 try 72 { 73 StorageFile file = getAsFile(name, generationId); 74 if (file.exists()) 75 { 76 throw StandardException.newException( 77 SQLState.FILE_EXISTS, file); 78 } 79 80 ContextManager cm = 81 ContextService.getFactory().getCurrentContextManager(); 82 83 RawTransaction tran = 84 factory.getRawStoreFactory().getXactFactory().findUserTransaction( 85 factory.getRawStoreFactory(), 86 cm, 87 AccessFactoryGlobals.USER_TRANS_NAME); 88 89 95 tran.blockBackup(true); 96 97 StorageFile directory = file.getParentDir(); 98 if (!directory.exists()) 99 { 100 if (!directory.mkdirs()) 101 { 102 throw StandardException.newException( 103 SQLState.FILE_CANNOT_CREATE_SEGMENT, directory); 104 } 105 } 106 107 os = file.getOutputStream(); 108 byte[] data = new byte[4096]; 109 int len; 110 111 factory.writeInProgress(); 112 try 113 { 114 while ((len = source.read(data)) != -1) { 115 os.write(data, 0, len); 116 } 117 factory.writableStorageFactory.sync( os, false); 118 } 119 finally 120 { 121 factory.writeFinished(); 122 } 123 } 124 125 catch (IOException ioe) 126 { 127 throw StandardException.newException( 128 SQLState.FILE_UNEXPECTED_EXCEPTION, ioe); 129 } 130 131 finally 132 { 133 try { 134 if (os != null) { 135 os.close(); 136 } 137 } catch (IOException ioe2) {} 138 139 try { 140 if (source != null)source.close(); 141 } catch (IOException ioe2) {} 142 } 143 144 return generationId; 145 } 146 147 151 public void remove(String name, long currentGenerationId, boolean purgeOnCommit) 152 throws StandardException 153 { 154 if (factory.isReadOnly()) 155 throw StandardException.newException(SQLState.FILE_READ_ONLY); 156 157 158 ContextManager cm = ContextService.getFactory().getCurrentContextManager(); 159 160 RawTransaction tran = 161 factory.getRawStoreFactory().getXactFactory().findUserTransaction( 162 factory.getRawStoreFactory(), 163 cm, 164 AccessFactoryGlobals.USER_TRANS_NAME); 165 166 172 tran.blockBackup(true); 173 174 tran.logAndDo(new RemoveFileOperation(name, currentGenerationId, purgeOnCommit)); 175 176 if (purgeOnCommit) { 177 178 Serviceable s = new RemoveFile(getAsFile(name, currentGenerationId)); 179 180 tran.addPostCommitWork(s); 181 } 182 } 183 184 188 public long replace(String name, long currentGenerationId, InputStream source, boolean purgeOnCommit) 189 throws StandardException 190 { 191 if (factory.isReadOnly()) 192 throw StandardException.newException(SQLState.FILE_READ_ONLY); 193 194 remove(name, currentGenerationId, purgeOnCommit); 195 196 long generationId = add(name, source); 197 198 return generationId; 199 } 200 201 202 205 public StorageFile getAsFile(String name, long generationId) 206 { 207 String versionedFileName = factory.getVersionedName(name, generationId); 208 209 return factory.storageFactory.newStorageFile( versionedFileName); 210 } 211 212 215 private StorageFile getAsFile(String name) 216 { 217 return factory.storageFactory.newStorageFile( name); 218 } 219 220 224 public InputStream getAsStream(String name, long generationId) 225 throws IOException 226 { 227 return getAsFile(name, generationId).getInputStream(); 228 } 229 230 public char getSeparatorChar() 231 { 232 return factory.storageFactory.getSeparator(); 233 } 234 } 236 237 class RemoveFile implements Serviceable 238 { 239 private final StorageFile fileToGo; 240 241 RemoveFile(StorageFile fileToGo) 242 { 243 this.fileToGo = fileToGo; 244 } 245 246 public int performWork(ContextManager context) 247 throws StandardException 248 { 249 if (fileToGo.exists()) 251 { 252 if (!fileToGo.delete()) 253 { 254 throw StandardException.newException( 255 SQLState.FILE_CANNOT_REMOVE_FILE, fileToGo); 256 } 257 } 258 return Serviceable.DONE; 259 } 260 261 public boolean serviceASAP() 262 { 263 return false; 264 } 265 266 272 public boolean serviceImmediately() 273 { 274 return true; 275 } 276 } 277 | Popular Tags |