Skip to content

Commit

Permalink
optimize database requests
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu DEHARBE <[email protected]>
  • Loading branch information
Mathieu-Deharbe committed Oct 10, 2024
1 parent 94c049d commit 0dcc187
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public interface ModificationRepository extends JpaRepository<ModificationEntity
@Query(value = "SELECT new ModificationEntity(m.id, m.type) FROM ModificationEntity m WHERE m.id IN (?1)")
List<ModificationEntity> findMetadataIn(List<UUID> uuids);

/**
* @return base data of the network modifications (those from the main table, not those specific to each modification)
*/
@Query(value = "SELECT new ModificationEntity(m.id, m.type, m.date, m.stashed, m.activated, m.messageType, m.messageValues) FROM ModificationEntity m WHERE m.id IN (?1)")
List<ModificationEntity> findBaseDataByIdIn(List<UUID> uuids);

@Query(value = "SELECT m FROM ModificationEntity m WHERE m.id IN (?1) ORDER BY m.modificationsOrder")
List<ModificationEntity> findAllByIdIn(List<UUID> uuids);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,6 @@ public ModificationInfos getModificationInfo(UUID modificationUuid) {
return getModificationInfos(optionalModificationEntity.get());
}

@Transactional(readOnly = true)
public ModificationInfos getMetadata(UUID modificationUuid) {
Optional<ModificationEntity> optionalModificationEntity = modificationRepository.findById(modificationUuid);
if (!optionalModificationEntity.isPresent()) {
throw new NetworkModificationException(MODIFICATION_NOT_FOUND, modificationUuid.toString());
}
return ModificationInfos.fromEntity(optionalModificationEntity.get());
}

@Transactional // To have the 2 delete in the same transaction (atomic)
public void deleteModificationGroup(UUID groupUuid, boolean errorOnGroupNotFound) {
try {
Expand Down Expand Up @@ -398,18 +389,27 @@ public List<ModificationInfos> getModificationsInfos(@NonNull List<UUID> uuids)
}

/**
* @param onlyMetadata if true, only returns the basic data common to all the modificaions
* @param onlyMetadata if true, only returns the basic data common to all the modifications
* @return the data from all the network modification contained in the composite modification sent as parameters
*/
@Transactional(readOnly = true)
public List<ModificationInfos> getCompositeModificationsContentInfos(@NonNull List<UUID> uuids, boolean onlyMetadata) {
List<ModificationInfos> entities = new ArrayList<>();
uuids.forEach(uuid -> {
List<UUID> foundEntities = modificationRepository.findModificationIdsByCompositeModificationId(uuid);
List<ModificationInfos> orderedModifications = foundEntities
.stream()
.map(onlyMetadata ? this::getMetadata : this::getModificationInfo)
.toList();
List<UUID> networkModificationsUuids = modificationRepository.findModificationIdsByCompositeModificationId(uuid);
List<ModificationInfos> orderedModifications;
if (onlyMetadata) {
List<ModificationEntity> networkModifications = modificationRepository.findBaseDataByIdIn(networkModificationsUuids);
orderedModifications = networkModifications
.stream()
.map(this::getModificationInfos)
.toList();
} else {
orderedModifications = networkModificationsUuids
.stream()
.map(this::getModificationInfo)
.toList();
}
entities.addAll(orderedModifications);
}
);
Expand Down

0 comments on commit 0dcc187

Please sign in to comment.