Top Description Fields Constructors Methods
org.eclipse.jgit.internal.storage.file

public Class FileSnapshot

extends Object
Class Inheritance
Known Direct Subclasses
org.eclipse.jgit.internal.storage.file.PackFileSnapshot
Static Imports
org.eclipse.jgit.util.FS.FileStoreAttributes.FALLBACK_FILESTORE_ATTRIBUTES, .FS.FileStoreAttributes.FALLBACK_TIMESTAMP_RESOLUTION

Caches when a file was last read, making it possible to detect future edits.

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

Modifier and TypeField and Description
private boolean
cannotBeRacilyClean

True once lastRead is far later than lastModified.

private static final DateTimeFormatter
private long
public static final FileSnapshot
DIRTY

A FileSnapshot that is considered to always be modified.

private final File
private final Object
fileKey

Object that uniquely identifies the given file, or null if a file key is not available

private boolean
private FS.FileStoreAttributes
fileStoreAttributeCache

measured FileStore attributes

private final Instant
lastModified

Last observed modification time of the path.

private boolean
private volatile Instant
lastRead

Last wall-clock time the path was read.

private static final Logger
public static final FileSnapshot
MISSING_FILE

A FileSnapshot that is clean if the file does not exist.

private static final Object
private long
private final long
size

Underlying file-system size in bytes.

private boolean
public static final long
UNKNOWN_SIZE

An unknown file size.

private static final Instant
private boolean
useConfig

if true read filesystem time resolution from configuration file otherwise use fallback resolution

private boolean

Constructor Summary

AccessConstructor and Description
protected
FileSnapshot(File
the path to remember meta data for. The path's current status information is saved.
file
)

Record a snapshot for a specific file path.

protected
FileSnapshot(File
the path to remember meta data for. The path's current status information is saved.
file
,
boolean
if true read filesystem time resolution from configuration file otherwise use fallback resolution
useConfig
)

Record a snapshot for a specific file path.

private
FileSnapshot(Instant read, Instant modified, long size, Duration fsTimestampResolution, Object fileKey)

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the two snapshots share the same information.
equals
(FileSnapshot
the other snapshot.
other
)

Compare two snapshots to see if they cache the same information.

public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

private FS.FileStoreAttributes
private long
private static BasicFileAttributes
private static Object
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

private boolean
isFileKeyChanged(Object currFileKey)

public boolean

Returns:

true if the path needs to be read again.
isModified
(File
the path the snapshot describes.
path
)

Check if the path may have been modified since the snapshot was saved.

private boolean
isModified(Instant currLastModified)

private boolean
private boolean
isSizeChanged(long currSize)

public long

Returns:

the delta in nanoseconds between lastModified and lastRead during last racy check
lastDelta
()

Get the delta in nanoseconds between lastModified and lastRead during last racy check

public Instant

Returns:

time of last snapshot update
lastModifiedInstant
()

Get time of last snapshot update

public long

Returns:

the racyLimitNanos threshold in nanoseconds during last racy check
lastRacyThreshold
()

Get the racyLimitNanos threshold in nanoseconds during last racy check

public static FileSnapshot

Returns:

the snapshot.
save
(File
the path to later remember. The path's current status information is saved.
path
)

Record a snapshot for a specific file path.

public static FileSnapshot

Returns:

the snapshot.
save
(Instant
the last modification time of the file
modified
)

Record a snapshot for a file for which the last modification time is already known.

public static FileSnapshot

Returns:

the snapshot.
saveNoConfig
(File
the path to later remember. The path's current status information is saved.
path
)

Record a snapshot for a specific file path without using config file to get filesystem timestamp resolution.

public void
setClean(FileSnapshot
the other snapshot.
other
)

Update this snapshot when the content hasn't changed.

public long

Returns:

file size in bytes of last snapshot update
size
()

Get file size in bytes of last snapshot update

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public void
waitUntilNotRacy()

Wait until this snapshot's file can't be racy anymore

pack-priv boolean

Returns:

true if #isModified(File) found the file key changed
wasFileKeyChanged
()

Whether #isModified(File) found the file key changed

pack-priv boolean

Returns:

true if #isModified(File) found the file's lastModified changed
wasLastModifiedChanged
()

Whether #isModified(File) found the file's lastModified changed

pack-priv boolean

Returns:

true if #isModified(File) detected that lastModified is racily clean
wasLastModifiedRacilyClean
()

Whether #isModified(File) detected that lastModified is racily clean

pack-priv boolean

Returns:

true if #isModified(File) found the file size changed
wasSizeChanged
()

Whether #isModified(File) found the file size changed

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

cannotBeRacilyCleanback to summary
private boolean cannotBeRacilyClean

True once lastRead is far later than lastModified.

dateFmtback to summary
private static final DateTimeFormatter dateFmt
deltaback to summary
private long delta
DIRTYback to summary
public static final FileSnapshot 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.

fileback to summary
private final File file
fileKeyback to summary
private final Object fileKey

Object that uniquely identifies the given file, or null if a file key is not available

fileKeyChangedback to summary
private boolean fileKeyChanged
fileStoreAttributeCacheback to summary
private FS.FileStoreAttributes fileStoreAttributeCache

measured FileStore attributes

lastModifiedback to summary
private final Instant lastModified

Last observed modification time of the path.

lastModifiedChangedback to summary
private boolean lastModifiedChanged
lastReadback to summary
private volatile Instant lastRead

