org.apache.derby.impl.store.raw.data
Class RAFContainer

java.lang.Object
  extended by org.apache.derby.impl.store.raw.data.BaseContainer
      extended by org.apache.derby.impl.store.raw.data.FileContainer
          extended by org.apache.derby.impl.store.raw.data.RAFContainer
All Implemented Interfaces:
java.security.PrivilegedExceptionAction, Cacheable, TypedFormat, Lockable
Direct Known Subclasses:
RAFContainer4, TempRAFContainer

 class RAFContainer
extends FileContainer
implements java.security.PrivilegedExceptionAction

RAFContainer (short for RandomAccessFileContainer) is a concrete subclass of FileContainer for FileContainers which are implemented on java.io.RandomAccessFile.


Field Summary
private  java.lang.String actionBackupLocation
           
private  int actionCode
           
private  BaseContainerHandle actionContainerHandle
           
private  boolean actionErrorOK
           
private  StorageFile actionFile
           
private  ContainerKey actionIdentity
           
private  LogInstant actionInstant
           
private  boolean actionStub
           
private  boolean actionTryAlternatePath
           
private static int BACKUP_CONTAINER_ACTION
           
private static int CREATE_CONTAINER_ACTION
           
protected  StorageRandomAccessFile fileData
           
private static int GET_FILE_NAME_ACTION
           
private static int GET_RANDOM_ACCESS_FILE_ACTION
           
private  boolean inBackup
           
private  boolean inRemove
           
protected  boolean needsSync
           
private static int OPEN_CONTAINER_ACTION
           
private static int REMOVE_FILE_ACTION
           
private static int STUBBIFY_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
RAFContainer(BaseDataFileFactory factory)
           
 
Method Summary
protected  void backupContainer(BaseContainerHandle handle, java.lang.String backupLocation)
          Backup the container.
 void clean(boolean forRemove)
          Clean the container.
private  void clearDirty()
           
(package private)  void closeContainer()
           
(package private)  void createContainer(ContainerKey newIdentity)
          Create a new container.
 Cacheable createIdentity(java.lang.Object key, java.lang.Object createParameter)
          Create a new item.
protected  void encryptContainer(BaseContainerHandle handle, java.lang.String newFilePath)
          Create encrypted version of the container with the user specified encryption properties.
protected  void flushAll()
          flush the cache to ensure all of my pages are written to disk
(package private)  StorageFile getFileName(ContainerKey identity, boolean stub, boolean errorOK, boolean tryAlternatePath)
           
 boolean isDirty()
          We treat this container as dirty if it has the container file open.
(package private)  boolean openContainer(ContainerKey newIdentity)
          Open a container.
private  boolean padFile(StorageRandomAccessFile file, long pageOffset)
          Pad the file upto the passed in page offset.
protected  int preAllocate(long lastPreallocPagenum, int preAllocSize)
          Preallocate some pages if need be
private  void privBackupContainer(BaseContainerHandle handle, java.lang.String backupLocation)
          Backup the container.
protected  StorageFile privGetFileName(ContainerKey identity, boolean stub, boolean errorOK, boolean tryAlternatePath)
           
(package private)  StorageRandomAccessFile privGetRandomAccessFile(StorageFile file)
           
private  boolean privRemoveFile(StorageFile file)
           
protected  void readPage(long pageNumber, byte[] pageData)
          Read a page into the supplied array.
protected  void removeContainer(LogInstant instant, boolean leaveStub)
          Remove the container
(package private)  boolean removeFile(StorageFile file)
           
 java.lang.Object run()
           
 Cacheable setIdentity(java.lang.Object key)
          Set container's identity
private  void stubbify(LogInstant instant)
           
protected  void truncatePages(long lastValidPagenum)
          Truncate pages of a container.
protected  byte[] updatePageArray(long pageNumber, byte[] pageData, byte[] encryptionBuf, boolean encryptWithNewEngine)
          Update the page array with container header if the page is a first alloc page and encrypt the page data if the database is encrypted.
protected  void writePage(long pageNumber, byte[] pageData, boolean syncPage)
          Write a page from the supplied array.
