public boolean isTouched(TransactionId transactionId, K key) { XATransactionContext<K, V> transactionContext = transactionContextMap.get(transactionId); if (transactionContext == null) { throw new IllegalStateException("Cannot check for removed key outside of transactional context"); } return transactionContext.touched(key); } }
XATransactionContext<Long, String> xaTransactionContext = getXaTransactionContext(); assertThat(xaTransactionContext.touched(1L), is(false)); assertThat(xaTransactionContext.removed(1L), is(false)); assertThat(xaTransactionContext.updated(1L), is(false)); assertThat(xaTransactionContext.touched(1L), is(true)); assertThat(xaTransactionContext.removed(1L), is(false)); assertThat(xaTransactionContext.updated(1L), is(true)); assertThat(xaTransactionContext.touched(1L), is(true)); assertThat(xaTransactionContext.removed(1L), is(true)); assertThat(xaTransactionContext.updated(1L), is(false)); assertThat(xaTransactionContext.touched(1L), is(true)); assertThat(xaTransactionContext.removed(1L), is(false)); assertThat(xaTransactionContext.updated(1L), is(false));
@Test public void testEvictCommandCannotBeOverridden() { XATransactionContext<Long, String> xaTransactionContext = getXaTransactionContext(); xaTransactionContext.addCommand(1L, new StorePutCommand<>("old", new XAValueHolder<>("new", timeSource.getTimeMillis()))); assertThat(xaTransactionContext.touched(1L), is(true)); assertThat(xaTransactionContext.removed(1L), is(false)); assertThat(xaTransactionContext.updated(1L), is(true)); assertThat(xaTransactionContext.evicted(1L), is(false)); assertThat(xaTransactionContext.newValueHolderOf(1L).get(), equalTo("new")); assertThat(xaTransactionContext.oldValueOf(1L), equalTo("old")); assertThat(xaTransactionContext.newValueOf(1L), equalTo("new")); xaTransactionContext.addCommand(1L, new StoreEvictCommand<>("old")); assertThat(xaTransactionContext.touched(1L), is(true)); assertThat(xaTransactionContext.removed(1L), is(false)); assertThat(xaTransactionContext.updated(1L), is(false)); assertThat(xaTransactionContext.evicted(1L), is(true)); assertThat(xaTransactionContext.newValueHolderOf(1L), is(nullValue())); assertThat(xaTransactionContext.oldValueOf(1L), equalTo("old")); assertThat(xaTransactionContext.newValueOf(1L), is(nullValue())); xaTransactionContext.addCommand(1L, new StorePutCommand<>("old2", new XAValueHolder<>("new2", timeSource.getTimeMillis()))); assertThat(xaTransactionContext.touched(1L), is(true)); assertThat(xaTransactionContext.removed(1L), is(false)); assertThat(xaTransactionContext.updated(1L), is(false)); assertThat(xaTransactionContext.evicted(1L), is(true)); assertThat(xaTransactionContext.newValueHolderOf(1L), is(nullValue())); assertThat(xaTransactionContext.oldValueOf(1L), equalTo("old")); assertThat(xaTransactionContext.newValueOf(1L), is(nullValue())); }
XATransactionContext<Long, String> xaTransactionContext = getXaTransactionContext(); xaTransactionContext.addCommand(1L, new StorePutCommand<>("old", new XAValueHolder<>("new", timeSource.getTimeMillis()))); assertThat(xaTransactionContext.touched(1L), is(true)); assertThat(xaTransactionContext.removed(1L), is(false)); assertThat(xaTransactionContext.updated(1L), is(true)); assertThat(xaTransactionContext.touched(1L), is(true)); assertThat(xaTransactionContext.removed(1L), is(true)); assertThat(xaTransactionContext.updated(1L), is(false)); assertThat(xaTransactionContext.touched(1L), is(true)); assertThat(xaTransactionContext.removed(1L), is(true)); assertThat(xaTransactionContext.updated(1L), is(false)); assertThat(xaTransactionContext.touched(1L), is(true)); assertThat(xaTransactionContext.removed(1L), is(false)); assertThat(xaTransactionContext.updated(1L), is(true));
@Override public boolean containsKey(K key) throws StoreAccessException { checkKey(key); if (getCurrentContext().touched(key)) { return getCurrentContext().newValueHolderOf(key) != null; } ValueHolder<SoftLock<V>> softLockValueHolder = getSoftLockValueHolderFromUnderlyingStore(key); return softLockValueHolder != null && softLockValueHolder.get().getTransactionId() == null && softLockValueHolder.get().getOldValue() != null; }
checkKey(key); XATransactionContext<K, V> currentContext = getCurrentContext(); if (currentContext.touched(key)) { return updateCommandForKey(key, mappingFunction, replaceEqual, currentContext);
checkValue(value); XATransactionContext<K, V> currentContext = getCurrentContext(); if (currentContext.touched(key)) { V oldValue = currentContext.oldValueOf(key); V newValue = currentContext.newValueOf(key);
checkValue(newValue); XATransactionContext<K, V> currentContext = getCurrentContext(); if (currentContext.touched(key)) { V modifiedValue = currentContext.newValueOf(key); if (modifiedValue == null) {
@Override public boolean remove(K key) throws StoreAccessException { checkKey(key); XATransactionContext<K, V> currentContext = getCurrentContext(); if (currentContext.touched(key)) { V oldValue = currentContext.oldValueOf(key); V newValue = currentContext.newValueOf(key); currentContext.addCommand(key, new StoreRemoveCommand<>(oldValue)); return newValue != null; } ValueHolder<SoftLock<V>> softLockValueHolder = getSoftLockValueHolderFromUnderlyingStore(key); boolean status = false; if (softLockValueHolder != null) { SoftLock<V> softLock = softLockValueHolder.get(); if (isInDoubt(softLock)) { currentContext.addCommand(key, new StoreEvictCommand<>(softLock.getOldValue())); } else { status = currentContext.addCommand(key, new StoreRemoveCommand<>(softLock.getOldValue())); } } return status; }
checkKey(key); XATransactionContext<K, V> currentContext = getCurrentContext(); if (currentContext.touched(key)) { V computed = mappingFunction.apply(key, currentContext.newValueOf(key)); XAValueHolder<V> returnValueholder = null;
return new XAValueHolder<>(currentContext.oldValueOf(key), timeSource.getTimeMillis()); boolean updated = currentContext.touched(key);
@Override public PutStatus put(K key, V value) throws StoreAccessException { checkKey(key); checkValue(value); XATransactionContext<K, V> currentContext = getCurrentContext(); if (currentContext.touched(key)) { V oldValue = currentContext.oldValueOf(key); currentContext.addCommand(key, new StorePutCommand<>(oldValue, new XAValueHolder<>(value, timeSource.getTimeMillis()))); return PutStatus.PUT; } ValueHolder<SoftLock<V>> softLockValueHolder = getSoftLockValueHolderFromUnderlyingStore(key); if (softLockValueHolder != null) { SoftLock<V> softLock = softLockValueHolder.get(); if (isInDoubt(softLock)) { currentContext.addCommand(key, new StoreEvictCommand<>(softLock.getOldValue())); } else { if (currentContext.addCommand(key, new StorePutCommand<>(softLock.getOldValue(), new XAValueHolder<>(value, timeSource .getTimeMillis())))) { return PutStatus.PUT; } } } else { if (currentContext.addCommand(key, new StorePutCommand<>(null, new XAValueHolder<>(value, timeSource.getTimeMillis())))) { return PutStatus.PUT; } } return PutStatus.NOOP; }
@Override public ValueHolder<V> putIfAbsent(K key, V value, Consumer<Boolean> put) throws StoreAccessException { checkKey(key); checkValue(value); XATransactionContext<K, V> currentContext = getCurrentContext(); if (currentContext.touched(key)) { V oldValue = currentContext.oldValueOf(key); V newValue = currentContext.newValueOf(key); if (newValue == null) { currentContext.addCommand(key, new StorePutCommand<>(oldValue, new XAValueHolder<>(value, timeSource.getTimeMillis()))); return null; } else { return currentContext.newValueHolderOf(key); } } ValueHolder<SoftLock<V>> softLockValueHolder = getSoftLockValueHolderFromUnderlyingStore(key); if (softLockValueHolder != null) { SoftLock<V> softLock = softLockValueHolder.get(); if (isInDoubt(softLock)) { currentContext.addCommand(key, new StoreEvictCommand<>(softLock.getOldValue())); return null; } else { return new XAValueHolder<>(softLockValueHolder, softLock.getOldValue()); } } else { currentContext.addCommand(key, new StorePutCommand<>(null, new XAValueHolder<>(value, timeSource.getTimeMillis()))); return null; } }
checkValue(value); XATransactionContext<K, V> currentContext = getCurrentContext(); if (currentContext.touched(key)) { V newValue = currentContext.newValueOf(key); if (newValue == null) {
public boolean isTouched(TransactionId transactionId, K key) { XATransactionContext<K, V> transactionContext = transactionContextMap.get(transactionId); if (transactionContext == null) { throw new IllegalStateException("Cannot check for removed key outside of transactional context"); } return transactionContext.touched(key); } }
@Override public boolean containsKey(K key) throws StoreAccessException { checkKey(key); if (getCurrentContext().touched(key)) { return getCurrentContext().newValueHolderOf(key) != null; } ValueHolder<SoftLock<V>> softLockValueHolder = getSoftLockValueHolderFromUnderlyingStore(key); return softLockValueHolder != null && softLockValueHolder.get().getTransactionId() == null && softLockValueHolder.get().getOldValue() != null; }
checkKey(key); XATransactionContext<K, V> currentContext = getCurrentContext(); if (currentContext.touched(key)) { return updateCommandForKey(key, mappingFunction, replaceEqual, currentContext);
@Override public boolean remove(K key) throws StoreAccessException { checkKey(key); XATransactionContext<K, V> currentContext = getCurrentContext(); if (currentContext.touched(key)) { V oldValue = currentContext.oldValueOf(key); V newValue = currentContext.newValueOf(key); currentContext.addCommand(key, new StoreRemoveCommand<>(oldValue)); return newValue != null; } ValueHolder<SoftLock<V>> softLockValueHolder = getSoftLockValueHolderFromUnderlyingStore(key); boolean status = false; if (softLockValueHolder != null) { SoftLock<V> softLock = softLockValueHolder.get(); if (isInDoubt(softLock)) { currentContext.addCommand(key, new StoreEvictCommand<>(softLock.getOldValue())); } else { status = currentContext.addCommand(key, new StoreRemoveCommand<>(softLock.getOldValue())); } } return status; }
@Override public ValueHolder<V> putIfAbsent(K key, V value) throws StoreAccessException { checkKey(key); checkValue(value); XATransactionContext<K, V> currentContext = getCurrentContext(); if (currentContext.touched(key)) { V oldValue = currentContext.oldValueOf(key); V newValue = currentContext.newValueOf(key); if (newValue == null) { currentContext.addCommand(key, new StorePutCommand<>(oldValue, new XAValueHolder<>(value, timeSource.getTimeMillis()))); return null; } else { return currentContext.newValueHolderOf(key); } } ValueHolder<SoftLock<V>> softLockValueHolder = getSoftLockValueHolderFromUnderlyingStore(key); if (softLockValueHolder != null) { SoftLock<V> softLock = softLockValueHolder.get(); if (isInDoubt(softLock)) { currentContext.addCommand(key, new StoreEvictCommand<>(softLock.getOldValue())); return null; } else { return new XAValueHolder<>(softLockValueHolder, softLock.getOldValue()); } } else { currentContext.addCommand(key, new StorePutCommand<>(null, new XAValueHolder<>(value, timeSource.getTimeMillis()))); return null; } }
@Override public PutStatus put(K key, V value) throws StoreAccessException { checkKey(key); checkValue(value); XATransactionContext<K, V> currentContext = getCurrentContext(); if (currentContext.touched(key)) { V oldValue = currentContext.oldValueOf(key); currentContext.addCommand(key, new StorePutCommand<>(oldValue, new XAValueHolder<>(value, timeSource.getTimeMillis()))); return PutStatus.PUT; } ValueHolder<SoftLock<V>> softLockValueHolder = getSoftLockValueHolderFromUnderlyingStore(key); if (softLockValueHolder != null) { SoftLock<V> softLock = softLockValueHolder.get(); if (isInDoubt(softLock)) { currentContext.addCommand(key, new StoreEvictCommand<>(softLock.getOldValue())); } else { if (currentContext.addCommand(key, new StorePutCommand<>(softLock.getOldValue(), new XAValueHolder<>(value, timeSource .getTimeMillis())))) { return PutStatus.PUT; } } } else { if (currentContext.addCommand(key, new StorePutCommand<>(null, new XAValueHolder<>(value, timeSource.getTimeMillis())))) { return PutStatus.PUT; } } return PutStatus.NOOP; }