Performance optimization to avoid loading Collaboration table per map.

This commit is contained in:
Paulo Gustavo Veiga 2022-09-26 10:25:23 -07:00
parent 0c88b8a474
commit 86964febc1
3 changed files with 33 additions and 17 deletions

View File

@ -112,10 +112,18 @@ public class Collaborator implements Serializable {
public boolean identityEquality(@Nullable Collaborator that) {
if (this == that) return true;
if (that == null) return false;
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (id != that.getId()) {
return false;
}
if (id != that.getId()) return false;
return email != null ? email.equals(that.getEmail()) : that.getEmail() == null;
}

View File

@ -63,7 +63,7 @@ public class Mindmap implements Serializable {
@Column(name = "public")
private boolean isPublic;
@OneToMany(mappedBy = "mindMap", orphanRemoval = true, cascade = {CascadeType.ALL})
@OneToMany(mappedBy = "mindMap", orphanRemoval = true, cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
private Set<Collaboration> collaborations = new HashSet<>();
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
@ -177,15 +177,14 @@ public class Mindmap implements Serializable {
return result;
}
public boolean isCreator(@NotNull User user) {
return this.getCreator()!=null && this.getCreator().identityEquality(user);
}
public boolean isPublic() {
return isPublic;
}
//@Todo: This is a hack to overcome some problem with JS EL. For some reason, ${mindmap.public} fails as not supported.
// More research is needed...
public boolean isAccessible() {
return isPublic();
}
public void setPublic(boolean isPublic) {
this.isPublic = isPublic;

View File

@ -132,8 +132,17 @@ public class RestMindmapInfo {
}
public String getRole() {
final Optional<Collaboration> collaboration = mindmap.findCollaboration(Utils.getUser());
return collaboration.map(value -> value.getRole().getLabel()).orElse(ROLE_NONE);
final User user = Utils.getUser();
String result;
if (mindmap.isCreator(user)) {
// Performance hack. In case that the person is the creator, assume that the role is owner.
// This is to avoid loading all the collaboration maps per map.
result = CollaborationRole.OWNER.getLabel();
} else {
final Optional<Collaboration> collaboration = mindmap.findCollaboration(user);
result = collaboration.map(value -> value.getRole().getLabel()).orElse(ROLE_NONE);
}
return result;
}
public void setRole(String value) {