private  void writeRAFHeader(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, deallocatePage, decryptPage, doPreAllocatePages, dropContainer, encryptPage, getAllocPage, getAnyPage, getContainerProperties, getContainerVersion, 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, 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

BACKUP_CONTAINER_ACTION

private static final int BACKUP_CONTAINER_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

actionIdentity

private ContainerKey actionIdentity

actionStub

private boolean actionStub

actionErrorOK

private boolean actionErrorOK

actionTryAlternatePath

private boolean actionTryAlternatePath

actionFile

private StorageFile actionFile

actionInstant

private LogInstant actionInstant

actionBackupLocation

private java.lang.String actionBackupLocation

actionContainerHandle

private BaseContainerHandle actionContainerHandle

inBackup

private boolean inBackup

inRemove

private boolean inRemove
Constructor Detail

RAFContainer

RAFContainer(BaseDataFileFactory factory)
Method Detail

isDirty

public boolean isDirty()
Description copied from class: FileContainer
We treat this container as dirty if it has the container file open.

Specified by:
isDirty in interface Cacheable
Overrides:
isDirty in class FileContainer
See Also:
Cacheable.isDirty()

setIdentity

public Cacheable setIdentity(java.lang.Object key)
                      throws StandardException
Set container's identity

Specified by:
setIdentity in interface Cacheable
Overrides:
setIdentity in class FileContainer
Returns:
a valid object if the container was successfully opened, null if it does not exist.
Throws:
StandardException - Standard Derby error policy
See Also:
Cacheable.setIdentity(java.lang.Object)

createIdentity

public Cacheable createIdentity(java.lang.Object key,
                                java.lang.Object createParameter)
                         throws StandardException
Description copied from interface: Cacheable
Create a new item.

Create a new item and set the identity of the object to represent it. The object will be in the No Identity state, ie. it will have just been created or clearIdentity() was just called.
The object must copy the information out of key, not just store a reference to key if the key is not immutable. After this call the expression getIdentity().equals(key) must return true.

If the class of the object needs to change (e.g. to support a different format) then the object should create a new object, call its initParameter() with the parameters the original object was called with, set its identity and return a reference to it. The cache manager will discard the reference to the old object.
If an exception is thrown the object must be left in the no-identity state.
MT - single thread required - Method must only be called be cache manager and the cache manager will guarantee only one thread can be calling it.

Specified by:
createIdentity in interface Cacheable
Overrides:
createIdentity in class FileContainer
Returns:
an object reference if the object can take on the identity, null otherwise.
Throws:
StandardException - Standard Derby error policy
See Also:
CacheManager.create(java.lang.Object, java.lang.Object)

removeContainer

protected void removeContainer(LogInstant instant,
                               boolean leaveStub)
                        throws StandardException
Remove the container

Specified by:
removeContainer in class BaseContainer
leaveStub - 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:
closeContainer in class FileContainer

readPage

protected void readPage(long pageNumber,
                        byte[] pageData)
                 throws java.io.IOException,
                        StandardException
Read a page into the supplied array.
MT - thread safe

Specified by:
readPage in class FileContainer
Throws:
java.io.IOException - exception reading page
StandardException - Standard Derby error policy

writePage

protected void writePage(long pageNumber,
                         byte[] pageData,
                         boolean syncPage)
                  throws java.io.IOException,
                         StandardException
Write a page from the supplied array.
MT - thread safe

Specified by:
writePage in class FileContainer
Throws:
StandardException - Standard Derby error policy
java.io.IOException - IO error accessing page

updatePageArray

protected byte[] updatePageArray(long pageNumber,
                                 byte[] pageData,
                                 byte[] encryptionBuf,
                                 boolean encryptWithNewEngine)
                          throws StandardException,
                                 java.io.IOException
Update the page array with container header if the page is a first alloc page and encrypt the page data if the database is encrypted.

Parameters:
pageNumber - the page number of the page
pageData - byte array that has the actual page data.
encryptionBuf - buffer that is used to store encryted version of the page.
Returns:
byte array of the the page data as it should be on the disk.
Throws:
StandardException
java.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.IOException
StandardException

clean

public void clean(boolean forRemove)
           throws StandardException
Clean 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:
clean in interface Cacheable
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:
preAllocate in class FileContainer
Parameters:
lastPreallocPagenum - the last preallocated page number as known by the allocation page
preAllocSize - 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 StandardException
Truncate pages of a container.

Truncate all pages from lastValidPagenum+1 through the end of the file.

Specified by:
truncatePages in class BaseContainer
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(StorageRandomAccessFile file,
                            boolean create,
                            boolean syncFile)
                     throws java.io.IOException,
                            StandardException
Write the header of a random access file and sync it

Parameters:
create - if true, the container is being created if false, the container already exist
syncFile - if true, sync the file
Throws:
java.io.IOException
StandardException

flushAll

protected void flushAll()
                 throws StandardException
flush the cache to ensure all of my pages are written to disk

Specified by:
flushAll in class BaseContainer
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: FileContainer
Create 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:
createContainer in class FileContainer
Throws:
StandardException - Derby Standard error policy

removeFile

boolean removeFile(StorageFile file)
             throws java.lang.SecurityException,
                    StandardException
Throws:
java.lang.SecurityException
StandardException

privRemoveFile

private boolean privRemoveFile(StorageFile file)
                        throws StandardException
Throws:
StandardException

openContainer

boolean openContainer(ContainerKey newIdentity)
                throws StandardException
Description copied from class: FileContainer
Open 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:
openContainer in class FileContainer
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.

Specified by:
backupContainer in class FileContainer
Parameters:
handle - the container handle.
backupLocation - location of the backup container.
Throws:
StandardException - Standard Derby error policy

privBackupContainer

private void privBackupContainer(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.

Parameters:
handle - the container handle.
backupLocation - location of the backup container.
Throws:
StandardException - Derby Standard error policy

encryptContainer

protected void encryptContainer(BaseContainerHandle handle,
                                java.lang.String newFilePath)
                         throws StandardException
Create encrypted version of the container with the user specified encryption properties. Read all the pages of the container from the original container through the page cache, encrypt each page data with new encryption mechanism and write to the specified container file.

Specified by:
encryptContainer in class BaseContainer
Parameters:
handle - the container handle.
newFilePath - file to store the new encrypted version of the container
Throws:
StandardException - Derby Standard error policy

privGetRandomAccessFile

StorageRandomAccessFile privGetRandomAccessFile(StorageFile file)
                                          throws java.lang.SecurityException,
                                                 StandardException
Throws:
java.lang.SecurityException
StandardException

run

public java.lang.Object run()
                     throws StandardException,
                            java.io.IOException
Specified by:
run in interface java.security.PrivilegedExceptionAction
Throws:
StandardException
java.io.IOException

Built on Thu 2011-03-10 11:54:14+0000, from revision ???

Apache Derby V10.6 Internals - Copyright © 2004,2007 The Apache Software Foundation. All Rights Reserved.