Last wall-clock time the path was read.

LOGback to summary
private static final Logger LOG
MISSING_FILEback to summary
public static final FileSnapshot 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.

MISSING_FILEKEYback to summary
private static final Object MISSING_FILEKEY
racyThresholdback to summary
private long racyThreshold
sizeback to summary
private final long size

Underlying file-system size in bytes. When set to UNKNOWN_SIZE the size is not considered for modification checks.

sizeChangedback to summary
private boolean sizeChanged
UNKNOWN_SIZEback to summary
public static final long UNKNOWN_SIZE

An unknown file size. This value is used when a comparison needs to happen purely on the lastUpdate.

UNKNOWN_TIMEback to summary
private static final Instant UNKNOWN_TIME
useConfigback to summary
private boolean useConfig

if true read filesystem time resolution from configuration file otherwise use fallback resolution

wasRacyCleanback to summary
private boolean wasRacyClean

Constructor Detail

FileSnapshotback to summary
protected FileSnapshot(File file)

Record a snapshot for a specific file path.

This method should be invoked before the file is accessed.

Parameters
file:File

the path to remember meta data for. The path's current status information is saved.

FileSnapshotback to summary
protected FileSnapshot(File file, boolean useConfig)

Record a snapshot for a specific file path.

This method should be invoked before the file is accessed.

Parameters
file:File

the path to remember meta data for. The path's current status information is saved.

useConfig:boolean

if true read filesystem time resolution from configuration file otherwise use fallback resolution

FileSnapshotback to summary
private FileSnapshot(Instant read, Instant modified, long size, Duration fsTimestampResolution, Object fileKey)

Method Detail

equalsback to summary
public boolean equals(FileSnapshot other)

Compare two snapshots to see if they cache the same information.

Parameters
other:FileSnapshot

the other snapshot.

Returns:boolean

true if the two snapshots share the same information.

Annotations
@SuppressWarnings:NonOverridingEquals
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
fileStoreAttributeCacheback to summary
private FS.FileStoreAttributes fileStoreAttributeCache()
getEffectiveRacyThresholdback to summary
private long getEffectiveRacyThreshold()
getFileAttributesback to summary
private static BasicFileAttributes getFileAttributes(File path) throws NoSuchElementException
getFileKeyback to summary
private static Object getFileKey(BasicFileAttributes fileAttributes)
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
isFileKeyChangedback to summary
private boolean isFileKeyChanged(Object currFileKey)
isModifiedback to summary
public boolean isModified(File path)

Check if the path may have been modified since the snapshot was saved.

Parameters
path:File

the path the snapshot describes.

Returns:boolean

true if the path needs to be read again.

isModifiedback to summary
private boolean isModified(Instant currLastModified)
isRacyCleanback to summary
private boolean isRacyClean(Instant read)
isSizeChangedback to summary
private boolean isSizeChanged(long currSize)
lastDeltaback to summary
public long lastDelta()

Get the delta in nanoseconds between lastModified and lastRead during last racy check

Returns:long

the delta in nanoseconds between lastModified and lastRead during last racy check

lastModifiedInstantback to summary
public Instant lastModifiedInstant()

Get time of last snapshot update

Returns:Instant

time of last snapshot update

lastRacyThresholdback to summary
public long lastRacyThreshold()

Get the racyLimitNanos threshold in nanoseconds during last racy check

Returns:long

the racyLimitNanos threshold in nanoseconds during last racy check

saveback to summary
public static FileSnapshot save(File path)

Record a snapshot for a specific file path.

This method should be invoked before the file is accessed.

Parameters
path:File

the path to later remember. The path's current status information is saved.

Returns:FileSnapshot

the snapshot.

saveback to summary
public static FileSnapshot save(Instant modified)

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:Instant

the last modification time of the file

Returns:FileSnapshot

the snapshot.

saveNoConfigback to summary
public static FileSnapshot saveNoConfig(File path)

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:File

the path to later remember. The path's current status information is saved.

Returns:FileSnapshot

the snapshot.

setCleanback to summary
public void setClean(FileSnapshot other)

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, and equals(FileSnapshot) is true, it can use setClean(FileSnapshot) to make a future isModified(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:FileSnapshot

the other snapshot.

sizeback to summary
public long size()

Get file size in bytes of last snapshot update

Returns:long

file size in bytes of last snapshot update

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@SuppressWarnings:nls, ReferenceEquality
@Override
waitUntilNotRacyback to summary
public void waitUntilNotRacy() throws InterruptedException

Wait until this snapshot's file can't be racy anymore

Exceptions
InterruptedException:
if sleep was interrupted
wasFileKeyChangedback to summary
pack-priv boolean wasFileKeyChanged()

Whether #isModified(File) found the file key changed

Returns:boolean

true if #isModified(File) found the file key changed

wasLastModifiedChangedback to summary
pack-priv boolean wasLastModifiedChanged()

Whether #isModified(File) found the file's lastModified changed

Returns:boolean

true if #isModified(File) found the file's lastModified changed

wasLastModifiedRacilyCleanback to summary
pack-priv boolean wasLastModifiedRacilyClean()

Whether #isModified(File) detected that lastModified is racily clean

Returns:boolean

true if #isModified(File) detected that lastModified is racily clean

wasSizeChangedback to summary
pack-priv boolean wasSizeChanged()

Whether #isModified(File) found the file size changed

Returns:boolean

true if #isModified(File) found the file size changed