Class AbstractFileWordList.Cache

java.lang.Object
org.passay.dictionary.AbstractFileWordList.Cache
Enclosing class:
AbstractFileWordList

private static class AbstractFileWordList.Cache extends Object
Cache of word indices to byte offsets where word starts in backing file.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    (package private) static class 
    Cache entry that indicates cached word index and byte offset of start of word in backing file.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final boolean
    Whether to allocate a direct buffer.
    private boolean
    Whether this cache is ready for use.
    private LongBuffer
    Map of word indices to the byte offset in the file where the word starts.
    private int
    Modulus of indices to cache.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Cache(long fileSize, int cachePercent, boolean direct)
    Creates a new cache instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    get(int index)
    Gets the byte offset into the backing file for the word at the index that is less than or equal to the supplied index.
    (package private) void
    put(int index, long position)
    Puts an entry that maps the word at given index to the byte offset in into the backing file.
    private void
    resize(long size)
    Creates a new byte buffer of the supplied size for use as the cache.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • allocateDirect

      private final boolean allocateDirect
      Whether to allocate a direct buffer.
    • map

      private LongBuffer map
      Map of word indices to the byte offset in the file where the word starts.
    • modulus

      private int modulus
      Modulus of indices to cache.
    • initialized

      private boolean initialized
      Whether this cache is ready for use.
  • Constructor Details

    • Cache

      Cache(long fileSize, int cachePercent, boolean direct)
      Creates a new cache instance.
      Parameters:
      fileSize - Size of file in bytes.
      cachePercent - Percent of words to cache.
      direct - Whether to allocate a direct byte buffer.
      Throws:
      IllegalArgumentException - if cachePercent is not between 0-100 or the computed cache size exceeds Integer.MAX_VALUE
  • Method Details

    • put

      void put(int index, long position)
      Puts an entry that maps the word at given index to the byte offset in into the backing file. The operation only succeeds if it is determined that the supplied index should be stored based on the cachePercent, otherwise this is a no-op.
      Parameters:
      index - Word at index.
      position - Byte offset into backing for file where word starts.
      Throws:
      IllegalStateException - if the cache has already been initialized
    • get

      Gets the byte offset into the backing file for the word at the index that is less than or equal to the supplied index.
      Parameters:
      index - Word at index.
      Returns:
      Nearest cache entry for given index.
    • resize

      private void resize(long size)
      Creates a new byte buffer of the supplied size for use as the cache. If the cache already exists, it's contents are copied into the new buffer.
      Parameters:
      size - Of byte buffer to create
      Throws:
      IllegalArgumentException - if size exceeds Integer.MAX_VALUE
    • toString

      public String toString()
      Overrides:
      toString in class Object