Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.javathis.utilities.pagedArray
Class JTPagedArray  view JTPagedArray download JTPagedArray.java

java.lang.Object
  extended bycom.javathis.utilities.pagedArray.JTPagedArray

public final class JTPagedArray
extends java.lang.Object

An array-like implementation allowing you to limit the amount of memory required for large collections of data by moving parts of it automatically in and out of memory.

A JTPagedArray contains a cache for holding pages. Each page holds elements of the data set. An element is a structured list of at least one field. Each field holds a numberic value. Each field is expressed by a FieldDetails object. An ElementDetails object holds the FieldDetails objects.

Collections of neighboring elements are handled in pages. These pages are read in and out as units. Pages that are currently in memory are stored in a cache. The size of this cache can be handled automaticly, or by the user of the JTPagedArray (CacheDetails).

The user of the JTPagedArray can preform prefetchs. A loadPage tells the JTPagedArray to read in collections of elements prior to their being needed (loadPage(long) 55 ).


Nested Class Summary
private  class JTPagedArray.CachePage
          A simplified version of a linked list of other CachePage objects, and Pageobjects.
private  class JTPagedArray.Page
          A Page contains the state and location details on a page of bytes.
private  class JTPagedArray.PageLoader
          A PageLoader handles loading pages from the swap file into memory.
 
Field Summary
private  int bits2ShiftPageElementIndex
           
private  CacheDetails cacheDetails
           
private  int currentCachePages
           
private  long currentFileSize
           
private  long dataSetElementCount
           
private  int dataSetPageCount
           
private  JTPagedArray.Page[] dataSetPages
           
private  long dataSetSize
           
private  ElementDetails elementDetails
           
private  int elementSize
           
private  JTPagedArray.CachePage firstCachePage
           
private  JTPagedArray.CachePage firstCachePageNotInCache
           
private  byte[] fullDataSet
           
private static byte IS_DIRTY
           
private static byte IS_FALSE
           
private static byte IS_IN_USE
           
private static byte IS_LOCKED
           
private static byte IS_STORED
           
private  boolean isAllPagesInCacheLocked
           
private  boolean isFullDataSetDirty
           
private  boolean isPageElementCountPOW2
           
private  JTPagedArray.CachePage lastCachePage
           
private static java.util.ResourceBundle MAIN_RESOURCE_BUNDLE
           
private  int maxCachePages
           
private  int pageElementCount
           
private  int pagesCachedCount
           
private  int pageSize
           
private  int pagesNotCachedCount
           
private  java.io.RandomAccessFile raSwapFile
           
private static java.lang.String SWAP_FILE_PREFIX
           
private static java.lang.String SWAP_FILE_SUFFIX
           
private  java.lang.String swapDirectory
           
private  java.io.File swapFile
           
 
Constructor Summary
JTPagedArray(long dataSetElementCount, ElementDetails elementDetails)
          Creates a new JTPagedArray object with the provided number of elements using dataSetElementCount, using the provided ElementDetails, and will create a new CacheDetails with default settings.
JTPagedArray(long dataSetElementCount, ElementDetails elementDetails, CacheDetails cacheDetails)
          Creates a new JTPagedArray object with the provided number of elements using dataSetElementCount, using the provided ElementDetails, and with the provided CacheDetails.
JTPagedArray(long dataSetElementCount, int elementFieldDetailsCount)
          Creates a new JTPagedArray object with the provided number of elements using dataSetElementCount, the provided number of fields for each element using elementFieldDetailsCount of type byte, and will create a new CacheDetails with default settings.
JTPagedArray(long dataSetElementCount, int elementFieldDetailsCount, CacheDetails cacheDetails)
          Creates a new JTPagedArray object with the provided number of elements using dataSetElementCount, the provided number of fields for each element using elementFieldDetailsCount of type byte, and with the provided CacheDetails.
 
Method Summary
 void cacheLock(boolean isLocked)
          Passing a value of true will lock the entire cache.
private  void createSwapFile()
           
 void destroy()
           
private  byte[] ensureCache(int numberOfPages)
          Ensures the cache has space for the provided number of pages, and returns the byte array of the last page to be paged out, returns null if there was already enough room for the requested number of pages.
protected  void finalize()
          Called on an object by the Virtual Machine at most once, at some point after the Object is determined unreachable but before it is destroyed.
 void flushCache()
          Flushes all cache pages from memory.
