Skip to content

Use by-id lookup for queries referring to identifier values #2854

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>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-2851-SNAPSHOT</version>

<name>Spring Data Redis</name>
<description>Spring Data module for Redis</description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[[cassandra.projections]]
[[redis.projections]]
= Projections

include::{commons}@data-commons::page$repositories/projections.adoc[leveloffset=+1]
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.springframework.data.redis.core.index.IndexConfiguration;
import org.springframework.data.redis.core.mapping.RedisMappingContext;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.repository.query.RedisPartTreeQuery;
import org.springframework.data.redis.repository.query.RedisQueryCreator;
import org.springframework.data.redis.repository.support.RedisRepositoryFactoryBean;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -106,15 +107,15 @@ public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader)
TypeReference.of(ReactiveClusterScriptingCommands.class),
TypeReference.of(ReactiveClusterGeoCommands.class),
TypeReference.of(ReactiveClusterHyperLogLogCommands.class), TypeReference.of(ReactiveRedisOperations.class),
TypeReference.of(ReactiveRedisConnectionFactory.class),
TypeReference.of(ReactiveRedisTemplate.class), TypeReference.of(RedisOperations.class),
TypeReference.of(RedisTemplate.class), TypeReference.of(StringRedisTemplate.class),
TypeReference.of(KeyspaceConfiguration.class), TypeReference.of(MappingConfiguration.class),
TypeReference.of(MappingRedisConverter.class), TypeReference.of(RedisConverter.class),
TypeReference.of(RedisCustomConversions.class), TypeReference.of(ReferenceResolver.class),
TypeReference.of(ReferenceResolverImpl.class), TypeReference.of(IndexConfiguration.class),
TypeReference.of(ConfigurableIndexDefinitionProvider.class), TypeReference.of(RedisMappingContext.class),
TypeReference.of(RedisRepositoryFactoryBean.class), TypeReference.of(RedisQueryCreator.class),
TypeReference.of(ReactiveRedisConnectionFactory.class), TypeReference.of(ReactiveRedisTemplate.class),
TypeReference.of(RedisOperations.class), TypeReference.of(RedisTemplate.class),
TypeReference.of(StringRedisTemplate.class), TypeReference.of(KeyspaceConfiguration.class),
TypeReference.of(MappingConfiguration.class), TypeReference.of(MappingRedisConverter.class),
TypeReference.of(RedisConverter.class), TypeReference.of(RedisCustomConversions.class),
TypeReference.of(ReferenceResolver.class), TypeReference.of(ReferenceResolverImpl.class),
TypeReference.of(IndexConfiguration.class), TypeReference.of(ConfigurableIndexDefinitionProvider.class),
TypeReference.of(RedisMappingContext.class), TypeReference.of(RedisRepositoryFactoryBean.class),
TypeReference.of(RedisQueryCreator.class), TypeReference.of(RedisPartTreeQuery.class),
TypeReference.of(MessageListener.class), TypeReference.of(RedisMessageListenerContainer.class),

TypeReference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@
import org.springframework.data.keyvalue.core.SortAccessor;
import org.springframework.data.keyvalue.core.SpelSortAccessor;
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoRadiusCommandArgs;
import org.springframework.data.redis.connection.util.ByteArrayWrapper;
import org.springframework.data.redis.core.convert.GeoIndexedPropertyValue;
import org.springframework.data.redis.core.convert.RedisConverter;
import org.springframework.data.redis.core.convert.RedisData;
import org.springframework.data.redis.core.mapping.RedisPersistentProperty;
import org.springframework.data.redis.repository.query.RedisOperationChain;
import org.springframework.data.redis.repository.query.RedisOperationChain.NearPath;
import org.springframework.data.redis.repository.query.RedisOperationChain.PathAndValue;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;

