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

net.sf.ehcache.transaction.xa.XATransactionContext Maven / Gradle / Ivy

Go to download

Ehcache is an open source, standards-based cache used to boost performance, offload the database and simplify scalability. Ehcache is robust, proven and full-featured and this has made it the most widely-used Java-based cache.

There is a newer version: 2.10.9.2
Show newest version
/**
 *  Copyright Terracotta, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.sf.ehcache.transaction.xa;

import net.sf.ehcache.Element;
import net.sf.ehcache.store.Store;
import net.sf.ehcache.transaction.xa.commands.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * An XATransactionContext represents the data local to a Transaction that involves a transactional Cache.

* It will queue operations ({@link Command Commands}), filter read operations on the cache (as for * returning null on a get on a "to be removed" key).

* * @author Ludovic Orban */ public class XATransactionContext { private static final Logger LOG = LoggerFactory.getLogger(XATransactionContext.class.getName()); private final ConcurrentMap commandElements = new ConcurrentHashMap(); private final Set removedKeys = new HashSet(); private final Set addedKeys = new HashSet(); private int sizeModifier; private final Map commands = new LinkedHashMap(); private final Store underlyingStore; /** * Constructor * * @param underlyingStore the underlying store */ public XATransactionContext(Store underlyingStore) { this.underlyingStore = underlyingStore; } /** * Add a command to the current LocalTransactionContext * * @param command Command to be deferred * @param element Element the command impacts, may be null */ public void addCommand(final Command command, final Element element) { Object key = command.getObjectKey(); if (element != null) { commandElements.put(key, element); } else { commandElements.remove(key); } if (command.isPut(key)) { boolean removed = removedKeys.remove(key); boolean added = addedKeys.add(key); if (removed || added && !underlyingStore.containsKey(key)) { sizeModifier++; } } else if (command.isRemove(key)) { removedKeys.add(key); if (addedKeys.remove(key) || underlyingStore.containsKey(key)) { sizeModifier--; } } commands.put(key, command); LOG.debug("XA context added new command [{}], it now contains {} command(s)", command, commands.size()); } /** * All ordered pending commands * * @return List of all pending commands */ public List getCommands() { return new ArrayList(commands.values()); } /** * Filter to get operations on underlying Store.

* Should the key still be transaction local, or locally pending deletion * * @param key the key * @return the potential Element instance for that key */ public Element get(Object key) { return removedKeys.contains(key) ? null : commandElements.get(key); } /** * Queries the local tx context, whether the key is pending removal * * @param key the key pending removal * @return true if key is pending removal */ public boolean isRemoved(Object key) { return removedKeys.contains(key); } /** * Queries the local tx context, whether the key is pending removal * * @return true if key is pending removal */ public Collection getAddedKeys() { return Collections.unmodifiableSet(addedKeys); } /** * getter to all keys pending deletion from the store * * @return list of all keys */ public Collection getRemovedKeys() { return Collections.unmodifiableSet(removedKeys); } /** * The underlying store's size modifier.

* Plus all pending put commands, and minus all pending removals (dependent on whether their in the underlying store) * * @return the modifier to be applied on the {@link net.sf.ehcache.store.Store#getSize()} */ public int getSizeModifier() { return sizeModifier; } @Override public String toString() { return "XATransactionContext with " + commands.size() + " command(s)"; } }