private  void flushPage(JTPagedArray.Page page)
          Copies the page to the swap file.
 void getBytes(long elementIndex, byte[] bytes)
          Fills the provided byte array with the bytes in the element at the provided index.
 void getBytes(long elementIndex, int numberOfElements, byte[] bytes)
          Fills the provided byte array with the bytes starting in the element at the provided index and continueing in sequence for the provided number of elements.
 CacheDetails getCacheDetails()
          Returns a reference to the JTPagedArray's CacheDetails.
protected  int getCurrentCachePageCount()
           
protected  long getCurrentCacheSize()
           
protected  int getCurrentPageElementCount()
           
protected  int getCurrentPageInCount()
          Returns the total number of page ins that have happened from the time the JTPagedArray wsa created.
protected  int getCurrentPageOutCount()
          Returns the total number of page outs that have happened from the time the JTPagedArray wsa created.
protected  int[] getCurrentPagesInCache()
          Returns an array of all the indexs for the pages that are in cache.
protected  int getCurrentPageSize()
           
protected  int getDataSetPageCount()
           
 long getDataSetSize()
          Returns the total number of bytes used by the JTPagedArray's data set.
 long getElementCount()
          Returns the total number of elements in the JTPagedArray's data set.
 ElementDetails getElementDetails()
          Returns a reference to the JTPagedArray's ElementDetails.
 void getPagedElementAt(long elementIndex, PagedElement element)
          Fills the provided PagedElement with the values of the element at the provided index.
protected  java.lang.String getSwapDirectory()
           
protected  java.lang.String getSwapFilename()
          Returns the name of the Swap file.
 boolean isCacheLocked()
          Returns true if the entire cache is locked.
protected  boolean isPageDirty(int pageIndex)
          Checks if an element in the page at the provided index has been changed, and not written to disk.
protected  boolean isPageInCache(int pageIndex)
          Checks if the page at the provided index is in cache.
protected  boolean isPageLocked(int pageIndex)
          Checks if the page at the provided index is locked in cache.
 boolean isPageLocked(long elementIndex)
          Returns true if the page holding the element at the provided index is locked.
private  void jtInit()
           
 void loadPage(long elementIndex)
          Starts a request to load a page from the swwap file containing the element at the provided index.
 void loadPage(long startingElementIndex, long numberOfElements)
          Starts a request to load all pages from the swwap file containing the element starting at the provided index and continuing for the provided number of elements.
private  void pageIn(int pageIndex)
          Page into the cache the page represented by the provided index.
 void pageLock(long elementIndex, boolean isLocked)
          Passing a value of true will lock the page containing the element at the provided index in the cache.
 void pageLock(long startingElementIndex, long numberOfElements, boolean isLocked)
          Passing a value of true will lock all pages containing the elements starting at the provided index in the cache continuing in sequence for the number of elements provided..
private  byte[] pageOut(JTPagedArray.CachePage cacheEntry)
          Page out the cache the page represented by the provided index.
 void setBytes(long elementIndex, byte[] bytes)
          Fills in the element at the provided index with the bytes in the provided byte array.
 void setBytes(long elementIndex, int numberOfElements, byte[] bytes)
          Fills in the elements starting at the provided index and continueing in sequence for the provided number of elements with the bytes in the provided byte array.
protected  void setMaxCacheSize(long maxBytes)
          Changes the maximum size of the cache (in bytes) to the provided long.
 void setPagedElementAt(long elementIndex, PagedElement element)
          Fills the element at the provided index with the values of the provided PagedElement.
 void setSwapDirectory(java.lang.String swapDirectory)
          Changes the Swap Directory used by the swap file to the location provided.
 void writeCache()
          Writes any pages to the swap file that have changed and not written yet.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAIN_RESOURCE_BUNDLE

private static final java.util.ResourceBundle MAIN_RESOURCE_BUNDLE

IS_FALSE

private static final byte IS_FALSE
See Also:
Constant Field Values

IS_IN_USE

private static final byte IS_IN_USE
See Also:
Constant Field Values

IS_LOCKED

private static final byte IS_LOCKED
See Also:
Constant Field Values

IS_STORED

private static final byte IS_STORED
See Also:
Constant Field Values

IS_DIRTY

private static final byte IS_DIRTY
See Also:
Constant Field Values

SWAP_FILE_PREFIX

private static final java.lang.String SWAP_FILE_PREFIX

SWAP_FILE_SUFFIX

private static final java.lang.String SWAP_FILE_SUFFIX

pageElementCount

private int pageElementCount

bits2ShiftPageElementIndex

private int bits2ShiftPageElementIndex

