Skip to content

Commit

Permalink
Polish for Issue spring-projects#2655 and PR spring-projects#2672.
Browse files Browse the repository at this point in the history
See spring-projects#2655
Original pull request: spring-projects#2672
  • Loading branch information
jxblum committed Oct 11, 2023
1 parent 5e36814 commit 420d314
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public Mono<Long> add(K key, Map<V, Point> memberCoordinateMap) {

Mono<List<GeoLocation<ByteBuffer>>> serializedList = Flux
.fromIterable(() -> memberCoordinateMap.entrySet().iterator())
.map(entry -> new GeoLocation<>(rawValue(entry.getKey()), entry.getValue())).collectList();
.map(entry -> new GeoLocation<>(rawValue(entry.getKey()), entry.getValue()))
.collectList();

return serializedList.flatMap(list -> geoCommands.geoAdd(rawKey(key), list));
});
Expand All @@ -106,7 +107,8 @@ public Mono<Long> add(K key, Iterable<GeoLocation<V>> geoLocations) {
return createMono(geoCommands -> {

Mono<List<GeoLocation<ByteBuffer>>> serializedList = Flux.fromIterable(geoLocations)
.map(location -> new GeoLocation<>(rawValue(location.getName()), location.getPoint())).collectList();
.map(location -> new GeoLocation<>(rawValue(location.getName()), location.getPoint()))
.collectList();

return serializedList.flatMap(list -> geoCommands.geoAdd(rawKey(key), list));
});
Expand Down Expand Up @@ -220,7 +222,7 @@ public Flux<GeoResult<GeoLocation<V>>> radius(K key, V member, double radius) {

return createFlux(geoCommands ->
geoCommands.geoRadiusByMember(rawKey(key), rawValue(member), new Distance(radius)) //
.map(this::readGeoResult));
.map(this::readGeoResult));
}

@Override
Expand Down Expand Up @@ -265,7 +267,7 @@ public Mono<Boolean> delete(K key) {

Assert.notNull(key, "Key must not be null");

return template.doCreateMono(connection -> connection.keyCommands().del(rawKey(key))).map(l -> l != 0);
return template.doCreateMono(connection -> connection.keyCommands().del(rawKey(key))).map(count -> count != 0);
}

@Override
Expand All @@ -274,10 +276,11 @@ public Flux<GeoResult<GeoLocation<V>>> search(K key, GeoReference<V> reference,

Assert.notNull(key, "Key must not be null");
Assert.notNull(reference, "GeoReference must not be null");

GeoReference<ByteBuffer> rawReference = getGeoReference(reference);

return createFlux(geoCommands -> geoCommands
.geoSearch(rawKey(key), rawReference, geoPredicate, args).map(this::readGeoResult));
return createFlux(geoCommands -> geoCommands.geoSearch(rawKey(key), rawReference, geoPredicate, args)
.map(this::readGeoResult));
}

