Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Createsequence committed Jul 15, 2024
1 parent 94f4490 commit 8bba142
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -136,15 +137,32 @@ protected abstract MethodInvoker createMethodInvoker(
* @param id id
* @param repository repository
* @return old repository target
* @see #ormRepositoryMap
* @see #getRepository
*/
public abstract R registerRepository(String id, @NonNull R repository);
public synchronized R registerRepository(String id, @NonNull R repository) {
Asserts.isNotNull(repository, "repository cannot be null");
QueryRepository<R> repo = createRepository(id, repository);
return Optional.ofNullable(ormRepositoryMap.put(id, repo))
.map(QueryRepository::getTarget)
.orElse(null);
}

/**
* Create repository.
*
* @param id id
* @param repository repository
* @return repo
*/
@NonNull
protected abstract QueryRepository<R> createRepository(String id, @NonNull R repository);

/**
* Get repository by given annotation.
*
* @param standardAnnotation standard annotation
* @return repository
* @see #registerRepository
*/
protected synchronized QueryRepository<R> getRepository(OrmAssembleAnnotation<A> standardAnnotation) {
QueryDefinition queryDefinition = standardAnnotation.getQueryDefinition();
Expand Down Expand Up @@ -204,22 +222,4 @@ protected static class OrmAssembleAnnotation<A extends Annotation>
extends StandardAssembleAnnotationAdapter<A> implements StandardAssembleAnnotation<A> {
private final QueryDefinition queryDefinition;
}

/**
* Repository provider.
*
* @param <R> repository type
* @author huangchengxing
*/
@FunctionalInterface
public interface RepositoryTargetProvider<R> {

/**
* Get repository by given id.
*
* @param queryDefinition query definition
* @return repository
*/
R get(QueryDefinition queryDefinition);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cn.crane4j.core.support.query;

/**
* Repository provider.
*
* @param <R> repository type
* @author huangchengxing
*/
@FunctionalInterface
public interface RepositoryTargetProvider<R> {

/**
* Get repository by given id.
*
* @param queryDefinition query definition
* @return repository
*/
R get(QueryDefinition queryDefinition);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import cn.crane4j.core.support.query.AbstractQueryAssembleAnnotationHandler;
import cn.crane4j.core.support.query.QueryDefinition;
import cn.crane4j.core.support.query.QueryRepository;
import cn.crane4j.core.support.query.RepositoryTargetProvider;
import cn.crane4j.core.util.Asserts;
import cn.crane4j.core.util.CollectionUtils;
import cn.hutool.core.text.CharSequenceUtil;
Expand All @@ -33,7 +34,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -181,18 +181,24 @@ public void setRepositoryTargetProvider(
throw new UnsupportedOperationException("RepositoryTargetProvider is not supported to set!");
}

/**
* Create repository.
*
* @param id id
* @param queryDefinition repository
* @return repo
*/
@NonNull
@Override
public QueryDefinition registerRepository(String id, @NonNull QueryDefinition queryDefinition) {
protected DataSourceRepository createRepository(String id, @NonNull QueryDefinition queryDefinition) {
Asserts.isNotEmpty(queryDefinition.getConditionColumn(), "condition column cannot be empty!");
return Optional.ofNullable(ormRepositoryMap.put(id, new DataSourceRepository(queryDefinition)))
.map(QueryRepository::getTarget)
.orElse(null);
return new DataSourceRepository(queryDefinition);
}

@SuppressWarnings("java:S6548")
@Getter
@RequiredArgsConstructor
private static class DataSourceRepository implements QueryRepository<QueryDefinition> {
protected static class DataSourceRepository implements QueryRepository<QueryDefinition> {
private final QueryDefinition target;
@Override
public String getPrimaryKeyProperty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import cn.crane4j.core.support.query.AbstractQueryAssembleAnnotationHandler;
import cn.crane4j.core.support.query.QueryDefinition;
import cn.crane4j.core.support.query.QueryRepository;
import cn.crane4j.core.util.Asserts;
import cn.crane4j.core.util.CollectionUtils;
import cn.crane4j.core.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
Expand All @@ -28,7 +27,6 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -99,19 +97,16 @@ private String resolveSelectColumn(String column, BaseMapperQueryRepository repo
}

/**
* Register repository.
* Create repository.
*
* @param id id
* @param repository repository
* @return old repository target
* @return repo
*/
@NonNull
@Override
public BaseMapper<?> registerRepository(String id, @NonNull BaseMapper<?> repository) {
Asserts.isNotNull(repository, "repository cannot be null");
BaseMapperQueryRepository ormRepository = new BaseMapperQueryRepository(id, repository);
return Optional.ofNullable(ormRepositoryMap.put(id, ormRepository))
.map(QueryRepository::getTarget)
.orElse(null);
protected BaseMapperQueryRepository createRepository(String id, @NonNull BaseMapper<?> repository) {
return new BaseMapperQueryRepository(id, repository);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import cn.crane4j.core.support.AnnotationFinder;
import cn.crane4j.core.support.Crane4jGlobalConfiguration;
import cn.crane4j.core.support.container.MethodInvokerContainerCreator;
import cn.crane4j.core.support.query.AbstractQueryAssembleAnnotationHandler;
import cn.crane4j.core.support.query.QueryDefinition;
import cn.crane4j.core.support.query.RepositoryTargetProvider;
import cn.crane4j.core.util.Try;
import cn.crane4j.extension.mybatis.plus.AssembleMpAnnotationHandler;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
Expand Down Expand Up @@ -104,7 +105,7 @@ public static class Properties {
* @since 2.9.0
*/
@RequiredArgsConstructor
public static class MapperLazyLoader implements AbstractQueryAssembleAnnotationHandler.RepositoryTargetProvider<BaseMapper<?>> {
public static class MapperLazyLoader implements RepositoryTargetProvider<BaseMapper<?>> {

private final BeanFactory beanFactory;

Expand Down Expand Up @@ -155,7 +156,10 @@ public void run(ApplicationArguments args) {
AssembleMpAnnotationHandler handler = applicationContext.getBean(AssembleMpAnnotationHandler.class);
mappers.entrySet().stream()
.filter(e -> mapperFilter.test(e.getKey(), e.getValue()))
.forEach(e -> handler.registerRepository(e.getKey(), e.getValue()));
.forEach(e -> Try.of(() -> handler.registerRepository(e.getKey(), e.getValue()))
.subscribeFailure(ex -> log.error("Failed to register mapper: {}", e.getKey(), ex))
.perform()
);
log.info("crane4j mybatis-plus extension component initialization completed.");
}
}
Expand Down

0 comments on commit 8bba142

Please sign in to comment.