pageSize

private int pageSize

elementSize

private int elementSize

dataSetPageCount

private int dataSetPageCount

dataSetSize

private long dataSetSize

dataSetElementCount

private long dataSetElementCount

isPageElementCountPOW2

private boolean isPageElementCountPOW2

cacheDetails

private CacheDetails cacheDetails

elementDetails

private ElementDetails elementDetails

fullDataSet

private volatile byte[] fullDataSet

isFullDataSetDirty

private volatile boolean isFullDataSetDirty

pagesCachedCount

private volatile int pagesCachedCount

pagesNotCachedCount

private volatile int pagesNotCachedCount

maxCachePages

private volatile int maxCachePages

currentCachePages

private volatile int currentCachePages

currentFileSize

private volatile long currentFileSize

isAllPagesInCacheLocked

private volatile boolean isAllPagesInCacheLocked

swapDirectory

private volatile java.lang.String swapDirectory

swapFile

private volatile java.io.File swapFile

dataSetPages

private volatile JTPagedArray.Page[] dataSetPages

firstCachePage

private volatile JTPagedArray.CachePage firstCachePage

lastCachePage

private volatile JTPagedArray.CachePage lastCachePage

firstCachePageNotInCache

private volatile JTPagedArray.CachePage firstCachePageNotInCache

raSwapFile

private volatile java.io.RandomAccessFile raSwapFile
Constructor Detail

JTPagedArray

public JTPagedArray(long dataSetElementCount,
                    int elementFieldDetailsCount)
Creates a new JTPagedArray object with the provided number of elements using dataSetElementCount, the provided number of fields for each element using elementFieldDetailsCount of type byte, and will create a new CacheDetails with default settings.

Note: Delegates to the following constructor:

JTPagedArray(long dataSetElementCount, int elementFieldDetailsCount, CacheDetails cacheDetails)


JTPagedArray

public JTPagedArray(long dataSetElementCount,
                    int elementFieldDetailsCount,
                    CacheDetails cacheDetails)
Creates a new JTPagedArray object with the provided number of elements using dataSetElementCount, the provided number of fields for each element using elementFieldDetailsCount of type byte, and with the provided CacheDetails.


JTPagedArray

public JTPagedArray(long dataSetElementCount,
                    ElementDetails elementDetails)
Creates a new JTPagedArray object with the provided number of elements using dataSetElementCount, using the provided ElementDetails, and will create a new CacheDetails with default settings.

Note: Delegates to the following constructor:

JTPagedArray(long dataSetElementCount, ElementDetails elementDetails, CacheDetails cacheDetails)


JTPagedArray

public JTPagedArray(long dataSetElementCount,
                    ElementDetails elementDetails,
                    CacheDetails cacheDetails)
Creates a new JTPagedArray object with the provided number of elements using dataSetElementCount, using the provided ElementDetails, and with the provided CacheDetails.

Method Detail

destroy

public void destroy()

finalize

protected void finalize()
Description copied from class: java.lang.Object
Called on an object by the Virtual Machine at most once, at some point after the Object is determined unreachable but before it is destroyed. You would think that this means it eventually is called on every Object, but this is not necessarily the case. If execution terminates abnormally, garbage collection does not always happen. Thus you cannot rely on this method to always work. For finer control over garbage collection, use references from the java.lang.ref package.

Virtual Machines are free to not call this method if they can determine that it does nothing important; for example, if your class extends Object and overrides finalize to do simply super.finalize().

finalize() will be called by a java.lang.Thread that has no locks on any Objects, and may be called concurrently. There are no guarantees on the order in which multiple objects are finalized. This means that finalize() is usually unsuited for performing actions that must be thread-safe, and that your implementation must be use defensive programming if it is to always work.

If an Exception is thrown from finalize() during garbage collection, it will be patently ignored and the Object will still be destroyed.

It is allowed, although not typical, for user code to call finalize() directly. User invocation does not affect whether automatic invocation will occur. It is also permitted, although not recommended, for a finalize() method to "revive" an object by making it reachable from normal code again.

Unlike constructors, finalize() does not get called for an object's superclass unless the implementation specifically calls super.finalize().

The default implementation does nothing.


jtInit

private void jtInit()

ensureCache

private byte[] ensureCache(int numberOfPages)
                    throws com.javathis.utilities.exceptions.InsufficientSpaceException
Ensures the cache has space for the provided number of pages, and returns the byte array of the last page to be paged out, returns null if there was already enough room for the requested number of pages.


