FileDocCategorySizeDatePackage
CacheKey.javaAPI DocGlassfish v2 API14859Tue May 22 16:54:34 BST 2007oracle.toplink.essentials.internal.identitymaps

CacheKey

public class CacheKey extends Object implements Serializable, Cloneable

Purpose: Container class for storing objects in an IdentityMap.

Responsibilities:

  • Hold key and object.
  • Maintain and update the current writeLockValue.
since
TOPLink/Java 1.0

Fields Summary
protected Vector
key
The key holds the vector of primary key values for the object.
protected int
hash
Calculated hash value for CacheKey from primary key values.
protected Object
object
protected IdentityMap
mapOwner
protected Object
writeLockValue
The writeLock value is being held as an object so that it might contain a number or timestamp.
protected Object
wrapper
The cached wrapper for the object, used in EJB.
protected ConcurrencyManager
mutex
The cache key hold a reference to the concurrency manager to perform the cache key level locking.
protected Record
record
This is used for Document Preservation to cache the record that this object was built from
protected long
lastUpdatedQueryId
This attribute is the system time in milli seconds that the object was last refreshed on
protected int
invalidationState
Invalidation State can be used to indicate whether this cache key is considered valid
public static final int
CHECK_INVALIDATION_POLICY
The following constants are used for the invalidationState variable
public static final int
CACHE_KEY_INVALID
protected long
readTime
The read time stores the millisecond value of the last time the object help by this cache key was confirmed as up to date.
Constructors Summary
public CacheKey(Vector primaryKeys)


       
        this.key = primaryKeys;
        this.hash = computeHash(primaryKeys);
    
public CacheKey(Vector primaryKey, Object object, Object lockValue)

        this(primaryKey);
        //bug4649617  use setter instead of this.object = object to avoid hard reference on object in subclasses
		setObject(object);
        this.writeLockValue = lockValue;
    
public CacheKey(Vector primaryKey, Object object, Object lockValue, long readTime)

        this(primaryKey, object, lockValue);
        this.readTime = readTime;
    
Methods Summary
public voidacquire()
Acquire the lock on the cachek key object.

        getMutex().acquire(false);
    
public voidacquire(boolean forMerge)
Acquire the lock on the cachek key object. For the merge process called with true from the merge process, if true then the refresh will not refresh the object

        getMutex().acquire(forMerge);
    
public voidacquireDeferredLock()
Acquire the deferred lcok.

        getMutex().acquireDeferredLock();
    
public booleanacquireNoWait()
Acquire the lock on the cache key object. But only if the object has no lock on it Added for CR 2317

        return getMutex().acquireNoWait(false);
    
public booleanacquireNoWait(boolean forMerge)
Acquire the lock on the cache key object. But only if the object has no lock on it Added for CR 2317 called with true from the merge process, if true then the refresh will not refresh the object

        return getMutex().acquireNoWait(forMerge);
    
public voidacquireReadLock()
Acquire the read lock on the cachek key object.

        getMutex().acquireReadLock();
    
public booleanacquireReadLockNoWait()
Acquire the read lock on the cache key object.

        return getMutex().acquireReadLockNoWait();
    
public voidcheckReadLock()
Check the read lock on the cachek key object. This can be called to ensure the cache key has a valid built object. It does not hold a lock, so the object could be refreshed afterwards.

        getMutex().checkReadLock();
    
public java.lang.Objectclone()
INTERNAL: Clones itself.

        Object object = null;

        try {
            object = super.clone();
        } catch (Exception exception) {
            throw new InternalError(exception.toString());
        }

        return object;
    
private intcomputeArrayHashCode(java.lang.Object obj)
Compute the hashcode for supported array types

param
obj - an array
return
hashCode value

        if (obj.getClass() == ClassConstants.APBYTE) {
            return Arrays.hashCode((byte []) obj);
        } else if (obj.getClass() == ClassConstants.APCHAR) {
            return Arrays.hashCode((char []) obj);
        } else {
            return Arrays.hashCode((Object []) obj);
        }
    
protected intcomputeHash(java.util.Vector primaryKey)
Compute a hash value for the CacheKey dependent upon the values of the primary key instead of the identity of the receiver. This method is intended for use by constructors only.

        int computedHashValue = 0;

        for (int index = 0; index < primaryKey.size(); index++) {
            Object value = primaryKey.elementAt(index);
            if (value != null) {
            	//gf bug 1193: fix to handle array hashcodes properly
            	if (value.getClass().isArray()) {
            		computedHashValue = computedHashValue ^ computeArrayHashCode(value);
            	} else {
            		computedHashValue = computedHashValue ^ (value.hashCode());
            	}
            }
        }
        return computedHashValue;
    
public booleanequals(java.lang.Object object)
Determine if the receiver is equal to anObject. If anObject is a CacheKey, do further comparison, otherwise, return false.

see
CacheKey#equals(CacheKey)

        if (object instanceof CacheKey) {
            return equals((CacheKey)object);
        }

        return false;
    
