Class RAFContainer
- java.lang.Object
-
- org.apache.derby.impl.store.raw.data.BaseContainer
-
- org.apache.derby.impl.store.raw.data.FileContainer
-
- org.apache.derby.impl.store.raw.data.RAFContainer
-
- All Implemented Interfaces:
java.security.PrivilegedExceptionAction<java.lang.Object>,Cacheable,TypedFormat,Lockable
- Direct Known Subclasses:
RAFContainer4,TempRAFContainer
class RAFContainer extends FileContainer implements java.security.PrivilegedExceptionAction<java.lang.Object>
RAFContainer (short for RandomAccessFileContainer) is a concrete subclass of FileContainer for FileContainers which are implemented on java.io.RandomAccessFile.
-
-
Field Summary
Fields Modifier and Type Field Description private intactionCodeprivate booleanactionErrorOKprivate StorageFileactionFileprivate ContainerKeyactionIdentityprivate LogInstantactionInstantprivate booleanactionStubprivate booleanactionTryAlternatePathprivate static intCREATE_CONTAINER_ACTIONprotected StorageRandomAccessFilefileDataprivate java.lang.StringfileNameprivate static intGET_FILE_NAME_ACTIONprivate static intGET_RANDOM_ACCESS_FILE_ACTIONprivate booleaninBackupprivate booleaninRemoveprotected booleanneedsSyncprivate static intOPEN_CONTAINER_ACTIONprivate static intREMOVE_FILE_ACTIONprivate static intREOPEN_CONTAINER_ACTIONprivate static intSTUBBIFY_ACTION-
Fields inherited from class org.apache.derby.impl.store.raw.data.FileContainer
allocCache, canUpdate, CHECKSUM_SIZE, CONTAINER_INFO_SIZE, containerCache, containerInfo, containerVersion, dataFactory, estimatedRowCount, FIRST_ALLOC_PAGE_NUMBER, FIRST_ALLOC_PAGE_OFFSET, firstAllocPageNumber, firstAllocPageOffset, formatIdInteger, initialPages, isDirty, lastLogInstant, minimumRecordSize, pageCache, pageSize, preDirty, SPACE_TRACE, spareSpace
-
Fields inherited from class org.apache.derby.impl.store.raw.data.BaseContainer
identity, isCommittedDrop, isDropped, isReusableRecordId
-
-
Constructor Summary
Constructors Constructor Description RAFContainer(BaseDataFileFactory factory)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidbackupContainer(BaseContainerHandle handle, java.lang.String backupLocation)Backup the container.voidclean(boolean forRemove)Clean the container.private voidclearDirty()(package private) voidcloseContainer()private voidcopyFile(StorageFile from, java.io.File to)Copy the contents of aStorageFileto ajava.io.File.(package private) voidcreateContainer(ContainerKey newIdentity)Create a new container.protected voidencryptOrDecryptContainer(BaseContainerHandle handle, java.lang.String newFilePath, boolean doEncrypt)Creates encrypted or decrypted version of the container.protected voidflushAll()flush the cache to ensure all of my pages are written to disk(package private) StorageFilegetFileName(ContainerKey identity, boolean stub, boolean errorOK, boolean tryAlternatePath)private java.io.RandomAccessFilegetRandomAccessFile(java.io.File file)Get a RandomAccessFile for accessing a file in read-write mode.(package private) StorageRandomAccessFilegetRandomAccessFile(StorageFile file)booleanisDirty()We treat this container as dirty if it has the container file open.(package private) booleanopenContainer(ContainerKey newIdentity)Open a container.private booleanpadFile(StorageRandomAccessFile file, long pageOffset)Pad the file upto the passed in page offset.protected intpreAllocate(long lastPreallocPagenum, int preAllocSize)Preallocate some pages if need beprotected StorageFileprivGetFileName(ContainerKey identity, boolean stub, boolean errorOK, boolean tryAlternatePath)private booleanprivRemoveFile(StorageFile file)protected voidreadPage(long pageNumber, byte[] pageData)Read a page into the supplied array.protected voidremoveContainer(LogInstant instant, boolean leaveStub)Remove the containerprivate voidremoveFile(java.io.File file)Remove a file.(package private) booleanremoveFile(StorageFile file)protected voidreopenContainer(ContainerKey currentIdentity)Only used by RAFContainer4 (NIO) to reopen RAF when its channel gets closed due to interrupts.java.lang.Objectrun()private voidstubbify(LogInstant instant)protected voidtruncatePages(long lastValidPagenum)Truncate pages of a container.protected byte[]updatePageArray(long pageNumber, byte[] pageData, byte[] encryptionBuf, boolean encryptWithNewEngine)Updates the page array with container header if the page is a first allocation page and encrypts the page data if the database is encrypted.protected voidwritePage(long pageNumber, byte[] pageData, boolean syncPage)Write a page from the supplied array.private voidwriteRAFHeader(java.lang.Object identity, StorageRandomAccessFile file, boolean create, boolean syncFile)Write the header of a random access file and sync it-
Methods inherited from class org.apache.derby.impl.store.raw.data.FileContainer
bumpContainerVersion, canUpdate, clearIdentity, clearPreallocThreshold, compressContainer, createIdent, createIdentity, deallocatePage, decryptPage, doPreAllocatePages, dropContainer, encryptPage, getAllocPage, getAnyPage, getContainerProperties, getContainerVersion, getContextService, getEmbryonicPage, getEmbryonicPage, getEncryptionBuffer, getEstimatedPageCount, getEstimatedRowCount, getFirstHeadPage, getHeadPage, getLastPageNumber, getLatchedPage, getMinimumRecordSize, getNextHeadPage, getPage, getPageForCompress, getPageForInsert, getPageSize, getReusableRecordIdSequenceNumber, getSpaceInfo, getSpareSpace, getTypeFormatId, incrementReusableRecordIdSequenceNumber, initPage, latchPage, letGo, logCreateContainerInfo, newPage, preDirty, prepareForBulkLoad, readHeader, reCreatePageForRedoRecovery, setDirty, setEstimatedRowCount, setIdent, setIdentity, trackUnfilledPage, updateEstimatedRowCount, writeAtOffset, writeHeader, writeHeader
-
Methods inherited from class org.apache.derby.impl.store.raw.data.BaseContainer
addPage, compressContainer, fillInIdentity, getAllocPage, getAnyPage, getCommittedDropState, getContainerId, getContainerStatus, getDeallocLock, getDroppedState, getFirstPage, getIdentity, getNextPage, getSegmentId, isReusableRecordId, lockAttributes, lockerAlwaysCompatible, lockEvent, removePage, requestCompatible, setCommittedDropState, setDroppedState, setReusableRecordIdState, truncate, unlockEvent, use
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.derby.iapi.services.cache.Cacheable
getIdentity
-
-
-
-
Field Detail
-
fileData
protected StorageRandomAccessFile fileData
-
needsSync
protected boolean needsSync
-
actionCode
private int actionCode
-
GET_FILE_NAME_ACTION
private static final int GET_FILE_NAME_ACTION
- See Also:
- Constant Field Values
-
CREATE_CONTAINER_ACTION
private static final int CREATE_CONTAINER_ACTION
- See Also:
- Constant Field Values
-
REMOVE_FILE_ACTION
private static final int REMOVE_FILE_ACTION
- See Also:
- Constant Field Values
-
OPEN_CONTAINER_ACTION
private static final int OPEN_CONTAINER_ACTION
- See Also:
- Constant Field Values
-
STUBBIFY_ACTION
private static final int STUBBIFY_ACTION
- See Also:
- Constant Field Values
-
GET_RANDOM_ACCESS_FILE_ACTION
private static final int GET_RANDOM_ACCESS_FILE_ACTION
- See Also:
- Constant Field Values
-
REOPEN_CONTAINER_ACTION
private static final int REOPEN_CONTAINER_ACTION
- See Also:
- Constant Field Values
-
actionIdentity
private ContainerKey actionIdentity
-
actionStub
private boolean actionStub
-
actionErrorOK
private boolean actionErrorOK
-
actionTryAlternatePath
private boolean actionTryAlternatePath
-
actionFile
private StorageFile actionFile
-
actionInstant
private LogInstant actionInstant
-
inBackup
private boolean inBackup
-
inRemove
private boolean inRemove
-
fileName
private java.lang.String fileName
-
-
Constructor Detail
-
RAFContainer
RAFContainer(BaseDataFileFactory factory)
-
-
Method Detail
-
isDirty
public boolean isDirty()
Description copied from class:FileContainerWe treat this container as dirty if it has the container file open.- Specified by:
isDirtyin interfaceCacheable- Overrides:
isDirtyin classFileContainer- See Also:
Cacheable.isDirty()
-
removeContainer
protected void removeContainer(LogInstant instant, boolean leaveStub) throws StandardException
Remove the container- Specified by:
removeContainerin classBaseContainerleaveStub- if true, leave a stub. If false, remove everything- Throws:
StandardException- Standard Derby error policy- See Also:
RawContainerHandle.removeContainer(org.apache.derby.iapi.store.raw.log.LogInstant)
-
closeContainer
void closeContainer()
- Specified by:
closeContainerin classFileContainer
-
readPage
protected void readPage(long pageNumber, byte[] pageData) throws java.io.IOException, StandardExceptionRead a page into the supplied array.
MT - thread safe- Specified by:
readPagein classFileContainer- Throws:
java.io.IOException- exception reading pageStandardException- Standard Derby error policy
-
writePage
protected void writePage(long pageNumber, byte[] pageData, boolean syncPage) throws java.io.IOException, StandardExceptionWrite a page from the supplied array.
MT - thread safe- Specified by:
writePagein classFileContainer- Throws:
StandardException- Standard Derby error policyjava.io.IOException- IO error accessing page
-
updatePageArray
protected byte[] updatePageArray(long pageNumber, byte[] pageData, byte[] encryptionBuf, boolean encryptWithNewEngine) throws StandardException, java.io.IOExceptionUpdates the page array with container header if the page is a first allocation page and encrypts the page data if the database is encrypted.- Parameters:
pageNumber- the page number of the pagepageData- byte array that has the actual page data.encryptionBuf- buffer that is used to store encrypted version of the page, ornullif encryption is to be skippedencryptWithNewEngine- whether to use the new encryption engine for encryption (only considered ifencryptionBuf != null)- Returns:
- byte array of the the page data as it should be on the disk.
- Throws:
StandardExceptionjava.io.IOException
-
padFile
private boolean padFile(StorageRandomAccessFile file, long pageOffset) throws java.io.IOException, StandardException
Pad the file upto the passed in page offset. Returns true if the file needed padding.- Throws:
java.io.IOExceptionStandardException
-
clean
public void clean(boolean forRemove) throws StandardExceptionClean the container.Write out the container header and sync all dirty pages of this container to disk before returning.
checkpoint calls this interface through callbacks by telling the cache manager to clean all containers in the open container cache. This sync of the file happens as part of writing and then syncing the container header in writeRAFHeader().
- Specified by:
cleanin interfaceCacheable- Parameters:
forRemove- Is clean called because container is being removed?- Throws:
StandardException- Standard exception policy.
-
clearDirty
private void clearDirty()
-
preAllocate
protected int preAllocate(long lastPreallocPagenum, int preAllocSize)Preallocate some pages if need be- Specified by:
preAllocatein classFileContainer- Parameters:
lastPreallocPagenum- the last preallocated page number as known by the allocation pagepreAllocSize- try to preallocate this page number of pages. Since only the container knows how many pages are actually on disk, it may determine that certain number of pages that the allocation page thinks need to be preallocated is already allocated, in those case, act as if the preallocation is successful.- Returns:
- number of successfully preallocated page, or 0 if no page has been preallocated
-
truncatePages
protected void truncatePages(long lastValidPagenum) throws StandardExceptionTruncate pages of a container.Truncate all pages from lastValidPagenum+1 through the end of the file.
- Specified by:
truncatePagesin classBaseContainer- Parameters:
lastValidPagenum- The page number of the last valid page of the file. All pages after this one are truncated.- Throws:
StandardException- Standard exception policy.
-
writeRAFHeader
private void writeRAFHeader(java.lang.Object identity, StorageRandomAccessFile file, boolean create, boolean syncFile) throws java.io.IOException, StandardExceptionWrite the header of a random access file and sync it- Parameters:
create- if true, the container is being created if false, the container already existsyncFile- if true, sync the file- Throws:
java.io.IOExceptionStandardException
-
flushAll
protected void flushAll() throws StandardExceptionflush the cache to ensure all of my pages are written to disk- Specified by:
flushAllin classBaseContainer- Throws:
StandardException- Standard Derby error policy
-
getFileName
StorageFile getFileName(ContainerKey identity, boolean stub, boolean errorOK, boolean tryAlternatePath) throws StandardException
- Throws:
StandardException
-
privGetFileName
protected StorageFile privGetFileName(ContainerKey identity, boolean stub, boolean errorOK, boolean tryAlternatePath) throws StandardException
- Throws:
StandardException
-
createContainer
void createContainer(ContainerKey newIdentity) throws StandardException
Description copied from class:FileContainerCreate a new container.Create a new container, all references to identity must be through the passed in identity, this object will no identity until after this method returns.
- Specified by:
createContainerin classFileContainer- Throws:
StandardException- Derby Standard error policy
-
copyFile
private void copyFile(StorageFile from, java.io.File to) throws StandardException
Copy the contents of aStorageFileto ajava.io.File.- Parameters:
from- the file to copy fromto- the file to copy to- Throws:
StandardException- if the copying failed
-
removeFile
private void removeFile(java.io.File file) throws StandardExceptionRemove a file.- Parameters:
file- the file to remove- Throws:
StandardException- if the file could not be removed
-
removeFile
boolean removeFile(StorageFile file) throws java.lang.SecurityException, StandardException
- Throws:
java.lang.SecurityExceptionStandardException
-
privRemoveFile
private boolean privRemoveFile(StorageFile file) throws StandardException
- Throws:
StandardException
-
openContainer
boolean openContainer(ContainerKey newIdentity) throws StandardException
Description copied from class:FileContainerOpen a container.Longer descrption of routine.
Open a container. Open the file that maps to this container, if the file does not exist then we assume the container was never created. If the file exists but we have trouble opening it then we throw some exception.
MT - single thread required - Enforced by cache manager.- Specified by:
openContainerin classFileContainer- Throws:
StandardException- Standard exception policy.
-
reopenContainer
protected void reopenContainer(ContainerKey currentIdentity) throws StandardException
Only used by RAFContainer4 (NIO) to reopen RAF when its channel gets closed due to interrupts.- Parameters:
currentIdentity-- Throws:
StandardException- standard exception policy
-
stubbify
private void stubbify(LogInstant instant) throws StandardException
- Throws:
StandardException
-
backupContainer
protected void backupContainer(BaseContainerHandle handle, java.lang.String backupLocation) throws StandardException
Backup the container. The container is written to the backup by reading the pages through the page cache, and then writing into the backup container. If the container is dropped(commitetd drop), only container stub is copied to the backup using simple file copy. MT - At any given time only one backup thread is allowed, but when backup in progress DML/DDL operations can run in parallel. Pages are latched while writing them to the backup to avoid copying partial changes to the pages. Online backup does not acquire any user level locks , so users can drop tables when backup is in progress. So it is possible that Container Removal request can come in when container backup is in progress. This case is handled by using the synchronization on this object monitor and using inRemove and inBackup flags. Conatiner removal checks if backup is in progress and wait for the backup to yield to continue the removal. Basic idea is to give preference to remove by stopping the backup of the container temporarily, when the remove container is requested by another thread. Generally, it takes more time to backup a regular container than the stub becuase stub is just one page. After each page copy, a check is made to find if a remove is requested and if it is then backup of the container is aborted and the backup thread puts itself into the wait state until remove request thread notifies that the remove is complete. When remove request compeletes stub is copied into the backup. Compress is blocked when backup is in progesss, so truncation of the container can not happen when backup is in progess. No need to synchronize backup of the container with truncation.- Specified by:
backupContainerin classFileContainer- Parameters:
handle- the container handle.backupLocation- location of the backup container.- Throws:
StandardException- Derby Standard error policy
-
encryptOrDecryptContainer
protected void encryptOrDecryptContainer(BaseContainerHandle handle, java.lang.String newFilePath, boolean doEncrypt) throws StandardException
Creates encrypted or decrypted version of the container. Reads all the pages of the container from the original container through the page cache, then either encrypts page data with the new encryption mechanism or leaves the page data un-encrypted, and finally writes the data to the specified new container file.The encryption and decryption engines used to carry out the cryptographic operation(s) are configured through the raw store, and accessed via the data factory. Note that the pages have already been decrypted before being put into the page cache.
- Specified by:
encryptOrDecryptContainerin classBaseContainer- Parameters:
handle- the container handlenewFilePath- file to store the new version of the container indoEncrypt- tells whether to encrypt or not- Throws:
StandardException- Derby Standard error policy
-
getRandomAccessFile
private java.io.RandomAccessFile getRandomAccessFile(java.io.File file) throws java.io.IOExceptionGet a RandomAccessFile for accessing a file in read-write mode.- Parameters:
file- the file to access- Returns:
- a RandomAccessFile
- Throws:
java.io.FileNotFoundException- iffilecannot be opened in read-write modejava.io.IOException- if some other I/O error happens
-
getRandomAccessFile
StorageRandomAccessFile getRandomAccessFile(StorageFile file) throws java.lang.SecurityException, StandardException
- Throws:
java.lang.SecurityExceptionStandardException
-
run
public java.lang.Object run() throws StandardException- Specified by:
runin interfacejava.security.PrivilegedExceptionAction<java.lang.Object>- Throws:
StandardException
-
-