FileDocCategorySizeDatePackage
WeakIdentityMap.javaAPI DocGlassfish v2 API5262Tue May 22 16:54:36 BST 2007oracle.toplink.essentials.internal.identitymaps

WeakIdentityMap

public class WeakIdentityMap extends FullIdentityMap

Purpose: A WeakIdentityMap holds all objects referenced by the application only. The weak identity map is similar to the full identity map except for the fact that it allows full garbage collection.

Responsibilities:

  • Guarantees identity
  • Allows garbage collection
since
TOPLink/Java 1.0

Fields Summary
protected int
cleanupCount
Keep track of a counter to amortize cleanup of dead cache keys
protected int
cleanupSize
PERF: Keep track of a cleanup size to avoid cleanup bottleneck for large caches.
Constructors Summary
public WeakIdentityMap(int size)

        super(size);
        this.cleanupCount = 0;
        this.cleanupSize = size;
    
Methods Summary
protected voidcleanupDeadCacheKeys()
Search for any cache keys that have been garbage collected and remove them. This must be done because allthough the objects held by the cache keys will garbage collect, the keys themselves will not and must be cleaned up. This is a linear opperation so is amortized through the cleanupCount to occur only once per cycle avergaing to make the total time still constant.

        for (Enumeration keysEnum = getCacheKeys().elements(); keysEnum.hasMoreElements();) {
            CacheKey key = (CacheKey)keysEnum.nextElement();
            if (key.getObject() == null) {
                // Check lock first.
                //Change for CR 2317
                if (key.acquireNoWait()) {
                    try {
                        if (key.getObject() == null) {
                            getCacheKeys().remove(key);
                        }
                    } finally {
                        key.release();
                    }
                }

                //change complete CR 2317  
            }
        }
    
public oracle.toplink.essentials.internal.identitymaps.CacheKeycreateCacheKey(java.util.Vector primaryKey, java.lang.Object object, java.lang.Object writeLockValue, long readTime)

        return new WeakCacheKey(primaryKey, object, writeLockValue, readTime);
    
protected intgetCleanupCount()
Used to amortized the cleanup of dead cache keys.

        return cleanupCount;
    
protected intgetCleanupSize()
Used to amortized the cleanup of dead cache keys.

        return cleanupSize;
    
protected voidput(oracle.toplink.essentials.internal.identitymaps.CacheKey cacheKey)
Store the object in the cache with the cache key.

        //CR3712  Add the method back.
        synchronized (this) {
            if (getCleanupCount() > getCleanupSize()) {
                cleanupDeadCacheKeys();
                setCleanupCount(0);
                // PERF: Avoid cleanup bottleneck for large cache sizes, increase next cleanup.
                if (getSize() > getCleanupSize()) {
                    setCleanupSize(getSize());
                }
            }
            setCleanupCount(getCleanupCount() + 1);
        }
        super.put(cacheKey);
    
protected voidsetCleanupCount(int cleanupCount)

        this.cleanupCount = cleanupCount;
    
protected voidsetCleanupSize(int cleanupSize)

        this.cleanupSize = cleanupSize;