Skip to content

DATAREDIS-719 - Rework FutureResult and implementations for Jedis/Lettuce. #289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.DATAREDIS-719-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@
*/
package org.springframework.data.redis.connection;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -725,7 +734,7 @@ public List<byte[]> mGet(byte[]... keys) {
*/
@Override
public Boolean mSet(Map<byte[], byte[]> tuple) {
return delegate.mSet(tuple);
return convertAndReturn(delegate.mSet(tuple), identityConverter);
}

/*
Expand Down Expand Up @@ -923,7 +932,7 @@ public void select(int dbIndex) {
*/
@Override
public Boolean set(byte[] key, byte[] value) {
return delegate.set(key, value);
return convertAndReturn(delegate.set(key, value), identityConverter);
}

/*
Expand All @@ -932,7 +941,7 @@ public Boolean set(byte[] key, byte[] value) {
*/
@Override
public Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption option) {
return delegate.set(key, value, expiration, option);
return convertAndReturn(delegate.set(key, value, expiration, option), identityConverter);
}

/*
Expand All @@ -959,7 +968,7 @@ public void setConfig(String param, String value) {
*/
@Override
public Boolean setEx(byte[] key, long seconds, byte[] value) {
return delegate.setEx(key, seconds, value);
return convertAndReturn(delegate.setEx(key, seconds, value), identityConverter);
}

/*
Expand All @@ -968,7 +977,7 @@ public Boolean setEx(byte[] key, long seconds, byte[] value) {
*/
@Override
public Boolean pSetEx(byte[] key, long milliseconds, byte[] value) {
return delegate.pSetEx(key, milliseconds, value);
return convertAndReturn(delegate.pSetEx(key, milliseconds, value), identityConverter);
}

/*
Expand Down Expand Up @@ -2106,7 +2115,7 @@ public Boolean mSetNXString(Map<String, String> tuple) {
*/
@Override
public Boolean mSetString(Map<String, String> tuple) {
return delegate.mSet(serialize(tuple));
return mSet(serialize(tuple));
}

/*
Expand Down Expand Up @@ -2241,7 +2250,7 @@ public Long sDiffStore(String destKey, String... keys) {
*/
@Override
public Boolean set(String key, String value) {
return delegate.set(serialize(key), serialize(value));
return set(serialize(key), serialize(value));
}

/*
Expand All @@ -2268,7 +2277,7 @@ public Boolean setBit(String key, long offset, boolean value) {
*/
@Override
public Boolean setEx(String key, long seconds, String value) {
return delegate.setEx(serialize(key), seconds, serialize(value));
return setEx(serialize(key), seconds, serialize(value));
}

/*
Expand Down Expand Up @@ -3441,7 +3450,7 @@ public Set<byte[]> zRangeByLex(byte[] key, Range range, Limit limit) {
*/
@Override
public Set<String> zRangeByLex(String key) {
return this.zRangeByLex(key, Range.unbounded());
return zRangeByLex(key, Range.unbounded());
}

/*
Expand All @@ -3450,7 +3459,7 @@ public Set<String> zRangeByLex(String key) {
*/
@Override
public Set<String> zRangeByLex(String key, Range range) {
return this.zRangeByLex(key, range, null);
return zRangeByLex(key, range, null);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.data.redis.connection;

import java.util.function.Supplier;

import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.Nullable;

Expand All @@ -24,27 +26,64 @@
* @author Jennifer Hickey
* @author Christoph Strobl
* @author Mark Paluch
* @param <T> The data type of the object that holds the future result (usually of type Future)
* @param <T> The data type of the object that holds the future result (usually type of the
* {@link java.util.concurrent.Future} or response wrapper).
*/
public abstract class FutureResult<T> {

protected T resultHolder;
private T resultHolder;
private final Supplier<?> defaultConversionResult;

protected boolean status = false;
private boolean status = false;

@SuppressWarnings("rawtypes") //
protected @Nullable Converter converter;
protected Converter converter;

/**
* Create new {@link FutureResult} for given object actually holding the result itself.
*
* @param resultHolder must not be {@literal null}.
*/
public FutureResult(T resultHolder) {
this.resultHolder = resultHolder;
this(resultHolder, val -> val);
}

/**
* Create new {@link FutureResult} for given object actually holding the result itself and a converter capable of
* transforming the result via {@link #convert(Object)}.
*
* @param resultHolder must not be {@literal null}.
* @param converter can be {@literal null} and will be defaulted to an identity converter {@code value -> value} to
* preserve the original value.
*/
@SuppressWarnings("rawtypes")
public FutureResult(T resultHolder, Converter converter) {
public FutureResult(T resultHolder, @Nullable Converter converter) {
this(resultHolder, converter, () -> null);
}

/**
* Create new {@link FutureResult} for given object actually holding the result itself and a converter capable of
* transforming the result via {@link #convert(Object)}.
*
* @param resultHolder must not be {@literal null}.
* @param converter can be {@literal null} and will be defaulted to an identity converter {@code value -> value} to
* preserve the original value.
* @param defaultConversionResult must not be {@literal null}.
* @since 2.1
*/
public FutureResult(T resultHolder, @Nullable Converter converter, Supplier<?> defaultConversionResult) {

this.resultHolder = resultHolder;
this.converter = converter;
this.converter = converter != null ? converter : val -> val;
this.defaultConversionResult = defaultConversionResult;
}

/**
* Get the object holding the actual result.
*
* @return never {@literal null}.
* @since 1.1
*/
public T getResultHolder() {
return resultHolder;
}
Expand All @@ -60,14 +99,18 @@ public T getResultHolder() {
public Object convert(@Nullable Object result) {

if (result == null) {
return null;
return computeDefaultResult(null);
}

return (converter != null) ? converter.convert(result) : result;
return computeDefaultResult(converter.convert(result));
}

@SuppressWarnings("rawtypes")
@Nullable
private Object computeDefaultResult(@Nullable Object source) {
return source != null ? source : defaultConversionResult.get();
}

@SuppressWarnings("rawtypes")
public Converter getConverter() {
return converter;
}
Expand All @@ -93,4 +136,12 @@ public void setStatus(boolean status) {
*/
@Nullable
public abstract Object get();

/**
* Indicate whether or not the actual result needs to be {@link #convert(Object) converted} before handing over.
*
* @return {@literal true} if result conversion is required.
* @since 2.1
*/
public abstract boolean conversionRequired();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.springframework.data.redis.connection.convert;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

Expand All @@ -31,13 +30,13 @@
*
* @author Jennifer Hickey
* @author Christoph Strobl
* @author Mark Paluch
* @param <T> The type of {@link FutureResult} of the individual tx operations
*/
public class TransactionResultConverter<T> implements Converter<List<Object>, List<Object>> {

private Queue<FutureResult<T>> txResults = new LinkedList<>();

private Converter<Exception, DataAccessException> exceptionConverter;
private final Queue<FutureResult<T>> txResults;
private final Converter<Exception, DataAccessException> exceptionConverter;

public TransactionResultConverter(Queue<FutureResult<T>> txResults,
Converter<Exception, DataAccessException> exceptionConverter) {
Expand Down Expand Up @@ -71,7 +70,7 @@ public List<Object> convert(List<Object> execResults) {
: new RedisSystemException("Error reading future result.", source);
}
if (!(futureResult.isStatus())) {
convertedResults.add(futureResult.convert(result));
convertedResults.add(futureResult.conversionRequired() ? futureResult.convert(result) : result);
}
}

Expand Down
Loading