org.crosswire.jsword.book.sword.state

## Class OpenFileStateManager

- java.lang.Object
- - 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 `BookMetaData` 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 `BookMetaData`, 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

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `private static org.slf4j.Logger` | `LOGGER`
    `private static OpenFileStateManager` | `manager`
    `private Map<BookMetaData,Queue<OpenFileState>>` | `metaToStates`
    `private ScheduledFuture<?>` | `monitoringThread`
    `private boolean` | `shuttingDown`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `private` | `OpenFileStateManager(int cleanupIntervalSeconds, int maxExpiry)` prevent instantiation

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `private void` | `ensureNotShuttingDown()`
    `GenBookBackendState` | `getGenBookBackendState(BookMetaData metadata)`
    `private <T extends OpenFileState> T` | `getInstance(BookMetaData metadata)`
    `private Queue<OpenFileState>` | `getQueueForMeta(BookMetaData metadata)`
    `RawBackendState` | `getRawBackendState(BookMetaData metadata)`
    `RawFileBackendState` | `getRawFileBackendState(BookMetaData metadata)`
    `RawLDBackendState` | `getRawLDBackendState(BookMetaData metadata)`
    `ZLDBackendState` | `getZLDBackendState(BookMetaData metadata)`
    `ZVerseBackendState` | `getZVerseBackendState(BookMetaData 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<BookMetaData,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(BookMetaData metadata)
                                             throws BookException

      Throws:
        `BookException`

    - #### getRawFileBackendState

          public RawFileBackendState getRawFileBackendState(BookMetaData metadata)
                                                     throws BookException

      Throws:
        `BookException`

    - #### getGenBookBackendState

          public GenBookBackendState getGenBookBackendState(BookMetaData metadata)
                                                     throws BookException

      Throws:
        `BookException`

    - #### getRawLDBackendState

          public RawLDBackendState getRawLDBackendState(BookMetaData metadata)
                                                 throws BookException

      Throws:
        `BookException`

    - #### getZLDBackendState

          public ZLDBackendState getZLDBackendState(BookMetaData metadata)
                                             throws BookException

      Throws:
        `BookException`

    - #### getZVerseBackendState

          public ZVerseBackendState getZVerseBackendState(BookMetaData metadata,
                                                 BlockType blockType)
                                                   throws BookException

      Throws:
        `BookException`

    - #### getInstance

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

    - #### getQueueForMeta

          private Queue<OpenFileState> getQueueForMeta(BookMetaData metadata)

    - #### release

          public void release(OpenFileState fileState)

    - #### shutDown

          public void shutDown()

      Shuts down all open files

    - #### ensureNotShuttingDown

          private void ensureNotShuttingDown()
                                      throws BookException

      Throws:
        `BookException`