Package org.passay.dictionary
Class AbstractFileWordList.Cache
java.lang.Object
org.passay.dictionary.AbstractFileWordList.Cache
- Enclosing class:
- AbstractFileWordList
Cache of word indices to byte offsets where word starts in backing file.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classCache entry that indicates cached word index and byte offset of start of word in backing file. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final booleanWhether to allocate a direct buffer.private booleanWhether this cache is ready for use.private LongBufferMap of word indices to the byte offset in the file where the word starts.private intModulus of indices to cache. -
Constructor Summary
ConstructorsConstructorDescriptionCache(long fileSize, int cachePercent, boolean direct) Creates a new cache instance. -
Method Summary
Modifier and TypeMethodDescription(package private) AbstractFileWordList.Cache.Entryget(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) voidput(int index, long position) Puts an entry that maps the word at given index to the byte offset in into the backing file.private voidresize(long size) Creates a new byte buffer of the supplied size for use as the cache.toString()
-
Field Details
-
allocateDirect
private final boolean allocateDirectWhether to allocate a direct buffer. -
map
Map of word indices to the byte offset in the file where the word starts. -
modulus
private int modulusModulus of indices to cache. -
initialized
private boolean initializedWhether 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 exceedsInteger.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 exceedsInteger.MAX_VALUE
-
toString
-