Top Description Inners Fields Constructors Methods
org.eclipse.jgit.lib

public Class RepositoryCache

extends Object
Class Inheritance
Imports
java.io.File, .IOException, java.util.ArrayList, .Collection, .Map, java.util.concurrent.ConcurrentHashMap, .ScheduledFuture, .ScheduledThreadPoolExecutor, .TimeUnit, org.eclipse.jgit.annotations.NonNull, org.eclipse.jgit.errors.RepositoryNotFoundException, org.eclipse.jgit.internal.storage.file.FileRepository, org.eclipse.jgit.lib.internal.WorkQueue, org.eclipse.jgit.util.FS, .IO, .RawParseUtils, org.slf4j.Logger, .LoggerFactory

Cache of active org.eclipse.jgit.lib.Repository instances.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
RepositoryCache.FileKey

Location of a Repository, using the standard java.io.File API.

public static interface
RepositoryCache.Key

Abstract hash key for RepositoryCache entries.

private static class

Field Summary

Modifier and TypeField and Description
private static final RepositoryCache
private final Map<RepositoryCache.Key, Repository>
private ScheduledFuture<?>
private volatile long
private static final Logger
private final RepositoryCache.Lock[]
private final Object

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
clear()

Unregister all repositories from the cache.

private void
private void
pack-priv static void
public static void
close(Repository
repository to unregister.
db
)

Close and remove a repository from the cache.

private void
private Collection<RepositoryCache.Key>
public static Collection<RepositoryCache.Key>

Returns:

the locations of all repositories registered in the cache.
getRegisteredKeys
()

Get the locations of all repositories registered in the cache.

pack-priv static boolean
private boolean
private RepositoryCache.Lock
public static Repository

Returns:

the repository instance requested; caller must close when done.
open
(RepositoryCache.Key
where the local repository is. Typically a org.eclipse.jgit.lib.RepositoryCache.FileKey.
location
)

Open an existing repository, reusing a cached instance if possible.

public static Repository

Returns:

the repository instance requested; caller must close when done.
open
(RepositoryCache.Key
where the local repository is. Typically a org.eclipse.jgit.lib.RepositoryCache.FileKey.
location
,
boolean
If true, and the repository is not found, throws RepositoryNotFoundException. If false, a repository instance is created and registered anyway.
mustExist
)

Open a repository, reusing a cached instance if possible.

private Repository
openRepository(final RepositoryCache.Key location, final boolean mustExist)

pack-priv static void
reconfigure(RepositoryCacheConfig repositoryCacheConfig)

public static void
register(Repository
repository to register.
db
)

Register one repository into the cache.

private void
public static void
unregister(Repository
repository to unregister.
db
)

Remove a repository from the cache.

public static void
unregister(RepositoryCache.Key
location of the repository to remove.
location
)

Remove a repository from the cache.

private void
private Repository
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cacheback to summary
private static final RepositoryCache cache
cacheMapback to summary
private final Map<RepositoryCache.Key, Repository> cacheMap
cleanupTaskback to summary
private ScheduledFuture<?> cleanupTask
expireAfterback to summary
private volatile long expireAfter
LOGback to summary
private static final Logger LOG
openLocksback to summary
private final RepositoryCache.Lock[] openLocks
schedulerLockback to summary
private final Object schedulerLock

Constructor Detail

RepositoryCacheback to summary
private RepositoryCache()

Method Detail

clearback to summary
public static void clear()

Unregister all repositories from the cache.

clearAllback to summary
private void clearAll()
clearAllExpiredback to summary
private void clearAllExpired()
clearExpiredback to summary
pack-priv static void clearExpired()
closeback to summary
public static void close(Repository db)

Close and remove a repository from the cache.

Removes a repository from the cache, if it is still registered here, and close it.

Parameters
db:Repository

repository to unregister.

configureEvictionback to summary
private void configureEviction(RepositoryCacheConfig repositoryCacheConfig)
getKeysback to summary
private Collection<RepositoryCache.Key> getKeys()
getRegisteredKeysback to summary
public static Collection<RepositoryCache.Key> getRegisteredKeys()

Get the locations of all repositories registered in the cache.

Returns:Collection<RepositoryCache.Key>

the locations of all repositories registered in the cache.

Since
4.1
isCachedback to summary
pack-priv static boolean isCached(Repository repo)
isExpiredback to summary
private boolean isExpired(Repository db)
lockForback to summary
private RepositoryCache.Lock lockFor(RepositoryCache.Key location)
openback to summary
public static Repository open(RepositoryCache.Key location) throws IOException, RepositoryNotFoundException

Open an existing repository, reusing a cached instance if possible.

When done with the repository, the caller must call org.eclipse.jgit.lib.Repository#close() to decrement the repository's usage counter.

Parameters
location:RepositoryCache.Key

