Browse Source

Improve `PropertyMatch` performance using precompiled regex patterns.

Signed-off-by: Kamil Krzywanski <kamilkrzywanski01@gmail.com>
Closes #3375
Original pull request: #3376
3.5.x
Kamil Krzywanski 2 months ago committed by Mark Paluch
parent
commit
549e8d4037
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 16
      src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

16
src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

@ -29,6 +29,7 @@ import java.util.Set;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -93,6 +94,7 @@ import org.springframework.util.ReflectionUtils.FieldFilter;
* @author Mark Paluch * @author Mark Paluch
* @author Mikael Klamra * @author Mikael Klamra
* @author Christoph Strobl * @author Christoph Strobl
* @author Kamil Krzywański
*/ */
public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?, P>, P extends PersistentProperty<P>> public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?, P>, P extends PersistentProperty<P>>
implements MappingContext<E, P>, ApplicationEventPublisherAware, ApplicationContextAware, BeanFactoryAware, implements MappingContext<E, P>, ApplicationEventPublisherAware, ApplicationContextAware, BeanFactoryAware,
@ -812,8 +814,8 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
*/ */
static class PropertyMatch { static class PropertyMatch {
private final @Nullable String namePattern; private final @Nullable Pattern namePatternRegex;
private final @Nullable String typeName; private final @Nullable String typeName;
/** /**
* Creates a new {@link PropertyMatch} for the given name pattern and type name. At least one of the parameters * Creates a new {@link PropertyMatch} for the given name pattern and type name. At least one of the parameters
@ -826,8 +828,8 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
Assert.isTrue(!((namePattern == null) && (typeName == null)), "Either name pattern or type name must be given"); Assert.isTrue(!((namePattern == null) && (typeName == null)), "Either name pattern or type name must be given");
this.namePattern = namePattern; this.namePatternRegex = namePattern == null ? null : Pattern.compile(namePattern);
this.typeName = typeName; this.typeName = typeName;
} }
/** /**
@ -862,9 +864,9 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
Assert.notNull(name, "Name must not be null"); Assert.notNull(name, "Name must not be null");
Assert.notNull(type, "Type must not be null"); Assert.notNull(type, "Type must not be null");
if ((namePattern != null) && !name.matches(namePattern)) { if (namePatternRegex != null && !namePatternRegex.matcher(name).matches()) {
return false; return false;
} }
if ((typeName != null) && !type.getName().equals(typeName)) { if ((typeName != null) && !type.getName().equals(typeName)) {
return false; return false;

Loading…
Cancel
Save