Skip to content

Commit

Permalink
reduce fetched data
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximilianAnzinger committed Oct 29, 2023
1 parent 7cb5f1c commit d70079a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package de.tum.in.www1.artemis.repository;

import static org.springframework.data.jpa.repository.EntityGraph.EntityGraphType.LOAD;

import java.util.Optional;
import java.util.Set;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand Down Expand Up @@ -47,17 +50,19 @@ public interface CompetencyRepository extends JpaRepository<Competency, Long> {
Optional<Competency> findByIdWithLectureUnits(@Param("competencyId") long competencyId);

@Query("""
SELECT c
FROM Competency c
LEFT JOIN FETCH c.userProgress
LEFT JOIN FETCH c.exercises
LEFT JOIN FETCH c.lectureUnits lu
LEFT JOIN FETCH lu.completedUsers
LEFT JOIN FETCH lu.lecture
LEFT JOIN FETCH lu.exercise
WHERE c.id = :competencyId
SELECT competency
FROM Competency competency
LEFT JOIN competency.userProgress progress
ON competency.id = progress.learningGoal.id AND progress.user.id = :userId
LEFT JOIN FETCH competency.exercises
LEFT JOIN FETCH competency.lectureUnits lectureUnits
LEFT JOIN lectureUnits.completedUsers completedUsers
ON lectureUnits.id = completedUsers.lectureUnit.id AND completedUsers.user.id = :userId
LEFT JOIN FETCH lectureUnits.lecture
WHERE competency.id = :competencyId
""")
Optional<Competency> findByIdWithExercisesAndLectureUnits(@Param("competencyId") Long competencyId);
@EntityGraph(type = LOAD, attributePaths = { "userProgress", "lectureUnits.completedUsers" })
Optional<Competency> findByIdWithExercisesAndLectureUnitsAndProgressForUser(@Param("competencyId") long competencyId, @Param("userId") long userId);

@Query("""
SELECT c
Expand Down Expand Up @@ -162,8 +167,8 @@ default Competency findByIdWithLectureUnitsElseThrow(Long competencyId) {
return findByIdWithLectureUnits(competencyId).orElseThrow(() -> new EntityNotFoundException("Competency", competencyId));
}

default Competency findByIdWithExercisesAndLectureUnitsElseThrow(Long competencyId) {
return findByIdWithExercisesAndLectureUnits(competencyId).orElseThrow(() -> new EntityNotFoundException("Competency", competencyId));
default Competency findByIdWithExercisesAndLectureUnitsAndProgressForUserElseThrow(long competencyId, long userId) {
return findByIdWithExercisesAndLectureUnitsAndProgressForUser(competencyId, userId).orElseThrow(() -> new EntityNotFoundException("Competency", competencyId));
}

long countByCourse(Course course);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,10 @@ public ResponseEntity<Competency> getCompetency(@PathVariable Long competencyId,
long start = System.nanoTime();
var currentUser = userRepository.getUserWithGroupsAndAuthorities();
var course = courseRepository.findByIdElseThrow(courseId);
// TODO: this database request is quite intense and might lead to memory issues
// it loads many completed user objects even if those are not necessary which could include are large object graph
var competency = competencyRepository.findByIdWithExercisesAndLectureUnitsElseThrow(competencyId);
var competency = competencyRepository.findByIdWithExercisesAndLectureUnitsAndProgressForUserElseThrow(competencyId, currentUser.getId());
checkAuthorizationForCompetency(Role.STUDENT, course, competency);

competency.setUserProgress(competencyProgressRepository.findByCompetencyIdAndUserId(competencyId, currentUser.getId()).map(Set::of).orElse(Set.of()));
// Set completion status and remove exercise units (redundant as we also return all exercises)
competency.setLectureUnits(competency.getLectureUnits().stream().filter(lectureUnit -> !(lectureUnit instanceof ExerciseUnit))
.filter(lectureUnit -> authorizationCheckService.isAllowedToSeeLectureUnit(lectureUnit, currentUser))
competency.setLectureUnits(competency.getLectureUnits().stream().filter(lectureUnit -> authorizationCheckService.isAllowedToSeeLectureUnit(lectureUnit, currentUser))
.peek(lectureUnit -> lectureUnit.setCompleted(lectureUnit.isCompletedFor(currentUser))).collect(Collectors.toSet()));
competency.setExercises(
competency.getExercises().stream().filter(exercise -> authorizationCheckService.isAllowedToSeeExercise(exercise, currentUser)).collect(Collectors.toSet()));
Expand Down

0 comments on commit d70079a

Please sign in to comment.