-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/antora/modules/ROOT/pages/repositories/projections.adoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
src/main/java/org/springframework/data/redis/repository/query/RedisPartTreeQuery.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
) inRepositoryRuntimeHints
.