Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Commit a09032b

Browse files
Fix numbers of queries ran in the project resource
1 parent 29e9043 commit a09032b

File tree

6 files changed

+59
-28
lines changed

6 files changed

+59
-28
lines changed

src/main/java/nl/tudelft/ewi/devhub/server/database/controllers/Commits.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nl.tudelft.ewi.devhub.server.database.controllers;
22

33
import com.google.common.collect.Lists;
4+
import com.google.common.collect.Maps;
45
import com.google.common.eventbus.EventBus;
56
import com.google.inject.Inject;
67
import com.google.inject.persist.Transactional;
@@ -14,13 +15,11 @@
1415
import nl.tudelft.ewi.git.web.api.RepositoriesApi;
1516

1617
import javax.persistence.EntityManager;
17-
import java.util.Collection;
18-
import java.util.Date;
19-
import java.util.List;
20-
import java.util.Optional;
18+
import java.util.*;
2119
import java.util.stream.Collectors;
2220
import java.util.stream.Stream;
2321

22+
import static com.google.common.collect.Maps.uniqueIndex;
2423
import static nl.tudelft.ewi.devhub.server.database.entities.QCommit.commit;
2524

2625
@Slf4j
@@ -42,36 +41,54 @@ public Optional<Commit> retrieve(RepositoryEntity repository, String commitId) {
4241
return Optional.ofNullable(entityManager.find(Commit.class, key));
4342
}
4443

