Class FileContainer

    • Field Detail

      • containerCache

        protected final CacheManager containerCache
      • pageSize

        protected int pageSize
      • spareSpace

        protected int spareSpace
      • minimumRecordSize

        protected int minimumRecordSize
      • initialPages

        protected short initialPages
      • canUpdate

        protected boolean canUpdate
      • PreAllocThreshold

        private int PreAllocThreshold
      • PreAllocSize

        private int PreAllocSize
      • bulkIncreaseContainerSize

        private boolean bulkIncreaseContainerSize
      • DEFAULT_PRE_ALLOC_SIZE

        private static final int DEFAULT_PRE_ALLOC_SIZE
        See Also:
        Constant Field Values
      • firstAllocPageNumber

        protected long firstAllocPageNumber
      • firstAllocPageOffset

        protected long firstAllocPageOffset
      • containerVersion

        protected long containerVersion
      • estimatedRowCount

        protected long estimatedRowCount
      • lastLogInstant

        protected LogInstant lastLogInstant
      • reusableRecordIdSequenceNumber

        private long reusableRecordIdSequenceNumber
        The sequence number for reusable recordIds . As long as this number does not change, recordIds will be stable within the container.
      • lastInsertedPage

        private long[] lastInsertedPage
        The page that was last inserted into. Use this for getPageForInsert. Remember the last allocated non-overflow page, and remember it in memory only. Use Get/Set method to access this field except when we know it is being single thread access.
      • lastInsertedPage_index

        private int lastInsertedPage_index
      • lastUnfilledPage

        private long lastUnfilledPage
        The last unfilled page found. Use this for getPageForInsert. Remember the last unfilled page found, and remember it in memory only. Use Get/Set method to access this field except when we know it is being single thread access.
      • lastAllocatedPage

        private long lastAllocatedPage
        The last allocated page. This global var is access *without* synchronization. It is used as a hint for page allocation to find the next reusable page.
      • estimatedPageCount

        private long estimatedPageCount
        An estimated page count. Use this for getEstimatedPagecount. Remember it in memory only.
      • preDirty

        protected boolean preDirty
      • isDirty

        protected boolean isDirty
      • containerInfo

        byte[] containerInfo
      • checksum

        private java.util.zip.CRC32 checksum
      • encryptionBuffer

        private byte[] encryptionBuffer
      • CONTAINER_FORMAT_ID_SIZE

        private static final int CONTAINER_FORMAT_ID_SIZE
        the container format must fit in this many bytes
        See Also:
        Constant Field Values
      • CONTAINER_INFO_SIZE

        protected static final int CONTAINER_INFO_SIZE
        The size of the persistently stored container info ContainerHeader contains the following information: 4 bytes int FormatId 4 bytes int status 4 bytes int pageSize 4 bytes int spareSpace 4 bytes int minimumRecordSize 2 bytes short initialPages 2 bytes short spare1 8 bytes long first Allocation page number 8 bytes long first Allocation page offset 8 bytes long container version 8 bytes long estimated number of rows 8 bytes long reusable recordId sequence number 8 bytes long spare3 8 bytes long checksum container info size is 80 bytes, with 10 bytes of spare space
        See Also:
        Constant Field Values
      • FIRST_ALLOC_PAGE_NUMBER

        public static final long FIRST_ALLOC_PAGE_NUMBER
        where the first alloc page is located - the logical page number and the physical page offset NOTE if it is not 0 this is not going to work for Stream file which doesn't support seek
        See Also:
        Constant Field Values
      • FIRST_ALLOC_PAGE_OFFSET

        public static final long FIRST_ALLOC_PAGE_OFFSET
        See Also:
        Constant Field Values
      • FILE_REUSABLE_RECORDID

        private static final int FILE_REUSABLE_RECORDID
        See Also:
        Constant Field Values
      • SPACE_TRACE

        protected static final java.lang.String SPACE_TRACE
    • Method Detail

      • getTypeFormatId

        public int getTypeFormatId()
        Return my format identifier.
        Specified by:
        getTypeFormatId in interface TypedFormat
        Returns:
        The identifier. (A UUID stuffed in an array of 16 bytes).
      • 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
        Returns:
        an object reference if the object can take on the identity, null otherwise.
        Throws:
        StandardException - If forCreate is true and the object cannot be created.
        See Also:
        CacheManager.create(java.lang.Object, java.lang.Object)
      • clearIdentity

        public void clearIdentity()
        Description copied from interface: Cacheable
        Put the object into 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:
        clearIdentity in interface Cacheable
        Overrides:
        clearIdentity in class BaseContainer
      • isDirty

        public boolean isDirty()
        We treat this container as dirty if it has the container file open.
        Specified by:
        isDirty in interface Cacheable
        See Also:
        Cacheable.isDirty()
      • preDirty

        public void preDirty​(boolean preDirtyOn)
        Description copied from class: BaseContainer
        The container is about to be modified. Loggable actions use this to make sure the container gets cleaned if a checkpoint is taken after any log record is sent to the log stream but before the container is actually dirtied.
        Specified by:
        preDirty in class BaseContainer
      • setDirty

        protected void setDirty​(boolean dirty)
      • createContainer

        abstract void createContainer​(ContainerKey newIdentity)
                               throws StandardException
        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.

        Throws:
        StandardException - Derby Standard error policy
      • openContainer

        abstract boolean openContainer​(ContainerKey newIdentity)
                                throws StandardException
        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.

        Throws:
        StandardException - Standard exception policy.
      • closeContainer

        abstract void closeContainer()
      • bumpContainerVersion

        protected final void bumpContainerVersion​(LogInstant instant)
        increment the version by one and return the new version.
        MT - caller must synchronized this in the same sync block that modifies the container header.
      • getContainerProperties

        public void getContainerProperties​(java.util.Properties prop)
                                    throws StandardException
        Request the system properties associated with a container.

        Request the value of properties that are associated with a container. The following properties can be requested: derby.storage.pageSize derby.storage.pageReservedSpace derby.storage.minimumRecordSize derby.storage.reusableRecordId derby.storage.initialPages

        To get the value of a particular property add it to the property list, and on return the value of the property will be set to it's current value. For example: get_prop(ConglomerateController cc) { Properties prop = new Properties(); prop.put("derby.storage.pageSize", ""); cc.getContainerProperties(prop); System.out.println( "table's page size = " + prop.getProperty("derby.storage.pageSize"); }

        Specified by:
        getContainerProperties in class BaseContainer
        Parameters:
        prop - Property list to fill in.
        Throws:
        StandardException - Standard exception policy.
      • readHeader

        protected void readHeader​(byte[] epage)
                           throws java.io.IOException,
                                  StandardException
        Read the container's header. When this method is called, the embryonic page that is passed in must have been read directly from the file or the input stream, even if the alloc page may still be in cache. This is because a stubbify operation only writes the stub to disk, it does not get rid of any stale page from the page cache. So if it so happens that the stubbified container object is aged out of the container cache but the first alloc page hasn't, then when any stale page of this container wants to be written out, the container needs to be reopened, which is when this routine is called. We must not get the alloc page in cache because it may be stale page and it may still say the container has not been dropped.
        MT - single thread required - Enforced by caller.
        Parameters:
        epage - the embryonic page to read the header from
        Throws:
        StandardException - Derby Standard error policy
        java.io.IOException - error in reading the header from file
      • initContainerHeader

        private void initContainerHeader​(boolean changeContainer)
      • readHeaderFromArray

        private void readHeaderFromArray​(byte[] a)
                                  throws StandardException,
                                         java.io.IOException
        Read containerInfo from a byte array The container Header array must be written by or of the same format as put together by writeHeaderFromArray.
        Throws:
        StandardException - Derby Standard error policy
        java.io.IOException - error in reading the header from file
      • writeHeader

        protected void writeHeader​(java.lang.Object identity,
                                   byte[] pageData)
                            throws StandardException,
                                   java.io.IOException
        Write the container header to a page array (the first allocation page)
        Throws:
        StandardException - Derby Standard error policy
        java.io.IOException - error in writing the header to file
      • writeHeader

        protected void writeHeader​(java.lang.Object identity,
                                   StorageRandomAccessFile file,
                                   boolean create,
                                   byte[] epage)
                            throws java.io.IOException,
                                   StandardException
        Write the container header directly to file. Subclasses that can writes the container header is expected to manufacture a DataOutput stream which is used here.
        MT - single thread required - Enforced by caller
        Throws:
        StandardException - Derby Standard error policy
        java.io.IOException - error in writing the header to file
      • writeAtOffset

        void writeAtOffset​(StorageRandomAccessFile file,
                           byte[] bytes,
                           long offset)
                    throws java.io.IOException,
                           StandardException
        Write a sequence of bytes at the given offset in a file. This method is not thread safe, so the caller must make sure that no other thread is performing operations that may change current position in the file.
        Parameters:
        file - the file to write to
        bytes - the bytes to write
        offset - the offset to start writing at
        Throws:
        java.io.IOException - if an I/O error occurs while writing
        StandardException - Derby Standard error policy
      • getEmbryonicPage

        protected byte[] getEmbryonicPage​(java.io.DataInput fileData)
                                   throws java.io.IOException,
                                          StandardException
        Get an embryonic page from the dataInput stream. The embryonic page will be read in from the input stream (fileData), which is assumed to be positioned at the beginning of the first allocation page.
        Throws:
        java.io.IOException - error in read the embryonic page from file
        StandardException - Derby Standard error policy
      • getEmbryonicPage

        byte[] getEmbryonicPage​(StorageRandomAccessFile file,
                                long offset)
                         throws java.io.IOException,
                                StandardException
        Read an embryonic page (that is, a section of the first alloc page that is so large that we know all the borrowed space is included in it) from the specified offset in a StorageRandomAccessFile. This method is not thread safe, so the caller must make sure that no other thread is performing operations that may change current position in the file.
        Parameters:
        file - the file to read from
        offset - where to start reading (normally FileContainer.FIRST_ALLOC_PAGE_OFFSET)
        Returns:
        a byte array containing the embryonic page
        Throws:
        java.io.IOException - if an I/O error occurs while reading
        StandardException - Derby Standard error policy
      • writeHeaderToArray

        private void writeHeaderToArray​(byte[] a)
                                 throws java.io.IOException
        Write containerInfo into a byte array The container Header thus put together can be read by readHeaderFromArray.
        Throws:
        java.io.IOException - error in writing the header
      • createInfoFromLog

        private void createInfoFromLog​(ByteArray byteArray)
                                throws StandardException
        Set container properties from the passed in ByteArray, which is created by logCreateContainerInfo. This information is used to recreate the container during recovery load tran. The following container properties are set: pageSize spareSpace minimumRecordSize isReusableRecordId initialPages
        Throws:
        StandardException
      • createInfoFromProp

        private void createInfoFromProp​(java.util.Properties createArgs)
                                 throws StandardException
        Set container properties from the passed in createArgs. The following container properties are set: pageSize spareSpace minimumRecordSize isReusableRecordId initialPages RESOLVE - in the future setting parameters should be overridable by sub-class, e.g. one implementation of Container may require a minimum page size of 4k.
        Throws:
        StandardException
      • canUpdate

        protected boolean canUpdate()
        Description copied from class: BaseContainer
        Can the container be updated.
        Specified by:
        canUpdate in class BaseContainer
        Returns:
        true if the container can be updated, false otherwise.
      • deallocatePage

        protected void deallocatePage​(BaseContainerHandle handle,
                                      BasePage page)
                               throws StandardException
        Deallocate a page from the container.
        Specified by:
        deallocatePage in class BaseContainer
        Parameters:
        handle - the container handle doing the deallocation
        page - the page to be deallocated. It is latched upon entry and will be unlatched by the caller of this function
        Throws:
        StandardException - Derby Standard error policy
      • compressContainer

        protected void compressContainer​(RawTransaction ntt,
                                         BaseContainerHandle allocHandle)
                                  throws StandardException
        Compress free space from container.
        MT - thread aware - It is assumed that our caller (our super class) has already arranged a logical lock on page allocation to only allow a single thread through here. Compressing free space is done in allocation page units, working it's way from the end of the container to the beginning. Each loop operates on the last allocation page in the container. Freeing space in the container page involves 2 transactions, an update to an allocation page, N data pages, and possibly the delete of the allocation page. The User Transaction (UT) initiated the compress call. The Nested Top Transaction (NTT) is the transaction started by RawStore inside the compress call. This NTT is committed before compress returns. The NTT is used to access high traffic data structures such as the AllocPage. This is outline of the algorithm used in compressing the container. Until a non free page is found loop, in each loop return to the OS all space at the end of the container occupied by free pages, including the allocation page itself if all of it's pages are free. 1) Find last 2 allocation pages in container (last if there is only one). 2) invalidate the allocation information cached by the container. Without the cache no page can be gotten from the container. Pages already in the page cache are not affected. Thus by latching the allocPage and invalidating the allocation cache, this NTT blocks out all page gets from this container until it commits. 3) the allocPage determines which pages can be released to the OS, mark that in its data structure (the alloc extent). Mark the contiguous block of nallocated/free pages at the end of the file as unallocated. This change is associated with the NTT. 4) The NTT calls the OS to deallocate the space from the file. Note that the system can handle being booted and asked to get an allocated page which is past end of file, it just extends the file automatically. 5) If freeing all space on the alloc page, and there is more than one alloc page, then free the alloc page - this requires an update to the previous alloc page which the loop has kept latched also. 6) if the last alloc page was deleted, restart loop at #1 All NTT latches are released before this routine returns. If we use an NTT, the caller has to commit the NTT to release the allocPage latch. If we don't use an NTT, the allocPage latch is released as this routine returns.
        Specified by:
        compressContainer in class BaseContainer
        Parameters:
        ntt - - the nested top transaction for the purpose of freeing space. If ntt is null, use the user transaction for allocation. #param allocHandle - the container handle opened by the ntt, use this to latch the alloc page
        Throws:
        StandardException - Standard Derby error policy
      • newPage

        protected BasePage newPage​(BaseContainerHandle userHandle,
                                   RawTransaction ntt,
                                   BaseContainerHandle allocHandle,
                                   boolean isOverflow)
                            throws StandardException
        Create a new page in the container.
        MT - thread aware - It is assumed that our caller (our super class) has already arranged a logical lock on page allocation to only allow a single thread through here. Adding a new page involves 2 transactions and 2 pages. The User Transaction (UT) initiated the addPage call and expects a latched page (owns by the UT) to be returned. The Nested Top Transaction (NTT) is the transaction started by RawStore inside an addPage call. This NTT is committed before the page is returned. The NTT is used to accessed high traffic data structure such as the AllocPage. This is outline of the algorithm used in adding a page: 1) find or make an allocPage which can handle the addding of a new page. Latch the allocPage with the NTT. 2) invalidate the allocation information cached by the container. Without the cache no page can be gotten from the container. Pages already in the page cache is not affected. Thus by latching the allocPage and invalidating the allocation cache, this NTT blocks out all page gets from this container until it commits. 3) the allocPage determines which page can be allocated, mark that in its data structure (the alloc extent) and returns the page number of the new page. This change is associated with the NTT. 4) the NTT gets or creates the new page in the page cache (bypassing the lookup of the allocPage since that is already latched by the NTT and will deadlock). 5) the NTT initializes the page (mark it is being a VALID page). 6) the page latch is transfered to the UT from the NTT. 7) the new page is returned, latched by UT If we use an NTT, the caller has to commit the NTT to release the allocPage latch. If we don't use an NTT, the allocPage latch is released as this routine returns.
        Specified by:
        newPage in class BaseContainer
        Parameters:
        userHandle - - the container handle opened by the user transaction, use this to latch the new user page
        ntt - - the nested top transaction for the purpose of allocating the new page If ntt is null, use the user transaction for allocation. #param allocHandle - the container handle opened by the ntt, use this to latch the alloc page
        Throws:
        StandardException - Standard Derby error policy
      • clearPreallocThreshold

        protected void clearPreallocThreshold()
        Description copied from class: BaseContainer
        The container will have no pre-allocate threshold, i.e., if the implementation supports it, page preallocation will happen the next time a new page is allocated.
        Specified by:
        clearPreallocThreshold in class BaseContainer
      • initPage

        protected BasePage initPage​(BaseContainerHandle allochandle,
                                    PageKey pkey,
                                    PageCreationArgs createArgs,
                                    long pageOffset,
                                    boolean reuse,
                                    boolean overflow)
                             throws StandardException
        Initialize a page
        Parameters:
        allochandle - the contianer handle to initialize the page with - the ntt
        pkey - the page number of the page to be initialized
        createArgs - the arguments for page creation
        reuse - is true if we are reusing a page that has already been initialized once
        Returns:
        a latched page that has been initialized.
        Throws:
        StandardException - Derby Standard error policy
      • getUserPage

        private BasePage getUserPage​(BaseContainerHandle handle,
                                     long pageNumber,
                                     boolean overflowOK,
                                     boolean wait)
                              throws StandardException
        Get a page in the container. Get User page is the generic base routine for all user (client to raw store) getPage. This routine coordinate with allocation/deallocation to ensure that no page can be gotten from the container while page is in the middle of being allocated or deallocated. This routine latches the page.
        Parameters:
        handle - the container handle
        pageNumber - the page number of the page to get
        overflowOK - if true then an overflow page is OK, if false, then only non-overflow page is OK
        wait - if true then wait for a latch
        Returns:
        the latched page
        MT - thread safe
        Throws:
        StandardException - Standard Derby error policy
      • trackUnfilledPage

        protected void trackUnfilledPage​(long pagenumber,
                                         boolean unfilled)
      • reCreatePageForRedoRecovery

        protected BasePage reCreatePageForRedoRecovery​(BaseContainerHandle handle,
                                                       int pageFormat,
                                                       long pageNumber,
                                                       long pageOffset)
                                                throws StandardException
        ReCreate a page for rollforward recovery.

        During redo recovery it is possible for the system to try to redo the creation of a page (ie. going from non-existence to version 0). It first trys to read the page from disk, but a few different types of errors can occur: o the page does not exist at all on disk, this can happen during rollforward recovery applied to a backup where the file was copied and the page was added to the file during the time frame of the backup but after the physical file was copied. o space in the file exists, but it was never initalized. This can happen if you happen to crash at just the right moment during the allocation process. Also on some OS's it is possible to read from a part of the file that was not ever written - resulting in garbage from the store's point of view (often the result is all 0's). All these errors are easy to recover from as the system can easily create a version 0 from scratch and write it to disk. Because the system does not sync allocation of data pages, it is also possible at this point that whlie writing the version 0 to disk to create it we may encounter an out of disk space error (caught in this routine as a StandardException from the create() call. We can't recovery from this without help from outside, so the caught exception is nested and a new exception thrown which the recovery system will output to the user asking them to check their disk for space/errors.

        Specified by:
        reCreatePageForRedoRecovery in class BaseContainer
        Throws:
        StandardException - Standard exception policy.
      • getPageForCompress

        protected BasePage getPageForCompress​(BaseContainerHandle handle,
                                              int flag,
                                              long pageno)
                                       throws StandardException
        Get candidate page to move a row for compressing the table.

        The caller is moving rows from the end of the table toward the beginning, with the goal of freeing up a block of empty pages at the end of the container which can be returned to the OS.

        On entry pageno will be latched by the caller. Only return pages with numbers below pageno. Attempting to return pageno will result in a latch/latch deadlock on the same thread.

        Specified by:
        getPageForCompress in class BaseContainer
        Throws:
        StandardException - Standard exception policy.
      • getLatchedPage

        protected BasePage getLatchedPage​(BaseContainerHandle handle,
                                          long pageNumber)
                                   throws StandardException
        Get a latched page. Incase of backup page Latch is necessary to prevent modification to the page when it is being written to the backup. Backup process relies on latches to get consistent snap shot of the page , user level table/page/row locks are NOT acquired by the online backup mechanism.
        Parameters:
        handle - the container handle used to latch the page
        pageNumber - the page number of the page to get
        Returns:
        the latched page
        Throws:
        StandardException - Standard Derby error policy
      • updateEstimatedRowCount

        protected void updateEstimatedRowCount​(int delta)
        Update estimated row count by page as it leaves the cache. The estimated row count is updated without logging!
      • readPage

        protected abstract void readPage​(long pageNumber,
                                         byte[] pageData)
                                  throws java.io.IOException,
                                         StandardException
        Read a page into the supplied array.
        MT - thread safe
        Throws:
        java.io.IOException - error reading page
        StandardException - standard Derby error message
      • writePage

        protected abstract void writePage​(long pageNumber,
                                          byte[] pageData,
                                          boolean syncPage)
                                   throws java.io.IOException,
                                          StandardException
        Write a page from the supplied array.
        MT - thread safe
        Throws:
        java.io.IOException - error writing page
        StandardException - Standard Derby error policy
      • decryptPage

        protected void decryptPage​(byte[] pageData,
                                   int pageSize)
                            throws StandardException
        Decrypts a page
        MT - MT safe.
        Throws:
        StandardException - Standard Derby error policy
      • encryptPage

        protected byte[] encryptPage​(byte[] pageData,
                                     int pageSize,
                                     byte[] encryptionBuffer,
                                     boolean newEngine)
                              throws StandardException
        Encrypts a page.
        MT - not safe, call within synchronized block and only use the returned byte array withing synchronized block.
        Throws:
        StandardException - Standard Derby error policy
      • getEncryptionBuffer

        protected byte[] getEncryptionBuffer()
        Get encryption buffer. MT - not safe, call within synchronized block and only use the returned byte array withing synchronized block.
        Returns:
        byte array to be used for encryping a page.
      • preAllocate

        protected abstract int preAllocate​(long lastPreallocPagenum,
                                           int preAllocSize)
        preAllocate writes out the preallocated pages to disk if necessary.
        Make sure the container is large enough and the pages are well formatted. The only reason to do this is to save some I/O during page initialization. Once the initPage log record is written, it is expected that the page really do exist and is well formed or recovery will fail. However, we can gain some performance by writing a bunch of pages at a time rather than one at a time.
        If it doesn't make sense for the the implementation to have pre-allocation, just return 0.
        If the container is not being logged, don't actually do anything, just return 0.
        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
      • doPreAllocatePages

        protected int doPreAllocatePages​(long lastPreallocPagenum,
                                         int preAllocSize)
        Preallocate the pages - actually doing it, called by subclass only
      • getPageSize

        protected int getPageSize()
      • getSpareSpace

        protected int getSpareSpace()
      • getMinimumRecordSize

        protected int getMinimumRecordSize()
      • getLastInsertedPage

        private long getLastInsertedPage()
      • getLastUnfilledPage

        private long getLastUnfilledPage()
      • initializeLastInsertedPage

        private void initializeLastInsertedPage​(int size)
      • setLastInsertedPage

        private void setLastInsertedPage​(long val)
      • setLastUnfilledPage

        private void setLastUnfilledPage​(long val)
      • getContextService

        static ContextService getContextService()
        Privileged lookup of the ContextService. Must be limited to package visibility so that user code can't call this entry point.
      • getServiceModule

        private static java.lang.Object getServiceModule​(java.lang.Object serviceModule,
                                                         java.lang.String factoryInterface)
        Privileged module lookup. Must be private so that user code can't call this entry point.