public booleanequals(oracle.toplink.essentials.internal.identitymaps.CacheKey key)
Determine if the receiver is equal to key. Use an index compare, because it is much faster than enumerations.

        if (this == key) {
            return true;
        }
        if (getKey().size() == key.getKey().size()) {
            for (int index = 0; index < getKey().size(); index++) {
                Object myValue = getKey().elementAt(index);
                Object comparisionValue = key.getKey().elementAt(index);

                if (myValue == null) {
                    if (comparisionValue != null) {
                        return false;
                    }
                } else if (myValue.getClass().isArray()) {
                    //gf bug 1193: fix array comparison logic to exit if they don't match, and continue the loop if they do
                    if (((myValue.getClass() == ClassConstants.APBYTE) && (comparisionValue.getClass() == ClassConstants.APBYTE)) ) {
                        if (!Helper.compareByteArrays((byte[])myValue, (byte[])comparisionValue)){
                            return false;
                        }
                    } else if (((myValue.getClass() == ClassConstants.APCHAR) && (comparisionValue.getClass() == ClassConstants.APCHAR)) ) {
                        if (!Helper.compareCharArrays((char[])myValue, (char[])comparisionValue)){
                            return false;
                        }
                    } else {
                        if (!Helper.compareArrays((Object[])myValue, (Object[])comparisionValue)) {
                            return false;
                        }
                    }
                } else {
                    if (!(myValue.equals(comparisionValue))) {
                        return false;
                    }
                }
            }
            return true;
        }
        return false;
    
public intgetInvalidationState()
INTERNAL: Return the value of the invalidationState Variable The return value will be a constant CHECK_INVALIDATION_POLICY - The Invalidation policy is must be checked for this cache key's sate CACHE_KEY_INVALID - This cache key has been labeled invalid.

        return invalidationState;
    
public java.util.VectorgetKey()

        return key;
    
public longgetLastUpdatedQueryId()
INTERNAL: This method returns the system time in millis seconds at which this object was last refreshed CR #4365 CR #2698903 ... instead of using millis we will now use id's instead. Method renamed appropriately.

        return this.lastUpdatedQueryId;
    
public synchronized oracle.toplink.essentials.internal.helper.ConcurrencyManagergetMutex()
Return the concurrency manager.

        if (mutex == null) {
            mutex = new ConcurrencyManager(this);
        }
        return mutex;
    
public java.lang.ObjectgetObject()

        return object;
    
public oracle.toplink.essentials.internal.identitymaps.IdentityMapgetOwningMap()

        return this.mapOwner;
    
public longgetReadTime()
INTERNAL: Return the current value of the Read Time variable

        return readTime;
    
public oracle.toplink.essentials.sessions.RecordgetRecord()

        return record;
    
public java.lang.ObjectgetWrapper()

        return wrapper;
    
public java.lang.ObjectgetWriteLockValue()

        return writeLockValue;
    
public inthashCode()
Overrides hashCode() in Object to use the primaryKey's hashCode for storage in data structures.

        return hash;
    
public booleanisAcquired()
Return if the lock is acquired

        return getMutex().isAcquired();
    
public voidrelease()
Release the lock on the cachek key object.

        getMutex().release();
    
public voidreleaseDeferredLock()
Release the deferred lock

        getMutex().releaseDeferredLock();
    
public voidreleaseReadLock()
Release the read lock on the cachek key object.

        getMutex().releaseReadLock();
    
public voidsetInvalidationState(int invalidationState)
INTERNAL: Set the value of the invalidationState Variable The possible values are from an enumeration of constants CHECK_INVALIDATION_POLICY - The invalidation policy is must be checked for this cache key's sate CACHE_KEY_INVALID - This cache key has been labeled invalid.

        this.invalidationState = invalidationState;
    
public voidsetKey(java.util.Vector key)

        this.key = key;
        this.hash = computeHash(key);
    
public voidsetLastUpdatedQueryId(long id)
INTERNAL: This method sets the system time in millis seconds at which this object was last refreshed CR #4365 CR #2698903 ... instead of using millis we will now use ids instead. Method renamed appropriately.

        this.lastUpdatedQueryId = id;
    
public voidsetMutex(oracle.toplink.essentials.internal.helper.ConcurrencyManager mutex)
Set the concurrency manager.

        this.mutex = mutex;
    
public voidsetObject(java.lang.Object object)

        this.object = object;
    
public voidsetOwningMap(oracle.toplink.essentials.internal.identitymaps.IdentityMap map)

        this.mapOwner = map;
    
public voidsetReadTime(long readTime)
INTERNAL: Set the read time of this cache key

        this.readTime = readTime;
        invalidationState = CHECK_INVALIDATION_POLICY;
    
public voidsetRecord(oracle.toplink.essentials.sessions.Record newRecord)

        this.record = newRecord;
    
public voidsetWrapper(java.lang.Object wrapper)

        this.wrapper = wrapper;
    
public voidsetWriteLockValue(java.lang.Object writeLockValue)

        this.writeLockValue = writeLockValue;
    
public java.lang.StringtoString()

        int hashCode = 0;
        if (getObject() != null) {
            hashCode = getObject().hashCode();
        }

        return "[" + getKey() + ": " + hashCode + ": " + getWriteLockValue() + ": " + getReadTime() + ": " + getObject() + "]";
    
public voidupdateAccess()
Notifies that cache key that it has been accessed. Allows the LRU sub-cache to be maintained.

        // Nothing required by default.