45-
@Transactional
46-
public List<Commit> retrieveCommits(RepositoryEntity repositoryEntity, Collection<String> commitIds) {
44+
private List<Commit> retrieve(RepositoryEntity repositoryEntity, Collection<String> commitIds) {
4745
return query().from(commit)
4846
.where(commit.repository.eq(repositoryEntity).and(commit.commitId.in(commitIds)))
4947
.list(commit);
5048
}
5149

50+
protected Commit createCommit(RepositoryEntity repositoryEntity, String commitId) {
51+
final Commit commit = new Commit();
52+
commit.setCommitId(commitId);
53+
commit.setRepository(repositoryEntity);
54+
commit.setComments(Lists.newArrayList());
55+
commit.setPushTime(new Date());
56+
enhanceCommitSafely(commit);
57+
58+
CreateCommitEvent createCommitEvent = new CreateCommitEvent();
59+
createCommitEvent.setCommitId(commitId);
60+
createCommitEvent.setRepositoryName(repositoryEntity.getRepositoryName());
61+
eventBus.post(createCommitEvent);
62+
return persist(commit);
63+
}
64+
5265
/**
5366
* Ensure that a commit exists in the database. Recursively check if the parents exists as well.
5467
*
5568
* @param repositoryEntity Repository to search commits for.
5669
* @param commitId Commit id of the commit.
57-
* @return The created commit entity.
70+
* @return The existing or created commit entity.
5871
*/
5972
@Transactional
6073
public Commit ensureExists(RepositoryEntity repositoryEntity, String commitId) {
61-
return retrieve(repositoryEntity, commitId).orElseGet(() -> {
62-
final Commit commit = new Commit();
63-
commit.setCommitId(commitId);
64-
commit.setRepository(repositoryEntity);
65-
commit.setComments(Lists.newArrayList());
66-
commit.setPushTime(new Date());
67-
enhanceCommitSafely(commit);
68-
69-
CreateCommitEvent createCommitEvent = new CreateCommitEvent();
70-
createCommitEvent.setCommitId(commitId);
71-
createCommitEvent.setRepositoryName(repositoryEntity.getRepositoryName());
72-
eventBus.post(createCommitEvent);
73-
return persist(commit);
74-
});
74+
return retrieve(repositoryEntity, commitId).orElseGet(() -> createCommit(repositoryEntity, commitId));
75+
}
76+
77+
/**
78+
* Ensure that a commit exists in the database. Recursively check if the parents exists as well.
79+
*
80+
* @param repositoryEntity Repository to search commits for.
81+
* @param commitIds Commit ids of the commit.
82+
* @return The existing or created commit entities.
83+
*/
84+
@Transactional
85+
public List<Commit> ensureExists(RepositoryEntity repositoryEntity, Collection<String> commitIds) {
86+
Map<String, Commit> existingCommits = uniqueIndex(retrieve(repositoryEntity, commitIds), Commit::getCommitId);
87+
88+
return commitIds.stream()
89+
.map(commitId -> Optional.ofNullable(existingCommits.get(commitId)).orElseGet(() ->
90+
createCommit(repositoryEntity, commitId)))
91+
.collect(Collectors.toList());
7592
}
7693

7794
/**

src/main/java/nl/tudelft/ewi/devhub/server/web/resources/repository/AbstractProjectResource.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,7 @@ protected Map<String, Object> getBranchOverviewParameters(String branchName, int
202202
CommitSubList commits = branchApi.retrieveCommitsInBranch((page - 1) * PAGE_SIZE, PAGE_SIZE);
203203

204204
Collection<String> commitIds = getCommitIds(commits);
205-
List<Commit> commitEntities = commitIds.stream()
206-
.map(commitId -> this.commits.ensureExists(repositoryEntity, commitId))
207-
.collect(Collectors.toList());
205+
List<Commit> commitEntities = this.commits.ensureExists(repositoryEntity, commitIds);
208206
Map<String, Commit> commitEntitiesByCommitId = Maps.uniqueIndex(commitEntities, Commit::getCommitId);
209207

210208
parameters.put("commits", commits);

src/test/java/nl/tudelft/ewi/devhub/server/backend/warnings/CheckstyleWarningGeneratorTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import nl.tudelft.ewi.devhub.server.database.entities.Commit;
77
import nl.tudelft.ewi.devhub.server.database.entities.Group;
88
import nl.tudelft.ewi.devhub.server.database.entities.GroupRepository;
9+
import nl.tudelft.ewi.devhub.server.database.entities.RepositoryEntity;
910
import nl.tudelft.ewi.devhub.server.database.entities.warnings.CheckstyleWarning;
1011
import nl.tudelft.ewi.git.models.BlameModel;
1112

@@ -24,14 +25,21 @@
2425
import org.junit.runner.RunWith;
2526
import org.mockito.InjectMocks;
2627
import org.mockito.Mock;
28+
import org.mockito.Mockito;
2729
import org.mockito.junit.MockitoJUnitRunner;
2830

2931
import java.io.IOException;
3032
import java.io.InputStreamReader;
33+
import java.util.Collections;
3134
import java.util.Set;
3235

36+
import static java.util.Collections.singletonList;
3337
import static org.hamcrest.Matchers.contains;
3438
import static org.junit.Assert.assertThat;
39+
import static org.mockito.ArgumentMatchers.any;
40+
import static org.mockito.ArgumentMatchers.anyList;
41+
import static org.mockito.ArgumentMatchers.anyListOf;
42+
import static org.mockito.ArgumentMatchers.anyString;
3543
import static org.mockito.Matchers.any;
3644
import static org.mockito.Matchers.anyInt;
3745
import static org.mockito.Matchers.anyString;
@@ -63,7 +71,7 @@ public void initializeMocks(){
6371
when(commit.getCommitId()).thenReturn(COMMIT_ID);
6472
when(commit.getRepository()).thenReturn(groupRepository);
6573
when(groupRepository.getRepositoryName()).thenReturn("");
66-
when(commits.ensureExists(any(), any())).thenReturn(commit);
74+
when(commits.ensureExists(any(RepositoryEntity.class), anyString())).thenReturn(commit);
6775

6876
when(repositories.getRepository(anyString())).thenReturn(repository);
6977
when(repository.getCommit(COMMIT_ID)).thenReturn(commitApi);

src/test/java/nl/tudelft/ewi/devhub/server/backend/warnings/FindBugsWarningGeneratorTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import nl.tudelft.ewi.devhub.server.database.entities.Commit;
77
import nl.tudelft.ewi.devhub.server.database.entities.Group;
88
import nl.tudelft.ewi.devhub.server.database.entities.GroupRepository;
9+
import nl.tudelft.ewi.devhub.server.database.entities.RepositoryEntity;
910
import nl.tudelft.ewi.devhub.server.database.entities.warnings.FindbugsWarning;
1011
import nl.tudelft.ewi.git.models.BlameModel;
1112
import nl.tudelft.ewi.git.models.DetailedCommitModel;
@@ -31,8 +32,10 @@
3132
import java.io.InputStreamReader;
3233
import java.util.Set;
3334

35+
import static java.util.Collections.singletonList;
3436
import static org.hamcrest.Matchers.contains;
3537
import static org.junit.Assert.assertThat;
38+
import static org.mockito.ArgumentMatchers.anyList;
3639
import static org.mockito.Matchers.any;
3740
import static org.mockito.Matchers.anyInt;
3841
import static org.mockito.Matchers.anyString;
@@ -66,7 +69,7 @@ public void initializeMocks() {
6669
when(commit.getCommitId()).thenReturn(COMMIT_ID);
6770
when(commit.getRepository()).thenReturn(groupRepository);
6871
when(groupRepository.getRepositoryName()).thenReturn("");
69-
when(commits.ensureExists(any(), any())).thenReturn(commit);
72+
when(commits.ensureExists(any(RepositoryEntity.class), anyString())).thenReturn(commit);
7073

7174
when(repositories.getRepository(anyString())).thenReturn(repository);
7275
when(repository.getCommit(COMMIT_ID)).thenReturn(commitApi);

src/test/java/nl/tudelft/ewi/devhub/server/backend/warnings/PMDWarningGeneratorTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import nl.tudelft.ewi.devhub.server.database.entities.Commit;
77
import nl.tudelft.ewi.devhub.server.database.entities.Group;
88
import nl.tudelft.ewi.devhub.server.database.entities.GroupRepository;
9+
import nl.tudelft.ewi.devhub.server.database.entities.RepositoryEntity;
910
import nl.tudelft.ewi.devhub.server.database.entities.warnings.PMDWarning;
1011
import nl.tudelft.ewi.git.models.BlameModel;
1112

@@ -30,8 +31,10 @@
3031
import java.io.InputStreamReader;
3132
import java.util.Set;
3233

34+
import static java.util.Collections.singletonList;
3335
import static org.hamcrest.Matchers.hasItems;
3436
import static org.junit.Assert.assertThat;
37+
import static org.mockito.ArgumentMatchers.anyList;
3538
import static org.mockito.Matchers.any;
3639
import static org.mockito.Matchers.anyInt;
3740
import static org.mockito.Matchers.anyString;
@@ -63,7 +66,8 @@ public void initializeMocks() {
6366
when(commit.getCommitId()).thenReturn(COMMIT_ID);
6467
when(commit.getRepository()).thenReturn(groupRepository);
6568
when(groupRepository.getRepositoryName()).thenReturn("");
66-
when(commits.ensureExists(any(), any())).thenReturn(commit);
69+
70+
when(commits.ensureExists(any(RepositoryEntity.class), anyString())).thenReturn(commit);
6771

6872
when(repositories.getRepository(anyString())).thenReturn(repository);
6973
when(repository.getCommit(COMMIT_ID)).thenReturn(commitApi);

src/test/java/nl/tudelft/ewi/devhub/server/web/resources/repository/AbstractProjectResourceTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.*;
3939
import java.util.concurrent.Executors;
4040

41+
import static java.util.Collections.singletonList;
4142
import static org.junit.Assert.assertEquals;
4243
import static org.mockito.Matchers.any;
4344
import static org.mockito.Matchers.anyString;
@@ -90,7 +91,7 @@ public void setUp() throws Throwable {
9091
groupRepository.setRepositoryName(REPOSITORY_NAME);
9192
group.setRepository(groupRepository);
9293

93-
when(commits.ensureExists(any(), any())).thenReturn(commit);
94+
when(commits.ensureExists(any(RepositoryEntity.class), anyString())).thenReturn(commit);
9495

9596
projectResource = spy(new ProjectResource(templateEngine, currentUser, group, null, null,
9697
null, repositoriesApi, null, commitComments, commentMailer, commits, null, null,

0 commit comments

Comments
 (0)