DATAREDIS-660 - Adapt to API changes in mapping subsystem.
This commit is contained in:
@@ -23,7 +23,6 @@ import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
@@ -219,10 +218,10 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
|
||||
rdo.setId(converter.getConversionService().convert(id, String.class));
|
||||
|
||||
if (!(item instanceof RedisData)) {
|
||||
KeyValuePersistentProperty idProperty = converter.getMappingContext().getPersistentEntity(item.getClass()).get()
|
||||
.getIdProperty().get();
|
||||
converter.getMappingContext().getPersistentEntity(item.getClass()).get().getPropertyAccessor(item)
|
||||
.setProperty(idProperty, Optional.ofNullable(id));
|
||||
KeyValuePersistentProperty idProperty = converter.getMappingContext()
|
||||
.getRequiredPersistentEntity(item.getClass()).getRequiredIdProperty();
|
||||
converter.getMappingContext().getRequiredPersistentEntity(item.getClass()).getPropertyAccessor(item)
|
||||
.setProperty(idProperty, id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -441,8 +440,8 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
|
||||
|
||||
public void update(final PartialUpdate<?> update) {
|
||||
|
||||
final RedisPersistentEntity<?> entity = this.converter.getMappingContext().getPersistentEntity(update.getTarget())
|
||||
.get();
|
||||
final RedisPersistentEntity<?> entity = this.converter.getMappingContext()
|
||||
.getRequiredPersistentEntity(update.getTarget());
|
||||
|
||||
final String keyspace = entity.getKeySpace();
|
||||
final Object id = update.getId();
|
||||
@@ -632,32 +631,28 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
|
||||
return target;
|
||||
}
|
||||
|
||||
RedisPersistentEntity<?> entity = this.converter.getMappingContext().getPersistentEntity(target.getClass()).get();
|
||||
RedisPersistentEntity<?> entity = this.converter.getMappingContext().getRequiredPersistentEntity(target.getClass());
|
||||
if (entity.hasExplictTimeToLiveProperty()) {
|
||||
|
||||
Optional<RedisPersistentProperty> ttlProperty = entity.getExplicitTimeToLiveProperty();
|
||||
if (!ttlProperty.isPresent()) {
|
||||
RedisPersistentProperty ttlProperty = entity.getExplicitTimeToLiveProperty();
|
||||
if (ttlProperty == null) {
|
||||
return target;
|
||||
}
|
||||
|
||||
final Optional<TimeToLive> ttl = ttlProperty.get().findAnnotation(TimeToLive.class);
|
||||
TimeToLive ttl = ttlProperty.findAnnotation(TimeToLive.class);
|
||||
|
||||
Long timeout = redisOps.execute(new RedisCallback<Long>() {
|
||||
Long timeout = redisOps.execute((RedisCallback<Long>) connection -> {
|
||||
|
||||
@Override
|
||||
public Long doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
|
||||
if (ObjectUtils.nullSafeEquals(TimeUnit.SECONDS, ttl.get().unit())) {
|
||||
return connection.ttl(key);
|
||||
}
|
||||
|
||||
return connection.pTtl(key, ttl.get().unit());
|
||||
if (ObjectUtils.nullSafeEquals(TimeUnit.SECONDS, ttl.unit())) {
|
||||
return connection.ttl(key);
|
||||
}
|
||||
|
||||
return connection.pTtl(key, ttl.unit());
|
||||
});
|
||||
|
||||
if (timeout != null || !ttlProperty.get().getType().isPrimitive()) {
|
||||
entity.getPropertyAccessor(target).setProperty(ttlProperty.get(),
|
||||
Optional.ofNullable(converter.getConversionService().convert(timeout, ttlProperty.get().getType())));
|
||||
if (timeout != null || !ttlProperty.getType().isPrimitive()) {
|
||||
entity.getPropertyAccessor(target).setProperty(ttlProperty,
|
||||
converter.getConversionService().convert(timeout, ttlProperty.getType()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -757,7 +752,7 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link MessageListener} implementation used to capture Redis keypspace notifications. Tries to read a previously
|
||||
* {@link MessageListener} implementation used to capture Redis keyspace notifications. Tries to read a previously
|
||||
* created phantom key {@code keyspace:id:phantom} to provide the expired object as part of the published
|
||||
* {@link RedisKeyExpiredEvent}.
|
||||
*
|
||||
|
||||
@@ -16,8 +16,17 @@
|
||||
package org.springframework.data.redis.core.convert;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -37,11 +46,11 @@ import org.springframework.data.keyvalue.core.mapping.KeySpaceResolver;
|
||||
import org.springframework.data.mapping.Alias;
|
||||
import org.springframework.data.mapping.Association;
|
||||
import org.springframework.data.mapping.AssociationHandler;
|
||||
import org.springframework.data.mapping.MappingException;
|
||||
import org.springframework.data.mapping.PersistentPropertyAccessor;
|
||||
import org.springframework.data.mapping.PreferredConstructor;
|
||||
import org.springframework.data.mapping.PropertyHandler;
|
||||
import org.springframework.data.mapping.context.PersistentPropertyPath;
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
import org.springframework.data.mapping.model.PersistentEntityParameterValueProvider;
|
||||
import org.springframework.data.mapping.model.PropertyValueProvider;
|
||||
import org.springframework.data.redis.core.PartialUpdate;
|
||||
@@ -169,8 +178,12 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
return null;
|
||||
}
|
||||
|
||||
TypeInformation<?> readType = typeMapper.readType(source).orElse(ClassTypeInformation.from(type));
|
||||
final Optional<RedisPersistentEntity<?>> entity = mappingContext.getPersistentEntity(readType);
|
||||
TypeInformation<?> readType = typeMapper.readType(source);
|
||||
if (readType == null) {
|
||||
readType = ClassTypeInformation.from(type);
|
||||
}
|
||||
|
||||
RedisPersistentEntity<?> entity = mappingContext.getPersistentEntity(readType);
|
||||
|
||||
if (customConversions.hasCustomReadTarget(Map.class, readType.getType())) {
|
||||
|
||||
@@ -187,9 +200,8 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
}
|
||||
R instance = (R) conversionService.convert(partial, readType.getType());
|
||||
|
||||
if (entity.isPresent() && entity.get().hasIdProperty()) {
|
||||
entity.get().getPropertyAccessor(instance).setProperty(entity.get().getIdProperty().get(),
|
||||
Optional.ofNullable(source.getId()));
|
||||
if (entity != null && entity.hasIdProperty()) {
|
||||
entity.getPropertyAccessor(instance).setProperty(entity.getRequiredIdProperty(), source.getId());
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
@@ -199,23 +211,22 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
readType.getType());
|
||||
}
|
||||
|
||||
EntityInstantiator instantiator = entityInstantiators.getInstantiatorFor(entity.get());
|
||||
EntityInstantiator instantiator = entityInstantiators.getInstantiatorFor(entity);
|
||||
|
||||
Object instance = instantiator.createInstance((RedisPersistentEntity<RedisPersistentProperty>) entity.get(),
|
||||
new PersistentEntityParameterValueProvider<RedisPersistentProperty>(entity.get(),
|
||||
new ConverterAwareParameterValueProvider(path, source, conversionService),
|
||||
Optional.of(this.conversionService)));
|
||||
Object instance = instantiator.createInstance((RedisPersistentEntity<RedisPersistentProperty>) entity,
|
||||
new PersistentEntityParameterValueProvider<>(entity,
|
||||
new ConverterAwareParameterValueProvider(path, source, conversionService), this.conversionService));
|
||||
|
||||
final PersistentPropertyAccessor accessor = entity.get().getPropertyAccessor(instance);
|
||||
final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(instance);
|
||||
|
||||
entity.get().doWithProperties(new PropertyHandler<RedisPersistentProperty>() {
|
||||
entity.doWithProperties(new PropertyHandler<RedisPersistentProperty>() {
|
||||
|
||||
@Override
|
||||
public void doWithPersistentProperty(RedisPersistentProperty persistentProperty) {
|
||||
|
||||
String currentPath = !path.isEmpty() ? path + "." + persistentProperty.getName() : persistentProperty.getName();
|
||||
|
||||
PreferredConstructor<?, RedisPersistentProperty> constructor = entity.get().getPersistenceConstructor().get();
|
||||
PreferredConstructor<?, RedisPersistentProperty> constructor = entity.getPersistenceConstructor();
|
||||
|
||||
if (constructor.isConstructorParameter(persistentProperty)) {
|
||||
return;
|
||||
@@ -224,29 +235,32 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
if (persistentProperty.isMap()) {
|
||||
|
||||
Map<?, ?> targetValue = null;
|
||||
Class<?> mapValueType = persistentProperty.getMapValueType()
|
||||
.orElseThrow(() -> new IllegalArgumentException("Unable to retrieve MapValueType!"));
|
||||
Class<?> mapValueType = persistentProperty.getMapValueType();
|
||||
|
||||
if (mapValueType == null) {
|
||||
throw new IllegalArgumentException("Unable to retrieve MapValueType!");
|
||||
}
|
||||
|
||||
if (conversionService.canConvert(byte[].class, mapValueType)) {
|
||||
targetValue = readMapOfSimpleTypes(currentPath, persistentProperty.getType(),
|
||||
persistentProperty.getComponentType().get(), mapValueType, source);
|
||||
persistentProperty.getComponentType(), mapValueType, source);
|
||||
} else {
|
||||
targetValue = readMapOfComplexTypes(currentPath, persistentProperty.getType(),
|
||||
persistentProperty.getComponentType().get(), mapValueType, source);
|
||||
persistentProperty.getComponentType(), mapValueType, source);
|
||||
}
|
||||
|
||||
if (targetValue != null) {
|
||||
accessor.setProperty(persistentProperty, Optional.ofNullable(targetValue));
|
||||
accessor.setProperty(persistentProperty, targetValue);
|
||||
}
|
||||
}
|
||||
|
||||
else if (persistentProperty.isCollectionLike()) {
|
||||
|
||||
Object targetValue = readCollectionOrArray(currentPath, persistentProperty.getType(),
|
||||
persistentProperty.getTypeInformation().getComponentType().get().getActualType().getType(),
|
||||
persistentProperty.getTypeInformation().getRequiredComponentType().getActualType().getType(),
|
||||
source.getBucket());
|
||||
if (targetValue != null) {
|
||||
accessor.setProperty(persistentProperty, Optional.ofNullable(targetValue));
|
||||
accessor.setProperty(persistentProperty, targetValue);
|
||||
}
|
||||
|
||||
} else if (persistentProperty.isEntity() && !conversionService.canConvert(byte[].class,
|
||||
@@ -265,28 +279,27 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
|
||||
R val = readInternal(currentPath, targetType, newBucket);
|
||||
|
||||
accessor.setProperty(persistentProperty, Optional.ofNullable(val));
|
||||
accessor.setProperty(persistentProperty, val);
|
||||
} else {
|
||||
|
||||
if (persistentProperty.isIdProperty() && StringUtils.isEmpty(path.isEmpty())) {
|
||||
|
||||
if (source.getBucket().get(currentPath) == null) {
|
||||
accessor.setProperty(persistentProperty,
|
||||
Optional.of(fromBytes(source.getBucket().get(currentPath), persistentProperty.getActualType())));
|
||||
fromBytes(source.getBucket().get(currentPath), persistentProperty.getActualType()));
|
||||
} else {
|
||||
accessor.setProperty(persistentProperty, Optional.ofNullable(source.getId()));
|
||||
accessor.setProperty(persistentProperty, source.getId());
|
||||
}
|
||||
}
|
||||
|
||||
Class<?> typeToUse = getTypeHint(currentPath, source.getBucket(), persistentProperty.getActualType());
|
||||
accessor.setProperty(persistentProperty,
|
||||
Optional.ofNullable(fromBytes(source.getBucket().get(currentPath), typeToUse)));
|
||||
accessor.setProperty(persistentProperty, fromBytes(source.getBucket().get(currentPath), typeToUse));
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
readAssociation(path, source, entity.get(), accessor);
|
||||
readAssociation(path, source, entity, accessor);
|
||||
|
||||
return (R) instance;
|
||||
}
|
||||
@@ -307,7 +320,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
Bucket bucket = source.getBucket().extract(currentPath + ".[");
|
||||
|
||||
Collection<Object> target = CollectionFactory.createCollection(association.getInverse().getType(),
|
||||
association.getInverse().getComponentType().orElse(Object.class), bucket.size());
|
||||
association.getInverse().getComponentType(), bucket.size());
|
||||
|
||||
for (Entry<String, byte[]> entry : bucket.entrySet()) {
|
||||
|
||||
@@ -321,7 +334,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
}
|
||||
}
|
||||
|
||||
accessor.setProperty(association.getInverse(), Optional.ofNullable(target));
|
||||
accessor.setProperty(association.getInverse(), target);
|
||||
|
||||
} else {
|
||||
|
||||
@@ -338,7 +351,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
|
||||
if (!CollectionUtils.isEmpty(rawHash)) {
|
||||
accessor.setProperty(association.getInverse(),
|
||||
Optional.ofNullable(read(association.getInverse().getActualType(), new RedisData(rawHash))));
|
||||
read(association.getInverse().getActualType(), new RedisData(rawHash)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -362,42 +375,42 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
return;
|
||||
}
|
||||
|
||||
final Optional<RedisPersistentEntity<?>> entity = mappingContext.getPersistentEntity(source.getClass());
|
||||
RedisPersistentEntity<?> entity = mappingContext.getPersistentEntity(source.getClass());
|
||||
|
||||
if (!customConversions.hasCustomWriteTarget(source.getClass())) {
|
||||
typeMapper.writeType(ClassUtils.getUserClass(source), sink);
|
||||
}
|
||||
|
||||
if (!entity.isPresent()) {
|
||||
if (entity == null) {
|
||||
|
||||
typeMapper.writeType(ClassUtils.getUserClass(source), sink);
|
||||
sink.getBucket().put("_raw", conversionService.convert(source, byte[].class));
|
||||
return;
|
||||
}
|
||||
|
||||
sink.setKeyspace(entity.get().getKeySpace());
|
||||
sink.setKeyspace(entity.getKeySpace());
|
||||
|
||||
writeInternal(entity.get().getKeySpace(), "", source, entity.get().getTypeInformation(), sink);
|
||||
writeInternal(entity.getKeySpace(), "", source, entity.getTypeInformation(), sink);
|
||||
|
||||
Optional<Object> identifyer = entity.get().getIdentifierAccessor(source).getIdentifier();
|
||||
Object identifier = entity.getIdentifierAccessor(source).getIdentifier();
|
||||
|
||||
if (identifyer.isPresent()) {
|
||||
sink.setId(getConversionService().convert(identifyer.get(), String.class));
|
||||
if (identifier != null) {
|
||||
sink.setId(getConversionService().convert(identifier, String.class));
|
||||
}
|
||||
|
||||
Long ttl = entity.get().getTimeToLiveAccessor().getTimeToLive(source);
|
||||
Long ttl = entity.getTimeToLiveAccessor().getTimeToLive(source);
|
||||
if (ttl != null && ttl > 0) {
|
||||
sink.setTimeToLive(ttl);
|
||||
}
|
||||
|
||||
for (IndexedData indexedData : indexResolver.resolveIndexesFor(entity.get().getTypeInformation(), source)) {
|
||||
for (IndexedData indexedData : indexResolver.resolveIndexesFor(entity.getTypeInformation(), source)) {
|
||||
sink.addIndexedData(indexedData);
|
||||
}
|
||||
}
|
||||
|
||||
protected void writePartialUpdate(PartialUpdate<?> update, RedisData sink) {
|
||||
|
||||
RedisPersistentEntity<?> entity = mappingContext.getPersistentEntity(update.getTarget()).get();
|
||||
RedisPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(update.getTarget());
|
||||
|
||||
write(update.getValue(), sink);
|
||||
if (sink.getBucket().keySet().contains(TYPE_HINT_ALIAS)) {
|
||||
@@ -441,7 +454,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
TypeInformation<?> ti = targetProperty == null ? ClassTypeInformation.OBJECT
|
||||
: (targetProperty.isMap()
|
||||
? (targetProperty.getTypeInformation().getMapValueType() != null
|
||||
? targetProperty.getTypeInformation().getMapValueType().get() : ClassTypeInformation.OBJECT)
|
||||
? targetProperty.getTypeInformation().getRequiredMapValueType() : ClassTypeInformation.OBJECT)
|
||||
: targetProperty.getTypeInformation().getActualType());
|
||||
|
||||
writeInternal(entity.getKeySpace(), pUpdate.getPropertyPath(), pUpdate.getValue(), ti, sink);
|
||||
@@ -452,27 +465,26 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
|
||||
if (targetProperty.isCollectionLike()) {
|
||||
|
||||
RedisPersistentEntity<?> ref = mappingContext.getPersistentEntity(targetProperty.getAssociation().get()
|
||||
.getInverse().getTypeInformation().getComponentType().get().getActualType()).get();
|
||||
RedisPersistentEntity<?> ref = mappingContext.getPersistentEntity(targetProperty.getRequiredAssociation()
|
||||
.getInverse().getTypeInformation().getRequiredComponentType().getActualType());
|
||||
|
||||
int i = 0;
|
||||
for (Object o : (Collection<?>) pUpdate.getValue()) {
|
||||
|
||||
Optional<Object> refId = ref.getPropertyAccessor(o).getProperty(ref.getIdProperty().get());
|
||||
if (refId.isPresent()) {
|
||||
sink.getBucket().put(pUpdate.getPropertyPath() + ".[" + i + "]",
|
||||
toBytes(ref.getKeySpace() + ":" + refId.get()));
|
||||
Object refId = ref.getPropertyAccessor(o).getProperty(ref.getRequiredIdProperty());
|
||||
if (refId != null) {
|
||||
sink.getBucket().put(pUpdate.getPropertyPath() + ".[" + i + "]", toBytes(ref.getKeySpace() + ":" + refId));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
RedisPersistentEntity<?> ref = mappingContext
|
||||
.getPersistentEntity(targetProperty.getAssociation().get().getInverse().getTypeInformation()).get();
|
||||
.getRequiredPersistentEntity(targetProperty.getRequiredAssociation().getInverse().getTypeInformation());
|
||||
|
||||
Optional<Object> refId = ref.getPropertyAccessor(pUpdate.getValue()).getProperty(ref.getIdProperty().get());
|
||||
if (refId.isPresent()) {
|
||||
sink.getBucket().put(pUpdate.getPropertyPath(), toBytes(ref.getKeySpace() + ":" + refId.get()));
|
||||
Object refId = ref.getPropertyAccessor(pUpdate.getValue()).getProperty(ref.getRequiredIdProperty());
|
||||
if (refId != null) {
|
||||
sink.getBucket().put(pUpdate.getPropertyPath(), toBytes(ref.getKeySpace() + ":" + refId));
|
||||
}
|
||||
}
|
||||
} else if (targetProperty.isCollectionLike()) {
|
||||
@@ -495,8 +507,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
pUpdate.getPropertyPath(), pUpdate.getValue()));
|
||||
}
|
||||
|
||||
writeMap(entity.getKeySpace(), pUpdate.getPropertyPath(), targetProperty.getMapValueType().orElse(Object.class),
|
||||
map, sink);
|
||||
writeMap(entity.getKeySpace(), pUpdate.getPropertyPath(), targetProperty.getMapValueType(), map, sink);
|
||||
} else {
|
||||
|
||||
writeInternal(entity.getKeySpace(), pUpdate.getPropertyPath(), pUpdate.getValue(),
|
||||
@@ -563,7 +574,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
toBytes(value.getClass().getName()));
|
||||
}
|
||||
|
||||
final RedisPersistentEntity<?> entity = mappingContext.getPersistentEntity(value.getClass()).get();
|
||||
final RedisPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(value.getClass());
|
||||
final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value);
|
||||
|
||||
entity.doWithProperties(new PropertyHandler<RedisPersistentProperty>() {
|
||||
@@ -573,37 +584,36 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
|
||||
String propertyStringPath = (!path.isEmpty() ? path + "." : "") + persistentProperty.getName();
|
||||
|
||||
Object propertyValue = accessor.getProperty(persistentProperty);
|
||||
if (persistentProperty.isIdProperty()) {
|
||||
|
||||
if (accessor.getProperty(persistentProperty).isPresent()) {
|
||||
sink.getBucket().put(propertyStringPath, toBytes(accessor.getProperty(persistentProperty).get()));
|
||||
if (propertyValue != null) {
|
||||
sink.getBucket().put(propertyStringPath, toBytes(propertyValue));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (persistentProperty.isMap()) {
|
||||
|
||||
if (accessor.getProperty(persistentProperty).isPresent()) {
|
||||
writeMap(keyspace, propertyStringPath, persistentProperty.getMapValueType().orElse(Object.class),
|
||||
(Map<?, ?>) accessor.getProperty(persistentProperty).get(), sink);
|
||||
if (propertyValue != null) {
|
||||
writeMap(keyspace, propertyStringPath, persistentProperty.getMapValueType(), (Map<?, ?>) propertyValue,
|
||||
sink);
|
||||
}
|
||||
} else if (persistentProperty.isCollectionLike()) {
|
||||
|
||||
final Optional<Object> propertyValue = accessor.getProperty(persistentProperty);
|
||||
|
||||
if (!propertyValue.isPresent()) {
|
||||
if (propertyValue == null) {
|
||||
writeCollection(keyspace, propertyStringPath, (Iterable<?>) null,
|
||||
persistentProperty.getTypeInformation().getComponentType().get(), sink);
|
||||
persistentProperty.getTypeInformation().getRequiredComponentType(), sink);
|
||||
} else {
|
||||
|
||||
if (Iterable.class.isAssignableFrom(propertyValue.get().getClass())) {
|
||||
if (Iterable.class.isAssignableFrom(propertyValue.getClass())) {
|
||||
|
||||
writeCollection(keyspace, propertyStringPath, (Iterable<?>) propertyValue.get(),
|
||||
persistentProperty.getTypeInformation().getComponentType().get(), sink);
|
||||
} else if (propertyValue.isPresent() && propertyValue.get().getClass().isArray()) {
|
||||
writeCollection(keyspace, propertyStringPath, (Iterable<?>) propertyValue,
|
||||
persistentProperty.getTypeInformation().getRequiredComponentType(), sink);
|
||||
} else if (propertyValue.getClass().isArray()) {
|
||||
|
||||
writeCollection(keyspace, propertyStringPath, CollectionUtils.arrayToList(propertyValue.get()),
|
||||
persistentProperty.getTypeInformation().getComponentType().get(), sink);
|
||||
writeCollection(keyspace, propertyStringPath, CollectionUtils.arrayToList(propertyValue),
|
||||
persistentProperty.getTypeInformation().getRequiredComponentType(), sink);
|
||||
} else {
|
||||
|
||||
throw new RuntimeException("Don't know how to handle " + propertyValue.getClass() + " type collection");
|
||||
@@ -612,16 +622,14 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
|
||||
} else if (persistentProperty.isEntity()) {
|
||||
|
||||
Optional<Object> propertyValue = accessor.getProperty(persistentProperty);
|
||||
if (propertyValue.isPresent()) {
|
||||
writeInternal(keyspace, propertyStringPath, propertyValue.get(),
|
||||
if (propertyValue != null) {
|
||||
writeInternal(keyspace, propertyStringPath, propertyValue,
|
||||
persistentProperty.getTypeInformation().getActualType(), sink);
|
||||
}
|
||||
} else {
|
||||
|
||||
Optional<Object> propertyValue = accessor.getProperty(persistentProperty);
|
||||
if (propertyValue.isPresent()) {
|
||||
writeToBucket(propertyStringPath, propertyValue.get(), sink, persistentProperty.getType());
|
||||
if (propertyValue != null) {
|
||||
writeToBucket(propertyStringPath, propertyValue, sink, persistentProperty.getType());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -644,25 +652,25 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
@Override
|
||||
public void doWithAssociation(Association<RedisPersistentProperty> association) {
|
||||
|
||||
Optional<Object> refObject = accessor.getProperty(association.getInverse());
|
||||
if (!refObject.isPresent()) {
|
||||
Object refObject = accessor.getProperty(association.getInverse());
|
||||
if (refObject == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (association.getInverse().isCollectionLike()) {
|
||||
|
||||
RedisPersistentEntity<?> ref = mappingContext.getPersistentEntity(
|
||||
association.getInverse().getTypeInformation().getComponentType().get().getActualType()).get();
|
||||
RedisPersistentEntity<?> ref = mappingContext.getRequiredPersistentEntity(
|
||||
association.getInverse().getTypeInformation().getRequiredComponentType().getActualType());
|
||||
|
||||
String keyspace = ref.getKeySpace();
|
||||
String propertyStringPath = (!path.isEmpty() ? path + "." : "") + association.getInverse().getName();
|
||||
|
||||
int i = 0;
|
||||
for (Object o : (Collection<?>) refObject.get()) {
|
||||
for (Object o : (Collection<?>) refObject) {
|
||||
|
||||
Optional<Object> refId = ref.getPropertyAccessor(o).getProperty(ref.getIdProperty().get());
|
||||
if (refId.isPresent()) {
|
||||
sink.getBucket().put(propertyStringPath + ".[" + i + "]", toBytes(keyspace + ":" + refId.get()));
|
||||
Object refId = ref.getPropertyAccessor(o).getProperty(ref.getRequiredIdProperty());
|
||||
if (refId != null) {
|
||||
sink.getBucket().put(propertyStringPath + ".[" + i + "]", toBytes(keyspace + ":" + refId));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@@ -670,14 +678,14 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
} else {
|
||||
|
||||
RedisPersistentEntity<?> ref = mappingContext
|
||||
.getPersistentEntity(association.getInverse().getTypeInformation()).get();
|
||||
.getRequiredPersistentEntity(association.getInverse().getTypeInformation());
|
||||
String keyspace = ref.getKeySpace();
|
||||
|
||||
Optional<Object> refId = ref.getPropertyAccessor(refObject.get()).getProperty(ref.getIdProperty().get());
|
||||
Object refId = ref.getPropertyAccessor(refObject).getProperty(ref.getIdProperty());
|
||||
|
||||
if (refId.isPresent()) {
|
||||
if (refId != null) {
|
||||
String propertyStringPath = (!path.isEmpty() ? path + "." : "") + association.getInverse().getName();
|
||||
sink.getBucket().put(propertyStringPath, toBytes(keyspace + ":" + refId.get()));
|
||||
sink.getBucket().put(propertyStringPath, toBytes(keyspace + ":" + refId));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -732,8 +740,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
Optional<Class<?>> targetType = customConversions.getCustomWriteTarget(value.getClass());
|
||||
|
||||
if (!targetType.filter(it -> ClassUtils.isAssignable(Map.class, it)).isPresent()
|
||||
&& customConversions.isSimpleType(value.getClass())
|
||||
&& value.getClass() != propertyType) {
|
||||
&& customConversions.isSimpleType(value.getClass()) && value.getClass() != propertyType) {
|
||||
sink.getBucket().put((!path.isEmpty() ? path + "." + TYPE_HINT_ALIAS : TYPE_HINT_ALIAS),
|
||||
toBytes(value.getClass().getName()));
|
||||
}
|
||||
@@ -1037,10 +1044,10 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> Optional<T> getPropertyValue(RedisPersistentProperty property) {
|
||||
public <T> T getPropertyValue(RedisPersistentProperty property) {
|
||||
|
||||
String name = StringUtils.hasText(path) ? path + "." + property.getName() : property.getName();
|
||||
return Optional.ofNullable((T) conversionService.convert(source.getBucket().get(name), property.getActualType()));
|
||||
return (T) conversionService.convert(source.getBucket().get(name), property.getActualType());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1065,8 +1072,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
||||
|
||||
@Override
|
||||
public Alias readAliasFrom(RedisData source) {
|
||||
return Alias
|
||||
.ofOptional(Optional.ofNullable(conversionService.convert(source.getBucket().get(typeKey), String.class)));
|
||||
return Alias.ofNullable(conversionService.convert(source.getBucket().get(typeKey), String.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
* Copyright 2015-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,7 +20,6 @@ import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.data.geo.Point;
|
||||
@@ -88,7 +87,7 @@ public class PathIndexResolver implements IndexResolver {
|
||||
* @see org.springframework.data.redis.core.convert.IndexResolver#resolveIndexesFor(org.springframework.data.util.TypeInformation, java.lang.Object)
|
||||
*/
|
||||
public Set<IndexedData> resolveIndexesFor(TypeInformation<?> typeInformation, Object value) {
|
||||
return doResolveIndexesFor(mappingContext.getPersistentEntity(typeInformation).get().getKeySpace(), "",
|
||||
return doResolveIndexesFor(mappingContext.getRequiredPersistentEntity(typeInformation).getKeySpace(), "",
|
||||
typeInformation, null, value);
|
||||
}
|
||||
|
||||
@@ -104,78 +103,78 @@ public class PathIndexResolver implements IndexResolver {
|
||||
private Set<IndexedData> doResolveIndexesFor(final String keyspace, final String path,
|
||||
TypeInformation<?> typeInformation, PersistentProperty<?> fallback, Object value) {
|
||||
|
||||
Optional<RedisPersistentEntity<?>> entity = mappingContext.getPersistentEntity(typeInformation);
|
||||
RedisPersistentEntity<?> entity = mappingContext.getPersistentEntity(typeInformation);
|
||||
|
||||
if (!entity.isPresent() || (value != null && VALUE_TYPES.contains(value.getClass()))) {
|
||||
if (entity == null || (value != null && VALUE_TYPES.contains(value.getClass()))) {
|
||||
return resolveIndex(keyspace, path, fallback, value);
|
||||
}
|
||||
|
||||
// this might happen on update where we address a property within an entity directly
|
||||
if (!ClassUtils.isAssignable(entity.get().getType(), value.getClass())) {
|
||||
if (!ClassUtils.isAssignable(entity.getType(), value.getClass())) {
|
||||
|
||||
String propertyName = path.lastIndexOf('.') > 0 ? path.substring(path.lastIndexOf('.') + 1, path.length()) : path;
|
||||
return resolveIndex(keyspace, path, entity.get().getPersistentProperty(propertyName).get(), value);
|
||||
return resolveIndex(keyspace, path, entity.getPersistentProperty(propertyName), value);
|
||||
}
|
||||
|
||||
final PersistentPropertyAccessor accessor = entity.get().getPropertyAccessor(value);
|
||||
final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value);
|
||||
final Set<IndexedData> indexes = new LinkedHashSet<IndexedData>();
|
||||
|
||||
entity.get().doWithProperties(new PropertyHandler<RedisPersistentProperty>() {
|
||||
entity.doWithProperties(new PropertyHandler<RedisPersistentProperty>() {
|
||||
|
||||
@Override
|
||||
public void doWithPersistentProperty(RedisPersistentProperty persistentProperty) {
|
||||
|
||||
String currentPath = !path.isEmpty() ? path + "." + persistentProperty.getName() : persistentProperty.getName();
|
||||
|
||||
Optional<Object> propertyValue = accessor.getProperty(persistentProperty);
|
||||
Object propertyValue = accessor.getProperty(persistentProperty);
|
||||
|
||||
if (propertyValue.isPresent()) {
|
||||
if (propertyValue == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
TypeInformation<?> typeHint = persistentProperty.isMap()
|
||||
? persistentProperty.getTypeInformation().getMapValueType().get()
|
||||
: persistentProperty.getTypeInformation().getActualType();
|
||||
TypeInformation<?> typeHint = persistentProperty.isMap()
|
||||
? persistentProperty.getTypeInformation().getRequiredMapValueType()
|
||||
: persistentProperty.getTypeInformation().getActualType();
|
||||
|
||||
if (persistentProperty.isMap()) {
|
||||
if (persistentProperty.isMap()) {
|
||||
|
||||
for (Entry<?, ?> entry : ((Map<?, ?>) propertyValue.get()).entrySet()) {
|
||||
for (Entry<?, ?> entry : ((Map<?, ?>) propertyValue).entrySet()) {
|
||||
|
||||
TypeInformation<?> typeToUse = updateTypeHintForActualValue(typeHint, entry.getValue());
|
||||
indexes.addAll(doResolveIndexesFor(keyspace, currentPath + "." + entry.getKey(),
|
||||
typeToUse.getActualType(), persistentProperty, entry.getValue()));
|
||||
}
|
||||
|
||||
} else if (persistentProperty.isCollectionLike()) {
|
||||
|
||||
final Iterable<?> iterable;
|
||||
|
||||
if (Iterable.class.isAssignableFrom(propertyValue.get().getClass())) {
|
||||
iterable = (Iterable<?>) propertyValue.get();
|
||||
} else if (propertyValue.get().getClass().isArray()) {
|
||||
iterable = CollectionUtils.arrayToList(propertyValue.get());
|
||||
} else {
|
||||
throw new RuntimeException(
|
||||
"Don't know how to handle " + propertyValue.get().getClass() + " type of collection");
|
||||
}
|
||||
|
||||
for (Object listValue : iterable) {
|
||||
|
||||
if (listValue != null) {
|
||||
TypeInformation<?> typeToUse = updateTypeHintForActualValue(typeHint, listValue);
|
||||
indexes.addAll(doResolveIndexesFor(keyspace, currentPath, typeToUse.getActualType(), persistentProperty,
|
||||
listValue));
|
||||
}
|
||||
}
|
||||
TypeInformation<?> typeToUse = updateTypeHintForActualValue(typeHint, entry.getValue());
|
||||
indexes.addAll(doResolveIndexesFor(keyspace, currentPath + "." + entry.getKey(), typeToUse.getActualType(),
|
||||
persistentProperty, entry.getValue()));
|
||||
}
|
||||
|
||||
else if (persistentProperty.isEntity()
|
||||
|| persistentProperty.getTypeInformation().getActualType().equals(ClassTypeInformation.OBJECT)) {
|
||||
} else if (persistentProperty.isCollectionLike()) {
|
||||
|
||||
typeHint = updateTypeHintForActualValue(typeHint, propertyValue.get());
|
||||
indexes.addAll(doResolveIndexesFor(keyspace, currentPath, typeHint.getActualType(), persistentProperty,
|
||||
propertyValue.get()));
|
||||
final Iterable<?> iterable;
|
||||
|
||||
if (Iterable.class.isAssignableFrom(propertyValue.getClass())) {
|
||||
iterable = (Iterable<?>) propertyValue;
|
||||
} else if (propertyValue.getClass().isArray()) {
|
||||
iterable = CollectionUtils.arrayToList(propertyValue);
|
||||
} else {
|
||||
indexes.addAll(resolveIndex(keyspace, currentPath, persistentProperty, propertyValue.get()));
|
||||
throw new RuntimeException("Don't know how to handle " + propertyValue.getClass() + " type of collection");
|
||||
}
|
||||
|
||||
for (Object listValue : iterable) {
|
||||
|
||||
if (listValue != null) {
|
||||
TypeInformation<?> typeToUse = updateTypeHintForActualValue(typeHint, listValue);
|
||||
indexes.addAll(
|
||||
doResolveIndexesFor(keyspace, currentPath, typeToUse.getActualType(), persistentProperty, listValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else if (persistentProperty.isEntity()
|
||||
|| persistentProperty.getTypeInformation().getActualType().equals(ClassTypeInformation.OBJECT)) {
|
||||
|
||||
typeHint = updateTypeHintForActualValue(typeHint, propertyValue);
|
||||
indexes.addAll(
|
||||
doResolveIndexesFor(keyspace, currentPath, typeHint.getActualType(), persistentProperty, propertyValue));
|
||||
} else {
|
||||
indexes.addAll(resolveIndex(keyspace, currentPath, persistentProperty, propertyValue));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -184,7 +183,7 @@ public class PathIndexResolver implements IndexResolver {
|
||||
|
||||
if (typeHint.equals(ClassTypeInformation.OBJECT) || typeHint.getClass().isInterface()) {
|
||||
try {
|
||||
typeHint = mappingContext.getPersistentEntity(propertyValue.getClass()).get().getTypeInformation();
|
||||
typeHint = mappingContext.getRequiredPersistentEntity(propertyValue.getClass()).getTypeInformation();
|
||||
} catch (Exception e) {
|
||||
// ignore for cases where property value cannot be resolved as an entity, in that case the provided type
|
||||
// hint has to be sufficient
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
* Copyright 2016-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,7 +19,6 @@ import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.context.expression.BeanFactoryResolver;
|
||||
@@ -86,13 +85,13 @@ public class SpelIndexResolver implements IndexResolver {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
Optional<RedisPersistentEntity<?>> entity = mappingContext.getPersistentEntity(typeInformation);
|
||||
RedisPersistentEntity<?> entity = mappingContext.getPersistentEntity(typeInformation);
|
||||
|
||||
if (!entity.isPresent()) {
|
||||
if (entity == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
String keyspace = entity.get().getKeySpace();
|
||||
String keyspace = entity.getKeySpace();
|
||||
|
||||
Set<IndexedData> indexes = new HashSet<IndexedData>();
|
||||
|
||||
|
||||
@@ -15,12 +15,10 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core.mapping;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.keyvalue.core.mapping.BasicKeyValuePersistentEntity;
|
||||
import org.springframework.data.keyvalue.core.mapping.KeySpaceResolver;
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
import org.springframework.data.mapping.MappingException;
|
||||
import org.springframework.data.redis.core.TimeToLive;
|
||||
import org.springframework.data.redis.core.TimeToLiveAccessor;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
@@ -28,8 +26,9 @@ import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link RedisPersistentEntity} implementation.
|
||||
*
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @param <T>
|
||||
*/
|
||||
public class BasicRedisPersistentEntity<T> extends BasicKeyValuePersistentEntity<T, RedisPersistentProperty>
|
||||
@@ -39,10 +38,10 @@ public class BasicRedisPersistentEntity<T> extends BasicKeyValuePersistentEntity
|
||||
|
||||
/**
|
||||
* Creates new {@link BasicRedisPersistentEntity}.
|
||||
*
|
||||
*
|
||||
* @param information must not be {@literal null}.
|
||||
* @param fallbackKeySpaceResolver can be {@literal null}.
|
||||
* @param timeToLiveResolver can be {@literal null}.
|
||||
* @param timeToLiveAccessor can be {@literal null}.
|
||||
*/
|
||||
public BasicRedisPersistentEntity(TypeInformation<T> information, KeySpaceResolver fallbackKeySpaceResolver,
|
||||
TimeToLiveAccessor timeToLiveAccessor) {
|
||||
@@ -75,14 +74,14 @@ public class BasicRedisPersistentEntity<T> extends BasicKeyValuePersistentEntity
|
||||
* @see org.springframework.data.redis.core.mapping.RedisPersistentEntity#getExplicitTimeToLiveProperty()
|
||||
*/
|
||||
@Override
|
||||
public Optional<RedisPersistentProperty> getExplicitTimeToLiveProperty() {
|
||||
public RedisPersistentProperty getExplicitTimeToLiveProperty() {
|
||||
return this.getPersistentProperty(TimeToLive.class);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mapping.model.BasicPersistentEntity#returnPropertyIfBetterIdPropertyCandidateOrNull(org.springframework.data.mapping.PersistentProperty)
|
||||
*/
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mapping.model.BasicPersistentEntity#returnPropertyIfBetterIdPropertyCandidateOrNull(org.springframework.data.mapping.PersistentProperty)
|
||||
*/
|
||||
@Override
|
||||
protected RedisPersistentProperty returnPropertyIfBetterIdPropertyCandidateOrNull(RedisPersistentProperty property) {
|
||||
|
||||
@@ -92,29 +91,27 @@ public class BasicRedisPersistentEntity<T> extends BasicKeyValuePersistentEntity
|
||||
return null;
|
||||
}
|
||||
|
||||
Optional<RedisPersistentProperty> currentIdProperty = getIdProperty();
|
||||
boolean currentIdPropertyIsSet = currentIdProperty.isPresent();
|
||||
RedisPersistentProperty currentIdProperty = getIdProperty();
|
||||
boolean currentIdPropertyIsSet = currentIdProperty != null;
|
||||
|
||||
if (!currentIdPropertyIsSet) {
|
||||
return property;
|
||||
}
|
||||
|
||||
boolean currentIdPropertyIsExplicit = currentIdProperty.get().isAnnotationPresent(Id.class);
|
||||
boolean currentIdPropertyIsExplicit = currentIdProperty.isAnnotationPresent(Id.class);
|
||||
boolean newIdPropertyIsExplicit = property.isAnnotationPresent(Id.class);
|
||||
|
||||
if (currentIdPropertyIsExplicit && newIdPropertyIsExplicit) {
|
||||
throw new MappingException(String.format(
|
||||
"Attempt to add explicit id property %s but already have an property %s registered "
|
||||
+ "as explicit id. Check your mapping configuration!",
|
||||
property.getField(), currentIdProperty.get().getField()));
|
||||
property.getField(), currentIdProperty.getField()));
|
||||
}
|
||||
|
||||
if (!currentIdPropertyIsExplicit && !newIdPropertyIsExplicit) {
|
||||
throw new MappingException(
|
||||
String.format(
|
||||
"Attempt to add id property %s but already have an property %s registered "
|
||||
+ "as id. Check your mapping configuration!",
|
||||
property.getField(), currentIdProperty.get().getField()));
|
||||
String.format("Attempt to add id property %s but already have an property %s registered "
|
||||
+ "as id. Check your mapping configuration!", property.getField(), currentIdProperty.getField()));
|
||||
}
|
||||
|
||||
if (newIdPropertyIsExplicit) {
|
||||
|
||||
@@ -19,7 +19,6 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
@@ -224,11 +223,9 @@ public class RedisMappingContext extends KeyValueMappingContext<RedisPersistentE
|
||||
|
||||
PersistentProperty<?> ttlProperty = resolveTtlProperty(type);
|
||||
|
||||
if (ttlProperty != null) {
|
||||
if (ttlProperty != null && ttlProperty.isAnnotationPresent(TimeToLive.class)) {
|
||||
|
||||
if (ttlProperty.findAnnotation(TimeToLive.class).isPresent()) {
|
||||
unit = ttlProperty.findAnnotation(TimeToLive.class).get().unit();
|
||||
}
|
||||
unit = ttlProperty.findAnnotation(TimeToLive.class).unit();
|
||||
}
|
||||
|
||||
if (source instanceof PartialUpdate) {
|
||||
@@ -248,11 +245,11 @@ public class RedisMappingContext extends KeyValueMappingContext<RedisPersistentE
|
||||
|
||||
} else if (ttlProperty != null) {
|
||||
|
||||
RedisPersistentEntity entity = mappingContext.getPersistentEntity(type).get();
|
||||
RedisPersistentEntity entity = mappingContext.getRequiredPersistentEntity(type);
|
||||
|
||||
Optional<Object> ttlPropertyValue = entity.getPropertyAccessor(source).getProperty(ttlProperty);
|
||||
if (ttlPropertyValue.isPresent()) {
|
||||
return TimeUnit.SECONDS.convert(((Number) ttlPropertyValue.get()).longValue(), unit);
|
||||
Object ttlPropertyValue = entity.getPropertyAccessor(source).getProperty(ttlProperty);
|
||||
if (ttlPropertyValue != null) {
|
||||
return TimeUnit.SECONDS.convert(((Number) ttlPropertyValue).longValue(), unit);
|
||||
}
|
||||
|
||||
} else {
|
||||
@@ -294,9 +291,9 @@ public class RedisMappingContext extends KeyValueMappingContext<RedisPersistentE
|
||||
defaultTimeout = keyspaceConfig.getKeyspaceSettings(type).getTimeToLive();
|
||||
}
|
||||
|
||||
Optional<RedisHash> hash = mappingContext.getPersistentEntity(type).get().findAnnotation(RedisHash.class);
|
||||
if (hash.isPresent() && hash.get().timeToLive() > 0) {
|
||||
defaultTimeout = hash.get().timeToLive();
|
||||
RedisHash hash = mappingContext.getRequiredPersistentEntity(type).findAnnotation(RedisHash.class);
|
||||
if (hash != null && hash.timeToLive() > 0) {
|
||||
defaultTimeout = hash.timeToLive();
|
||||
}
|
||||
|
||||
defaultTimeouts.put(type, defaultTimeout);
|
||||
@@ -310,13 +307,13 @@ public class RedisMappingContext extends KeyValueMappingContext<RedisPersistentE
|
||||
return timeoutProperties.get(type);
|
||||
}
|
||||
|
||||
RedisPersistentEntity entity = mappingContext.getPersistentEntity(type).get();
|
||||
Optional<PersistentProperty<?>> ttlProperty = entity.getPersistentProperty(TimeToLive.class);
|
||||
RedisPersistentEntity entity = mappingContext.getRequiredPersistentEntity(type);
|
||||
PersistentProperty<?> ttlProperty = entity.getPersistentProperty(TimeToLive.class);
|
||||
|
||||
if (ttlProperty.isPresent()) {
|
||||
if (ttlProperty != null) {
|
||||
|
||||
timeoutProperties.put(type, ttlProperty.get());
|
||||
return ttlProperty.get();
|
||||
timeoutProperties.put(type, ttlProperty);
|
||||
return ttlProperty;
|
||||
}
|
||||
|
||||
if (keyspaceConfig.hasSettingsFor(type)) {
|
||||
@@ -325,9 +322,9 @@ public class RedisMappingContext extends KeyValueMappingContext<RedisPersistentE
|
||||
if (StringUtils.hasText(settings.getTimeToLivePropertyName())) {
|
||||
|
||||
ttlProperty = entity.getPersistentProperty(settings.getTimeToLivePropertyName());
|
||||
if (ttlProperty.isPresent()) {
|
||||
timeoutProperties.put(type, ttlProperty.get());
|
||||
return ttlProperty.get();
|
||||
if (ttlProperty != null) {
|
||||
timeoutProperties.put(type, ttlProperty);
|
||||
return ttlProperty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
* Copyright 2015-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core.mapping;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
@@ -24,7 +22,7 @@ import org.springframework.data.redis.core.TimeToLiveAccessor;
|
||||
|
||||
/**
|
||||
* Redis specific {@link PersistentEntity}.
|
||||
*
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @param <T>
|
||||
* @since 1.7
|
||||
@@ -33,7 +31,7 @@ public interface RedisPersistentEntity<T> extends KeyValuePersistentEntity<T, Re
|
||||
|
||||
/**
|
||||
* Get the {@link TimeToLiveAccessor} associated with the entity.
|
||||
*
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
TimeToLiveAccessor getTimeToLiveAccessor();
|
||||
@@ -50,6 +48,6 @@ public interface RedisPersistentEntity<T> extends KeyValuePersistentEntity<T, Re
|
||||
* @return can be {@null}.
|
||||
* @since 1.8
|
||||
*/
|
||||
Optional<RedisPersistentProperty> getExplicitTimeToLiveProperty();
|
||||
RedisPersistentProperty getExplicitTimeToLiveProperty();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
* Copyright 2016-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -26,7 +26,7 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
import org.springframework.data.mapping.MappingException;
|
||||
import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -48,8 +48,8 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
* <p>
|
||||
* Flattening requires all property names to not interfere with JSON paths. Using dots or brackets in map keys or as
|
||||
* property names is not supported using flattening. The resulting hash cannot be mapped back into an Object.
|
||||
*
|
||||
* <strong>Example</strong>
|
||||
*
|
||||
* <pre>
|
||||
* <code>
|
||||
* class Person {
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.redis.repository.core;
|
||||
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
import org.springframework.data.mapping.MappingException;
|
||||
import org.springframework.data.redis.core.mapping.RedisPersistentEntity;
|
||||
import org.springframework.data.repository.core.support.PersistentEntityInformation;
|
||||
|
||||
@@ -23,8 +23,9 @@ import org.springframework.data.repository.core.support.PersistentEntityInformat
|
||||
* {@link RedisEntityInformation} implementation using a {@link RedisPersistentEntity} instance to lookup the necessary
|
||||
* information. Can be configured with a custom collection to be returned which will trump the one returned by the
|
||||
* {@link RedisPersistentEntity} if given.
|
||||
*
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @param <T>
|
||||
* @param <ID>
|
||||
*/
|
||||
|
||||
@@ -29,9 +29,10 @@ import org.springframework.data.repository.query.parser.AbstractQueryCreator;
|
||||
/**
|
||||
* {@link RepositoryFactorySupport} specific of handing Redis
|
||||
* {@link org.springframework.data.keyvalue.repository.KeyValueRepository}.
|
||||
*
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
* @since 1.7
|
||||
*/
|
||||
public class RedisRepositoryFactory extends KeyValueRepositoryFactory {
|
||||
@@ -78,10 +79,8 @@ public class RedisRepositoryFactory extends KeyValueRepositoryFactory {
|
||||
public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
|
||||
|
||||
RedisPersistentEntity<T> entity = (RedisPersistentEntity<T>) operations.getMappingContext()
|
||||
.getPersistentEntity(domainClass).get();
|
||||
EntityInformation<T, ID> entityInformation = (EntityInformation<T, ID>) new MappingRedisEntityInformation<T, ID>(
|
||||
entity);
|
||||
.getRequiredPersistentEntity(domainClass);
|
||||
|
||||
return entityInformation;
|
||||
return new MappingRedisEntityInformation<>(entity);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user