org.crosswire.jsword.book.sword.state
Class OpenFileStateManager

java.lang.Object
  extended by org.crosswire.jsword.book.sword.state.OpenFileStateManager

public final class OpenFileStateManager
extends Object

Manages the creation and re-distribution of open file states. This increases performance as more often than not, the same file state may be used. For example we may be carrying out a contains() operation followed by a read to disk for a particular key Each SwordBookMetaData has a corresponding a file state which is different to another. Furthermore, concurrent accesses cannot share this file state as the OpenFileState records where in the file it is, for reading several verses together for example. As a result, we want to key a lookup by SwordBookMetaData, which then gives us a pool of available file states... We create some more if none are available. In order to prevent memory leaks (OpenFileStates might be quite heavy as they do some internal caching of file data.. In order to avoid many file references piling up in memory, we implement a background cleaning thread which will clean up redundant keys every so often.

Author:
DM Smith, Chris Burrell
See Also:
The GNU Lesser General Public License for details.

Field Summary
private static org.slf4j.Logger LOGGER
           
private static OpenFileStateManager manager
           
private  Map<SwordBookMetaData,Queue<OpenFileState>> metaToStates
           
private  ScheduledFuture<?> monitoringThread
           
private  boolean shuttingDown
           
 
Constructor Summary
private OpenFileStateManager(int cleanupIntervalSeconds, int maxExpiry)
          prevent instantiation
 
Method Summary
private  void ensureNotShuttingDown()
           
 GenBookBackendState getGenBookBackendState(SwordBookMetaData metadata)
           
private
<T extends OpenFileState>
T
getInstance(SwordBookMetaData metadata)
           
private  Queue<OpenFileState> getQueueForMeta(SwordBookMetaData metadata)
           
 RawBackendState getRawBackendState(SwordBookMetaData metadata)
           
 RawFileBackendState getRawFileBackendState(SwordBookMetaData metadata)
           
 RawLDBackendState getRawLDBackendState(SwordBookMetaData metadata)
           
 ZLDBackendState getZLDBackendState(SwordBookMetaData metadata)
           
 ZVerseBackendState getZVerseBackendState(SwordBookMetaData metadata, BlockType blockType)
           
static void init(int cleanupIntervalSeconds, int maxExpiry)
          Allow the caller to initialize with their own settings.
static OpenFileStateManager instance()
          Singleton instance method to return the one and only Open File State Manager
 void release(OpenFileState fileState)
           
 void shutDown()
          Shuts down all open files
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

monitoringThread

private final ScheduledFuture<?> monitoringThread

metaToStates

private final Map<SwordBookMetaData,Queue<OpenFileState>> metaToStates

shuttingDown

private volatile boolean shuttingDown

manager

private static volatile OpenFileStateManager manager

LOGGER

private static final org.slf4j.Logger LOGGER
Constructor Detail

OpenFileStateManager

private OpenFileStateManager(int cleanupIntervalSeconds,
                             int maxExpiry)
prevent instantiation

Method Detail

init

public static void init(int cleanupIntervalSeconds,
                        int maxExpiry)
Allow the caller to initialize with their own settings. Should the OpenFileStateManager already be initialized a no-op will occur. No need for double-checked locking here

Parameters:
cleanupIntervalSeconds - seconds before cleanup
maxExpiry -

instance

public static OpenFileStateManager instance()
Singleton instance method to return the one and only Open File State Manager

Returns:
the singleton

getRawBackendState

public RawBackendState getRawBackendState(SwordBookMetaData metadata)
                                   throws BookException
Throws:
BookException

getRawFileBackendState

public RawFileBackendState getRawFileBackendState(SwordBookMetaData metadata)
                                           throws BookException
Throws:
BookException

getGenBookBackendState

public GenBookBackendState getGenBookBackendState(SwordBookMetaData metadata)
                                           throws BookException
Throws:
BookException

getRawLDBackendState

public RawLDBackendState getRawLDBackendState(SwordBookMetaData metadata)
                                       throws BookException
Throws:
BookException

getZLDBackendState

public ZLDBackendState getZLDBackendState(SwordBookMetaData metadata)
                                   throws BookException
Throws:
BookException

getZVerseBackendState

public ZVerseBackendState getZVerseBackendState(SwordBookMetaData metadata,
                                                BlockType blockType)
                                         throws BookException
Throws:
BookException

getInstance

private <T extends OpenFileState> T getInstance(SwordBookMetaData metadata)

getQueueForMeta

private Queue<OpenFileState> getQueueForMeta(SwordBookMetaData metadata)

release

public void release(OpenFileState fileState)

shutDown

public void shutDown()
Shuts down all open files


ensureNotShuttingDown

private void ensureNotShuttingDown()
                            throws BookException
Throws:
BookException

Copyright ยจ 2003-2015