where the local repository is. Typically a org.eclipse.jgit.lib.RepositoryCache.FileKey.

Returns:Repository

the repository instance requested; caller must close when done.

Exceptions
IOException:
the repository could not be read (likely its core.version property is not supported).
RepositoryNotFoundException:
there is no repository at the given location.
openback to summary
public static Repository open(RepositoryCache.Key location, boolean mustExist) throws IOException

Open a repository, reusing a cached instance if possible.

When done with the repository, the caller must call org.eclipse.jgit.lib.Repository#close() to decrement the repository's usage counter.

Parameters
location:RepositoryCache.Key

where the local repository is. Typically a org.eclipse.jgit.lib.RepositoryCache.FileKey.

mustExist:boolean

If true, and the repository is not found, throws RepositoryNotFoundException. If false, a repository instance is created and registered anyway.

Returns:Repository

the repository instance requested; caller must close when done.

Exceptions
IOException:
the repository could not be read (likely its core.version property is not supported).
RepositoryNotFoundException:
There is no repository at the given location, only thrown if mustExist is true.
openRepositoryback to summary
private Repository openRepository(final RepositoryCache.Key location, final boolean mustExist) throws IOException
reconfigureback to summary
pack-priv static void reconfigure(RepositoryCacheConfig repositoryCacheConfig)
registerback to summary
public static void register(Repository db)

Register one repository into the cache.

During registration the cache automatically increments the usage counter, permitting it to retain the reference. A org.eclipse.jgit.lib.RepositoryCache.FileKey for the repository's org.eclipse.jgit.lib.Repository#getDirectory() is used to index the repository in the cache.

If another repository already is registered in the cache at this location, the other instance is closed.

Parameters
db:Repository

repository to register.

registerRepositoryback to summary
private void registerRepository(RepositoryCache.Key location, Repository db)
unregisterback to summary
public static void unregister(Repository db)

Remove a repository from the cache.

Removes a repository from the cache, if it is still registered here. This method will not close the repository, only remove it from the cache. See org.eclipse.jgit.lib.RepositoryCache#close(Repository) to remove and close the repository.

Parameters
db:Repository

repository to unregister.

Since
4.3
unregisterback to summary
public static void unregister(RepositoryCache.Key location)

Remove a repository from the cache.

Removes a repository from the cache, if it is still registered here. This method will not close the repository, only remove it from the cache. See org.eclipse.jgit.lib.RepositoryCache#close(Repository) to remove and close the repository.

Parameters
location:RepositoryCache.Key

location of the repository to remove.

Since
4.1
unregisterAndCloseRepositoryback to summary
private void unregisterAndCloseRepository(RepositoryCache.Key location)
unregisterRepositoryback to summary
private Repository unregisterRepository(RepositoryCache.Key location)
org.eclipse.jgit.lib back to summary

public Class RepositoryCache.FileKey

extends Object
implements Key
Class Inheritance
All Implemented Interfaces
org.eclipse.jgit.lib.RepositoryCache.Key

Location of a Repository, using the standard java.io.File API.

Field Summary

Modifier and TypeField and Description
private final FS
private final File

Constructor Summary

AccessConstructor and Description
protected
FileKey(File
exact location of the repository.
directory
,
FS
the file system abstraction which will be necessary to perform certain file system operations.
fs
)

Create a pointer to a location on disk.

Method Summary

Modifier and TypeMethod and Description
private static File
public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

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

public static RepositoryCache.FileKey

Returns:

a key for the given directory.
exact
(File
location where the repository database is.
directory
,
FS
the file system abstraction which will be necessary to perform certain file system operations.
fs
)

Obtain a pointer to an exact location on disk.

public final File

Returns:

directory containing the repository database.
getFile
()

Get directory containing the repository database

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public static boolean

Returns:

true if the directory "looks like" a Git repository; false if it doesn't look enough like a Git directory to really be a Git directory.
isGitRepository
(File
the location of the directory to examine.
dir
,
FS
the file system abstraction which will be necessary to perform certain file system operations.
fs
)

Guess if a directory contains a Git repository.

private static boolean
public static RepositoryCache.FileKey

Returns:

a key for the given directory.
lenient
(File
location where the repository database might be.
directory
,
FS
the file system abstraction which will be necessary to perform certain file system operations.
fs
)

Obtain a pointer to a location on disk.

public Repository
open(boolean
true if the repository must exist in order to be opened; false if a new non-existent repository is permitted to be created (the caller is responsible for calling create).
mustExist
)

Implements org.eclipse.jgit.lib.RepositoryCache.Key.open.

Called by RepositoryCache#open(Key) if it doesn't exist yet.

private static String
public static File

Returns:

the actual directory location if a better match is found; null if there is no suitable match.
resolve
(File
location to guess from. Several permutations are tried.
directory
,
FS
the file system abstraction which will be necessary to perform certain file system operations.
fs
)

