Skip to content

Commit

Permalink
reducing to one database query
Browse files Browse the repository at this point in the history
  • Loading branch information
ksierks committed Sep 2, 2022
1 parent a133d1e commit 3510e44
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.List;
import java.util.Set;

import javax.persistence.Tuple;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
Expand Down Expand Up @@ -45,15 +47,15 @@ public interface SampleRepository extends IridaJpaRepository<Sample, Long>, Samp
public Sample getSampleBySampleName(Project p, String sampleName) throws EntityNotFoundException;

/**
* Get the {@link Sample} identifiers with the given string sample name from a list of projects.
* Get the {@link Sample} identifiers with the given list of sample names from a list of projects.
*
* @param projectIds The {@link Project} identifiers that the {@link Sample} belongs to.
* @param sampleName The string sample name for a sample
* @return The {@link Sample} for this identifier
* @param projectIds The {@link Project} identifiers that the {@link Sample} belongs to.
* @param sampleNames The list of sample names
* @return A list of {@link Sample} identifiers
* @throws EntityNotFoundException if a sample with this identifier doesn't exist
*/
@Query("select j.sample.id from ProjectSampleJoin j where j.project.id in ?1 and j.sample.sampleName = ?2")
public List<Long> getSampleBySampleNameInProjects(List<Long> projectIds, String sampleName)
@Query("select j.sample.sampleName, j.sample.id from ProjectSampleJoin j where j.project.id in ?1 and j.sample.sampleName in ?2")
public List<Tuple> getSampleIdsBySampleNameInProjects(List<Long> projectIds, List<String> sampleNames)
throws EntityNotFoundException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
Expand Down Expand Up @@ -59,9 +61,11 @@ public ValidateSampleNamesResponse validateSampleNames(Long projectId, ValidateS
if (associatedProjectIds != null) {
projectIds.addAll(associatedProjectIds);
}
List<String> sampleNames = samples.stream().map(ValidateSampleNameModel::getName).collect(Collectors.toList());
Map<String, List<Long>> foundSampleNames = sampleService.getSampleIdsBySampleNameForProjects(projectIds,
sampleNames);
for (ValidateSampleNameModel sample : samples) {
String sampleName = sample.getName();
List<Long> foundSampleIds = sampleService.getSamplesBySampleNameForProjects(projectIds, sampleName);
List<Long> foundSampleIds = foundSampleNames.get(sample.getName());
sample.setIds(foundSampleIds);
}
return new ValidateSampleNamesResponse(samples);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,12 @@ public Sample getSampleBySampleName(Project project, String sampleName) {
@Override
@Transactional(readOnly = true)
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SEQUENCER') or hasPermission(#project, 'canReadProject')")
public List<Long> getSamplesBySampleNameForProjects(List<Long> projectIds, String sampleName) {
List<Long> sampleIds = sampleRepository.getSampleBySampleNameInProjects(projectIds, sampleName);
return sampleIds;
public Map<String, List<Long>> getSampleIdsBySampleNameForProjects(List<Long> projectIds,
List<String> sampleNames) {
return sampleRepository.getSampleIdsBySampleNameInProjects(projectIds, sampleNames)
.stream()
.collect(Collectors.groupingBy(t -> (String) t.get(0),

This comment has been minimized.

Copy link
@ericenns

ericenns Sep 6, 2022

Member

Can you rename t to sampleNameIdTuple so that any other person looking at this can immediately understand the structure.

Collectors.mapping(t -> (Long) t.get(1), Collectors.toList())));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package ca.corefacility.bioinformatics.irida.service.sample;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.*;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -169,13 +166,13 @@ public Page<ProjectSampleJoin> getSamplesForProjectWithName(Project project, Str
public Sample getSampleBySampleName(Project project, String sampleName);

/**
* Get the {@link Sample} identifiers with the given sample name
* Get the {@link Sample} identifiers with the given list of sample names from a list of projects.
*
* @param projectIds The {@link Project} identifiers that the {@link Sample} belongs to.
* @param sampleName The name for the requested sample
* @return A {@link Sample} with the given ID
* @param projectIds The {@link Project} identifiers that the {@link Sample} belongs to.
* @param sampleNames The list of sample names
* @return A list of {@link Sample} identifiers
*/
public List<Long> getSamplesBySampleNameForProjects(List<Long> projectIds, String sampleName);
public Map<String, List<Long>> getSampleIdsBySampleNameForProjects(List<Long> projectIds, List<String> sampleNames);

/**
* Remove a {@link SequencingObject} from a given {@link Sample}. This will delete the
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/resources/js/pages/projects/share/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function ShareApp() {
},
}).then((response) => {
let filtered = response.data.samples.filter(
(sample) => sample.ids.length !== 0
(sample) => sample.ids && sample.ids.length !== 0
);
setExistingIds(
filtered
Expand Down

0 comments on commit 3510e44

Please sign in to comment.