1 21 22 package org.apache.derby.impl.io; 23 24 import org.apache.derby.iapi.store.raw.data.DataFactory; 25 26 import org.apache.derby.io.StorageFactory; 27 import org.apache.derby.io.StorageFile; 28 29 import java.io.File ; 30 import java.io.IOException ; 31 32 36 37 abstract class BaseStorageFactory implements StorageFactory 38 { 39 40 String home; 41 protected StorageFile tempDir; 42 protected String tempDirPath; 43 protected String dataDirectory; 44 protected String separatedDataDirectory; protected String uniqueName; 46 protected String canonicalName; 47 private static final String TEMP_DIR_PREFIX = "derbytmp_"; 48 49 52 BaseStorageFactory() 53 {} 54 55 77 public void init( String home, String databaseName, String tempDirName, String uniqueName) 78 throws IOException 79 { 80 if( databaseName != null) 81 { 82 dataDirectory = databaseName; 83 separatedDataDirectory = databaseName + getSeparator(); 84 } 85 this.home = home; 86 this.uniqueName = uniqueName; 87 tempDirPath = tempDirName; 88 doInit(); 89 } 91 abstract void doInit() throws IOException ; 92 93 public void shutdown() 94 { 95 } 96 97 98 108 public String getCanonicalName() throws IOException 109 { 110 return canonicalName; 111 } 112 113 120 public StorageFile newStorageFile( String path) 121 { 122 if( path != null && tempDirPath != null && path.startsWith( tempDirPath)) 123 return new DirFile( path); 124 return newPersistentFile( path); 125 } 126 127 135 public StorageFile newStorageFile( String directoryName, String fileName) 136 { 137 if( directoryName == null) 138 return newStorageFile( fileName); 139 else if( tempDirPath != null && directoryName.startsWith( tempDirPath)) 140 return new DirFile(directoryName, fileName); 141 else 142 return newPersistentFile( directoryName, fileName); 143 } 144 145 153 public StorageFile newStorageFile( StorageFile directoryName, String fileName) 154 { 155 if( directoryName == null) 156 return newStorageFile( fileName); 157 if( fileName == null) 158 return directoryName; 159 else if (tempDirPath != null && directoryName.getPath().startsWith(tempDirPath)) 160 return new DirFile( (DirFile) directoryName, fileName); 161 return newPersistentFile( directoryName, fileName); 162 } 163 164 172 abstract StorageFile newPersistentFile( String path); 173 174 183 abstract StorageFile newPersistentFile( String directoryName, String fileName); 184 185 194 abstract StorageFile newPersistentFile( StorageFile directoryName, String fileName); 195 196 201 public char getSeparator() 202 { 203 return File.separatorChar; 205 } 206 207 212 public StorageFile getTempDir() 213 { 214 return tempDir; 215 } 216 217 223 public boolean isFast() 224 { 225 return false; 226 } 227 228 public boolean isReadOnlyDatabase() 229 { 230 return true; 231 } 232 233 239 public boolean supportsRandomAccess() 240 { 241 return false; 242 } 243 244 void createTempDir() throws java.io.IOException 245 { 246 if( uniqueName == null) 247 return; 248 249 if( tempDirPath != null) 250 tempDir = new DirFile( tempDirPath, TEMP_DIR_PREFIX.concat(uniqueName)); 251 else if( isReadOnlyDatabase()) 252 tempDir = new DirFile( readOnlyTempRoot(), TEMP_DIR_PREFIX.concat(uniqueName)); 253 else 254 tempDir = new DirFile( canonicalName, DataFactory.TEMP_SEGMENT_NAME); 255 256 tempDir.deleteAll(); 258 259 tempDir.mkdirs(); 260 tempDirPath = tempDir.getPath(); 261 } 263 private String readOnlyTempRoot() throws java.io.IOException 264 { 265 File temp = File.createTempFile("derby", "tmp"); 268 String parent = temp.getParent(); 269 temp.delete(); 270 271 return parent; 272 } 273 274 public int getStorageFactoryVersion() 275 { 276 return StorageFactory.VERSION_NUMBER; 277 } 278 } 279 | Popular Tags |