RandomAccess buffers each having its set of pages (implemented by
ScratchFileBuffer). A buffer is created calling createBuffer().
Pages can be stored in main memory or in a temporary file. A mixed mode is supported storing a certain amount of pages in memory and only the additional ones in temporary file (defined by maximum main memory to be used).
Pages can be marked as 'free' in order to re-use them. For in-memory pages this will release the used memory while for pages in temporary file this simply marks the area as free to re-use.
If a temporary file was created (done with the first page to be stored
in temporary file) it is deleted when ScratchFile#close() is called.
Using this class for RandomAccess buffers allows for a direct control
on the maximum memory usage and allows processing large files for which we
otherwise would get an OutOfMemoryError in case of using RandomAccessReadBuffer.
This base class for providing pages is thread safe (the buffer implementations are not).
| Modifier and Type | Field and Description |
|---|---|
| private final List | |
| private static final int | ENLARGE_PAGE_COUNT
number of pages by which we enlarge the scratch file (reduce I/O-operations) |
| private File | |
| private final BitSet | |
| private static final int | INIT_UNRESTRICTED_MAINMEM_PAGECOUNT
in case of unrestricted main memory usage this is the initial number of pages
|
| private final int | |
| private volatile byte[][] | inMemoryPages
holds pointers to in-memory page content; will be initialized once in case of restricted
main memory, otherwise it is enlarged as needed and first initialized to a size of
|
| private final Object | |
| private volatile boolean | |
| private static final Log | |
| private final boolean | |
| private final int | |
| private static final int | |
| private volatile int | |
| private RandomAccessFile | |
| private final File | |
| private final boolean |
| Access | Constructor and Description |
|---|---|
| public | ScratchFile(File
The directory in which to create the scratch file
or scratchFileDirectory)null to created it in the default temporary directory.Initializes page handler. |
| public | ScratchFile(MemoryUsageSetting
set how memory/temporary files are used for buffering streams etc. memUsageSetting)Initializes page handler. |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | |
| public void | |
| public RandomAccess | Returns: A new buffer.Implements org. Creates a new buffer using this page handler. |
| private void | enlarge()
This will provide new free pages by either enlarging the scratch file
by a number of pages defined by |
| public static ScratchFile | Returns: instance configured to only use main memory with no size restrictionGetter for an instance using only unrestricted main memory for buffering
(same as |
| public static ScratchFile | Returns: instance configured to only use main memory with no size restriction or null if cannot create scratch filemaximum number of main-memory to be used; maxMainMemoryBytes)-1 for no restriction;
0 will also be interpreted here as no restrictionGetter for an instance to only use main-memory with the defined maximum. |
| pack-priv int | Returns: index of new pageReturns a new free page, either from free page pool or by enlarging scratch file (may be created). |
| pack-priv int | |
| private void | |
| pack-priv void | markPagesAsFree(int[]
pages indexes of pages to release pageIndexes, int off, int number of page indexes contained in provided array count)Allows a buffer which is cleared/closed to release its pages to be re-used. |
| pack-priv byte[] | |
| pack-priv void | |
| pack-priv void | writePage(int
index of page to write pageIdx, byte[] page to write (length has to be 4096) page)Writes updated page. |
| buffers | back to summary |
|---|---|
| private final List<ScratchFileBuffer> buffers | |
| ENLARGE_PAGE_COUNT | back to summary |
|---|---|
| private static final int ENLARGE_PAGE_COUNT number of pages by which we enlarge the scratch file (reduce I/O-operations) | |
| file | back to summary |
|---|---|
| private File file scratch file; only to be accessed under synchronization of | |
| freePages | back to summary |
|---|---|
| private final BitSet freePages | |
| INIT_UNRESTRICTED_MAINMEM_PAGECOUNT | back to summary |
|---|---|
| private static final int INIT_UNRESTRICTED_MAINMEM_PAGECOUNT in case of unrestricted main memory usage this is the initial number of pages
| |
| inMemoryMaxPageCount | back to summary |
|---|---|
| private final int inMemoryMaxPageCount | |
| inMemoryPages | back to summary |
|---|---|
| private volatile byte[][] inMemoryPages holds pointers to in-memory page content; will be initialized once in case of restricted
main memory, otherwise it is enlarged as needed and first initialized to a size of
| |
| ioLock | back to summary |
|---|---|
| private final Object ioLock | |
| isClosed | back to summary |
|---|---|
| private volatile boolean isClosed | |
| LOG | back to summary |
|---|---|
| private static final Log LOG | |
| maxMainMemoryIsRestricted | back to summary |
|---|---|
| private final boolean maxMainMemoryIsRestricted | |
| maxPageCount | back to summary |
|---|---|
| private final int maxPageCount | |
| PAGE_SIZE | back to summary |
|---|---|
| private static final int PAGE_SIZE | |
| pageCount | back to summary |
|---|---|
| private volatile int pageCount | |
| raf | back to summary |
|---|---|
| private RandomAccessFile raf random access to scratch file; only to be accessed under synchronization of | |
| scratchFileDirectory | back to summary |
|---|---|
| private final File scratchFileDirectory | |
| useScratchFile | back to summary |
|---|---|
| private final boolean useScratchFile | |
| ScratchFile | back to summary |
|---|---|
| public ScratchFile(File scratchFileDirectory) throws IOException Initializes page handler. If a All pages will be stored in the scratch file.
| |
| ScratchFile | back to summary |
|---|---|
| public ScratchFile(MemoryUsageSetting memUsageSetting) throws IOException Initializes page handler. If a Depending on the size of allowed memory usage a number of pages (memorySize/
| |
| checkClosed | back to summary |
|---|---|
| pack-priv void checkClosed() throws IOException Checks if this page handler has already been closed. If so,
an
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Implements java. Closes and deletes the temporary file. No further interaction with the scratch file or associated buffers can happen after this method is called. It also releases in-memory pages.
| |
| createBuffer | back to summary |
|---|---|
| public RandomAccess createBuffer() throws IOException Implements org. Creates a new buffer using this page handler.
| |
| enlarge | back to summary |
|---|---|
| private void enlarge() throws IOException This will provide new free pages by either enlarging the scratch file
by a number of pages defined by If scratch file usage is allowed and scratch file does not exist already it will be created. Only to be called under synchronization on | |
| getMainMemoryOnlyInstance | back to summary |
|---|---|
| public static ScratchFile getMainMemoryOnlyInstance() Getter for an instance using only unrestricted main memory for buffering
(same as
| |
| getMainMemoryOnlyInstance | back to summary |
|---|---|
| public static ScratchFile getMainMemoryOnlyInstance(long maxMainMemoryBytes) Getter for an instance to only use main-memory with the defined maximum.
| |
| getNewPage | back to summary |
|---|---|
| pack-priv int getNewPage() throws IOException Returns a new free page, either from free page pool or by enlarging scratch file (may be created).
| |
| getPageSize | back to summary |
|---|---|
| pack-priv int getPageSize() Returns byte size of a page.
| |
| initPages | back to summary |
|---|---|
| private void initPages() | |
| markPagesAsFree | back to summary |
|---|---|
| pack-priv void markPagesAsFree(int[] pageIndexes, int off, int count) Allows a buffer which is cleared/closed to release its pages to be re-used.
| |
| readPage | back to summary |
|---|---|
| pack-priv byte[] readPage(int pageIdx) throws IOException Reads the page with specified index.
| |
| removeBuffer | back to summary |
|---|---|
| pack-priv void removeBuffer(ScratchFileBuffer buffer) | |
| writePage | back to summary |
|---|---|
| pack-priv void writePage(int pageIdx, byte[] page) throws IOException Writes updated page. Page is either kept in-memory if pageIdx < Provided page byte array must not be re-used for other pages since we store it as is in case of in-memory handling.
| |