Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.hibernate.testing.cache;
  
  import java.util.UUID;
  
 

Author(s):
Strong Liu
 
 	private static final CoreMessageLogger LOG = Logger.getMessageLogger(
 	);
 	private final UUID uuid = UUID.randomUUID();
 	private final AtomicLong nextLockId = new AtomicLong();
Returns null if the item is not readable. Locked items are not readable, nor are items created after the start of this transaction.
 
 	public final Object get(Object keylong txTimestampthrows CacheException {
 		.debugf"getting key[%s] from region[%s]"keygetInternalRegion().getName() );
 		try {
 			Lockable item = (LockablegetInternalRegion().getkey );
 
 			boolean readable = item != null && item.isReadabletxTimestamp );
 			if ( readable ) {
 				.debugf"hit key[%s] in region[%s]"keygetInternalRegion().getName() );
 				return item.getValue();
 			}
 			else {
 				if ( item == null ) {
 					.debugf"miss key[%s] in region[%s]"keygetInternalRegion().getName());
 				} else {
 					.debugf"hit key[%s] in region[%s], but it is unreadable"keygetInternalRegion().getName() );
 				}
 				return null;
 			}
 		}
 		finally {
 		}
 	}
 
Returns false and fails to put the value if there is an existing un-writeable item mapped to this key.
 
 	public final boolean putFromLoad(Object keyObject valuelong txTimestampObject versionboolean minimalPutOverride)
 			throws CacheException {
 		try {
 			.debugf"putting key[%s] -> value[%s] into region[%s]"keyvaluegetInternalRegion().getName() );
 			Lockable item = (LockablegetInternalRegion().getkey );
 			boolean writeable = item == null || item.isWriteabletxTimestampversiongetVersionComparator() );
 			if ( writeable ) {
 				.debugf"putting key[%s] -> value[%s] into region[%s] success"keyvaluegetInternalRegion().getName() );
 				getInternalRegion().putkeynew ItemvalueversiongetInternalRegion().nextTimestamp() ) );
 				return true;
 			}
 			else {
 				.debugf"putting key[%s] -> value[%s] into region[%s] fail due to it is unwriteable"keyvaluegetInternalRegion().getName() );
 				return false;
 			}
 		}
 		finally {
 		}
 	}

Soft-lock a cache item.
 
 	public final SoftLock lockItem(Object keyObject versionthrows CacheException {
 
 		try {
 			.debugf"locking key[%s] in region[%s]"keygetInternalRegion().getName() );
 			Lockable item = (LockablegetInternalRegion().getkey );
 			final Lock lock = ( item == null ) ? new LocktimeoutnextLockId(), version ) : item.lock(
 					timeout,
			);
			getInternalRegion().putkeylock );
			return lock;
		}
		finally {
		}
	}

Soft-unlock a cache item.
	public final void unlockItem(Object keySoftLock lockthrows CacheException {
		try {
			.debugf"unlocking key[%s] in region[%s]"keygetInternalRegion().getName() );
			Lockable item = (LockablegetInternalRegion().getkey );
			if ( ( item != null ) && item.isUnlockablelock ) ) {
				decrementLockkey, (Lockitem );
			}
			else {
				handleLockExpirykeyitem );
			}
		}
		finally {
		}
	}
	private long nextLockId() {
	}

Unlock and re-put the given key, lock combination.
	protected void decrementLock(Object keyLock lock) {
		getInternalRegion().putkeylock );
	}

Handle the timeout of a previous lock mapped to this key
	protected void handleLockExpiry(Object keyLockable lock) {
		// create new lock that times out immediately
		Lock newLock = new Lockts.getAndIncrement(), null );
		newLock.unlockts );
		getInternalRegion().putkeynewLock );
	}

Interface type implemented by all wrapper objects in the cache.
	protected static interface Lockable {

Returns true if the enclosed value can be read by a transaction started at the given time.
		public boolean isReadable(long txTimestamp);

Returns true if the enclosed value can be replaced with one of the given version by a transaction started at the given time.
		public boolean isWriteable(long txTimestampObject versionComparator versionComparator);

Returns the enclosed value.
		public Object getValue();

Returns true if the given lock can be unlocked using the given SoftLock instance as a handle.
		public boolean isUnlockable(SoftLock lock);

Locks this entry, stamping it with the UUID and lockId given, with the lock timeout occuring at the specified time. The returned Lock object can be used to unlock the entry in the future.
		public Lock lock(long timeoutUUID uuidlong lockId);
	}

Wrapper type representing unlocked items.
	protected final static class Item implements SerializableLockable {
		private static final long serialVersionUID = 1L;
		private final Object value;
		private final Object version;
		private final long timestamp;

Creates an unlocked item wrapping the given value with a version and creation timestamp.
		Item(Object valueObject versionlong timestamp) {
			this. = value;
			this. = version;
			this. = timestamp;
		}
		public boolean isReadable(long txTimestamp) {
			return txTimestamp > ;
		}
		public boolean isWriteable(long txTimestampObject newVersionComparator versionComparator) {
			return  != null && versionComparator.comparenewVersion ) < 0;
		}
		public Object getValue() {
			return ;
		}
		public boolean isUnlockable(SoftLock lock) {
			return false;
		}
		public Lock lock(long timeoutUUID uuidlong lockId) {
			return new LocktimeoutuuidlockId );
		}
	}

Wrapper type representing locked items.
	protected final static class Lock implements SerializableLockableSoftLock {
		private static final long serialVersionUID = 2L;
		private final UUID sourceUuid;
		private final long lockId;
		private final Object version;
		private long timeout;
		private boolean concurrent;
		private int multiplicity = 1;
		private long unlockTimestamp;

Creates a locked item with the given identifiers and object version.
		Lock(long timeoutUUID sourceUuidlong lockIdObject version) {
			this. = timeout;
			this. = lockId;
			this. = version;
			this. = sourceUuid;
		}
		public boolean isReadable(long txTimestamp) {
			return false;
		}
		public boolean isWriteable(long txTimestampObject newVersionComparator versionComparator) {
			if ( txTimestamp >  ) {
				// if timedout then allow write
				return true;
			}
			if (  > 0 ) {
				// if still locked then disallow write
				return false;
			}
			return  == null ? txTimestamp >  : versionComparator.compare(
					newVersion
			) < 0;
		}
		public Object getValue() {
			return null;
		}
		public boolean isUnlockable(SoftLock lock) {
			return equalslock );
		}
		public boolean equals(Object o) {
			if ( o == this ) {
				return true;
			}
			else if ( o instanceof Lock ) {
				return (  == ( (Locko ). ) && .equals( ( (Locko ). );
			}
			else {
				return false;
			}
		}
		public int hashCode() {
			int hash = (  != null ? .hashCode() : 0 );
			int temp = (int;
			for ( int i = 1; i < . / .i++ ) {
				temp ^= (  >>> ( i * . ) );
			}
			return hash + temp;
		}

Returns true if this Lock has been concurrently locked by more than one transaction.
		public boolean wasLockedConcurrently() {
			return ;
		}
		public Lock lock(long timeoutUUID uuidlong lockId) {
			 = true;
			this. = timeout;
			return this;
		}

Unlocks this Lock, and timestamps the unlock event.
		public void unlock(long timestamp) {
			if ( -- == 0 ) {
				 = timestamp;
			}
		}
		public String toString() {
			StringBuilder sb = new StringBuilder"Lock Source-UUID:" +  + " Lock-ID:" +  );
			return sb.toString();
		}
	}
New to GrepCode? Check out our FAQ X