Guess the proper path for a Git repository.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

fsback to summary
private final FS fs
pathback to summary
private final File path

Constructor Detail

FileKeyback to summary
protected FileKey(File directory, FS fs)

Create a pointer to a location on disk.

Parameters
directory:File

exact location of the repository.

fs:FS

the file system abstraction which will be necessary to perform certain file system operations.

Method Detail

canonicalback to summary
private static File canonical(File path)
equalsback to summary
public boolean equals(Object o)

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
o: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
exactback to summary
public static RepositoryCache.FileKey exact(File directory, FS fs)

Obtain a pointer to an exact location on disk.

No guessing is performed, the given location is exactly the GIT_DIR directory of the repository.

Parameters
directory:File

location where the repository database is.

fs:FS

the file system abstraction which will be necessary to perform certain file system operations.

Returns:RepositoryCache.FileKey

a key for the given directory.

See Also
lenient(File, FS)
getFileback to summary
public final File getFile()

Get directory containing the repository database

Returns:File

directory containing the repository database.

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
isGitRepositoryback to summary
public static boolean isGitRepository(File dir, FS fs)

Guess if a directory contains a Git repository.

This method guesses by looking for the existence of some key files and directories.

Parameters
dir:File

the location of the directory to examine.

fs:FS

the file system abstraction which will be necessary to perform certain file system operations.

Returns:boolean

true if the directory "looks like" a Git repository; false if it doesn't look enough like a Git directory to really be a Git directory.

isValidHeadback to summary
private static boolean isValidHead(File head)
lenientback to summary
public static RepositoryCache.FileKey lenient(File directory, FS fs)

Obtain a pointer to a location on disk.

The method performs some basic guessing to locate the repository. Searched paths are:

  1. directory // assume exact match
  2. directory + "/.git" // assume working directory
  3. directory + ".git" // assume bare
Parameters
directory:File

location where the repository database might be.

fs:FS

the file system abstraction which will be necessary to perform certain file system operations.

Returns:RepositoryCache.FileKey

a key for the given directory.

See Also
exact(File, FS)
openback to summary
public Repository open(boolean mustExist) throws IOException

Implements org.eclipse.jgit.lib.RepositoryCache.Key.open.

Doc from org.eclipse.jgit.lib.RepositoryCache.Key.open.

Called by RepositoryCache#open(Key) if it doesn't exist yet.

If a repository does not exist yet in the cache, the cache will call this method to acquire a handle to it.

Parameters
mustExist:boolean

true if the repository must exist in order to be opened; false if a new non-existent repository is permitted to be created (the caller is responsible for calling create).

Returns:Repository

the new repository instance.

Annotations
@Override
Exceptions
IOException:
the repository could not be read (likely its core.version property is not supported).
readFirstLineback to summary
private static String readFirstLine(File head)
resolveback to summary
public static File resolve(File directory, FS fs)

Guess the proper path for a Git repository.

The method performs some basic guessing to locate the repository. Searched paths are:

  1. directory // assume exact match
  2. directory + "/.git" // assume working directory
  3. directory + ".git" // assume bare
Parameters
directory:File

location to guess from. Several permutations are tried.

fs:FS

the file system abstraction which will be necessary to perform certain file system operations.

Returns:File

the actual directory location if a better match is found; null if there is no suitable match.

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
@Override
org.eclipse.jgit.lib back to summary

public Interface RepositoryCache.Key

Known Direct Implementers
org.eclipse.jgit.lib.RepositoryCache.FileKey

Abstract hash key for RepositoryCache entries.

A Key instance should be lightweight, and implement hashCode() and equals() such that two Key instances are equal if they represent the same Repository location.

Method Summary

Modifier and TypeMethod and Description
public Repository

Returns:

the new repository instance.
open
(boolean
true if the repository must exist in order to be opened; false if a new non-existent repository is permitted to be created (the caller is responsible for calling create).
mustExist
)

Called by RepositoryCache#open(Key) if it doesn't exist yet.

Method Detail

openback to summary
public Repository open(boolean mustExist) throws IOException, RepositoryNotFoundException

Called by RepositoryCache#open(Key) if it doesn't exist yet.

If a repository does not exist yet in the cache, the cache will call this method to acquire a handle to it.

Parameters
mustExist:boolean

true if the repository must exist in order to be opened; false if a new non-existent repository is permitted to be created (the caller is responsible for calling create).

Returns:Repository

the new repository instance.

Exceptions
IOException:
the repository could not be read (likely its core.version property is not supported).
RepositoryNotFoundException:
There is no repository at the given location, only thrown if mustExist is true.
org.eclipse.jgit.lib back to summary

private Class RepositoryCache.Lock

extends Object
Class Inheritance

Constructor Summary

AccessConstructor and Description
private
Lock()

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Lockback to summary
private Lock()