DATAES-328 - Reduce null usage, use PageRequest.of factory method.

This commit is contained in:
Mark Paluch 2017-01-30 14:19:48 +01:00
parent 4c7dd4939a
commit 5f3bacf950
9 changed files with 39 additions and 34 deletions

View File

@ -689,7 +689,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
: 10000l; : 10000l;
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(deleteQuery.getQuery()).withIndices(indexName) SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(deleteQuery.getQuery()).withIndices(indexName)
.withTypes(typeName).withPageable(new PageRequest(0, pageSize)).build(); .withTypes(typeName).withPageable(PageRequest.of(0, pageSize)).build();
String scrollId = scan(searchQuery, scrollTimeInMillis, true); String scrollId = scan(searchQuery, scrollTimeInMillis, true);

View File

@ -33,6 +33,7 @@ import org.springframework.data.elasticsearch.core.facet.FacetRequest;
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Artur Konczak * @author Artur Konczak
* @author Mark Paluch
*/ */
public class NativeSearchQueryBuilder { public class NativeSearchQueryBuilder {
@ -43,7 +44,7 @@ public class NativeSearchQueryBuilder {
private List<FacetRequest> facetRequests = new ArrayList<>(); private List<FacetRequest> facetRequests = new ArrayList<>();
private List<AbstractAggregationBuilder> aggregationBuilders = new ArrayList<>(); private List<AbstractAggregationBuilder> aggregationBuilders = new ArrayList<>();
private HighlightBuilder.Field[] highlightFields; private HighlightBuilder.Field[] highlightFields;
private Pageable pageable; private Pageable pageable = Pageable.NONE;
private String[] indices; private String[] indices;
private String[] types; private String[] types;
private String[] fields; private String[] fields;
@ -141,9 +142,7 @@ public class NativeSearchQueryBuilder {
public NativeSearchQuery build() { public NativeSearchQuery build() {
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders, highlightFields); NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders, highlightFields);
if (pageable != null) {
nativeSearchQuery.setPageable(pageable); nativeSearchQuery.setPageable(pageable);
}
if (indices != null) { if (indices != null) {
nativeSearchQuery.addIndices(indices); nativeSearchQuery.addIndices(indices);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2016 the original author or authors. * Copyright 2013-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,11 +28,12 @@ import org.springframework.data.domain.Sort;
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch
*/ */
public interface Query { public interface Query {
public static final int DEFAULT_PAGE_SIZE = 10; int DEFAULT_PAGE_SIZE = 10;
public static final Pageable DEFAULT_PAGE = new PageRequest(0, DEFAULT_PAGE_SIZE); Pageable DEFAULT_PAGE = PageRequest.of(0, DEFAULT_PAGE_SIZE);
/** /**
* restrict result to entries on given page. Corresponds to the 'start' and 'rows' parameter in elasticsearch * restrict result to entries on given page. Corresponds to the 'start' and 'rows' parameter in elasticsearch

View File

@ -71,13 +71,13 @@ public class ElasticsearchRepositoryExtension extends CdiRepositoryExtensionSupp
private <T> CdiRepositoryBean<T> createRepositoryBean(Class<T> repositoryType, Set<Annotation> qualifiers, private <T> CdiRepositoryBean<T> createRepositoryBean(Class<T> repositoryType, Set<Annotation> qualifiers,
BeanManager beanManager) { BeanManager beanManager) {
Bean<ElasticsearchOperations> elasticsearchOperationsBean = this.elasticsearchOperationsMap.get(qualifiers); if (!this.elasticsearchOperationsMap.containsKey(qualifiers)) {
if (elasticsearchOperationsBean == null) {
throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.", throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.",
ElasticsearchOperations.class.getName(), qualifiers)); ElasticsearchOperations.class.getName(), qualifiers));
} }
Bean<ElasticsearchOperations> elasticsearchOperationsBean = this.elasticsearchOperationsMap.get(qualifiers);
return new ElasticsearchRepositoryBean<>(elasticsearchOperationsBean, qualifiers, repositoryType, beanManager, return new ElasticsearchRepositoryBean<>(elasticsearchOperationsBean, qualifiers, repositoryType, beanManager,
Optional.ofNullable(getCustomImplementationDetector())); Optional.ofNullable(getCustomImplementationDetector()));
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@
package org.springframework.data.elasticsearch.repository.query; package org.springframework.data.elasticsearch.repository.query;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery; import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
@ -33,6 +34,7 @@ import org.springframework.data.util.StreamUtils;
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Kevin Leturc * @author Kevin Leturc
* @author Mark Paluch
*/ */
public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery { public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery {
@ -58,8 +60,8 @@ public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery
return elasticsearchOperations.queryForPage(query, queryMethod.getEntityInformation().getJavaType()); return elasticsearchOperations.queryForPage(query, queryMethod.getEntityInformation().getJavaType());
} else if (queryMethod.isStreamQuery()) { } else if (queryMethod.isStreamQuery()) {
Class<?> entityType = queryMethod.getEntityInformation().getJavaType(); Class<?> entityType = queryMethod.getEntityInformation().getJavaType();
if (query.getPageable() == null) { if (query.getPageable() == Pageable.NONE) {
query.setPageable(new PageRequest(0, 20)); query.setPageable(PageRequest.of(0, 20));
} }
return StreamUtils.createStreamFromIterator((CloseableIterator<Object>) elasticsearchOperations.stream(query, entityType)); return StreamUtils.createStreamFromIterator((CloseableIterator<Object>) elasticsearchOperations.stream(query, entityType));
@ -67,7 +69,7 @@ public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery
} else if (queryMethod.isCollectionQuery()) { } else if (queryMethod.isCollectionQuery()) {
if (accessor.getPageable() == null) { if (accessor.getPageable() == null) {
int itemCount = (int) elasticsearchOperations.count(query, queryMethod.getEntityInformation().getJavaType()); int itemCount = (int) elasticsearchOperations.count(query, queryMethod.getEntityInformation().getJavaType());
query.setPageable(new PageRequest(0, Math.max(1, itemCount))); query.setPageable(PageRequest.of(0, Math.max(1, itemCount)));
} else { } else {
query.setPageable(accessor.getPageable()); query.setPageable(accessor.getPageable());
} }
@ -83,9 +85,9 @@ public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery
Object result = null; Object result = null;
if (queryMethod.isCollectionQuery()) { if (queryMethod.isCollectionQuery()) {
if (accessor.getPageable() == null) { if (accessor.getPageable() == Pageable.NONE) {
int itemCount = (int) elasticsearchOperations.count(query, queryMethod.getEntityInformation().getJavaType()); int itemCount = (int) elasticsearchOperations.count(query, queryMethod.getEntityInformation().getJavaType());
query.setPageable(new PageRequest(0, Math.max(1, itemCount))); query.setPageable(PageRequest.of(0, Math.max(1, itemCount)));
} else { } else {
query.setPageable(accessor.getPageable()); query.setPageable(accessor.getPageable());
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2015 the original author or authors. * Copyright 2013-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,7 +22,6 @@ import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.projection.ProjectionFactory; import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.core.RepositoryMetadata; import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.QueryMethod; import org.springframework.data.repository.query.QueryMethod;
import org.springframework.util.StringUtils;
/** /**
* ElasticsearchQueryMethod * ElasticsearchQueryMethod
@ -30,6 +29,7 @@ import org.springframework.util.StringUtils;
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Oliver Gierke * @author Oliver Gierke
* @author Mark Paluch
*/ */
public class ElasticsearchQueryMethod extends QueryMethod { public class ElasticsearchQueryMethod extends QueryMethod {
@ -45,7 +45,6 @@ public class ElasticsearchQueryMethod extends QueryMethod {
} }
public String getAnnotatedQuery() { public String getAnnotatedQuery() {
String query = (String) AnnotationUtils.getValue(queryAnnotation, "value"); return (String) AnnotationUtils.getValue(queryAnnotation, "value");
return StringUtils.hasText(query) ? query : null;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.springframework.core.convert.support.GenericConversionService; import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.convert.DateTimeConverters; import org.springframework.data.elasticsearch.core.convert.DateTimeConverters;
import org.springframework.data.elasticsearch.core.query.StringQuery; import org.springframework.data.elasticsearch.core.query.StringQuery;
@ -30,6 +31,7 @@ import org.springframework.util.Assert;
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Mark Paluch
*/ */
public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQuery { public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQuery {
@ -50,8 +52,8 @@ public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQue
} }
} }
public ElasticsearchStringQuery(ElasticsearchQueryMethod queryMethod, public ElasticsearchStringQuery(ElasticsearchQueryMethod queryMethod, ElasticsearchOperations elasticsearchOperations,
ElasticsearchOperations elasticsearchOperations, String query) { String query) {
super(queryMethod, elasticsearchOperations); super(queryMethod, elasticsearchOperations);
Assert.notNull(query, "Query cannot be empty"); Assert.notNull(query, "Query cannot be empty");
this.query = query; this.query = query;
@ -65,7 +67,7 @@ public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQue
stringQuery.setPageable(accessor.getPageable()); stringQuery.setPageable(accessor.getPageable());
return elasticsearchOperations.queryForPage(stringQuery, queryMethod.getEntityInformation().getJavaType()); return elasticsearchOperations.queryForPage(stringQuery, queryMethod.getEntityInformation().getJavaType());
} else if (queryMethod.isCollectionQuery()) { } else if (queryMethod.isCollectionQuery()) {
if (accessor.getPageable() != null) { if (accessor.getPageable() != Pageable.NONE) {
stringQuery.setPageable(accessor.getPageable()); stringQuery.setPageable(accessor.getPageable());
} }
return elasticsearchOperations.queryForList(stringQuery, queryMethod.getEntityInformation().getJavaType()); return elasticsearchOperations.queryForList(stringQuery, queryMethod.getEntityInformation().getJavaType());

View File

@ -106,7 +106,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
if (itemCount == 0) { if (itemCount == 0) {
return new PageImpl<>(Collections.<T>emptyList()); return new PageImpl<>(Collections.<T>emptyList());
} }
return this.findAll(new PageRequest(0, Math.max(1, itemCount))); return this.findAll(PageRequest.of(0, Math.max(1, itemCount)));
} }
@Override @Override
@ -122,7 +122,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
return new PageImpl<>(Collections.<T>emptyList()); return new PageImpl<>(Collections.<T>emptyList());
} }
SearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) SearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
.withPageable(new PageRequest(0, itemCount, sort)).build(); .withPageable(PageRequest.of(0, itemCount, sort)).build();
return elasticsearchOperations.queryForPage(query, getEntityClass()); return elasticsearchOperations.queryForPage(query, getEntityClass());
} }
@ -188,7 +188,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
if (count == 0) { if (count == 0) {
return new PageImpl<>(Collections.<T>emptyList()); return new PageImpl<>(Collections.<T>emptyList());
} }
searchQuery.setPageable(new PageRequest(0, count)); searchQuery.setPageable(PageRequest.of(0, count));
return elasticsearchOperations.queryForPage(searchQuery, getEntityClass()); return elasticsearchOperations.queryForPage(searchQuery, getEntityClass());
} }

View File

@ -18,11 +18,10 @@ package org.springframework.data.elasticsearch.repository.support;
import java.io.Serializable; import java.io.Serializable;
import java.util.Optional; import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.repository.core.support.PersistentEntityInformation; import org.springframework.data.repository.core.support.PersistentEntityInformation;
import org.springframework.util.Assert;
/** /**
* Elasticsearch specific implementation of * Elasticsearch specific implementation of
@ -39,17 +38,20 @@ import org.springframework.data.repository.core.support.PersistentEntityInformat
public class MappingElasticsearchEntityInformation<T, ID extends Serializable> public class MappingElasticsearchEntityInformation<T, ID extends Serializable>
extends PersistentEntityInformation<T, ID> implements ElasticsearchEntityInformation<T, ID> { extends PersistentEntityInformation<T, ID> implements ElasticsearchEntityInformation<T, ID> {
private static final Logger logger = LoggerFactory.getLogger(MappingElasticsearchEntityInformation.class);
private final ElasticsearchPersistentEntity<T> entityMetadata; private final ElasticsearchPersistentEntity<T> entityMetadata;
private final String indexName; private final String indexName;
private final String type; private final String type;
public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity) { public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity) {
this(entity, null, null); this(entity, entity.getIndexName(), entity.getIndexType());
} }
public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity, String indexName, String type) { public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity, String indexName, String type) {
super(entity); super(entity);
Assert.notNull(indexName, "IndexName must not be null!");
Assert.notNull(type, "IndexType must not be null!");
this.entityMetadata = entity; this.entityMetadata = entity;
this.indexName = indexName; this.indexName = indexName;
this.type = type; this.type = type;
@ -68,12 +70,12 @@ public class MappingElasticsearchEntityInformation<T, ID extends Serializable>
@Override @Override
public String getIndexName() { public String getIndexName() {
return indexName != null ? indexName : entityMetadata.getIndexName(); return indexName;
} }
@Override @Override
public String getType() { public String getType() {
return type != null ? type : entityMetadata.getIndexType(); return type;
} }
@Override @Override