Skip to content

Commit

Permalink
Remove preql for instance sql filtering. Optimising of listing instan…
Browse files Browse the repository at this point in the history
…ces in admin by batching of requests (return data for multiple instances in a single sql request). Optimising requests by fetching explicitly joined entities (plan, image, flavour... role, etc).
  • Loading branch information
stuartcaunt committed Nov 27, 2024
1 parent 4063cdc commit 5058a8a
Show file tree
Hide file tree
Showing 15 changed files with 242 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public CloudClient getCloudClient(Long id) {
return cloudClient;
}

public List<CloudClient> getCloudClients(List<Long> ids) {
return ids.stream().map(this::getCloudClient).toList();
}

public CloudProviderConfiguration getCloudProviderConfiguration(Long id) {
return this.cloudProviderService.getById(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public User getOwnerByInstanceId(Long instanceId) {
return null;
}

public List<User> getOwnersByInstanceIds(final List<Long> instanceIds) {
final List<InstanceMember> owners = repository.getOwnersByInstanceIds(instanceIds);
return instanceIds.stream().map(instanceId -> owners.stream().filter(owner -> {
return owner.getInstance().getId().equals(instanceId);
})
.findAny()
.map(InstanceMember::getUser)
.orElse(null))
.toList();
}

public void save(@NotNull InstanceMember instanceMember) {
this.repository.save(instanceMember);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Long countAll() {
return this.repository.countAll();
}

public Long countAll(QueryFilter filter) {
public Long countAll(final InstanceFilter filter) {
return this.repository.countAll(filter);
}

Expand Down Expand Up @@ -135,7 +135,7 @@ public void save(Instance instance) {
}
}

public List<Instance> getAll(QueryFilter filter, OrderBy orderBy, Pagination pagination) {
public List<Instance> getAll(final InstanceFilter filter, final OrderBy orderBy, final Pagination pagination) {
return this.repository.getAll(filter, orderBy, pagination);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ public List<InstanceSessionMember> getAllSessionMembersByInstanceId(@NotNull Lon
return this.instanceSessionMemberRepository.getAllSessionMembersByInstanceId(instanceId);
}

public List<List<InstanceSessionMember>> getAllSessionMembersByInstanceIds(@NotNull List<Long> instanceIds) {
List<InstanceSessionMember> ungroupedSessionMembers = this.instanceSessionMemberRepository.getAllSessionMembersByInstanceIds(instanceIds);
return instanceIds.stream().map(id -> {
return ungroupedSessionMembers.stream().filter(sessionMember -> sessionMember.getInstanceSession().getInstance().getId().equals(id)).toList();
}).toList();
}

public InstanceSessionMember getSessionMemberBySessionId(String sessionId) {
return this.instanceSessionMemberRepository.getBySessionId(sessionId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ public class Instance extends Timestampable {
@Column(name = "unrestricted_member_access", nullable = true)
private Date unrestrictedMemberAccess = null;

@OneToMany(orphanRemoval = true, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH})
@JoinColumn(name = "instance_id", foreignKey = @ForeignKey(name = "fk_instance_member_id"), nullable = false)
@OneToMany(mappedBy = "instance", orphanRemoval = true, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH}, fetch = FetchType.LAZY)
private List<InstanceMember> members = new ArrayList<>();

@ManyToMany()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.ill.visa.core.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import eu.ill.visa.core.entity.enumerations.InstanceMemberRole;
import jakarta.persistence.*;
import org.apache.commons.lang3.builder.EqualsBuilder;
Expand Down Expand Up @@ -33,6 +34,14 @@
where i.id = :instanceId
and m.role = 'OWNER'
"""),
@NamedQuery(name = "instanceMember.getOwnersByInstanceIds", query = """
SELECT m FROM InstanceMember m
LEFT JOIN FETCH m.instance i
LEFT JOIN FETCH m.user u
LEFT JOIN FETCH u.affiliation a
where i.id IN :instanceIds
and m.role = 'OWNER'
"""),
})
@Table(name = "instance_member")
public class InstanceMember extends Timestampable {
Expand All @@ -50,6 +59,11 @@ public class InstanceMember extends Timestampable {
@Column(name = "role", length = 255, nullable = false)
private InstanceMemberRole role;

@JsonIgnore
@ManyToOne(optional = false)
@JoinColumn(name = "instance_id", foreignKey = @ForeignKey(name = "fk_instance_id"), nullable = false)
private Instance instance;

private InstanceMember(Builder builder) {
this.user = builder.user;
this.role = builder.role;
Expand Down Expand Up @@ -91,6 +105,14 @@ public void setRole(InstanceMemberRole role) {
this.role = role;
}

public Instance getInstance() {
return instance;
}

public void setInstance(Instance instance) {
this.instance = instance;
}

@Override
public boolean equals(Object object) {
if (object instanceof InstanceMember) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
AND i.active = true
AND instance.deletedAt IS NULL
"""),
@NamedQuery(name = "instanceSessionMember.getAllForInstanceIds", query = """
SELECT i FROM InstanceSessionMember i
LEFT JOIN i.instanceSession instanceSession
LEFT JOIN instanceSession.instance instance
WHERE instance.id IN :instanceIds
AND i.active = true
AND instance.deletedAt IS NULL
"""),
@NamedQuery(name = "instanceSessionMember.getAllByConnectionId", query = """
SELECT i FROM InstanceSessionMember i
LEFT JOIN i.instanceSession instanceSession
Expand Down
4 changes: 3 additions & 1 deletion visa-core/src/main/java/eu/ill/visa/core/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
@NamedQuery(name = "user.getByIdWithRoles", query = """
SELECT u
FROM User u
LEFT OUTER JOIN FETCH u.userRoles
LEFT OUTER JOIN FETCH u.userRoles ur
LEFT OUTER JOIN FETCH ur.role r
LEFT OUTER JOIN FETCH u.affiliation a
WHERE u.id = :id
"""),
@NamedQuery(name = "user.getAll", query = """
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public InstanceMember getOwnerByInstanceId(Long instanceId) {
}
}

public List<InstanceMember> getOwnersByInstanceIds(List<Long> instanceIds) {
final TypedQuery<InstanceMember> query = getEntityManager().createNamedQuery("instanceMember.getOwnersByInstanceIds", InstanceMember.class);
query.setParameter("instanceIds", instanceIds);
return query.getResultList();
}

public void save(InstanceMember instanceMember) {
if (instanceMember.getId() == null) {
persist(instanceMember);
Expand Down
Loading

0 comments on commit 5058a8a

Please sign in to comment.