Class PagedChannelRandomAccessSource

  • All Implemented Interfaces:
    IRandomAccessSource

    class PagedChannelRandomAccessSource
    extends GroupedRandomAccessSource
    implements IRandomAccessSource
    A RandomAccessSource that is based on an underlying FileChannel. The channel is mapped into memory using a paging scheme to allow for efficient reads of very large files. As an implementation detail, we use GroupedRandomAccessSource functionality, but override to make determination of the underlying mapped page more efficient - and to close each page as another is opened
    • Field Detail

      • DEFAULT_MAX_OPEN_BUFFERS

        public static final int DEFAULT_MAX_OPEN_BUFFERS
        See Also:
        Constant Field Values
      • bufferSize

        private final int bufferSize
        The size of each of the buffers to use when mapping files into memory. This must be greater than 0 and less than Integer.MAX_VALUE
      • channel

        private final java.nio.channels.FileChannel channel
        The channel this source is based on
    • Constructor Detail

      • PagedChannelRandomAccessSource

        public PagedChannelRandomAccessSource​(java.nio.channels.FileChannel channel)
                                       throws java.io.IOException
        Constructs a new PagedChannelRandomAccessSource based on the specified FileChannel, with a default buffer configuration. The default buffer configuration is currently 2^26 total paged bytes, spread across a maximum of 16 active buffers. This arrangement resulted in a 24% speed improvement over the single buffer case in parametric tests extracting text from a 2.3 GB file.
        Parameters:
        channel - the channel to use as the backing store
        Throws:
        java.io.IOException - if the channel cannot be opened or mapped
      • PagedChannelRandomAccessSource

        public PagedChannelRandomAccessSource​(java.nio.channels.FileChannel channel,
                                              int totalBufferSize,
                                              int maxOpenBuffers)
                                       throws java.io.IOException
        Constructs a new PagedChannelRandomAccessSource based on the specified FileChannel, with a specific buffer size
        Parameters:
        channel - the channel to use as the backing store
        totalBufferSize - total buffer size
        maxOpenBuffers - open buffers
        Throws:
        java.io.IOException - if the channel cannot be opened or mapped
    • Method Detail

      • buildSources

        private static IRandomAccessSource[] buildSources​(java.nio.channels.FileChannel channel,
                                                          int bufferSize)
                                                   throws java.io.IOException
        Constructs a set of MappedChannelRandomAccessSources for each page (of size bufferSize) of the underlying channel
        Parameters:
        channel - the underlying channel
        bufferSize - the size of each page (the last page may be shorter)
        Returns:
        a list of sources that represent the pages of the channel
        Throws:
        java.io.IOException - if IO fails for any reason
      • getStartingSourceIndex

        protected int getStartingSourceIndex​(long offset)
        For a given offset, return the index of the source that contains the specified offset. This is an optimization feature to help optimize the access of the correct source without having to iterate through every single source each time. It is safe to always return 0, in which case the full set of sources will be searched. Subclasses should override this method if they are able to compute the source index more efficiently (for example FileChannelRandomAccessSource takes advantage of fixed size page buffers to compute the index)
        Overrides:
        getStartingSourceIndex in class GroupedRandomAccessSource
        Parameters:
        offset - the offset
        Returns:
        the index of the input source that contains the specified offset, or 0 if unknown
      • sourceReleased

        protected void sourceReleased​(IRandomAccessSource source)
                               throws java.io.IOException
        Called when a given source is no longer the active source. This gives subclasses the abilty to release resources, if appropriate. For now, close the source that is no longer being used. In the future, we may implement an MRU that allows multiple pages to be opened at a time
        Overrides:
        sourceReleased in class GroupedRandomAccessSource
        Parameters:
        source - the source that is no longer the active source
        Throws:
        java.io.IOException - if there are any problems
      • sourceInUse

        protected void sourceInUse​(IRandomAccessSource source)
                            throws java.io.IOException
        Called when a given source is about to become the active source. This gives subclasses the abilty to retrieve resources, if appropriate. Ensure that the source is mapped. In the future, we may implement an MRU that allows multiple pages to be opened at a time
        Overrides:
        sourceInUse in class GroupedRandomAccessSource
        Parameters:
        source - the source that is about to become the active source
        Throws:
        java.io.IOException - if there are any problems
      • close

        public void close()
                   throws java.io.IOException
        Closes this source. The underlying data structure or source (if any) will also be closed
        Closes all of the underlying sources. Cleans the mapped bytebuffers and closes the channel
        Specified by:
        close in interface IRandomAccessSource
        Overrides:
        close in class GroupedRandomAccessSource
        Throws:
        java.io.IOException - in case of any reading error.