|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.redis.repository.query; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.EnumSet; |
| 20 | +import java.util.Optional; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.function.Predicate; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +import org.springframework.dao.InvalidDataAccessApiUsageException; |
| 26 | +import org.springframework.data.domain.Example; |
| 27 | +import org.springframework.data.domain.ExampleMatcher.MatchMode; |
| 28 | +import org.springframework.data.domain.ExampleMatcher.PropertyValueTransformer; |
| 29 | +import org.springframework.data.domain.ExampleMatcher.StringMatcher; |
| 30 | +import org.springframework.data.mapping.PersistentPropertyAccessor; |
| 31 | +import org.springframework.data.mapping.context.MappingContext; |
| 32 | +import org.springframework.data.redis.core.convert.IndexResolver; |
| 33 | +import org.springframework.data.redis.core.convert.IndexedData; |
| 34 | +import org.springframework.data.redis.core.mapping.RedisPersistentEntity; |
| 35 | +import org.springframework.data.redis.core.mapping.RedisPersistentProperty; |
| 36 | +import org.springframework.data.support.ExampleMatcherAccessor; |
| 37 | +import org.springframework.lang.Nullable; |
| 38 | +import org.springframework.util.Assert; |
| 39 | +import org.springframework.util.StringUtils; |
| 40 | + |
| 41 | +/** |
| 42 | + * Mapper for Query-by-Example examples to an actual query. |
| 43 | + * <p/> |
| 44 | + * This mapper creates a {@link RedisOperationChain} for a given {@link Example} considering exact matches, |
| 45 | + * {@link PropertyValueTransformer value transformations} and {@link MatchMode} for indexed simple and nested type |
| 46 | + * properties. {@link java.util.Map} and {@link java.util.Collection} properties are not considered. |
| 47 | + * <p/> |
| 48 | + * Example matching is limited to case-sensitive and exact matches only. |
| 49 | + * |
| 50 | + * @author Mark Paluch |
| 51 | + * @since 2.1 |
| 52 | + */ |
| 53 | +public class ExampleQueryMapper { |
| 54 | + |
| 55 | + private final Set<StringMatcher> SUPPORTED_MATCHERS = EnumSet.of(StringMatcher.DEFAULT, StringMatcher.EXACT); |
| 56 | + |
| 57 | + private final MappingContext<RedisPersistentEntity<?>, RedisPersistentProperty> mappingContext; |
| 58 | + private final IndexResolver indexResolver; |
| 59 | + |
| 60 | + /** |
| 61 | + * Creates a new {@link ExampleQueryMapper} given {@link MappingContext} and {@link IndexResolver}. |
| 62 | + * |
| 63 | + * @param mappingContext must not be {@literal null}. |
| 64 | + * @param indexResolver must not be {@literal null}. |
| 65 | + */ |
| 66 | + public ExampleQueryMapper(MappingContext<RedisPersistentEntity<?>, RedisPersistentProperty> mappingContext, |
| 67 | + IndexResolver indexResolver) { |
| 68 | + |
| 69 | + Assert.notNull(mappingContext, "MappingContext must not be null!"); |
| 70 | + Assert.notNull(indexResolver, "IndexResolver must not be null!"); |
| 71 | + |
| 72 | + this.mappingContext = mappingContext; |
| 73 | + this.indexResolver = indexResolver; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Retrieve a mapped {@link RedisOperationChain} to query secondary indexes given {@link Example}. |
| 78 | + * |
| 79 | + * @param example must not be {@literal null}. |
| 80 | + * @return the mapped {@link RedisOperationChain}. |
| 81 | + */ |
| 82 | + public RedisOperationChain getMappedExample(Example<?> example) { |
| 83 | + |
| 84 | + RedisOperationChain chain = new RedisOperationChain(); |
| 85 | + |
| 86 | + ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); |
| 87 | + |
| 88 | + applyPropertySpecs("", example.getProbe(), mappingContext.getRequiredPersistentEntity(example.getProbeType()), |
| 89 | + matcherAccessor, example.getMatcher().getMatchMode(), chain); |
| 90 | + |
| 91 | + return chain; |
| 92 | + } |
| 93 | + |
| 94 | + private void applyPropertySpecs(String path, @Nullable Object probe, RedisPersistentEntity<?> persistentEntity, |
| 95 | + ExampleMatcherAccessor exampleSpecAccessor, MatchMode matchMode, RedisOperationChain chain) { |
| 96 | + |
| 97 | + if (probe == null) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + PersistentPropertyAccessor propertyAccessor = persistentEntity.getPropertyAccessor(probe); |
| 102 | + |
| 103 | + Set<IndexedData> indexedData = getIndexedData(path, probe, persistentEntity); |
| 104 | + Set<String> indexNames = indexedData.stream().map(IndexedData::getIndexName).distinct().collect(Collectors.toSet()); |
| 105 | + |
| 106 | + persistentEntity.forEach(property -> { |
| 107 | + |
| 108 | + if (property.isIdProperty()) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + String propertyPath = StringUtils.hasText(path) ? path + "." + property.getName() : property.getName(); |
| 113 | + |
| 114 | + if (exampleSpecAccessor.isIgnoredPath(propertyPath) || property.isCollectionLike() || property.isMap()) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + applyPropertySpec(propertyPath, indexNames::contains, exampleSpecAccessor, propertyAccessor, property, matchMode, |
| 119 | + chain); |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + private void applyPropertySpec(String path, Predicate<String> hasIndex, ExampleMatcherAccessor exampleSpecAccessor, |
| 124 | + PersistentPropertyAccessor propertyAccessor, RedisPersistentProperty property, MatchMode matchMode, |
| 125 | + RedisOperationChain chain) { |
| 126 | + |
| 127 | + StringMatcher stringMatcher = exampleSpecAccessor.getDefaultStringMatcher(); |
| 128 | + boolean ignoreCase = exampleSpecAccessor.isIgnoreCaseEnabled(); |
| 129 | + Object value = propertyAccessor.getProperty(property); |
| 130 | + |
| 131 | + if (exampleSpecAccessor.hasPropertySpecifiers()) { |
| 132 | + stringMatcher = exampleSpecAccessor.getStringMatcherForPath(path); |
| 133 | + ignoreCase = exampleSpecAccessor.isIgnoreCaseForPath(path); |
| 134 | + } |
| 135 | + |
| 136 | + if (ignoreCase) { |
| 137 | + throw new InvalidDataAccessApiUsageException("Redis Query-by-Example supports only case-sensitive matching."); |
| 138 | + } |
| 139 | + |
| 140 | + if (!SUPPORTED_MATCHERS.contains(stringMatcher)) { |
| 141 | + throw new InvalidDataAccessApiUsageException( |
| 142 | + String.format("Redis Query-by-Example does not support string matcher %s. Supported matchers are: %s.", |
| 143 | + stringMatcher, SUPPORTED_MATCHERS)); |
| 144 | + } |
| 145 | + |
| 146 | + if (exampleSpecAccessor.hasPropertySpecifier(path)) { |
| 147 | + |
| 148 | + PropertyValueTransformer valueTransformer = exampleSpecAccessor.getValueTransformerForPath(path); |
| 149 | + value = valueTransformer.apply(Optional.ofNullable(value)).orElse(null); |
| 150 | + } |
| 151 | + |
| 152 | + if (value == null) { |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + if (property.isEntity()) { |
| 157 | + applyPropertySpecs(path, value, mappingContext.getRequiredPersistentEntity(property), exampleSpecAccessor, |
| 158 | + matchMode, chain); |
| 159 | + } else { |
| 160 | + |
| 161 | + if (matchMode == MatchMode.ALL) { |
| 162 | + if (hasIndex.test(path)) { |
| 163 | + chain.sismember(path, value); |
| 164 | + } |
| 165 | + } else { |
| 166 | + chain.orSismember(path, value); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private Set<IndexedData> getIndexedData(String path, Object probe, RedisPersistentEntity<?> persistentEntity) { |
| 172 | + |
| 173 | + String keySpace = persistentEntity.getKeySpace(); |
| 174 | + return keySpace == null ? Collections.emptySet() |
| 175 | + : indexResolver.resolveIndexesFor(persistentEntity.getKeySpace(), path, persistentEntity.getTypeInformation(), |
| 176 | + probe); |
| 177 | + } |
| 178 | +} |
0 commit comments