pageIn

private void pageIn(int pageIndex)
             throws com.javathis.utilities.exceptions.InsufficientSpaceException
Page into the cache the page represented by the provided index. If the page is already in the cache no action is taken.


pageOut

private byte[] pageOut(JTPagedArray.CachePage cacheEntry)
                throws com.javathis.utilities.exceptions.InsufficientSpaceException
Page out the cache the page represented by the provided index. If the page is locked in the cache no action is taken. Returns the byte array of the last page to be paged out, returns null if the page was locked.


flushPage

private void flushPage(JTPagedArray.Page page)
                throws com.javathis.utilities.exceptions.InsufficientSpaceException
Copies the page to the swap file.


writeCache

public void writeCache()
Writes any pages to the swap file that have changed and not written yet.

Note: Does not empty the cache. To do this call flushCache() 55 .


flushCache

public void flushCache()
Flushes all cache pages from memory. Any pages not written to the swap file will be prior to being flushed.

Note: Cannot flush pages that are locked in the cache. Unlock all pages before calling this method.


createSwapFile

private void createSwapFile()
                     throws com.javathis.utilities.exceptions.InsufficientSpaceException

getSwapFilename

protected java.lang.String getSwapFilename()
Returns the name of the Swap file. Will return null if it has not been created, or the directory or file name is invalid.


getSwapDirectory

protected java.lang.String getSwapDirectory()

setSwapDirectory

public void setSwapDirectory(java.lang.String swapDirectory)
Changes the Swap Directory used by the swap file to the location provided.

Note: This can only be set prior to writting of te first page to the swap file. In this case an IllegalArgumentException will be thrown. Pass a null value or empty string to reset back to the defaul location.


isCacheLocked

public boolean isCacheLocked()
Returns true if the entire cache is locked.


isPageLocked

public boolean isPageLocked(long elementIndex)
Returns true if the page holding the element at the provided index is locked.


cacheLock

public void cacheLock(boolean isLocked)
Passing a value of true will lock the entire cache.

WARNING: When the entire cache is locked no pages can be written to disk. Therefore increasing the memory used by the cache with each new page read. This could cause the JVM to run out of memory.

Note: Once unlocked an attempt to reduce the amount of pages down to the set limit will be done. This has the potential for a lot disk IO, and this method will block until it finishes.


pageLock

public void pageLock(long elementIndex,
                     boolean isLocked)
Passing a value of true will lock the page containing the element at the provided index in the cache.

WARNING: When a page is locked in the cache it cannot be written to disk. Therefore allowing the potential of increasing the memory used by the cache with each new page read. This could cause the JVM to run out of memory.

Note: Once a page is unlocked an attempt to reduce the amount of pages down to the set limit will be done. This has the potential for a lot disk IO, and this method will block until it finishes.


pageLock

public void pageLock(long startingElementIndex,
                     long numberOfElements,
                     boolean isLocked)
Passing a value of true will lock all pages containing the elements starting at the provided index in the cache continuing in sequence for the number of elements provided..

WARNING: When a page is locked in the cache it cannot be written to disk. Therefore allowing the potential of increasing the memory used by the cache with each new page read. This could cause the JVM to run out of memory.

Note: Once a page is unlocked an attempt to reduce the amount of pages down to the set limit will be done. This has the potential for a lot disk IO, and this method will block until it finishes.


loadPage

public void loadPage(long elementIndex)
Starts a request to load a page from the swwap file containing the element at the provided index. This method is non-blocking, and therefore returns immediately.


loadPage

public void loadPage(long startingElementIndex,
                     long numberOfElements)
Starts a request to load all pages from the swwap file containing the element starting at the provided index and continuing for the provided number of elements. This method is non-blocking, and therefore returns immediately.


getDataSetSize

public long getDataSetSize()
Returns the total number of bytes used by the JTPagedArray's data set.


getElementCount

public long getElementCount()
Returns the total number of elements in the JTPagedArray's data set.


getElementDetails

public ElementDetails getElementDetails()
Returns a reference to the JTPagedArray's ElementDetails.


getCacheDetails

public CacheDetails getCacheDetails()
Returns a reference to the JTPagedArray's CacheDetails.


setMaxCacheSize

protected void setMaxCacheSize(long maxBytes)
                        throws com.javathis.utilities.exceptions.InsufficientSpaceException
Changes the maximum size of the cache (in bytes) to the provided long.

