Commit 06595672 authored by Matija Obreza's avatar Matija Obreza

Merge branch 'use-es-for-counting-accessions' into 'master'

Use approx count from ES when paginating accessions

See merge request genesys-pgr/genesys-server!173
parents c8f15d72 440862f5
......@@ -25,7 +25,9 @@ import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
import org.springframework.data.domain.Pageable;
public interface AccessionRepositoryCustom {
......@@ -39,4 +41,5 @@ public interface AccessionRepositoryCustom {
Accession findOne(FaoInstitute institute, String doi, String acceNumb, String genus);
List<Accession> findAll(AccessionFilter filter, Pageable page);
}
......@@ -36,16 +36,24 @@ import javax.persistence.criteria.Root;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionHistoric;
import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.support.Querydsl;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.core.types.dsl.PathBuilderFactory;
import com.querydsl.jpa.JPQLQuery;
@Repository
@Transactional(readOnly = true)
public class AccessionRepositoryCustomImpl implements AccessionRepositoryCustom, InitializingBean {
......@@ -57,6 +65,8 @@ public class AccessionRepositoryCustomImpl implements AccessionRepositoryCustom,
private CriteriaBuilder criteriaBuilder;
PathBuilder<Accession> pathBuilder = new PathBuilderFactory().create(Accession.class);
@Override
public void afterPropertiesSet() throws Exception {
this.criteriaBuilder = em.getCriteriaBuilder();
......@@ -244,4 +254,19 @@ public class AccessionRepositoryCustomImpl implements AccessionRepositoryCustom,
return activeAndHistoric;
}
@Override
public List<Accession> findAll(AccessionFilter filter, Pageable page) {
Querydsl querydsl = new Querydsl(em, pathBuilder);
JPQLQuery<Accession> query = querydsl.createQuery(QAccession.accession).select(QAccession.accession);
if (filter != null) {
// apply filter
query.where(filter.buildQuery());
}
querydsl.applyPagination(page, query);
return query.fetch();
}
}
......@@ -15,6 +15,7 @@
*/
package org.genesys2.server.service.impl;
import java.util.List;
import java.util.UUID;
import org.genesys2.server.model.genesys.Accession;
......@@ -22,10 +23,12 @@ import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -41,6 +44,9 @@ public class AccessionServiceImpl implements AccessionService {
@Autowired
private AccessionRepository accessionRepository;
@Autowired
private ElasticsearchService elasticsearchService;
private <T extends AccessionData> T lazyLoad(T accession) {
if (accession != null) {
AccessionId accessionId = accession.getAccessionId();
......@@ -99,6 +105,8 @@ public class AccessionServiceImpl implements AccessionService {
@Override
@Cacheable(value="apiResponses.accessionApi1.list", unless="#result == null", keyGenerator="shortFilterKeyGenerator")
public Page<Accession> list(AccessionFilter filter, Pageable page) {
return accessionRepository.findAll(filter.buildQuery(), page);
List<Accession> content = accessionRepository.findAll(filter, page);
long total = elasticsearchService.count(Accession.class, filter);
return new PageImpl<>(content, page, total);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment