All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.context.impl.NonTxInvocationContext Maven / Gradle / Ivy

package org.infinispan.context.impl;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

import org.infinispan.commons.util.CollectionFactory;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.remoting.transport.Address;

/**
 * Context to be used for non transactional calls, both remote and local.
 *
 * @author [email protected]
 * @since 4.0
 */
public class NonTxInvocationContext extends AbstractInvocationContext {

   private static final int INITIAL_CAPACITY = 4;

   private final Map lookedUpEntries;
   private Set lockedKeys;
   private Object lockOwner;


   public NonTxInvocationContext(int numEntries, Address origin) {
      super(origin);
      lookedUpEntries = CollectionFactory.makeMap(CollectionFactory.computeCapacity(numEntries));
   }

   public NonTxInvocationContext(Address origin) {
      super(origin);
      lookedUpEntries = CollectionFactory.makeMap(INITIAL_CAPACITY);
   }

   @Override
   public CacheEntry lookupEntry(Object k) {
      return lookedUpEntries.get(k);
   }

   @Override
   public void removeLookedUpEntry(Object key) {
      lookedUpEntries.remove(key);
   }

   @Override
   public void putLookedUpEntry(Object key, CacheEntry e) {
      lookedUpEntries.put(key, e);
   }

   @Override
   @SuppressWarnings("unchecked")
   public Map getLookedUpEntries() {
      return (Map)
            (lookedUpEntries == null ?
                   Collections.emptyMap() : lookedUpEntries);
   }

   @Override
   public boolean isInTxScope() {
      return false;
   }

   @Override
   public Object getLockOwner() {
      return lockOwner;
   }

   @Override
   public void setLockOwner(Object lockOwner) {
      this.lockOwner = lockOwner;
   }

   @Override
   public NonTxInvocationContext clone() {
      NonTxInvocationContext dolly = (NonTxInvocationContext) super.clone();
      dolly.lookedUpEntries.putAll(lookedUpEntries);
      return dolly;
   }

   @Override
   public void addLockedKey(Object key) {
      if (lockedKeys == null)
         lockedKeys = CollectionFactory.makeSet(INITIAL_CAPACITY);

      lockedKeys.add(key);
   }

   @Override
   public Set getLockedKeys() {
      return lockedKeys == null ? Collections.emptySet() : lockedKeys;
   }

   @Override
   public void clearLockedKeys() {
      lockedKeys = null;
   }
}