@Override
Expand All @@ -286,6 +289,7 @@ public Mono<Long> searchAndStore(K key, K destKey, GeoReference<V> reference,

Assert.notNull(key, "Key must not be null");
Assert.notNull(reference, "GeoReference must not be null");

GeoReference<ByteBuffer> rawReference = getGeoReference(reference);

return createMono(geoCommands -> geoCommands.geoSearchStore(rawKey(destKey), rawKey(key),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
*
* @author Mark Paluch
* @author Christoph Strobl
* @author John Blum
* @since 2.0
*/
class DefaultReactiveHashOperations<H, HK, HV> implements ReactiveHashOperations<H, HK, HV> {
Expand All @@ -62,7 +63,8 @@ public Mono<Long> remove(H key, Object... hashKeys) {
Assert.noNullElements(hashKeys, "Hash keys must not contain null elements");

return createMono(hashCommands -> Flux.fromArray(hashKeys) //
.map(o -> (HK) o).map(this::rawHashKey) //
.map(hashKey -> (HK) hashKey)
.map(this::rawHashKey) //
.collectList() //
.flatMap(hks -> hashCommands.hDel(rawKey(key), hks)));
}
Expand All @@ -84,8 +86,8 @@ public Mono<HV> get(H key, Object hashKey) {
Assert.notNull(key, "Key must not be null");
Assert.notNull(hashKey, "Hash key must not be null");

return createMono(hashCommands ->
hashCommands.hGet(rawKey(key), rawHashKey((HK) hashKey)).map(this::readHashValue));
return createMono(hashCommands -> hashCommands.hGet(rawKey(key), rawHashKey((HK) hashKey))
.map(this::readHashValue));
}

@Override
Expand All @@ -107,8 +109,7 @@ public Mono<Long> increment(H key, HK hashKey, long delta) {
Assert.notNull(key, "Key must not be null");
Assert.notNull(hashKey, "Hash key must not be null");

return template.doCreateMono(connection -> connection //
.numberCommands() //
return template.doCreateMono(connection -> connection.numberCommands()
.hIncrBy(rawKey(key), rawHashKey(hashKey), delta));
}

Expand All @@ -118,8 +119,7 @@ public Mono<Double> increment(H key, HK hashKey, double delta) {
Assert.notNull(key, "Key must not be null");
Assert.notNull(hashKey, "Hash key must not be null");

return template.doCreateMono(connection -> connection //
.numberCommands() //
return template.doCreateMono(connection -> connection.numberCommands()
.hIncrBy(rawKey(key), rawHashKey(hashKey), delta));
}

Expand All @@ -128,34 +128,35 @@ public Mono<HK> randomKey(H key) {

Assert.notNull(key, "Key must not be null");

return template.doCreateMono(connection -> connection //
.hashCommands().hRandField(rawKey(key))).map(this::readRequiredHashKey);
return template.doCreateMono(connection -> connection.hashCommands().hRandField(rawKey(key)))
.map(this::readRequiredHashKey);
}

@Override
public Mono<Map.Entry<HK, HV>> randomEntry(H key) {

Assert.notNull(key, "Key must not be null");

return createMono(hashCommands ->hashCommands.hRandFieldWithValues(rawKey(key))).map(this::deserializeHashEntry);
return createMono(hashCommands -> hashCommands.hRandFieldWithValues(rawKey(key)))
.map(this::deserializeHashEntry);
}

@Override
public Flux<HK> randomKeys(H key, long count) {

Assert.notNull(key, "Key must not be null");

return template.doCreateFlux(connection -> connection //
.hashCommands().hRandField(rawKey(key), count)).map(this::readRequiredHashKey);
return template.doCreateFlux(connection -> connection.hashCommands().hRandField(rawKey(key), count))
.map(this::readRequiredHashKey);
}

@Override
public Flux<Map.Entry<HK, HV>> randomEntries(H key, long count) {

Assert.notNull(key, "Key must not be null");

return template.doCreateFlux(connection -> connection //
.hashCommands().hRandFieldWithValues(rawKey(key), count)).map(this::deserializeHashEntry);
return template.doCreateFlux(connection -> connection.hashCommands().hRandFieldWithValues(rawKey(key), count))
.map(this::deserializeHashEntry);
}

@Override
Expand Down Expand Up @@ -211,7 +212,7 @@ public Flux<HV> values(H key) {

Assert.notNull(key, "Key must not be null");

return createFlux(connection -> connection.hVals(rawKey(key)) //
return createFlux(hashCommands -> hashCommands.hVals(rawKey(key)) //
.map(this::readRequiredHashValue));
}

Expand Down Expand Up @@ -278,28 +279,28 @@ private HK readRequiredHashKey(ByteBuffer buffer) {

HK hashKey = readHashKey(buffer);

if (hashKey == null) {
throw new InvalidDataAccessApiUsageException("Deserialized hash key is null");
if (hashKey != null) {
return hashKey;
}

return hashKey;
throw new InvalidDataAccessApiUsageException("Deserialized hash key is null");
}

@SuppressWarnings("unchecked")
@Nullable
private HV readHashValue(@Nullable ByteBuffer value) {
return (HV) (value == null ? null : serializationContext.getHashValueSerializationPair().read(value));
return value != null ? (HV) serializationContext.getHashValueSerializationPair().read(value) : null;
}

private HV readRequiredHashValue(ByteBuffer buffer) {

HV hashValue = readHashValue(buffer);

if (hashValue == null) {
throw new InvalidDataAccessApiUsageException("Deserialized hash value is null");
if (hashValue != null) {
return hashValue;
}

return hashValue;
throw new InvalidDataAccessApiUsageException("Deserialized hash value is null");
}

private Map.Entry<HK, HV> deserializeHashEntry(Map.Entry<ByteBuffer, ByteBuffer> source) {
Expand All @@ -309,9 +310,11 @@ private Map.Entry<HK, HV> deserializeHashEntry(Map.Entry<ByteBuffer, ByteBuffer>
private List<HV> deserializeHashValues(List<ByteBuffer> source) {

List<HV> values = new ArrayList<>(source.size());

for (ByteBuffer byteBuffer : source) {
values.add(readHashValue(byteBuffer));
}

return values;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,12 @@ private V readValue(ByteBuffer buffer) {

private V readRequiredValue(ByteBuffer buffer) {

V v = readValue(buffer);
V value = readValue(buffer);

if (v == null) {
throw new InvalidDataAccessApiUsageException("Deserialized list value is null");
if (value != null) {
return value;
}

return v;
throw new InvalidDataAccessApiUsageException("Deserialized list value is null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* @author Mark Paluch
* @author Christoph Strobl
* @author Roman Bezpalko
* @author John Blum
* @since 2.0
*/
class DefaultReactiveSetOperations<K, V> implements ReactiveSetOperations<K, V> {
Expand Down Expand Up @@ -424,12 +425,12 @@ private V readValue(ByteBuffer buffer) {

private V readRequiredValue(ByteBuffer buffer) {

V v = readValue(buffer);
V value = readValue(buffer);

if (v == null) {
throw new InvalidDataAccessApiUsageException("Deserialized set value is null");
if (value != null) {
return value;
}

return v;
throw new InvalidDataAccessApiUsageException("Deserialized set value is null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* @author Mark Paluch
* @author Christoph Strobl
* @author Jiahe Cai
* @author John Blum
* @since 2.0
*/
class DefaultReactiveValueOperations<K, V> implements ReactiveValueOperations<K, V> {
Expand Down Expand Up @@ -336,13 +337,13 @@ private V readValue(ByteBuffer buffer) {

private V readRequiredValue(ByteBuffer buffer) {

V v = readValue(buffer);
V value = readValue(buffer);

if (v == null) {
throw new InvalidDataAccessApiUsageException("Deserialized value is null");
if (value != null) {
return value;
}

return v;
throw new InvalidDataAccessApiUsageException("Deserialized value is null");
}

private SerializationPair<String> stringSerializationPair() {
Expand Down Expand Up @@ -372,5 +373,4 @@ private List<V> deserializeValues(List<ByteBuffer> source) {

return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.data.redis.util.RedisAssertions;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -744,13 +745,8 @@ private V readValue(ByteBuffer buffer) {

private V readRequiredValue(ByteBuffer buffer) {

V v = readValue(buffer);

if (v == null) {
throw new InvalidDataAccessApiUsageException("Deserialized sorted set value is null");
}

return v;
return RedisAssertions.requireNonNull(readValue(buffer),
() -> new InvalidDataAccessApiUsageException("Deserialized sorted set value is null"));
}

private TypedTuple<V> readTypedTuple(Tuple raw) {
Expand Down
Loading

0 comments on commit 420d314

Please sign in to comment.