Note: Maximum size will be rounded up to a multiple of the page size. A size less than the current size will cause data to be paged out. If the size is zero or less it will be ignored. Method will block untill all work is completed.


getDataSetPageCount

protected int getDataSetPageCount()

getCurrentPageSize

protected int getCurrentPageSize()

getCurrentPageElementCount

protected int getCurrentPageElementCount()

getCurrentCachePageCount

protected int getCurrentCachePageCount()

getCurrentCacheSize

protected long getCurrentCacheSize()

getCurrentPageInCount

protected int getCurrentPageInCount()
Returns the total number of page ins that have happened from the time the JTPagedArray wsa created.


getCurrentPageOutCount

protected int getCurrentPageOutCount()
Returns the total number of page outs that have happened from the time the JTPagedArray wsa created.


isPageLocked

protected boolean isPageLocked(int pageIndex)
Checks if the page at the provided index is locked in cache.


isPageDirty

protected boolean isPageDirty(int pageIndex)
Checks if an element in the page at the provided index has been changed, and not written to disk. This will be false once a page has been flushed.


isPageInCache

protected boolean isPageInCache(int pageIndex)
Checks if the page at the provided index is in cache.


getCurrentPagesInCache

protected int[] getCurrentPagesInCache()
Returns an array of all the indexs for the pages that are in cache.

Note: This array is sorted from the most currently used page to the least. Will return null if there is no cache being used.


getBytes

public void getBytes(long elementIndex,
                     byte[] bytes)
Fills the provided byte array with the bytes in the element at the provided index.

WARNING: Element indices are not checked for reasons of performance. Therefore an ArrayIndexOutOfBoundsException could be thrown. The byte array cannot be null or a NullPointerException will be thrown. Also if the byte array is too small a ArrayIndexOutOfBoundsException will be thrown.

Note: If the page containing the element is not in the cache it will have to be loaded prior to use. This can potientially cause other pages to be flushed out of the cache. This method blocks untill everything is completed.


getBytes

public void getBytes(long elementIndex,
                     int numberOfElements,
                     byte[] bytes)
Fills the provided byte array with the bytes starting in the element at the provided index and continueing in sequence for the provided number of elements.

WARNING: Element indices are not checked for reasons of performance. Therefore an ArrayIndexOutOfBoundsException could be thrown. The byte array cannot be null or a NullPointerException will be thrown. Also if the byte array is too small a ArrayIndexOutOfBoundsException will be thrown.

Note: If the page containing the element is not in the cache it will have to be loaded prior to use. This can potientially cause other pages to be flushed out of the cache. This method blocks untill everything is completed.


setBytes

public void setBytes(long elementIndex,
                     byte[] bytes)
Fills in the element at the provided index with the bytes in the provided byte array.

WARNING: Element indices are not checked for reasons of performance. Therefore an ArrayIndexOutOfBoundsException could be thrown. The byte array cannot be null or a NullPointerException will be thrown. Also if the byte array is too small a ArrayIndexOutOfBoundsException will be thrown.

Note: If the page containing the element is not in the cache it will have to be loaded prior to use. This can potientially cause other pages to be flushed out of the cache. This method blocks untill everything is completed.


setBytes

public void setBytes(long elementIndex,
                     int numberOfElements,
                     byte[] bytes)
Fills in the elements starting at the provided index and continueing in sequence for the provided number of elements with the bytes in the provided byte array.

WARNING: Element indices are not checked for reasons of performance. Therefore an ArrayIndexOutOfBoundsException could be thrown. The byte array cannot be null or a NullPointerException will be thrown. Also if the byte array is too small a ArrayIndexOutOfBoundsException will be thrown.

Note: If the page containing the element is not in the cache it will have to be loaded prior to use. This can potientially cause other pages to be flushed out of the cache. This method blocks untill everything is completed.


getPagedElementAt

public void getPagedElementAt(long elementIndex,
                              PagedElement element)
Fills the provided PagedElement with the values of the element at the provided index. This is in essense a clone of the internal element.

WARNING: Element indices are not checked for reasons of performance. Therefore an ArrayIndexOutOfBoundsException could be thrown. The PagedElement cannot be null or a NullPointerException will be thrown.


setPagedElementAt

public void setPagedElementAt(long elementIndex,
                              PagedElement element)
Fills the element at the provided index with the values of the provided PagedElement. This is in essense a clone of the provided element.

WARNING: Element indices are not checked for reasons of performance. Therefore an ArrayIndexOutOfBoundsException could be thrown. The PagedElement cannot be null or a NullPointerException will be thrown.