1 21 22 package org.apache.derby.impl.store.raw.data; 23 24 import org.apache.derby.iapi.reference.SQLState; 25 import org.apache.derby.impl.store.raw.data.BaseContainer; 26 import org.apache.derby.impl.store.raw.data.BaseContainerHandle; 27 import org.apache.derby.impl.store.raw.data.BasePage; 28 29 import org.apache.derby.iapi.services.cache.Cacheable; 30 import org.apache.derby.iapi.services.context.ContextService; 31 import org.apache.derby.iapi.services.monitor.Monitor; 32 import org.apache.derby.iapi.services.diag.Performance; 33 import org.apache.derby.iapi.services.sanity.SanityManager; 34 import org.apache.derby.iapi.services.io.FormatIdUtil; 35 36 import org.apache.derby.iapi.error.StandardException; 37 38 import org.apache.derby.iapi.store.raw.ContainerHandle; 39 import org.apache.derby.iapi.store.raw.ContainerKey; 40 import org.apache.derby.iapi.store.raw.Loggable; 41 import org.apache.derby.iapi.store.raw.log.LogInstant; 42 import org.apache.derby.iapi.store.raw.xact.RawTransaction; 43 44 import org.apache.derby.io.StorageFactory; 45 import org.apache.derby.io.WritableStorageFactory; 46 import org.apache.derby.io.StorageFile; 47 import org.apache.derby.io.StorageRandomAccessFile; 48 import org.apache.derby.iapi.services.io.FileUtil; 49 import java.util.Vector ; 50 51 import java.io.DataInput ; 52 import java.io.IOException ; 53 import java.io.File ; 54 import java.io.RandomAccessFile ; 55 import java.security.AccessController ; 56 import java.security.PrivilegedExceptionAction ; 57 import java.security.PrivilegedActionException ; 58 import java.lang.reflect.Method ; 59 import java.lang.reflect.Constructor ; 60 61 65 66 class RAFContainer extends FileContainer implements PrivilegedExceptionAction 67 { 68 69 72 protected StorageRandomAccessFile fileData; 73 74 77 protected boolean needsSync; 78 79 80 private int actionCode; 81 private static final int GET_FILE_NAME_ACTION = 1; 82 private static final int CREATE_CONTAINER_ACTION = 2; 83 private static final int REMOVE_FILE_ACTION = 3; 84 private static final int OPEN_CONTAINER_ACTION = 4; 85 private static final int STUBBIFY_ACTION = 5; 86 private static final int BACKUP_CONTAINER_ACTION = 6; 87 private static final int GET_RANDOM_ACCESS_FILE_ACTION = 7; 88 private ContainerKey actionIdentity; 89 private boolean actionStub; 90 private boolean actionErrorOK; 91 private boolean actionTryAlternatePath; 92 private StorageFile actionFile; 93 private LogInstant actionInstant; 94 private String actionBackupLocation; 95 private BaseContainerHandle actionContainerHandle; 96 97 private boolean inBackup = false; 98 private boolean inRemove = false; 99 100 105 private static Class fairLockClass; 106 private static Constructor fairLockConstructor; 107 private static Method lock; 108 private static Method unlock; 109 private static boolean hasJava5FairLocks = false; 110 111 static { 116 try { 117 fairLockClass = 118 Class.forName("java.util.concurrent.locks.ReentrantLock"); 119 120 fairLockConstructor = 121 fairLockClass.getConstructor(new Class [] { Boolean.TYPE }); 122 123 lock = fairLockClass.getMethod("lock", new Class [0]); 124 unlock = fairLockClass.getMethod("unlock", new Class [0]); 125 hasJava5FairLocks = true; 126 } 127 catch (NoSuchMethodException nsme) {} 128 catch (ClassNotFoundException cnfe) {} 129 } 130 131 136 private Object fairLock; 137 138 141 142 RAFContainer(BaseDataFileFactory factory) { 143 super(factory); 144 145 if (hasJava5FairLocks) { 147 try { 148 fairLock = 150 fairLockConstructor.newInstance( 151 new Object [] { Boolean.TRUE }); 152 } catch (Exception e) { 153 155 hasJava5FairLocks = false; 156 if (SanityManager.DEBUG) { 157 SanityManager.THROWASSERT( 158 "failed constructing ReentrantLock", e); 159 } 160 } 161 } 162 } 163 164 167 168 synchronized public boolean isDirty() { 169 return super.isDirty() || needsSync; 170 } 171 172 175 176 180 public Cacheable setIdentity(Object key) throws StandardException { 181 182 ContainerKey newIdentity = (ContainerKey) key; 183 184 if (newIdentity.getSegmentId() == ContainerHandle.TEMPORARY_SEGMENT) { 186 187 TempRAFContainer tmpContainer = new TempRAFContainer(dataFactory); 188 return tmpContainer.setIdent(newIdentity); 189 } 190 191 return setIdent(newIdentity); 192 } 193 194 197 public Cacheable createIdentity(Object key, Object createParameter) throws StandardException { 198 199 ContainerKey newIdentity = (ContainerKey) key; 200 201 if (newIdentity.getSegmentId() == ContainerHandle.TEMPORARY_SEGMENT) { 202 TempRAFContainer tmpContainer = new TempRAFContainer(dataFactory); 203 return tmpContainer.createIdent(newIdentity, createParameter); 204 } 205 206 return createIdent(newIdentity, createParameter); 207 } 208 209 210 213 214 219 protected void removeContainer(LogInstant instant, boolean leaveStub) 220 throws StandardException 221 { 222 223 try { 224 synchronized(this) 225 { 226 inRemove = true; 227 while(inBackup) 230 { 231 try { 232 wait(); 233 } 234 catch (InterruptedException ie) 235 { 236 throw StandardException.interrupt(ie); 237 } 238 } 239 } 240 241 pageCache.discard(identity); 243 stubbify(instant); 244 }finally 245 { 246 synchronized(this) { 247 inRemove = false; 248 notifyAll(); 249 } 250 } 251 252 } 254 255 final void closeContainer() { 256 257 if (fileData != null) { 258 try { 259 fileData.close(); 260 } catch (IOException ioe) { 261 } finally { 262 263 fileData = null; 264 } 265 } 266 } 267 268 269 272 273 280 protected void readPage(long pageNumber, byte[] pageData) 281 throws IOException , StandardException 282 { 283 if (SanityManager.DEBUG) { 284 SanityManager.ASSERT(!getCommittedDropState()); 285 } 286 287 long pageOffset = pageNumber * pageSize; 288 289 if (hasJava5FairLocks) { 291 try { 292 lock.invoke(fairLock, null); 293 } catch (Exception e) { 294 hasJava5FairLocks = false; 299 if (SanityManager.DEBUG) { 300 SanityManager.THROWASSERT( 301 "failed invoking ReentrantLock.lock()", e); 302 } 303 } 304 } 305 306 try { 307 synchronized (this) { 312 fileData.seek(pageOffset); 313 fileData.readFully(pageData, 0, pageSize); 314 } 315 } finally { 316 if (hasJava5FairLocks) { 318 try { 319 unlock.invoke(fairLock, null); 320 } catch (Exception e) { 321 hasJava5FairLocks = false; 328 if (SanityManager.DEBUG) { 329 SanityManager.THROWASSERT( 330 "failed invoking ReentrantLock.unlock()", e); 331 } 332 } 333 } 334 } 335 336 if (dataFactory.databaseEncrypted() && 337 pageNumber != FIRST_ALLOC_PAGE_NUMBER) 338 { 339 decryptPage(pageData, pageSize); 340 } 341 } 342 343 351 protected void writePage(long pageNumber, byte[] pageData, boolean syncPage) 352 throws IOException , StandardException 353 { 354 synchronized(this) 355 { 356 357 if (getCommittedDropState()) 358 { 359 362 return; 363 } 364 365 374 long pageOffset = pageNumber * pageSize; 375 376 byte [] encryptionBuf = null; 377 if (dataFactory.databaseEncrypted() 378 && pageNumber != FIRST_ALLOC_PAGE_NUMBER) 379 { 380 385 encryptionBuf = getEncryptionBuffer(); 386 } 387 388 byte[] dataToWrite = 389 updatePageArray(pageNumber, pageData, encryptionBuf, false); 390 391 try 392 { 393 fileData.seek(pageOffset); 394 395 400 if (fileData.getFilePointer() != pageOffset) 401 padFile(fileData, pageOffset); 402 403 dataFactory.writeInProgress(); 404 try 405 { 406 fileData.write(dataToWrite, 0, pageSize); 407 } 408 finally 409 { 410 dataFactory.writeFinished(); 411 } 412 } 413 catch (IOException ioe) 414 { 415 423 if (!padFile(fileData, pageOffset)) 424 throw ioe; 426 if (SanityManager.DEBUG) 427 { 428 SanityManager.ASSERT( 429 fileData.length() >= pageOffset, 430 "failed to blank filled missing pages"); 431 } 432 433 fileData.seek(pageOffset); 434 dataFactory.writeInProgress(); 435 try 436 { 437 fileData.write(dataToWrite, 0, pageSize); 438 } 439 finally 440 { 441 dataFactory.writeFinished(); 442 } 443 } 444 445 if (syncPage) 446 { 447 dataFactory.writeInProgress(); 448 try 449 { 450 if (!dataFactory.dataNotSyncedAtAllocation) 451 fileData.sync( false); 452 } 453 finally 454 { 455 dataFactory.writeFinished(); 456 } 457 } 458 else 459 { 460 needsSync = true; 461 } 462 } 463 464 } 465 466 475 private byte[] updatePageArray(long pageNumber, 476 byte[] pageData, 477 byte[] encryptionBuf, 478 boolean encryptWithNewEngine) 479 throws StandardException, IOException 480 { 481 if (pageNumber == FIRST_ALLOC_PAGE_NUMBER) 482 { 483 writeHeader(pageData); 487 488 if (SanityManager.DEBUG) 489 { 490 if (FormatIdUtil.readFormatIdInteger(pageData) != AllocPage.FORMAT_NUMBER) 491 SanityManager.THROWASSERT( 492 "expect " + 493 AllocPage.FORMAT_NUMBER + 494 "got " + 495 FormatIdUtil.readFormatIdInteger(pageData)); 496 } 497 498 return pageData; 499 500 } 501 else 502 { 503 if (dataFactory.databaseEncrypted() || encryptWithNewEngine) 504 { 505 return encryptPage(pageData, 506 pageSize, 507 encryptionBuf, 508 encryptWithNewEngine); 509 } 510 else 511 { 512 return pageData; 513 } 514 } 515 } 516 517 518 522 523 private boolean padFile(StorageRandomAccessFile file, long pageOffset) 524 throws IOException , StandardException { 525 526 long currentEOF = file.length(); 527 if (currentEOF >= pageOffset) 528 return false; 529 530 byte zero[] = new byte[pageSize]; 532 533 file.seek(currentEOF); 534 535 while(currentEOF < pageOffset) 536 { 537 dataFactory.writeInProgress(); 538 try 539 { 540 long len = pageOffset - currentEOF; 541 if (len > pageSize) 542 len = pageSize; 543 544 file.write(zero, 0, (int) len); 545 } 546 finally 547 { 548 dataFactory.writeFinished(); 549 } 550 currentEOF += pageSize; 551 } 552 553 return true; 554 } 555 556 572 public void clean(boolean forRemove) throws StandardException 573 { 574 boolean waited = false; 575 576 synchronized (this) { 577 578 if (getCommittedDropState()) { 581 clearDirty(); 582 return; 583 } 584 585 while(preDirty == true) 590 { 591 waited = true; 592 try 593 { 594 wait(); 595 } 596 catch (InterruptedException ie) 597 { 598 throw StandardException.interrupt(ie); 599 } 600 } 601 602 if (waited) 603 { 604 if (getCommittedDropState()) 606 { 607 clearDirty(); 608 return; 609 } 610 } 611 612 613 if (forRemove) { 614 615 618 } else if (isDirty()) { 619 620 try { 621 622 writeRAFHeader(fileData, 632 false, true); 635 clearDirty(); 636 637 } catch (IOException ioe) { 638 639 throw dataFactory.markCorrupt( 640 StandardException.newException( 641 SQLState.FILE_CONTAINER_EXCEPTION, ioe, this)); 642 } 643 } 644 } 645 } 646 647 private void clearDirty() { 648 isDirty = false; 649 needsSync = false; 650 } 651 652 653 656 protected int preAllocate(long lastPreallocPagenum, 657 int preAllocSize) 658 { 659 660 674 675 int n = doPreAllocatePages(lastPreallocPagenum, preAllocSize); 676 677 if (n > 0) { 679 synchronized(this) 680 { 681 boolean inwrite = false; 682 try 683 { 684 dataFactory.writeInProgress(); 685 inwrite = true; 686 687 if (!dataFactory.dataNotSyncedAtAllocation) 688 fileData.sync(false); 689 } 690 catch (IOException ioe) 691 { 692 n = 0; 696 } 697 catch (StandardException se) 698 { 699 n = 0; 701 } 702 finally 703 { 704 if (inwrite) 705 dataFactory.writeFinished(); 706 } 707 } 708 } 709 710 return n; 711 } 712 713 724 protected void truncatePages( 725 long lastValidPagenum) 726 throws StandardException 727 { 728 729 730 synchronized(this) 731 { 732 boolean inwrite = false; 733 try 734 { 735 dataFactory.writeInProgress(); 736 inwrite = true; 737 738 fileData.setLength((lastValidPagenum + 1) * pageSize); 739 } 740 catch (IOException ioe) 741 { 742 } 746 catch (StandardException se) 747 { 748 } 750 finally 751 { 752 if (inwrite) 753 dataFactory.writeFinished(); 754 } 755 } 756 757 return; 758 } 759 760 761 767 private void writeRAFHeader(StorageRandomAccessFile file, boolean create, 768 boolean syncFile) 769 throws IOException , StandardException 770 { 771 byte[] epage; 772 if (create) 773 { 774 epage = getEmbryonicPage((DataInput )null); 776 } 777 else 778 { 779 file.seek(FIRST_ALLOC_PAGE_OFFSET); 780 epage = getEmbryonicPage(file); 781 } 782 783 785 786 file.seek(FIRST_ALLOC_PAGE_OFFSET); 787 writeHeader(file, create, epage); 788 789 if (create) { 796 padFile(file, pageSize); 797 } 798 799 if (syncFile) 800 { 801 dataFactory.writeInProgress(); 802 try 803 { 804 if (!dataFactory.dataNotSyncedAtCheckpoint) 805 file.sync(false); 806 807 } 808 finally 809 { 810 dataFactory.writeFinished(); 811 } 812 } 813 814 epage = null; 815 } 816 817 822 protected void flushAll() throws StandardException { 823 824 pageCache.clean(identity); 825 826 clean(false); 828 } 829 830 831 synchronized StorageFile getFileName(ContainerKey identity, boolean stub, 832 boolean errorOK, boolean tryAlternatePath) 833 throws StandardException 834 { 835 837 actionCode = GET_FILE_NAME_ACTION; 838 actionIdentity = identity; 839 actionStub = stub; 840 actionErrorOK = errorOK; 841 actionTryAlternatePath = tryAlternatePath; 842 try 843 { 844 return (StorageFile) AccessController.doPrivileged( this); 845 } 846 catch( PrivilegedActionException pae){ throw (StandardException) pae.getException();} 847 finally{ actionIdentity = null; } 848 } 849 850 protected StorageFile privGetFileName(ContainerKey identity, boolean stub, 851 boolean errorOK, boolean tryAlternatePath) 852 throws StandardException 853 { 854 StorageFile container = dataFactory.getContainerPath( identity, stub); 855 856 if (!container.exists() && tryAlternatePath) 859 { 860 container = dataFactory.getAlternateContainerPath( identity, stub); 861 } 862 863 if (!container.exists()) { 864 865 StorageFile directory = container.getParentDir(); 866 867 if (!directory.exists()) 868 { 869 synchronized(dataFactory) 871 { 872 if (!directory.exists()) 873 { 874 if (!directory.mkdirs()) 875 { 876 if (errorOK) 877 { 878 return null; 879 } 880 else 881 { 882 throw StandardException.newException( 883 SQLState.FILE_CANNOT_CREATE_SEGMENT, 884 directory); 885 } 886 } 887 } 888 } 889 } 890 } 891 892 return container; 893 } 895 896 synchronized void createContainer(ContainerKey newIdentity) 897 throws StandardException 898 { 899 900 if (SanityManager.DEBUG) { 901 if ((spareSpace < 0) || (spareSpace > 100)) 902 SanityManager.THROWASSERT("invalid spare space " + spareSpace); 903 } 904 905 actionCode = CREATE_CONTAINER_ACTION; 906 actionIdentity = newIdentity; 907 try 908 { 909 AccessController.doPrivileged( this); 910 } 911 catch( PrivilegedActionException pae){ throw (StandardException) pae.getException();} 912 finally{ actionIdentity = null; } 913 } 915 synchronized boolean removeFile(StorageFile file) 916 throws SecurityException , StandardException 917 { 918 actionCode = REMOVE_FILE_ACTION; 919 actionFile = file; 920 try 921 { 922 return AccessController.doPrivileged( this) != null; 923 } 924 catch( PrivilegedActionException pae){ throw (StandardException) pae.getException();} 925 finally{ actionFile = null; } 926 } 928 private boolean privRemoveFile(StorageFile file) 929 throws StandardException 930 { 931 closeContainer(); 932 933 dataFactory.writeInProgress(); 934 try 935 { 936 if (file.exists()) 937 return file.delete(); 938 } 939 finally 940 { 941 dataFactory.writeFinished(); 942 } 943 944 return true; 945 } 947 synchronized boolean openContainer(ContainerKey newIdentity) 948 throws StandardException 949 { 950 actionCode = OPEN_CONTAINER_ACTION; 951 actionIdentity = newIdentity; 952 try 953 { 954 return AccessController.doPrivileged( this) != null; 955 } 956 catch( PrivilegedActionException pae){ throw (StandardException) pae.getException();} 957 finally{ actionIdentity = null; } 958 } 959 960 private synchronized void stubbify(LogInstant instant) 961 throws StandardException 962 { 963 setDroppedState(true); 967 setCommittedDropState(true); 968 969 1014 actionIdentity = (ContainerKey)getIdentity(); 1015 actionInstant = instant; 1016 actionCode = STUBBIFY_ACTION; 1017 try 1018 { 1019 AccessController.doPrivileged( this); 1020 } 1021 catch( PrivilegedActionException pae){ throw (StandardException) pae.getException();} 1022 finally 1023 { 1024 actionIdentity = null; 1025 actionInstant = null; 1026 } 1027 } 1028 1029 1030 1031 1032 1033 1040 protected void backupContainer(BaseContainerHandle handle, String backupLocation) 1041 throws StandardException 1042 { 1043 actionContainerHandle = handle; 1044 actionBackupLocation = backupLocation; 1045 actionCode = BACKUP_CONTAINER_ACTION; 1046 try 1047 { 1048 AccessController.doPrivileged(this); 1049 } 1050 catch( PrivilegedActionException pae){ throw (StandardException) pae.getException();} 1051 finally 1052 { 1053 actionContainerHandle = null; 1054 actionBackupLocation = null; 1055 } 1056 } 1057 1058 1059 1096 private void privBackupContainer(BaseContainerHandle handle, 1097 String backupLocation) 1098 throws StandardException 1099 { 1100 boolean backupCompleted = false; 1101 File backupFile = null; 1102 RandomAccessFile backupRaf = null; 1103 boolean isStub = false; 1104 BasePage page = null; 1105 1106 while(!backupCompleted) { 1107 try { 1108 1109 synchronized (this) { 1110 while (inRemove) 1113 { 1114 try { 1115 wait(); 1116 } 1117 catch (InterruptedException ie) 1118 { 1119 throw StandardException.interrupt(ie); 1120 } 1121 } 1122 1123 if (getCommittedDropState()) 1124 isStub = true; 1125 inBackup = true; 1126 } 1127 1128 if (isStub) { 1130 StorageFile file = privGetFileName((ContainerKey)getIdentity(), 1132 true, false, true); 1133 backupFile = new File(backupLocation, file.getName()); 1134 1135 if(!FileUtil.copyFile(dataFactory.getStorageFactory(), 1137 file, backupFile)) 1138 { 1139 throw StandardException.newException( 1140 SQLState.RAWSTORE_ERROR_COPYING_FILE, 1141 file, backupFile); 1142 } 1143 }else { 1144 long lastPageNumber= getLastPageNumber(handle); 1146 if (lastPageNumber == ContainerHandle.INVALID_PAGE_NUMBER) { 1147 return; 1155 } 1156 1157 StorageFile file = 1158 privGetFileName( 1159 (ContainerKey)getIdentity(), false, false, true); 1160 1161 backupFile = new File(backupLocation , file.getName()); 1162 backupRaf = new RandomAccessFile(backupFile, "rw"); 1163 1164 byte[] encryptionBuf = null; 1165 if (dataFactory.databaseEncrypted()) { 1166 encryptionBuf = new byte[pageSize]; 1173 } 1174 1175 for (long pageNumber = FIRST_ALLOC_PAGE_NUMBER; 1178 pageNumber <= lastPageNumber; pageNumber++) { 1179 page = getLatchedPage(handle, pageNumber); 1180 1181 1185 byte[] dataToWrite = updatePageArray(pageNumber, 1186 page.getPageArray(), 1187 encryptionBuf, false); 1188 backupRaf.write(dataToWrite, 0, pageSize); 1189 1190 page.unlatch(); 1193 page = null; 1194 1195 1200 synchronized (this) 1201 { 1202 if (inRemove) { 1203 break; 1204 } 1205 } 1206 } 1207 } 1208 1209 if(!isStub) { 1212 backupRaf.getFD().sync(); 1213 backupRaf.close(); 1214 backupRaf = null; 1215 } 1216 1217 backupCompleted = true; 1219 1220 }catch (IOException ioe) { 1221 throw StandardException.newException( 1222 SQLState.BACKUP_FILE_IO_ERROR, 1223 ioe, 1224 backupFile); 1225 } finally { 1226 synchronized (this) { 1227 inBackup = false; 1228 notifyAll(); 1229 } 1230 1231 if (page != null) { 1232 page.unlatch(); 1233 page = null; 1234 } 1235 1236 if (!backupCompleted && backupFile != null) 1240 { 1241 if (backupRaf != null) 1242 { 1243 try { 1244 backupRaf.close(); 1245 backupRaf = null; 1246 } catch (IOException ioe){ 1247 throw StandardException.newException( 1248 SQLState.BACKUP_FILE_IO_ERROR, 1249 ioe, 1250 backupFile); 1251 } 1252 } 1253 1254 if(backupFile.exists()) 1255 { 1256 if (!backupFile.delete()) 1257 throw StandardException.newException( 1258 SQLState.UNABLE_TO_DELETE_FILE, 1259 backupFile); 1260 } 1261 } 1262 } 1263 } 1264 } 1265 1266 1267 1268 1269 1283 protected void encryptContainer(BaseContainerHandle handle, 1284 String newFilePath) 1285 throws StandardException 1286 { 1287 BasePage page = null; 1288 StorageFile newFile = 1289 dataFactory.getStorageFactory().newStorageFile(newFilePath); 1290 StorageRandomAccessFile newRaf = null; 1291 try { 1292 long lastPageNumber= getLastPageNumber(handle); 1293 1294 newRaf = privGetRandomAccessFile(newFile); 1295 1296 byte[] encryptionBuf = null; 1297 encryptionBuf = new byte[pageSize]; 1298 1299 for (long pageNumber = FIRST_ALLOC_PAGE_NUMBER; 1302 pageNumber <= lastPageNumber; pageNumber++) 1303 { 1304 1305 page = getLatchedPage(handle, pageNumber); 1306 1307 1310 byte[] dataToWrite = updatePageArray(pageNumber, 1311 page.getPageArray(), 1312 encryptionBuf, 1313 true); 1314 newRaf.write(dataToWrite, 0, pageSize); 1315 1316 page.unlatch(); 1318 page = null; 1319 } 1320 1321 newRaf.sync(true); 1323 newRaf.close(); 1324 newRaf = null; 1325 1326 }catch (IOException ioe) { 1327 throw StandardException.newException( 1328 SQLState.FILE_CONTAINER_EXCEPTION, 1329 ioe, 1330 newFile); 1331 } finally { 1332 1333 if (page != null) { 1334 page.unlatch(); 1335 page = null; 1336 } 1337 1338 if (newRaf != null) { 1339 try { 1340 newRaf.close(); 1341 }catch (IOException ioe) 1342 { 1343 newRaf = null; 1344 throw StandardException.newException( 1345 SQLState.FILE_CONTAINER_EXCEPTION, 1346 ioe, 1347 newFile); 1348 1349 } 1350 } 1351 } 1352 } 1353 1354 1355 synchronized StorageRandomAccessFile privGetRandomAccessFile(StorageFile file) 1356 throws SecurityException , StandardException 1357 { 1358 actionCode = GET_RANDOM_ACCESS_FILE_ACTION; 1359 actionFile = file; 1360 try 1361 { 1362 return (StorageRandomAccessFile)AccessController.doPrivileged(this); 1363 } 1364 catch( PrivilegedActionException pae){ 1365 throw (StandardException) pae.getException(); 1366 } 1367 finally{ actionFile = null; } 1368 } 1369 1370 1371 1372 public Object run() throws StandardException, IOException 1374 { 1375 switch( actionCode) 1376 { 1377 case GET_FILE_NAME_ACTION: 1378 return privGetFileName( actionIdentity, actionStub, actionErrorOK, actionTryAlternatePath); 1379 1380 case CREATE_CONTAINER_ACTION: 1381 { 1382 StorageFile file = privGetFileName( actionIdentity, false, false, false); 1383 1384 try { 1385 if (file.exists()) { 1386 throw StandardException.newException( SQLState.FILE_EXISTS, file); 1389 } 1390 } catch (SecurityException se) { 1391 throw StandardException.newException( SQLState.FILE_CREATE, se, file); 1392 } 1393 1394 try { 1395 1396 1399 dataFactory.writeInProgress(); 1400 try 1401 { 1402 fileData = file.getRandomAccessFile( "rw"); 1403 } 1404 finally 1405 { 1406 dataFactory.writeFinished(); 1407 } 1408 1409 1426 writeRAFHeader(fileData, true, 1429 (actionIdentity.getSegmentId() != ContainerHandle.TEMPORARY_SEGMENT)); 1430 1431 } catch (SecurityException se) { 1432 1433 throw StandardException.newException( SQLState.FILE_CREATE, se, file); 1436 1437 } catch (IOException ioe) { 1438 1439 boolean fileDeleted; 1440 try { 1441 fileDeleted = privRemoveFile(file); 1442 } catch (SecurityException se) { 1443 throw StandardException.newException( SQLState.FILE_CREATE_NO_CLEANUP, ioe, file, se.toString()); 1444 } 1445 1446 if (!fileDeleted) { 1447 throw StandardException.newException( SQLState.FILE_CREATE_NO_CLEANUP, ioe, file, ioe.toString()); 1448 } 1449 1450 throw StandardException.newException( SQLState.FILE_CREATE, ioe, file); 1451 } 1452 1453 canUpdate = true; 1454 return null; 1455 } 1457 case REMOVE_FILE_ACTION: 1458 return privRemoveFile( actionFile) ? this : null; 1459 1460 case OPEN_CONTAINER_ACTION: 1461 { 1462 boolean isStub = false; 1464 StorageFile file = privGetFileName( actionIdentity, false, true, true); 1465 if (file == null) 1466 return null; 1467 1468 try { 1469 if (!file.exists()) { 1470 1471 file = privGetFileName( actionIdentity, true, true, true); 1473 if (!file.exists()) 1474 return null; 1475 isStub = true; 1476 } 1477 } catch (SecurityException se) { 1478 throw StandardException.newException( 1479 SQLState.DATA_UNEXPECTED_EXCEPTION, se); 1480 } 1481 1482 canUpdate = false; 1483 try { 1484 if (!dataFactory.isReadOnly() && file.canWrite()) 1485 canUpdate = true; 1486 } catch (SecurityException se) { 1487 } 1489 1490 try { 1491 1492 fileData = file.getRandomAccessFile(canUpdate ? "rw" : "r"); 1493 fileData.seek(FIRST_ALLOC_PAGE_OFFSET); 1494 readHeader(fileData); 1495 1496 if (SanityManager.DEBUG) 1497 { 1498 if (isStub) 1499 SanityManager.ASSERT(getDroppedState() && getCommittedDropState(), 1500 "a stub failed to set drop state"); 1501 } 1502 1503 } catch (IOException ioe) { 1504 1505 if (isStub) 1506 { 1507 throw dataFactory. 1508 markCorrupt(StandardException. 1509 newException(SQLState. 1510 FILE_CONTAINER_EXCEPTION, 1511 ioe, this)); 1512 } 1513 1514 StorageFile stub = 1516 privGetFileName(actionIdentity, true, true, true); 1517 1518 if (stub.exists()) 1519 { 1520 try 1521 { 1522 boolean delete_status = privRemoveFile(file); 1523 if (SanityManager.DEBUG) 1524 { 1525 if (!delete_status) 1526 { 1527 SanityManager.THROWASSERT( 1528 "delete of file (" + file + ") failed."); 1529 } 1530 } 1531 1532 fileData = 1533 stub.getRandomAccessFile(canUpdate ? "rw" : "r"); 1534 1535 readHeader(fileData); 1536 } 1537 catch (IOException ioe2) 1538 { 1539 throw dataFactory. 1540 markCorrupt(StandardException. 1541 newException(SQLState. 1542 FILE_CONTAINER_EXCEPTION, 1543 ioe2, this)); 1544 } 1545 1546 1548 } 1549 else 1550 throw dataFactory. 1551 markCorrupt(StandardException. 1552 newException(SQLState. 1553 FILE_CONTAINER_EXCEPTION, 1554 ioe, this)); 1555 } 1556 1557 return this; 1558 } 1560 case STUBBIFY_ACTION: 1561 { 1562 StorageFile file = privGetFileName( actionIdentity, false, false, true); 1563 StorageFile stub = privGetFileName( actionIdentity, true, false, false); 1564 1565 StorageRandomAccessFile stubData = null; 1566 1567 try 1568 { 1569 1583 if (!stub.exists()) 1584 { 1585 stubData = stub.getRandomAccessFile( "rw"); 1587 1588 writeRAFHeader(stubData, 1589 true, 1590 true); 1591 1592 stubData.close(); 1593 stubData = null; 1594 } 1595 1596 1597 dataFactory.flush(actionInstant); 1602 1603 privRemoveFile(file); 1610 1611 } 1612 catch (SecurityException se) 1613 { 1614 throw StandardException. 1615 newException(SQLState.FILE_CANNOT_REMOVE_FILE, se, file, 1616 se.toString()); 1617 } 1618 catch (IOException ioe) 1619 { 1620 try 1623 { 1624 if (stubData != null) 1625 { 1626 stubData.close(); 1627 stub.delete(); 1628 stubData = null; 1629 } 1630 1631 if (fileData != null) 1632 { 1633 fileData.close(); 1634 fileData = null; 1635 } 1636 } 1637 catch (IOException ioe2) 1638 { 1639 throw StandardException.newException( 1640 SQLState.FILE_CANNOT_REMOVE_FILE, ioe2, file, ioe.toString()); 1641 } 1642 catch (SecurityException se) 1643 { 1644 throw StandardException.newException( 1645 SQLState.FILE_CANNOT_REMOVE_FILE, se, file, stub); 1646 } 1647 } 1648 1649 dataFactory.stubFileToRemoveAfterCheckPoint(stub,actionInstant, getIdentity()); 1652 return null; 1653 } 1655 case BACKUP_CONTAINER_ACTION: { 1656 privBackupContainer(actionContainerHandle, actionBackupLocation); 1657 return null; 1658 } 1660 case GET_RANDOM_ACCESS_FILE_ACTION: { 1661 return actionFile.getRandomAccessFile("rw"); 1662 } 1664 1665 } return null; 1667 1668 } } 1670 | Popular Tags |