org.eclipse.jgit.lib
Class WindowCache

java.lang.Object
  extended by org.eclipse.jgit.lib.WindowCache

public class WindowCache
extends java.lang.Object

Caches slices of a PackFile in memory for faster read access.

The WindowCache serves as a Java based "buffer cache", loading segments of a PackFile into the JVM heap prior to use. As JGit often wants to do reads of only tiny slices of a file, the WindowCache tries to smooth out these tiny reads into larger block-sized IO operations.


Nested Class Summary
protected static class org.eclipse.jgit.lib.OffsetCache.Ref<V>
          A soft reference wrapped around a cached object.
 
Field Summary
protected  java.lang.ref.ReferenceQueue<V> queue
          ReferenceQueue that createRef(PackFile, long, Object) must use.
 
Method Summary
protected  void clear(org.eclipse.jgit.lib.WindowCache.WindowRef ref)
          Update accounting information now that an object has left the cache.
protected  org.eclipse.jgit.lib.WindowCache.WindowRef createRef(PackFile p, long o, org.eclipse.jgit.lib.ByteWindow v)
          Construct a Ref (SoftReference) around a cached entity.
protected  int hash(int packHash, long off)
          Compute the hash code value for a (PackFile,position) tuple.
protected  boolean isFull()
          Determine if the cache is full and requires eviction of entries.
protected  org.eclipse.jgit.lib.ByteWindow load(PackFile pack, long offset)
          Materialize an object that doesn't yet exist in the cache.
static void reconfigure(int packedGitLimit, int packedGitWindowSize, boolean packedGitMMAP, int deltaBaseCacheLimit)
          Deprecated. Use WindowCacheConfig instead.
static void reconfigure(WindowCacheConfig cfg)
          Modify the configuration of the window cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

queue

protected final java.lang.ref.ReferenceQueue<V> queue
ReferenceQueue that createRef(PackFile, long, Object) must use.

Method Detail

reconfigure

public static void reconfigure(int packedGitLimit,
                               int packedGitWindowSize,
                               boolean packedGitMMAP,
                               int deltaBaseCacheLimit)
Deprecated. Use WindowCacheConfig instead.

Modify the configuration of the window cache.

The new configuration is applied immediately. If the new limits are smaller than what what is currently cached, older entries will be purged as soon as possible to allow the cache to meet the new limit.

Parameters:
packedGitLimit - maximum number of bytes to hold within this instance.
packedGitWindowSize - number of bytes per window within the cache.
packedGitMMAP - true to enable use of mmap when creating windows.
deltaBaseCacheLimit - number of bytes to hold in the delta base cache.

reconfigure

public static void reconfigure(WindowCacheConfig cfg)
Modify the configuration of the window cache.

The new configuration is applied immediately. If the new limits are smaller than what what is currently cached, older entries will be purged as soon as possible to allow the cache to meet the new limit.

Parameters:
cfg - the new window cache configuration.
Throws:
java.lang.IllegalArgumentException - the cache configuration contains one or more invalid settings, usually too low of a limit.

hash

protected int hash(int packHash,
                   long off)
Compute the hash code value for a (PackFile,position) tuple.

For example, return packHash + (int) (position >>> 4). Implementors must override with a suitable hash (for example, a different right shift on the position).

Parameters:
packHash - hash code for the file being accessed.
off - position within the file being accessed.
Returns:
a reasonable hash code mixing the two values.

load

protected org.eclipse.jgit.lib.ByteWindow load(PackFile pack,
                                               long offset)
                                        throws java.io.IOException
Materialize an object that doesn't yet exist in the cache.

This method is invoked by getOrLoad(PackFile, long) when the specified entity does not yet exist in the cache. Internal locking ensures that at most one thread can call this method for each unique (pack,position), but multiple threads can call this method concurrently for different (pack,position) tuples.

Parameters:
pack - the file to materialize the entry from.
offset - offset within the file of the entry.
Returns:
the materialized object. Must never be null.
Throws:
java.io.IOException - the method was unable to materialize the object for this input pair. The usual reasons would be file corruption, file not found, out of file descriptors, etc.

createRef

protected org.eclipse.jgit.lib.WindowCache.WindowRef createRef(PackFile p,
                                                               long o,
                                                               org.eclipse.jgit.lib.ByteWindow v)
Construct a Ref (SoftReference) around a cached entity.

Implementing this is only necessary if the subclass is performing resource accounting during load(PackFile, long) and clear(Ref) requires some information to update the accounting.

Implementors MUST ensure that the returned reference uses the queue ReferenceQueue, otherwise clear(Ref) will not be invoked at the proper time.

Parameters:
p - the file to materialize the entry from.
o - offset within the file of the entry.
v - the object returned by load(PackFile, long).
Returns:
a soft reference subclass wrapped around v.

clear

protected void clear(org.eclipse.jgit.lib.WindowCache.WindowRef ref)
Update accounting information now that an object has left the cache.

This method is invoked exactly once for the combined load(PackFile, long) and createRef(PackFile, long, Object) invocation pair that was used to construct and insert an object into the cache.

Parameters:
ref - the reference wrapped around the object. Implementations must be prepared for ref.get() to return null.

isFull

protected boolean isFull()
Determine if the cache is full and requires eviction of entries.

By default this method returns false. Implementors may override to consult with the accounting updated by load(PackFile, long), createRef(PackFile, long, Object) and clear(Ref).

Returns:
true if the cache is still over-limit and requires eviction of more entries.