Class DirCache
The index file keeps track of which objects are currently checked out in the working directory, and the last modified time of those working files. Changes in the working directory can be detected by comparing the modification times to the cached modification time within the index file.
Index files are also used during merges, where the merge happens within the index file first, and the working directory is updated as a post-merge step. Conflicts are stored in the index file to allow tool (and human) based resolutions to be easily performed.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
(package private) static enum
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final Comparator<DirCacheEntry>
private int
Number of positions withinsortedEntries
that are valid.private static final int
private IndexChangedListener
listener to be informed on commitprivate final File
Location of the current version of the index file.private LockFile
Our active lock (if we hold it); null if we don't have it locked.private static final byte[]
private static final DirCacheEntry[]
private byte[]
index checksum when index was read from diskprivate Repository
Repository containing this indexprivate static final byte[]
private FileSnapshot
Keep track of whether the index has changed or notprivate DirCacheEntry[]
Individual file index entries, sorted by path name.private DirCacheTree
Cache tree for this index; null if the cache tree is not available.private DirCache.DirCacheVersion
If we read this index from disk, the original format.private byte[]
index checksum when index was written to disk -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuilder()
Create a new builder to update this cache.void
clear()
Empty this index, removing all entries.(package private) static int
cmp
(byte[] aPath, int aLen, byte[] bPath, int bLen) (package private) static int
cmp
(byte[] aPath, int aLen, DirCacheEntry b) (package private) static int
cmp
(DirCacheEntry a, DirCacheEntry b) boolean
commit()
Commit this change and release the lock.editor()
Create a new editor to recreate this cache.int
findEntry
(byte[] p, int pLen) Locate the position a path's entry is at in the index.(package private) int
findEntry
(int low, byte[] p, int pLen) int
Locate the position a path's entry is at in the index.private static String
formatExtensionName
(byte[] hdr) getCacheTree
(boolean build) Obtain (or build) the current cache tree structure.getEntriesWithin
(String path) Recursively get all entries within a subtree.getEntry
(int i) Get a specific entry.Get a specific entry.int
Total number of file entries stored in the index.(package private) DirCache.DirCacheVersion
boolean
Tells whether this index contains unmerged paths.private static boolean
is_DIRC
(byte[] hdr) boolean
Whether the memory state differs from the index fileboolean
lock()
Try to establish an update lock on the cache file.static DirCache
Create a new in-core index representation, lock it, and read from disk.static DirCache
lock
(File indexLocation, FS fs, IndexChangedListener indexChangedListener) Create a new in-core index representation, lock it, and read from disk.static DirCache
lock
(Repository repository, IndexChangedListener indexChangedListener) Create a new in-core index representation, lock it, and read from disk.static DirCache
Create a new empty index which is never stored on disk.(package private) int
nextEntry
(byte[] p, int pLen, int nextIdx) int
nextEntry
(int position) Determine the next index position past all entries with the same name.void
read()
Read the index from disk, if it has changed on disk.static DirCache
Create a new in-core index representation and read an index from disk.static DirCache
read
(ObjectReader reader, AnyObjectId treeId) Create a new in memory index read from the contents of a tree.static DirCache
read
(Repository repository) Create a new in-core index representation and read an index from disk.private void
readFrom
(InputStream inStream) private void
(package private) void
replace
(DirCacheEntry[] e, int cnt) private void
requireLocked
(LockFile tmp) private void
skipOptionalExtension
(InputStream in, MessageDigest md, byte[] hdr, long sz) (package private) void
toArray
(int i, DirCacheEntry[] dst, int off, int cnt) void
unlock()
Unlock this file and abort this change.private void
Update any smudged entries with information from the working tree.void
write()
Write the entry records from memory to disk.(package private) void
writeTo
(File dir, OutputStream os) Write all index trees to the object store, returning the root tree.
-
Field Details
-
SIG_DIRC
private static final byte[] SIG_DIRC -
EXT_TREE
private static final int EXT_TREE- See Also:
-
NO_ENTRIES
-
NO_CHECKSUM
private static final byte[] NO_CHECKSUM -
ENT_CMP
-
liveFile
Location of the current version of the index file. -
sortedEntries
Individual file index entries, sorted by path name. -
entryCnt
private int entryCntNumber of positions withinsortedEntries
that are valid. -
tree
Cache tree for this index; null if the cache tree is not available. -
myLock
Our active lock (if we hold it); null if we don't have it locked. -
snapshot
Keep track of whether the index has changed or not -
readIndexChecksum
private byte[] readIndexChecksumindex checksum when index was read from disk -
writeIndexChecksum
private byte[] writeIndexChecksumindex checksum when index was written to disk -
indexChangedListener
listener to be informed on commit -
repository
Repository containing this index -
version
If we read this index from disk, the original format.
-
-
Constructor Details
-
DirCache
Create a new in-core index representation.The new index will be empty. Callers may wish to read from the on disk file first with
read()
.- Parameters:
indexLocation
- location of the index file on disk.fs
- the file system abstraction which will be necessary to perform certain file system operations.
-
-
Method Details
-
cmp
-
cmp
-
cmp
static int cmp(byte[] aPath, int aLen, byte[] bPath, int bLen) -
newInCore
Create a new empty index which is never stored on disk.- Returns:
- an empty cache which has no backing store file. The cache may not be read or written, but it may be queried and updated (in memory).
-
read
Create a new in memory index read from the contents of a tree.- Parameters:
reader
- reader to access the tree objects from a repository.treeId
- tree to read. Must identify a tree, not a tree-ish.- Returns:
- a new cache which has no backing store file, but contains the
contents of
treeId
. - Throws:
IOException
- one or more trees not available from the ObjectReader.- Since:
- 4.2
-
read
Create a new in-core index representation and read an index from disk.The new index will be read before it is returned to the caller. Read failures are reported as exceptions and therefore prevent the method from returning a partially populated index.
- Parameters:
repository
- repository containing the index to read- Returns:
- a cache representing the contents of the specified index file (if it exists) or an empty cache if the file does not exist.
- Throws:
IOException
- the index file is present but could not be read.CorruptObjectException
- the index file is using a format or extension that this library does not support.
-
read
Create a new in-core index representation and read an index from disk.The new index will be read before it is returned to the caller. Read failures are reported as exceptions and therefore prevent the method from returning a partially populated index.
- Parameters:
indexLocation
- location of the index file on disk.fs
- the file system abstraction which will be necessary to perform certain file system operations.- Returns:
- a cache representing the contents of the specified index file (if it exists) or an empty cache if the file does not exist.
- Throws:
IOException
- the index file is present but could not be read.CorruptObjectException
- the index file is using a format or extension that this library does not support.
-
lock
Create a new in-core index representation, lock it, and read from disk.The new index will be locked and then read before it is returned to the caller. Read failures are reported as exceptions and therefore prevent the method from returning a partially populated index. On read failure, the lock is released.
- Parameters:
indexLocation
- location of the index file on disk.fs
- the file system abstraction which will be necessary to perform certain file system operations.- Returns:
- a cache representing the contents of the specified index file (if it exists) or an empty cache if the file does not exist.
- Throws:
IOException
- the index file is present but could not be read, or the lock could not be obtained.CorruptObjectException
- the index file is using a format or extension that this library does not support.
-
lock
public static DirCache lock(Repository repository, IndexChangedListener indexChangedListener) throws CorruptObjectException, IOException Create a new in-core index representation, lock it, and read from disk.The new index will be locked and then read before it is returned to the caller. Read failures are reported as exceptions and therefore prevent the method from returning a partially populated index. On read failure, the lock is released.
- Parameters:
repository
- repository containing the index to lock and readindexChangedListener
- listener to be informed when DirCache is committed- Returns:
- a cache representing the contents of the specified index file (if it exists) or an empty cache if the file does not exist.
- Throws:
IOException
- the index file is present but could not be read, or the lock could not be obtained.CorruptObjectException
- the index file is using a format or extension that this library does not support.- Since:
- 2.0
-
lock
public static DirCache lock(File indexLocation, FS fs, IndexChangedListener indexChangedListener) throws CorruptObjectException, IOException Create a new in-core index representation, lock it, and read from disk.The new index will be locked and then read before it is returned to the caller. Read failures are reported as exceptions and therefore prevent the method from returning a partially populated index. On read failure, the lock is released.
- Parameters:
indexLocation
- location of the index file on disk.fs
- the file system abstraction which will be necessary to perform certain file system operations.indexChangedListener
- listener to be informed when DirCache is committed- Returns:
- a cache representing the contents of the specified index file (if it exists) or an empty cache if the file does not exist.
- Throws:
IOException
- the index file is present but could not be read, or the lock could not be obtained.CorruptObjectException
- the index file is using a format or extension that this library does not support.
-
builder
Create a new builder to update this cache.Callers should add all entries to the builder, then use
DirCacheBuilder.finish()
to update this instance.- Returns:
- a new builder instance for this cache.
-
editor
Create a new editor to recreate this cache.Callers should add commands to the editor, then use
DirCacheEditor.finish()
to update this instance.- Returns:
- a new builder instance for this cache.
-
getVersion
DirCache.DirCacheVersion getVersion() -
replace
-
read
Read the index from disk, if it has changed on disk.This method tries to avoid loading the index if it has not changed since the last time we consulted it. A missing index file will be treated as though it were present but had no file entries in it.
- Throws:
IOException
- the index file is present but could not be read. This DirCache instance may not be populated correctly.CorruptObjectException
- the index file is using a format or extension that this library does not support.
-
isOutdated
Whether the memory state differs from the index file- Returns:
true
if the memory state differs from the index file- Throws:
IOException
-
clear
public void clear()Empty this index, removing all entries. -
readFrom
- Throws:
IOException
CorruptObjectException
-
skipOptionalExtension
private void skipOptionalExtension(InputStream in, MessageDigest md, byte[] hdr, long sz) throws IOException - Throws:
IOException
-
formatExtensionName
-
is_DIRC
private static boolean is_DIRC(byte[] hdr) -
lock
Try to establish an update lock on the cache file.- Returns:
- true if the lock is now held by the caller; false if it is held by someone else.
- Throws:
IOException
- the output file could not be created. The caller does not hold the lock.
-
write
Write the entry records from memory to disk.The cache must be locked first by calling
lock()
and receiving true as the return value. Applications are encouraged to lock the index, then invokeread()
to ensure the in-memory data is current, prior to updating the in-memory entries.Once written the lock is closed and must be either committed with
commit()
or rolled back withunlock()
.- Throws:
IOException
- the output file could not be created. The caller no longer holds the lock.
-
writeTo
- Throws:
IOException
-
commit
public boolean commit()Commit this change and release the lock.If this method fails (returns false) the lock is still released.
- Returns:
- true if the commit was successful and the file contains the new data; false if the commit failed and the file remains with the old data.
- Throws:
IllegalStateException
- the lock is not held.
-
requireLocked
-
unlock
public void unlock()Unlock this file and abort this change.The temporary file (if created) is deleted before returning.
-
findEntry
Locate the position a path's entry is at in the index. For details refer to #findEntry(byte[], int).- Parameters:
path
- the path to search for.- Returns:
- if >= 0 then the return value is the position of the entry in
the index; pass to
getEntry(int)
to obtain the entry information. If < 0 the entry does not exist in the index.
-
findEntry
public int findEntry(byte[] p, int pLen) Locate the position a path's entry is at in the index.If there is at least one entry in the index for this path the position of the lowest stage is returned. Subsequent stages can be identified by testing consecutive entries until the path differs.
If no path matches the entry -(position+1) is returned, where position is the location it would have gone within the index.
- Parameters:
p
- the byte array starting with the path to search for.pLen
- the length of the path in bytes- Returns:
- if >= 0 then the return value is the position of the entry in
the index; pass to
getEntry(int)
to obtain the entry information. If < 0 the entry does not exist in the index. - Since:
- 3.4
-
findEntry
int findEntry(int low, byte[] p, int pLen) -
nextEntry
public int nextEntry(int position) Determine the next index position past all entries with the same name.As index entries are sorted by path name, then stage number, this method advances the supplied position to the first position in the index whose path name does not match the path name of the supplied position's entry.
- Parameters:
position
- entry position of the path that should be skipped.- Returns:
- position of the next entry whose path is after the input.
-
nextEntry
int nextEntry(byte[] p, int pLen, int nextIdx) -
getEntryCount
public int getEntryCount()Total number of file entries stored in the index.This count includes unmerged stages for a file entry if the file is currently conflicted in a merge. This means the total number of entries in the index may be up to 3 times larger than the number of files in the working directory.
Note that this value counts only files.
- Returns:
- number of entries available.
- See Also:
-
getEntry
Get a specific entry.- Parameters:
i
- position of the entry to get.- Returns:
- the entry at position
i
.
-
getEntry
Get a specific entry.- Parameters:
path
- the path to search for.- Returns:
- the entry for the given
path
.
-
getEntriesWithin
Recursively get all entries within a subtree.- Parameters:
path
- the subtree path to get all entries within.- Returns:
- all entries recursively contained within the subtree.
-
toArray
-
getCacheTree
Obtain (or build) the current cache tree structure.This method can optionally recreate the cache tree, without flushing the tree objects themselves to disk.
- Parameters:
build
- if true and the cache tree is not present in the index it will be generated and returned to the caller.- Returns:
- the cache tree; null if there is no current cache tree available
and
build
was false.
-
writeTree
Write all index trees to the object store, returning the root tree.- Parameters:
ow
- the writer to use when serializing to the store. The caller is responsible for flushing the inserter before trying to use the returned tree identity.- Returns:
- identity for the root tree.
- Throws:
UnmergedPathException
- one or more paths contain higher-order stages (stage > 0), which cannot be stored in a tree object.IllegalStateException
- one or more paths contain an invalid mode which should never appear in a tree object.IOException
- an unexpected error occurred writing to the object store.
-
hasUnmergedPaths
public boolean hasUnmergedPaths()Tells whether this index contains unmerged paths.- Returns:
true
if this index contains unmerged paths. Means: at least one entry is of a stage different from 0.false
will be returned if all entries are of stage 0.
-
registerIndexChangedListener
-
updateSmudgedEntries
Update any smudged entries with information from the working tree.- Throws:
IOException
-