Expand Down Expand Up @@ -100,7 +102,7 @@ private <T> List<T> doFind(RedisOperationChain criteria, long offset, int rows,

RedisCallback<Map<byte[], Map<byte[], byte[]>>> callback = connection -> {

List<byte[]> keys = findKeys(criteria, rows, keyspace, connection);
List<byte[]> keys = findKeys(criteria, rows, keyspace, type, connection);
byte[] keyspaceBin = getRequiredAdapter().getConverter().getConversionService().convert(keyspace + ":",
byte[].class);

Expand Down Expand Up @@ -143,18 +145,35 @@ private <T> List<T> doFind(RedisOperationChain criteria, long offset, int rows,
return result;
}

private List<byte[]> findKeys(RedisOperationChain criteria, int rows, String keyspace, RedisConnection connection) {
private List<byte[]> findKeys(RedisOperationChain criteria, int rows, String keyspace, Class<?> domainType,
RedisConnection connection) {

List<byte[]> allKeys = new ArrayList<>();

if (!criteria.getSismember().isEmpty()) {
allKeys.addAll(connection.sInter(keys(keyspace + ":", criteria.getSismember())));

Set<PathAndValue> sismember = criteria.getSismember();
if (sismember.size() == 1) {
KeySelector keySelector = KeySelector.of(getRequiredAdapter().getConverter(), sismember, domainType);
if (!keySelector.setValueLookup().isEmpty()) {
allKeys.addAll(connection.sInter(keys(keyspace + ":", keySelector.setValueLookup())));
}

allKeys.addAll(keySelector.keys());
} else {
allKeys.addAll(connection.sInter(keys(keyspace + ":", sismember)));
}
}

if (!criteria.getOrSismember().isEmpty()) {
KeySelector keySelector = KeySelector.of(getRequiredAdapter().getConverter(), criteria.getOrSismember(),
domainType);

if (!keySelector.setValueLookup().isEmpty()) {
allKeys.addAll(connection.sUnion(keys(keyspace + ":", criteria.getOrSismember())));
}

allKeys.addAll(keySelector.keys());

if (criteria.getNear() != null) {

GeoRadiusCommandArgs limit = GeoRadiusCommandArgs.newGeoRadiusArgs();
Expand All @@ -170,7 +189,6 @@ private List<byte[]> findKeys(RedisOperationChain criteria, int rows, String key
}
}


Set<ByteArrayWrapper> unique = new LinkedHashSet<>(allKeys.size());
allKeys.forEach(key -> unique.add(new ByteArrayWrapper(key)));

Expand Down Expand Up @@ -244,4 +262,34 @@ public RedisOperationChain resolve(KeyValueQuery<?> query) {
return (RedisOperationChain) query.getCriteria();
}
}

/**
* Value object capturing the direct object keys and set of values that need to be looked up from the secondary
* indexes.
*
* @param keys
* @param setValueLookup
*/
record KeySelector(Collection<byte[]> keys, Set<PathAndValue> setValueLookup) {

static KeySelector of(RedisConverter converter, Set<PathAndValue> pathAndValues, Class<?> domainType) {

Set<byte[]> keys = new LinkedHashSet<>();
Set<PathAndValue> remainder = new LinkedHashSet<>();

for (PathAndValue pathAndValue : pathAndValues) {

PersistentPropertyPath<RedisPersistentProperty> path = converter.getMappingContext()
.getPersistentPropertyPath(pathAndValue.getPath(), domainType);
if (path.getLeafProperty().isIdProperty()) {
byte[] key = converter.getConversionService().convert(pathAndValue.getFirstValue(), byte[].class);
keys.add(key);
} else {
remainder.add(pathAndValue);
}
}

return new KeySelector(keys, remainder);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1046,6 +1055,11 @@ public IndexResolver getIndexResolver() {
return this.indexResolver;
}

@Override
public EntityInstantiators getEntityInstantiators() {
return entityInstantiators;
}

@Override
public ConversionService getConversionService() {
return this.conversionService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.data.redis.core.convert;

import org.springframework.data.convert.EntityConverter;
import org.springframework.data.mapping.model.EntityInstantiators;
import org.springframework.data.redis.core.mapping.RedisMappingContext;
import org.springframework.data.redis.core.mapping.RedisPersistentEntity;
import org.springframework.data.redis.core.mapping.RedisPersistentProperty;
Expand All @@ -40,4 +41,10 @@ public interface RedisConverter
*/
@Nullable
IndexResolver getIndexResolver();

/**
* @return the configured {@link EntityInstantiators}.
* @since 3.2.4
*/
EntityInstantiators getEntityInstantiators();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.data.redis.core.convert.KeyspaceConfiguration;
import org.springframework.data.redis.core.index.IndexConfiguration;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.repository.query.RedisPartTreeQuery;
import org.springframework.data.redis.repository.query.RedisQueryCreator;
import org.springframework.data.redis.repository.support.RedisRepositoryFactoryBean;
import org.springframework.data.repository.config.DefaultRepositoryBaseClass;
Expand All @@ -52,7 +53,7 @@
@Documented
@Inherited
@Import(RedisRepositoriesRegistrar.class)
@QueryCreatorType(RedisQueryCreator.class)
@QueryCreatorType(value = RedisQueryCreator.class, repositoryQueryType = RedisPartTreeQuery.class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should register RedisPartTreeQuery for reflective access (MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS) in RepositoryRuntimeHints.

public @interface EnableRedisRepositories {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright 2024 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.repository.query;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.DtoInstantiatingConverter;
import org.springframework.data.keyvalue.core.KeyValueOperations;
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
import org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.EntityInstantiators;
import org.springframework.data.redis.core.RedisKeyValueAdapter;
import org.springframework.data.redis.core.convert.RedisConverter;
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.ParametersParameterAccessor;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
import org.springframework.data.repository.query.ResultProcessor;
import org.springframework.data.repository.query.ReturnedType;
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.data.util.Streamable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
* Redis-specific implementation of {@link KeyValuePartTreeQuery} supporting projections.
*
* @author Mark Paluch
* @since 3.2.4
*/
public class RedisPartTreeQuery extends KeyValuePartTreeQuery {

private final RedisKeyValueAdapter adapter;

public RedisPartTreeQuery(QueryMethod queryMethod, QueryMethodEvaluationContextProvider evaluationContextProvider,
KeyValueOperations template, Class<? extends AbstractQueryCreator<?, ?>> queryCreator) {
super(queryMethod, evaluationContextProvider, template, queryCreator);
this.adapter = (RedisKeyValueAdapter) template.getKeyValueAdapter();
}

@Override
public Object execute(Object[] parameters) {

ParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters);
KeyValueQuery<?> query = prepareQuery(parameters);
ResultProcessor processor = getQueryMethod().getResultProcessor().withDynamicProjection(accessor);

RedisConverter converter = adapter.getConverter();
Converter<Object, Object> resultPostProcessor = new ResultProcessingConverter(processor,
converter.getMappingContext(), converter.getEntityInstantiators());

Object source = doExecute(parameters, query);
return source != null ? processor.processResult(resultPostProcessor.convert(source)) : null;
}

/**
* A {@link Converter} to post-process all source objects using the given {@link ResultProcessor}.
*
* @author Mark Paluch
*/
static final class ResultProcessingConverter implements Converter<Object, Object> {

private final ResultProcessor processor;
private final MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> context;
private final EntityInstantiators instantiators;

public ResultProcessingConverter(ResultProcessor processor,
MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> context,
EntityInstantiators instantiators) {

Assert.notNull(processor, "Processor must not be null!");
Assert.notNull(context, "MappingContext must not be null!");
Assert.notNull(instantiators, "Instantiators must not be null!");

this.processor = processor;
this.context = context;
this.instantiators = instantiators;
}

/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
@Override
public Object convert(Object source) {

if (source instanceof Set<?> s) {

Set<Object> target = new LinkedHashSet<>(s.size());

for (Object o : s) {
target.add(convert(o));
}

return target;
}

if (source instanceof Collection<?> c) {

List<Object> target = new ArrayList<>(c.size());

for (Object o : c) {
target.add(convert(o));
}

return target;
}

if (source instanceof Streamable<?> s) {
return s.map(this::convert);
}

ReturnedType returnedType = processor.getReturnedType();

if (ReflectionUtils.isVoid(returnedType.getReturnedType())) {
return null;
}

if (ClassUtils.isPrimitiveOrWrapper(returnedType.getReturnedType())) {
return source;
}

Converter<Object, Object> converter = new DtoInstantiatingConverter(returnedType.getReturnedType(), context,
instantiators);

return processor.processResult(source, converter);
}
}
}
Loading