Skip to content

Commit

Permalink
Merge pull request #325 from BBMRI-ERIC/fix/memory_consumption
Browse files Browse the repository at this point in the history
fix: memory consumption in attachments and notifications
  • Loading branch information
RadovanTomik committed May 21, 2024
2 parents 2bee2a9 + 4c890a5 commit d4bc135
Show file tree
Hide file tree
Showing 12 changed files with 348 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.action.Action;
import org.springframework.stereotype.Component;
Expand All @@ -25,7 +24,6 @@ public class InitializeStateForResourceAction implements Action<String, String>

@Override
@Transactional
@Async
public void execute(StateContext<String, String> context) {
String negotiationId = context.getMessage().getHeaders().get("negotiationId", String.class);
Negotiation negotiation = negotiationRepository.findDetailedById(negotiationId).orElseThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
Expand Down Expand Up @@ -76,18 +75,16 @@ public class Negotiation extends AuditEntity {

@OneToMany(
mappedBy = "negotiation",
cascade = {CascadeType.MERGE},
fetch = FetchType.LAZY)
cascade = {CascadeType.MERGE})
private Set<Attachment> attachments;

@OneToMany(
mappedBy = "negotiation",
cascade = {CascadeType.PERSIST, CascadeType.REMOVE},
fetch = FetchType.LAZY)
cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
@Exclude
private Set<PersonNegotiationRole> persons = new HashSet<>();

@OneToMany(mappedBy = "negotiation", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@OneToMany(mappedBy = "negotiation", cascade = CascadeType.MERGE)
@Exclude
private Set<Request> requests;

Expand All @@ -104,7 +101,7 @@ public class Negotiation extends AuditEntity {
@Enumerated(EnumType.STRING)
private NegotiationState currentState = NegotiationState.SUBMITTED;

@ElementCollection(fetch = FetchType.EAGER)
@ElementCollection
@CollectionTable(
name = "resource_state_per_negotiation",
joinColumns = {@JoinColumn(name = "negotiation_id", referencedColumnName = "id")})
Expand All @@ -115,17 +112,13 @@ public class Negotiation extends AuditEntity {
@Builder.Default
private Map<String, NegotiationResourceState> currentStatePerResource = new HashMap<>();

@OneToMany(
fetch = FetchType.EAGER,
cascade = {CascadeType.ALL})
@OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name = "negotiation_id", referencedColumnName = "id")
@Setter(AccessLevel.NONE)
@Builder.Default
private Set<NegotiationLifecycleRecord> lifecycleHistory = creteInitialHistory();

@OneToMany(
fetch = FetchType.EAGER,
cascade = {CascadeType.ALL})
@OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name = "negotiation_id", referencedColumnName = "id")
@Setter(AccessLevel.NONE)
@Builder.Default
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package eu.bbmri_eric.negotiator.database.model.views;

import eu.bbmri_eric.negotiator.database.model.NotificationEmailStatus;
import eu.bbmri_eric.negotiator.database.model.Person;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class NotificationViewDTO {
private Long id;
private String message;
private NotificationEmailStatus emailStatus;
private String negotiationId;
private String negotiationTitle;
private Person recipient;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ Optional<NegotiationResourceState> findNegotiationResourceStateById(
List<Negotiation> findByModifiedDateBeforeAndCurrentState(
LocalDateTime thresholdTime, NegotiationState currentState);

@Query(
value =
"SELECT EXISTS ("
+ "SELECT n.id "
+ "FROM negotiation n "
+ "WHERE n.id = :negotiationId AND "
+ "n.created_by = :personId)",
nativeQuery = true)
boolean isNegotiationCreator(String negotiationId, Long personId);

@Query(
value =
"SELECT EXISTS ("
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

import eu.bbmri_eric.negotiator.database.model.Notification;
import eu.bbmri_eric.negotiator.database.model.NotificationEmailStatus;
import eu.bbmri_eric.negotiator.database.model.views.NotificationViewDTO;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface NotificationRepository extends JpaRepository<Notification, Long> {

List<Notification> findByRecipientId(Long personId);

List<Notification> findByEmailStatus(NotificationEmailStatus status);

List<Notification> findByEmailStatusAndMessageEndsWith(
NotificationEmailStatus status, String messageSuffix);

List<Notification> findByRecipientIdAndEmailStatus(
@Query(
"SELECT new eu.bbmri_eric.negotiator.database.model.views.NotificationViewDTO("
+ "nt.id, nt.message, nt.emailStatus, ng.id, ng.title, p) "
+ "FROM Notification nt "
+ "JOIN nt.negotiation ng "
+ "JOIN nt.recipient p "
+ "WHERE p.id = :recipientId AND "
+ "nt.emailStatus = :status")
List<NotificationViewDTO> findViewByRecipientIdAndEmailStatus(
Long recipientId, NotificationEmailStatus status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public interface PersonRepository
@EntityGraph(value = "person-detailed")
boolean existsByIdAndResourcesIn(Long id, Set<Resource> resources);

@Query(
value =
"SELECT EXISTS ("
+ "SELECT n.id "
+ "FROM negotiation n "
+ "WHERE n.id = :negotiationId AND "
+ "n.created_by = :personId)",
nativeQuery = true)
boolean isNegotiationCreator(Long personId, String negotiationId);

@Query(
value =
"SELECT EXISTS (SELECT rs.id "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public class NegotiationServiceImpl implements NegotiationService {

@Override
public boolean isNegotiationCreator(String negotiationId) {
return negotiationRepository.isNegotiationCreator(
negotiationId, NegotiatorUserDetailsService.getCurrentlyAuthenticatedUserInternalId());
return personRepository.isNegotiationCreator(
NegotiatorUserDetailsService.getCurrentlyAuthenticatedUserInternalId(), negotiationId);
}

/**
Expand All @@ -64,9 +64,6 @@ public boolean isNegotiationCreator(String negotiationId) {
*/
@Override
public boolean isAuthorizedForNegotiation(String negotiationId) {
boolean isrepre =
personService.isRepresentativeOfAnyResourceOfNegotiation(
NegotiatorUserDetailsService.getCurrentlyAuthenticatedUserInternalId(), negotiationId);
return isNegotiationCreator(negotiationId)
|| personService.isRepresentativeOfAnyResourceOfNegotiation(
NegotiatorUserDetailsService.getCurrentlyAuthenticatedUserInternalId(), negotiationId);
Expand Down Expand Up @@ -98,12 +95,7 @@ private List<Attachment> findAttachments(Set<AttachmentMetadataDTO> attachmentDT

@Override
public boolean exists(String negotiationId) {
try {
findEntityById(negotiationId, false);
return true;
} catch (EntityNotFoundException ex) {
return false;
}
return negotiationRepository.existsById(negotiationId);
}

private void addPersonToNegotiation(
Expand Down

0 comments on commit d4bc135

Please sign in to comment.