This object tracks the last modified time of a file. Later during an
invocation of isModified(File)
the object will return true if the
file may have been modified and should be re-read from disk.
A snapshot does not "live update" when the underlying filesystem changes.
Callers must poll for updates by periodically invoking
isModified(File)
.
To work around the "racy git" problem (where a file may be modified multiple times within the granularity of the filesystem modification clock) this class may return true from isModified(File) if the last modification time of the file is less than 3 seconds ago.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final FileSnapshot
A FileSnapshot that is considered to always be modified.static final FileSnapshot
A FileSnapshot that is clean if the file does not exist.static final long
An unknown file size. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
FileSnapshot
(File file) Record a snapshot for a specific file path.protected
FileSnapshot
(File file, boolean useConfig) Record a snapshot for a specific file path. -
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
equals
(FileSnapshot other) Compare two snapshots to see if they cache the same information.int
hashCode()
boolean
isModified
(File path) Check if the path may have been modified since the snapshot was saved.long
Get the delta in nanoseconds between lastModified and lastRead during last racy checklong
Deprecated.Get time of last snapshot updatelong
Get the racyLimitNanos threshold in nanoseconds during last racy checkstatic FileSnapshot
save
(long modified) Deprecated.usesave(Instant)
instead.static FileSnapshot
Record a snapshot for a specific file path.static FileSnapshot
Record a snapshot for a file for which the last modification time is already known.static FileSnapshot
saveNoConfig
(File path) Record a snapshot for a specific file path without using config file to get filesystem timestamp resolution.void
setClean
(FileSnapshot other) Update this snapshot when the content hasn't changed.long
size()
Get file size in bytes of last snapshot updatetoString()
void
Wait until this snapshot's file can't be racy anymore
-
Field Details
-
UNKNOWN_SIZE
public static final long UNKNOWN_SIZEAn unknown file size. This value is used when a comparison needs to happen purely on the lastUpdate.- See Also:
-
DIRTY
A FileSnapshot that is considered to always be modified.This instance is useful for application code that wants to lazily read a file, but only after
isModified(File)
gets invoked. The returned snapshot contains only invalid status information. -
MISSING_FILE
A FileSnapshot that is clean if the file does not exist.This instance is useful if the application wants to consider a missing file to be clean.
isModified(File)
will return false if the file path does not exist.
-
-
Constructor Details
-
FileSnapshot
Record a snapshot for a specific file path.This method should be invoked before the file is accessed.
- Parameters:
file
- the path to remember meta data for. The path's current status information is saved.
-
FileSnapshot
Record a snapshot for a specific file path.This method should be invoked before the file is accessed.
- Parameters:
file
- the path to remember meta data for. The path's current status information is saved.useConfig
- iftrue
read filesystem time resolution from configuration file otherwise use fallback resolution
-
-
Method Details
-
save
Record a snapshot for a specific file path.This method should be invoked before the file is accessed.
- Parameters:
path
- the path to later remember. The path's current status information is saved.- Returns:
- the snapshot.
-
saveNoConfig
Record a snapshot for a specific file path without using config file to get filesystem timestamp resolution.This method should be invoked before the file is accessed. It is used by FileBasedConfig to avoid endless recursion.
- Parameters:
path
- the path to later remember. The path's current status information is saved.- Returns:
- the snapshot.
-
save
Deprecated.usesave(Instant)
instead.Record a snapshot for a file for which the last modification time is already known.This method should be invoked before the file is accessed.
Note that this method cannot rely on measuring file timestamp resolution to avoid racy git issues caused by finite file timestamp resolution since it's unknown in which filesystem the file is located. Hence the worst case fallback for timestamp resolution is used.
- Parameters:
modified
- the last modification time of the file- Returns:
- the snapshot.
-
save
Record a snapshot for a file for which the last modification time is already known.This method should be invoked before the file is accessed.
Note that this method cannot rely on measuring file timestamp resolution to avoid racy git issues caused by finite file timestamp resolution since it's unknown in which filesystem the file is located. Hence the worst case fallback for timestamp resolution is used.
- Parameters:
modified
- the last modification time of the file- Returns:
- the snapshot.
-
lastModified
Deprecated.uselastModifiedInstant()
insteadGet time of last snapshot update- Returns:
- time of last snapshot update
-
lastModifiedInstant
Get time of last snapshot update- Returns:
- time of last snapshot update
-
size
public long size()Get file size in bytes of last snapshot update- Returns:
- file size in bytes of last snapshot update
-
isModified
Check if the path may have been modified since the snapshot was saved.- Parameters:
path
- the path the snapshot describes.- Returns:
- true if the path needs to be read again.
-
setClean
Update this snapshot when the content hasn't changed.If the caller gets true from
isModified(File)
, re-reads the content, discovers the content is identical, andequals(FileSnapshot)
is true, it can usesetClean(FileSnapshot)
to make a futureisModified(File)
return false. The logic goes something like this:if (snapshot.isModified(path)) { FileSnapshot other = FileSnapshot.save(path); Content newContent = ...; if (oldContent.equals(newContent) && snapshot.equals(other)) snapshot.setClean(other); }
- Parameters:
other
- the other snapshot.
-
waitUntilNotRacy
Wait until this snapshot's file can't be racy anymore- Throws:
InterruptedException
- if sleep was interrupted
-
equals
Compare two snapshots to see if they cache the same information.- Parameters:
other
- the other snapshot.- Returns:
- true if the two snapshots share the same information.
-
equals
-
hashCode
public int hashCode() -
lastDelta
public long lastDelta()Get the delta in nanoseconds between lastModified and lastRead during last racy check- Returns:
- the delta in nanoseconds between lastModified and lastRead during last racy check
-
lastRacyThreshold
public long lastRacyThreshold()Get the racyLimitNanos threshold in nanoseconds during last racy check- Returns:
- the racyLimitNanos threshold in nanoseconds during last racy check
-
toString
-
lastModifiedInstant()
instead