Package loci.formats
Class Memoizer
java.lang.Object
loci.formats.ReaderWrapper
loci.formats.Memoizer
- All Implemented Interfaces:
Closeable,AutoCloseable,ICompressedTileReader,IFormatHandler,IFormatReader,IMetadataConfigurable,IPyramidHandler
ReaderWrapper implementation which caches the state of the
delegate (including and other ReaderWrapper instances)
after setId(String) has been called.
Initializing a Bio-Formats reader can consume substantial time and memory.
Most of the initialization time is spent in the setId(String) call.
Various factors can impact the performance of this step including the file
size, the amount of metadata in the image and also the file format itself.
With the Memoizer reader wrapper, if the time required to call the
setId(String) method is larger than minimumElapsed, the
initialized reader including all reader wrappers will be cached in a memo
file via saveMemo().
Any subsequent call to setId(String) with a reader decorated by
the Memoizer on the same input file will load the reader from the memo file
using loadMemo() instead of performing a full reader
initialization.
In essence, the speed-up gained from memoization will happen only after the
first initialization of the reader for a particular file.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic classprivate static classHelper implementation that can be used to implementMemoizer.Deserclasses for libraries working solely with byte arrays. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longDefault value forminimumElapsedif none is provided in the constructor.private final FileDirectory where all memo files should be created.private booleanIftrue, then all memo files will be created in the same directory as the original file.private booleanWhether theReaderWrapper.readerinstance currently active was loaded from the memo file duringsetId(String).private static final org.slf4j.LoggerDefaultLoggerfor the memoizer classprivate Fileprivate final longMinimum number of milliseconds which must elapse during the call tosetId(java.lang.String)before a memo file will be created.private loci.common.Locationprivate MetadataStoreMetadataStorecreated internally.private booleanWhether theReaderWrapper.readerinstance was saved to a memo file onsetId(String).protected Memoizer.Deserprivate OMEXMLServiceprivate booleanprivate booleanprivate Fileprivate MetadataStoreMetadataStoreset by the caller.static final IntegerDefault file version.private booleanBoolean specifying whether to invalidate the memo file based upon mismatched major/minor version numbers.Fields inherited from class loci.formats.ReaderWrapper
readerFields inherited from interface loci.formats.IFormatReader
CAN_GROUP, CANNOT_GROUP, MUST_GROUP -
Constructor Summary
ConstructorsConstructorDescriptionMemoizer()Constructs a memoizer around a newImageReadercreating memo files under the same directory as the original file only if the call tosetId(java.lang.String)takes longer than 100L in milliseconds.Memoizer(long minimumElapsed) Constructs a memoizer around a newImageReadercreating memo files under the same directory as the original file only if the call tosetId(java.lang.String)takes longer thanminimumElapsedin milliseconds.Constructs a memoizer around a newImageReadercreating memo files under thedirectoryargument including the full path of the original file only if the call tosetId(java.lang.String)takes longer thanminimumElapsedin milliseconds.Constructs a memoizer around the givenIFormatReadercreating memo files under the same directory as the original file only if the call tosetId(java.lang.String)takes longer than 100L in milliseconds.Memoizer(IFormatReader r, long minimumElapsed) Constructs a memoizer around the givenIFormatReadercreating memo files under the same directory as the original file only if the call tosetId(java.lang.String)takes longer thanminimumElapsedin milliseconds.Memoizer(IFormatReader r, long minimumElapsed, File directory) Constructs a memoizer around the givenIFormatReadercreating memo files under thedirectoryargument including the full path of the original file only if the call tosetId(java.lang.String)takes longer thanminimumElapsedin milliseconds.Memoizer(IFormatReader r, File directory) Constructs a memoizer around the givenIFormatReadercreating memo file under thedirectoryargument including the full path of the original file only if the call tosetId(java.lang.String)takes longer than 100L in milliseconds. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcleanup()voidclose()voidclose(boolean fileOnly) Closes the currently open file.booleanForce the current memo file to be deleted, if it exists.protected booleandeleteQuietly(File file) Attempts to delete an existing file, logging at warn if the deletion returns false or at error if an exception is thrown.booleangenerateMemo(String file) Convenience method to generate (or regenerate) the memo file for a given file.protected Memoizer.DesergetDeser()Returns a configuredKryoinstance.getMemoFile(String id) Constructs aFileobject fromidstring.Retrieves the current metadata store for this reader.protected OMEXMLServiceprotected IFormatReaderReturn theIFormatReaderinstance that is passed in or null if it has been invalidated, which will include the instance being closed.booleanReturns whether theReaderWrapper.readerinstance currently active was loaded from the memo file duringsetId(String).booleanReturns whether theReaderWrapper.readerinstance currently active was saved to the memo file duringsetId(String).booleanReturns whether or not version checking is done based upon major/minor version numbers.protected booleanisWritableDirectory(File writeDirectory) Test if the givenFileinstance is a writable directory.private static voidloadMemo()Load a memo file if possible, returning a null if not.static voidbooleansaveMemo()Save a reader including all reader wrappers inside a memo file.voidSets the current file name.voidsetMetadataStore(MetadataStore store) Sets the default metadata store for this reader.voidsetVersionChecking(boolean version) Set whether version checking is done based upon major/minor version numbers.voidskipSave(boolean skip) Set whether a memo file should be saved if a valid file is not present.protected org.perf4j.slf4j.Slf4JStopWatchbooleanReturnstrueif the version of the memo file as returned byMemoizer.Deser.loadReleaseVersion()andMemoizer.Deser.loadRevision()do not match the current version as specified byFormatTools.VERSION.Methods inherited from class loci.formats.ReaderWrapper
coreIndexToSeries, duplicate, fileGroupOption, get16BitLookupTable, get8BitLookupTable, getAdvancedSeriesUsedFiles, getAdvancedUsedFiles, getBitsPerPixel, getCoreIndex, getCoreMetadataList, getCurrentFile, getDatasetStructureDescription, getDimensionOrder, getDomains, getEffectiveSizeC, getFillColor, getFormat, getGlobalMetadata, getImageCount, getIndex, getIndex, getMetadataOptions, getMetadataStoreRoot, getMetadataValue, getModuloC, getModuloT, getModuloZ, getNativeDataType, getOptimalTileHeight, getOptimalTileWidth, getPixelType, getPossibleDomains, getReader, getRequiredDirectories, getResolution, getResolutionCount, getRGBChannelCount, getSeries, getSeriesCount, getSeriesMetadata, getSeriesMetadataValue, getSeriesUsedFiles, getSeriesUsedFiles, getSizeC, getSizeT, getSizeX, getSizeY, getSizeZ, getSuffixes, getSupportedMetadataLevels, getThumbSizeX, getThumbSizeY, getTileCodec, getTileCodecOptions, getTileColumns, getTileRows, getUnderlyingReaders, getUsedFiles, getUsedFiles, getZCTCoords, getZCTModuloCoords, hasCompanionFiles, hasFlattenedResolutions, isFalseColor, isGroupFiles, isIndexed, isInterleaved, isInterleaved, isLittleEndian, isMetadataComplete, isMetadataFiltered, isNormalized, isOrderCertain, isOriginalMetadataPopulated, isRGB, isSingleFile, isThisType, isThisType, isThisType, isThisType, isThumbnailSeries, openBytes, openBytes, openBytes, openBytes, openCompressedBytes, openCompressedBytes, openPlane, openThumbBytes, reopenFile, seriesToCoreIndex, setCoreIndex, setFillColor, setFlattenedResolutions, setGroupFiles, setMetadataFiltered, setMetadataOptions, setNormalized, setOriginalMetadataPopulated, setResolution, setSeries, unwrap, unwrap, unwrap
-
Field Details
-
VERSION
Default file version. Bumping this number will invalidate all other cached items. This should happen when the order and type of objects stored in the memo file changes. -
DEFAULT_MINIMUM_ELAPSED
public static final long DEFAULT_MINIMUM_ELAPSEDDefault value forminimumElapsedif none is provided in the constructor.- See Also:
-
LOGGER
private static final org.slf4j.Logger LOGGERDefaultLoggerfor the memoizer class -
minimumElapsed
private final long minimumElapsedMinimum number of milliseconds which must elapse during the call tosetId(java.lang.String)before a memo file will be created. Default toDEFAULT_MINIMUM_ELAPSEDif not specified via the constructor. -
directory
Directory where all memo files should be created. If this value is non-null, then all memo files will be created under it. Can be overriden bydoInPlaceCaching. -
doInPlaceCaching
private boolean doInPlaceCachingIftrue, then all memo files will be created in the same directory as the original file. -
ser
-
service
-
realFile
private loci.common.Location realFile -
memoFile
-
tempFile
-
skipLoad
private boolean skipLoad -
skipSave
private boolean skipSave -
versionChecking
private boolean versionCheckingBoolean specifying whether to invalidate the memo file based upon mismatched major/minor version numbers. By default, the Git commit hash is used to invalidate the memo file. -
loadedFromMemo
private boolean loadedFromMemoWhether theReaderWrapper.readerinstance currently active was loaded from the memo file duringsetId(String). -
savedToMemo
private boolean savedToMemoWhether theReaderWrapper.readerinstance was saved to a memo file onsetId(String). -
userMetadataStore
MetadataStoreset by the caller. This value will be held locally and not set on theReaderWrapper.readerdelegate until the execution ofsetId(String). If no value has been set by the caller, then no special actions are taken duringsetId(String). If a value is set, however, we must be careful with attempting to serialize it- See Also:
-
replacementMetadataStore
MetadataStorecreated internally.- See Also:
-
-
Constructor Details
-
Memoizer
public Memoizer()Constructs a memoizer around a newImageReadercreating memo files under the same directory as the original file only if the call tosetId(java.lang.String)takes longer than 100L in milliseconds. -
Memoizer
public Memoizer(long minimumElapsed) Constructs a memoizer around a newImageReadercreating memo files under the same directory as the original file only if the call tosetId(java.lang.String)takes longer thanminimumElapsedin milliseconds.- Parameters:
minimumElapsed- a long specifying the number of milliseconds which must elapse during the call tosetId(java.lang.String)before a memo file will be created.
-
Memoizer
Constructs a memoizer around a newImageReadercreating memo files under thedirectoryargument including the full path of the original file only if the call tosetId(java.lang.String)takes longer thanminimumElapsedin milliseconds.- Parameters:
minimumElapsed- a long specifying the number of milliseconds which must elapse during the call tosetId(java.lang.String)before a memo file will be created.directory- aFilespecifying the directory where all memo files should be created. Ifnull, disable memoization.
-
Memoizer
Constructs a memoizer around the givenIFormatReadercreating memo files under the same directory as the original file only if the call tosetId(java.lang.String)takes longer than 100L in milliseconds.- Parameters:
r- anIFormatReaderinstance. Ifnull, a newImageReaderis created.
-
Memoizer
Constructs a memoizer around the givenIFormatReadercreating memo files under the same directory as the original file only if the call tosetId(java.lang.String)takes longer thanminimumElapsedin milliseconds.- Parameters:
r- anIFormatReaderinstance Ifnull, a newImageReaderis created.minimumElapsed- a long specifying the number of milliseconds which must elapse during the call tosetId(java.lang.String)before a memo file will be created.
-
Memoizer
Constructs a memoizer around the givenIFormatReadercreating memo file under thedirectoryargument including the full path of the original file only if the call tosetId(java.lang.String)takes longer than 100L in milliseconds.- Parameters:
r- anIFormatReaderinstance Ifnull, a newImageReaderis created.directory- aFilespecifying the directory where all memo files should be created. Ifnull, disable memoization.
-
Memoizer
Constructs a memoizer around the givenIFormatReadercreating memo files under thedirectoryargument including the full path of the original file only if the call tosetId(java.lang.String)takes longer thanminimumElapsedin milliseconds.- Parameters:
r- anIFormatReaderinstance Ifnull, a newImageReaderis created.minimumElapsed- a long specifying the number of milliseconds which must elapse during the call tosetId(java.lang.String)before a memo file will be created.directory- aFilespecifying the directory where all memo files should be created. Ifnull, disable memoization.
-
-
Method Details
-
isLoadedFromMemo
public boolean isLoadedFromMemo()Returns whether theReaderWrapper.readerinstance currently active was loaded from the memo file duringsetId(String).- Returns:
trueif the reader was loaded from the memo file,falseotherwise.
-
isSavedToMemo
public boolean isSavedToMemo()Returns whether theReaderWrapper.readerinstance currently active was saved to the memo file duringsetId(String).- Returns:
trueif the reader was saved to the memo file,falseotherwise.
-
isVersionChecking
public boolean isVersionChecking()Returns whether or not version checking is done based upon major/minor version numbers.- Returns:
trueif version checking is done based upon major/minor version numbers,falseotherwise.
-
versionMismatch
Returnstrueif the version of the memo file as returned byMemoizer.Deser.loadReleaseVersion()andMemoizer.Deser.loadRevision()do not match the current version as specified byFormatTools.VERSION.- Throws:
IOException
-
setVersionChecking
public void setVersionChecking(boolean version) Set whether version checking is done based upon major/minor version numbers. Iftrue, then a mismatch between the major/minor version of the calling code (e.g. 4.4) and the major/minor version saved in the memo file (e.g. 5.0) will result in the memo file being invalidated. Iffalse(default), a mismatch in the Git commit hashes will invalidate the memo file. This method allows for less strict version checking.- Parameters:
version- a boolean specifying whether version checking is done based upon major/minor version numbers to invalidate the memo file
-
skipSave
public void skipSave(boolean skip) Set whether a memo file should be saved if a valid file is not present. Iffalse(default), then a memo file will be saved. This may cause an existing memo file to be overwritten. Iftrue, then a memo file will not be saved. This effectively makes the current Memoizer read-only. -
cleanup
protected void cleanup() -
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classReaderWrapper- Throws:
IOException
-
close
Description copied from interface:IFormatReaderCloses the currently open file. If the flag is set, this is all that happens; if unset, it is equivalent to callingCloseable.close().- Specified by:
closein interfaceIFormatReader- Overrides:
closein classReaderWrapper- Throws:
IOException
-
setId
Description copied from interface:IFormatHandlerSets the current file name.- Specified by:
setIdin interfaceIFormatHandler- Overrides:
setIdin classReaderWrapper- Throws:
FormatExceptionIOException
-
setMetadataStore
Description copied from interface:IFormatReaderSets the default metadata store for this reader.- Specified by:
setMetadataStorein interfaceIFormatReader- Overrides:
setMetadataStorein classReaderWrapper- Parameters:
store- a metadata store implementation.
-
getMetadataStore
Description copied from interface:IFormatReaderRetrieves the current metadata store for this reader. You can be assured that this method will never return anullmetadata store.- Specified by:
getMetadataStorein interfaceIFormatReader- Overrides:
getMetadataStorein classReaderWrapper- Returns:
- A metadata store implementation.
-
deleteQuietly
Attempts to delete an existing file, logging at warn if the deletion returns false or at error if an exception is thrown.- Returns:
- the result from
File.delete()orfalseif an exception is thrown.
-
getDeser
Returns a configuredKryoinstance. This method can be modified by consumers. The returned instance is not thread-safe.- Returns:
- a non-null
Kryoinstance.
-
getService
- Throws:
MissingLibraryException
-
stopWatch
protected org.perf4j.slf4j.Slf4JStopWatch stopWatch() -
getMemoFile
Constructs aFileobject fromidstring. This method can be modified by consumers, but then existing memo files will not be found.- Parameters:
id- the path passed tosetId(java.lang.String)- Returns:
- a
Fileobject pointing at the location of the memo file
-
isWritableDirectory
Test if the givenFileinstance is a writable directory.- Parameters:
writeDirectory- a possible writable directory- Returns:
- if the given
Fileis indeed a writable directory
-
loadMemo
Load a memo file if possible, returning a null if not. Corrupt memo files will be deleted if possible. Kryo exceptions should never propagate to the caller. Only the regular Bio-Formats exceptions should be thrown.- Throws:
IOExceptionFormatException
-
saveMemo
public boolean saveMemo()Save a reader including all reader wrappers inside a memo file. -
deleteMemo
public boolean deleteMemo()Force the current memo file to be deleted, if it exists.- Returns:
- true if the delete succeeded
-
getMemoFile
- Returns:
- current memo file (may be null)
-
handleMetadataStore
Return theIFormatReaderinstance that is passed in or null if it has been invalidated, which will include the instance being closed.- Serialization: If an unknown
MetadataStoreimplementation is passed in when no memo file exists, then a replacementMetadataStorewill be created and set on theReaderWrapper.readerdelegate before callingReaderWrapper.setId(String). This stack will then be serialized, before any values are copied intouserMetadataStore. - Deserialization: If an unknown
MetadataStoreimplementation is set before callingsetId(String)then ...
- Throws:
MissingLibraryException
- Serialization: If an unknown
-
generateMemo
Convenience method to generate (or regenerate) the memo file for a given file. Uses the cache directory and timing settings passed via Memoizer's constructor.- Parameters:
file- the file for which to generate a memo file- Returns:
- true if a memo file was saved
- Throws:
IOException
-
main
- Throws:
Exception
-
load
- Throws:
Exception
-