private Element getCurrentElement(final Object key, final XATransactionContext context) { Element previous = context.get(key); if (previous == null && !context.isRemoved(key)) { previous = getQuietFromUnderlyingStore(key); } return previous; }
/** {@inheritDoc} */ public Element replace(Element element) throws NullPointerException { LOG.debug("cache {} replace1 {}", cache.getName(), element); XATransactionContext context = getOrCreateTransactionContext(); Element previous = getCurrentElement(element.getKey(), context); if (previous != null) { Element oldElement = getQuietFromUnderlyingStore(element.getObjectKey()); Element elementForWrite = copyElementForWrite(element); context.addCommand(new StorePutCommand(oldElement, elementForWrite), elementForWrite); } return copyElementForRead(previous); }
/** {@inheritDoc} */ public Element getQuiet(Object key) { LOG.debug("cache {} getQuiet {}", cache.getName(), key); XATransactionContext context = getTransactionContext(); Element element; if (context == null) { element = getQuietFromUnderlyingStore(key); } else { element = context.get(key); if (element == null && !context.isRemoved(key)) { element = getQuietFromUnderlyingStore(key); } } return copyElementForRead(element); }
/** {@inheritDoc} */ public boolean replace(Element old, Element element, ElementValueComparator comparator) throws NullPointerException, IllegalArgumentException { LOG.debug("cache {} replace2 {}", cache.getName(), element); XATransactionContext context = getOrCreateTransactionContext(); Element previous = getCurrentElement(element.getKey(), context); boolean replaced = false; if (previous != null && comparator.equals(previous, copyElementForWrite(old))) { Element oldElement = getQuietFromUnderlyingStore(element.getObjectKey()); Element elementForWrite = copyElementForWrite(element); context.addCommand(new StorePutCommand(oldElement, elementForWrite), elementForWrite); replaced = true; } return replaced; }
/** {@inheritDoc} */ public Element removeElement(Element element, ElementValueComparator comparator) throws NullPointerException { LOG.debug("cache {} removeElement {}", cache.getName(), element); XATransactionContext context = getOrCreateTransactionContext(); Element previous = getCurrentElement(element.getKey(), context); Element elementForWrite = copyElementForWrite(element); if (previous != null && comparator.equals(previous, elementForWrite)) { Element oldElement = getQuietFromUnderlyingStore(element.getObjectKey()); context.addCommand( new StoreRemoveCommand(element.getObjectKey(), oldElement), elementForWrite); return copyElementForRead(previous); } return null; }
private boolean internalPut(final StorePutCommand putCommand) { final Element element = putCommand.getElement(); boolean isNull; if (element == null) { return true; } XATransactionContext context = getOrCreateTransactionContext(); // In case this key is currently being updated... isNull = underlyingStore.get(element.getKey()) == null; if (isNull) { isNull = context.get(element.getKey()) == null; } context.addCommand(putCommand, element); return isNull; }
/** {@inheritDoc} */ public int getTerracottaClusteredSize() { try { Transaction transaction = transactionManagerLookup.getTransactionManager().getTransaction(); if (transaction == null) { return underlyingStore.getTerracottaClusteredSize(); } } catch (SystemException se) { throw new TransactionException("cannot get the current transaction", se); } LOG.debug("cache {} getTerracottaClusteredSize", cache.getName()); XATransactionContext context = getOrCreateTransactionContext(); int size = underlyingStore.getTerracottaClusteredSize(); return size + context.getSizeModifier(); }
/** {@inheritDoc} */ public List getKeys() { LOG.debug("cache {} getKeys", cache.getName()); XATransactionContext context = getOrCreateTransactionContext(); Set<Object> keys = new LargeSet<Object>() { @Override public int sourceSize() { return underlyingStore.getSize(); } @Override public Iterator<Object> sourceIterator() { return underlyingStore.getKeys().iterator(); } }; keys.addAll(context.getAddedKeys()); keys.removeAll(context.getRemovedKeys()); return new SetAsList<Object>(keys); }
/** {@inheritDoc} */ public boolean containsKey(Object key) { LOG.debug("cache {} containsKey", cache.getName(), key); XATransactionContext context = getOrCreateTransactionContext(); return !context.isRemoved(key) && (context.getAddedKeys().contains(key) || underlyingStore.containsKey(key)); }
/** {@inheritDoc} */ public int getSize() { LOG.debug("cache {} getSize", cache.getName()); XATransactionContext context = getOrCreateTransactionContext(); int size = underlyingStore.getSize(); return Math.max(0, size + context.getSizeModifier()); }