public class DirCache
extends java.lang.Object
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.
Modifier and Type | Field and Description |
---|---|
(package private) static java.util.Comparator<DirCacheEntry> |
ENT_CMP |
private int |
entryCnt
Number of positions within
sortedEntries that are valid. |
private static int |
EXT_TREE |
private IndexChangedListener |
indexChangedListener
listener to be informed on commit
|
private java.io.File |
liveFile
Location of the current version of the index file.
|
private LockFile |
myLock
Our active lock (if we hold it); null if we don't have it locked.
|
private static byte[] |
NO_CHECKSUM |
private static DirCacheEntry[] |
NO_ENTRIES |
private byte[] |
readIndexChecksum
index checksum when index was read from disk
|
private Repository |
repository
Repository containing this index
|
private static byte[] |
SIG_DIRC |
private FileSnapshot |
snapshot
Keep track of whether the index has changed or not
|
private DirCacheEntry[] |
sortedEntries
Individual file index entries, sorted by path name.
|
private DirCacheTree |
tree
Cache tree for this index; null if the cache tree is not available.
|
private byte[] |
writeIndexChecksum
index checksum when index was written to disk
|
Constructor and Description |
---|
DirCache(java.io.File indexLocation,
FS fs)
Create a new in-core index representation.
|
Modifier and Type | Method and Description |
---|---|
DirCacheBuilder |
builder()
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.
|
DirCacheEditor |
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 |
findEntry(java.lang.String path)
Locate the position a path's entry is at in the index.
|
private static java.lang.String |
formatExtensionName(byte[] hdr) |
DirCacheTree |
getCacheTree(boolean build)
Obtain (or build) the current cache tree structure.
|
DirCacheEntry[] |
getEntriesWithin(java.lang.String path)
Recursively get all entries within a subtree.
|
DirCacheEntry |
getEntry(int i)
Get a specific entry.
|
DirCacheEntry |
getEntry(java.lang.String path)
Get a specific entry.
|
int |
getEntryCount()
Total number of file entries stored in the index.
|
boolean |
hasUnmergedPaths()
Tells whether this index contains unmerged paths.
|
private static boolean |
is_DIRC(byte[] hdr) |
boolean |
isOutdated()
Whether the memory state differs from the index file
|
boolean |
lock()
Try to establish an update lock on the cache file.
|
static DirCache |
lock(java.io.File indexLocation,
FS fs)
Create a new in-core index representation, lock it, and read from disk.
|
static DirCache |
lock(java.io.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 |
newInCore()
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 |
read(java.io.File indexLocation,
FS fs)
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(java.io.InputStream inStream) |
private void |
registerIndexChangedListener(IndexChangedListener listener) |
(package private) void |
replace(DirCacheEntry[] e,
int cnt) |
private void |
requireLocked(LockFile tmp) |
private void |
skipOptionalExtension(java.io.InputStream in,
java.security.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 |
updateSmudgedEntries()
Update any smudged entries with information from the working tree.
|
void |
write()
Write the entry records from memory to disk.
|
(package private) void |
writeTo(java.io.File dir,
java.io.OutputStream os) |
ObjectId |
writeTree(ObjectInserter ow)
Write all index trees to the object store, returning the root tree.
|
private static final byte[] SIG_DIRC
private static final int EXT_TREE
private static final DirCacheEntry[] NO_ENTRIES
private static final byte[] NO_CHECKSUM
static final java.util.Comparator<DirCacheEntry> ENT_CMP
private final java.io.File liveFile
private DirCacheEntry[] sortedEntries
private int entryCnt
sortedEntries
that are valid.private DirCacheTree tree
private LockFile myLock
private FileSnapshot snapshot
private byte[] readIndexChecksum
private byte[] writeIndexChecksum
private IndexChangedListener indexChangedListener
private Repository repository
public DirCache(java.io.File indexLocation, FS fs)
The new index will be empty. Callers may wish to read from the on disk
file first with read()
.
indexLocation
- location of the index file on disk.fs
- the file system abstraction which will be necessary to perform
certain file system operations.static int cmp(DirCacheEntry a, DirCacheEntry b)
static int cmp(byte[] aPath, int aLen, DirCacheEntry b)
static int cmp(byte[] aPath, int aLen, byte[] bPath, int bLen)
public static DirCache newInCore()
public static DirCache read(ObjectReader reader, AnyObjectId treeId) throws java.io.IOException
reader
- reader to access the tree objects from a repository.treeId
- tree to read. Must identify a tree, not a tree-ish.treeId
.java.io.IOException
- one or more trees not available from the ObjectReader.public static DirCache read(Repository repository) throws CorruptObjectException, java.io.IOException
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.
repository
- repository containing the index to readjava.io.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.public static DirCache read(java.io.File indexLocation, FS fs) throws CorruptObjectException, java.io.IOException
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.
indexLocation
- location of the index file on disk.fs
- the file system abstraction which will be necessary to perform
certain file system operations.java.io.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.public static DirCache lock(java.io.File indexLocation, FS fs) throws CorruptObjectException, java.io.IOException
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.
indexLocation
- location of the index file on disk.fs
- the file system abstraction which will be necessary to perform
certain file system operations.java.io.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.public static DirCache lock(Repository repository, IndexChangedListener indexChangedListener) throws CorruptObjectException, java.io.IOException
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.
repository
- repository containing the index to lock and readindexChangedListener
- listener to be informed when DirCache is committedjava.io.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.public static DirCache lock(java.io.File indexLocation, FS fs, IndexChangedListener indexChangedListener) throws CorruptObjectException, java.io.IOException
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.
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 committedjava.io.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.public DirCacheBuilder builder()
Callers should add all entries to the builder, then use
DirCacheBuilder.finish()
to update this
instance.
public DirCacheEditor editor()
Callers should add commands to the editor, then use
DirCacheEditor.finish()
to update this
instance.
void replace(DirCacheEntry[] e, int cnt)
public void read() throws java.io.IOException, CorruptObjectException
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.
java.io.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.public boolean isOutdated() throws java.io.IOException
true
if the memory state differs from the index filejava.io.IOException
public void clear()
private void readFrom(java.io.InputStream inStream) throws java.io.IOException, CorruptObjectException
java.io.IOException
CorruptObjectException
private void skipOptionalExtension(java.io.InputStream in, java.security.MessageDigest md, byte[] hdr, long sz) throws java.io.IOException
java.io.IOException
private static java.lang.String formatExtensionName(byte[] hdr)
private static boolean is_DIRC(byte[] hdr)
public boolean lock() throws java.io.IOException
java.io.IOException
- the output file could not be created. The caller does not
hold the lock.public void write() throws java.io.IOException
The cache must be locked first by calling lock()
and receiving
true as the return value. Applications are encouraged to lock the index,
then invoke read()
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 with unlock()
.
java.io.IOException
- the output file could not be created. The caller no longer
holds the lock.void writeTo(java.io.File dir, java.io.OutputStream os) throws java.io.IOException
java.io.IOException
public boolean commit()
If this method fails (returns false) the lock is still released.
java.lang.IllegalStateException
- the lock is not held.private void requireLocked(LockFile tmp)
public void unlock()
The temporary file (if created) is deleted before returning.
public int findEntry(java.lang.String path)
path
- the path to search for.getEntry(int)
to obtain the entry
information. If < 0 the entry does not exist in the index.public int findEntry(byte[] p, int pLen)
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.
p
- the byte array starting with the path to search for.pLen
- the length of the path in bytesgetEntry(int)
to obtain the entry
information. If < 0 the entry does not exist in the index.int findEntry(int low, byte[] p, int pLen)
public int nextEntry(int position)
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.
position
- entry position of the path that should be skipped.int nextEntry(byte[] p, int pLen, int nextIdx)
public int getEntryCount()
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.
getEntry(int)
public DirCacheEntry getEntry(int i)
i
- position of the entry to get.i
.public DirCacheEntry getEntry(java.lang.String path)
path
- the path to search for.path
.public DirCacheEntry[] getEntriesWithin(java.lang.String path)
path
- the subtree path to get all entries within.void toArray(int i, DirCacheEntry[] dst, int off, int cnt)
public DirCacheTree getCacheTree(boolean build)
This method can optionally recreate the cache tree, without flushing the tree objects themselves to disk.
build
- if true and the cache tree is not present in the index it will
be generated and returned to the caller.build
was false.public ObjectId writeTree(ObjectInserter ow) throws UnmergedPathException, java.io.IOException
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.UnmergedPathException
- one or more paths contain higher-order stages (stage > 0),
which cannot be stored in a tree object.java.lang.IllegalStateException
- one or more paths contain an invalid mode which should never
appear in a tree object.java.io.IOException
- an unexpected error occurred writing to the object store.public boolean hasUnmergedPaths()
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.private void registerIndexChangedListener(IndexChangedListener listener)
private void updateSmudgedEntries() throws java.io.IOException
java.io.IOException