From 2629c4fbdaa40befa1f7a6e2c797dfda3c8ee19f Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Mon, 22 Jan 2024 14:59:43 +0100 Subject: [PATCH 01/24] Optimize get tabular modification Signed-off-by: Etienne Homer --- .../entities/TabularModificationEntity.java | 11 +++-- .../GeneratorModificationRepository.java | 37 +++++++++++++++++ .../LoadModificationRepository.java | 21 ++++++++++ .../repositories/ModificationRepository.java | 7 +--- .../NetworkModificationRepository.java | 40 ++++++++++++++++--- 5 files changed, 99 insertions(+), 17 deletions(-) create mode 100644 src/main/java/org/gridsuite/modification/server/repositories/GeneratorModificationRepository.java create mode 100644 src/main/java/org/gridsuite/modification/server/repositories/LoadModificationRepository.java diff --git a/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java b/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java index 80ef6cdd2..188e64a5a 100644 --- a/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java +++ b/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java @@ -7,21 +7,20 @@ package org.gridsuite.modification.server.entities; import jakarta.persistence.*; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.NonNull; -import lombok.Setter; +import lombok.*; import java.util.List; import java.util.stream.Collectors; import org.gridsuite.modification.server.ModificationType; import org.gridsuite.modification.server.dto.*; +import org.gridsuite.modification.server.entities.equipment.modification.GeneratorModificationEntity; /** * @author Etienne Homer */ @NoArgsConstructor +@AllArgsConstructor @Getter @Setter @Entity @@ -62,10 +61,10 @@ public void update(@NonNull ModificationInfos modificationInfos) { private void assignAttributes(TabularModificationInfos tabularModificationInfos) { modificationType = tabularModificationInfos.getModificationType(); if (modifications == null) { - modifications = tabularModificationInfos.getModifications().stream().map(ModificationInfos::toEntity).toList(); + modifications = tabularModificationInfos.getModifications().stream().map(modificationInfos -> modificationInfos.toEntity()).toList(); } else { modifications.clear(); - modifications.addAll(tabularModificationInfos.getModifications().stream().map(ModificationInfos::toEntity).toList()); + modifications.addAll(tabularModificationInfos.getModifications().stream().map(modificationInfos -> modificationInfos.toEntity()).toList()); } } } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/GeneratorModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/GeneratorModificationRepository.java new file mode 100644 index 000000000..a8c72c418 --- /dev/null +++ b/src/main/java/org/gridsuite/modification/server/repositories/GeneratorModificationRepository.java @@ -0,0 +1,37 @@ +/* + Copyright (c) 2024, RTE (http://www.rte-france.com) + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.gridsuite.modification.server.repositories; + +import org.gridsuite.modification.server.entities.ModificationEntity; +import org.gridsuite.modification.server.entities.TabularModificationEntity; +import org.gridsuite.modification.server.entities.equipment.modification.GeneratorModificationEntity; +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.UUID; + +/** + * @author Etienne Homer + */ +@Repository +public interface GeneratorModificationRepository extends JpaRepository { + + @EntityGraph(attributePaths = {"reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD) + List findAllWithReactiveCapabilityCurvePointsByIdIn(List ids); + + @EntityGraph(attributePaths = {"modifications", "modifications.reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD) + Optional findAllWithReactiveCapabilityCurvePointsById(UUID id); + +// @EntityGraph(attributePaths = {"reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD) +// Set findAllReactiveCapabilityCurvePointsByIdIn(List ids); + +} diff --git a/src/main/java/org/gridsuite/modification/server/repositories/LoadModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/LoadModificationRepository.java new file mode 100644 index 000000000..8e36cc3fa --- /dev/null +++ b/src/main/java/org/gridsuite/modification/server/repositories/LoadModificationRepository.java @@ -0,0 +1,21 @@ +/* + Copyright (c) 2024, RTE (http://www.rte-france.com) + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.gridsuite.modification.server.repositories; + +import org.gridsuite.modification.server.entities.equipment.modification.LoadModificationEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.UUID; + +/** + * @author Etienne Homer + */ +@Repository +public interface LoadModificationRepository extends JpaRepository { + +} diff --git a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java index 43a0aba48..8cec7ae9a 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java @@ -32,11 +32,8 @@ public interface ModificationRepository extends JpaRepository findAllBaseByGroupId(UUID uuid); - @EntityGraph(attributePaths = {"modifications", "modifications.reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD) - Optional findAllWithReactiveCapabilityCurvePointsById(UUID id); - - @EntityGraph(attributePaths = {"reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD) - Set findAllReactiveCapabilityCurvePointsByIdIn(List ids); + @Query(value = "SELECT modifications_id from tabular_modification_modifications where tabular_modification_entity_id= ?1", nativeQuery = true) + List findSubModificationsIds(UUID uuid); Integer countByGroupIdAndStashed(UUID groupId, boolean stashed); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index 64627cee5..d0a53ad12 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -12,6 +12,8 @@ import org.gridsuite.modification.server.entities.ModificationEntity; import org.gridsuite.modification.server.entities.ModificationGroupEntity; import org.gridsuite.modification.server.entities.TabularModificationEntity; +import org.gridsuite.modification.server.entities.equipment.modification.GeneratorModificationEntity; +import org.gridsuite.modification.server.entities.equipment.modification.LoadModificationEntity; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @@ -32,17 +34,25 @@ public class NetworkModificationRepository { private final ModificationRepository modificationRepository; + private final GeneratorModificationRepository generatorModificationRepository; + + private final LoadModificationRepository loadModificationRepository; + private static final String MODIFICATION_NOT_FOUND_MESSAGE = "Modification (%s) not found"; - public NetworkModificationRepository(ModificationGroupRepository modificationGroupRepository, ModificationRepository modificationRepository) { + public NetworkModificationRepository(ModificationGroupRepository modificationGroupRepository, ModificationRepository modificationRepository, GeneratorModificationRepository generatorModificationRepository, LoadModificationRepository loadModificationRepository) { this.modificationGroupRepository = modificationGroupRepository; this.modificationRepository = modificationRepository; + this.generatorModificationRepository = generatorModificationRepository; + this.loadModificationRepository = loadModificationRepository; } @Transactional // To have the 2 delete in the same transaction (atomic) public void deleteAll() { modificationRepository.deleteAll(); modificationGroupRepository.deleteAll(); + generatorModificationRepository.deleteAll(); + loadModificationRepository.deleteAll(); } @Transactional // To have all create in the same transaction (atomic) @@ -155,10 +165,10 @@ public TabularModificationEntity loadTabularModificationSubEntities(Modification TabularModificationEntity tabularModificationEntity = (TabularModificationEntity) modificationEntity; switch (tabularModificationEntity.getModificationType()) { case GENERATOR_MODIFICATION: - tabularModificationEntity = modificationRepository.findAllWithReactiveCapabilityCurvePointsById(modificationEntity.getId()).orElseThrow(() -> - new NetworkModificationException(MODIFICATION_NOT_FOUND, String.format(MODIFICATION_NOT_FOUND_MESSAGE, modificationEntity.getId())) - ); - modificationRepository.findAllReactiveCapabilityCurvePointsByIdIn(tabularModificationEntity.getModifications().stream().map(ModificationEntity::getId).toList()); +// tabularModificationEntity = generatorModificationRepository.findAllWithReactiveCapabilityCurvePointsById(modificationEntity.getId()).orElseThrow(() -> +// new NetworkModificationException(MODIFICATION_NOT_FOUND, String.format(MODIFICATION_NOT_FOUND_MESSAGE, modificationEntity.getId())) +// ); + generatorModificationRepository.findAllWithReactiveCapabilityCurvePointsByIdIn(tabularModificationEntity.getModifications().stream().map(ModificationEntity::getId).toList()); break; default: break; @@ -191,7 +201,25 @@ public ModificationInfos getModificationInfo(UUID modificationUuid) { if (!optionalModificationEntity.isPresent()) { throw new NetworkModificationException(MODIFICATION_NOT_FOUND, modificationUuid.toString()); } - return getModificationInfos(optionalModificationEntity.get()); + ModificationEntity modificationEntity = optionalModificationEntity.get(); + if(modificationEntity instanceof TabularModificationEntity tabularModificationEntity) { + List entities = new ArrayList<>(); + List ids = modificationRepository.findSubModificationsIds(modificationUuid); + switch (tabularModificationEntity.getModificationType()) { + case GENERATOR_MODIFICATION -> { + List generatorModificationEntities = generatorModificationRepository.findAllWithReactiveCapabilityCurvePointsByIdIn(ids); + entities.addAll(generatorModificationEntities); + } + case LOAD_MODIFICATION -> { + List loadModificationEntities = loadModificationRepository.findAllById(ids); + entities.addAll(loadModificationEntities); + + } + } + tabularModificationEntity.setModifications(entities); + return getModificationInfos(tabularModificationEntity); + } + return getModificationInfos(modificationEntity); } @Transactional // To have the 2 delete in the same transaction (atomic) From 0610ab2c91ee2994209b6644ecab951fe60c61aa Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Mon, 22 Jan 2024 15:09:48 +0100 Subject: [PATCH 02/24] clean Signed-off-by: Etienne Homer --- .../GeneratorModificationRepository.java | 6 ------ .../NetworkModificationRepository.java | 14 ++------------ 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/GeneratorModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/GeneratorModificationRepository.java index a8c72c418..9e0a1ecc2 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/GeneratorModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/GeneratorModificationRepository.java @@ -6,17 +6,14 @@ */ package org.gridsuite.modification.server.repositories; -import org.gridsuite.modification.server.entities.ModificationEntity; import org.gridsuite.modification.server.entities.TabularModificationEntity; import org.gridsuite.modification.server.entities.equipment.modification.GeneratorModificationEntity; import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.List; import java.util.Optional; -import java.util.Set; import java.util.UUID; /** @@ -31,7 +28,4 @@ public interface GeneratorModificationRepository extends JpaRepository findAllWithReactiveCapabilityCurvePointsById(UUID id); -// @EntityGraph(attributePaths = {"reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD) -// Set findAllReactiveCapabilityCurvePointsByIdIn(List ids); - } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index d0a53ad12..88ae361cc 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -165,9 +165,6 @@ public TabularModificationEntity loadTabularModificationSubEntities(Modification TabularModificationEntity tabularModificationEntity = (TabularModificationEntity) modificationEntity; switch (tabularModificationEntity.getModificationType()) { case GENERATOR_MODIFICATION: -// tabularModificationEntity = generatorModificationRepository.findAllWithReactiveCapabilityCurvePointsById(modificationEntity.getId()).orElseThrow(() -> -// new NetworkModificationException(MODIFICATION_NOT_FOUND, String.format(MODIFICATION_NOT_FOUND_MESSAGE, modificationEntity.getId())) -// ); generatorModificationRepository.findAllWithReactiveCapabilityCurvePointsByIdIn(tabularModificationEntity.getModifications().stream().map(ModificationEntity::getId).toList()); break; default: @@ -206,15 +203,8 @@ public ModificationInfos getModificationInfo(UUID modificationUuid) { List entities = new ArrayList<>(); List ids = modificationRepository.findSubModificationsIds(modificationUuid); switch (tabularModificationEntity.getModificationType()) { - case GENERATOR_MODIFICATION -> { - List generatorModificationEntities = generatorModificationRepository.findAllWithReactiveCapabilityCurvePointsByIdIn(ids); - entities.addAll(generatorModificationEntities); - } - case LOAD_MODIFICATION -> { - List loadModificationEntities = loadModificationRepository.findAllById(ids); - entities.addAll(loadModificationEntities); - - } + case GENERATOR_MODIFICATION -> entities.addAll(generatorModificationRepository.findAllWithReactiveCapabilityCurvePointsByIdIn(ids)); + case LOAD_MODIFICATION -> entities.addAll(loadModificationRepository.findAllById(ids)); } tabularModificationEntity.setModifications(entities); return getModificationInfos(tabularModificationEntity); From 737df215c39a83291765e22911aee92e6924dd53 Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Mon, 22 Jan 2024 16:33:05 +0100 Subject: [PATCH 03/24] Clean Signed-off-by: Etienne Homer --- .../server/repositories/NetworkModificationRepository.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index 88ae361cc..45659b217 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -161,8 +161,7 @@ public List getModificationsMetadata(UUID groupUuid, boolean } } - public TabularModificationEntity loadTabularModificationSubEntities(ModificationEntity modificationEntity) { - TabularModificationEntity tabularModificationEntity = (TabularModificationEntity) modificationEntity; + public TabularModificationEntity loadTabularModificationSubEntities(TabularModificationEntity tabularModificationEntity) { switch (tabularModificationEntity.getModificationType()) { case GENERATOR_MODIFICATION: generatorModificationRepository.findAllWithReactiveCapabilityCurvePointsByIdIn(tabularModificationEntity.getModifications().stream().map(ModificationEntity::getId).toList()); @@ -174,8 +173,8 @@ public TabularModificationEntity loadTabularModificationSubEntities(Modification } public ModificationInfos getModificationInfos(ModificationEntity modificationEntity) { - if (modificationEntity instanceof TabularModificationEntity) { - return loadTabularModificationSubEntities(modificationEntity).toModificationInfos(); + if (modificationEntity instanceof TabularModificationEntity tabularModificationEntity) { + return loadTabularModificationSubEntities(tabularModificationEntity).toModificationInfos(); } return modificationEntity.toModificationInfos(); } From 030ce7b686d1ecdda9585d8d572cd5a68a97237d Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Mon, 22 Jan 2024 16:35:59 +0100 Subject: [PATCH 04/24] clean Signed-off-by: Etienne Homer --- .../server/entities/TabularModificationEntity.java | 1 - .../server/repositories/ModificationRepository.java | 5 ----- .../server/repositories/NetworkModificationRepository.java | 4 +--- 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java b/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java index 188e64a5a..d7b08e33f 100644 --- a/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java +++ b/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java @@ -14,7 +14,6 @@ import org.gridsuite.modification.server.ModificationType; import org.gridsuite.modification.server.dto.*; -import org.gridsuite.modification.server.entities.equipment.modification.GeneratorModificationEntity; /** * @author Etienne Homer diff --git a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java index 8cec7ae9a..40a5efafa 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java @@ -7,14 +7,9 @@ package org.gridsuite.modification.server.repositories; import java.util.List; -import java.util.Optional; -import java.util.Set; import java.util.UUID; import org.gridsuite.modification.server.entities.ModificationEntity; -import org.gridsuite.modification.server.entities.TabularModificationEntity; -import org.gridsuite.modification.server.entities.equipment.modification.GeneratorModificationEntity; -import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index 45659b217..ef929786e 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -12,8 +12,6 @@ import org.gridsuite.modification.server.entities.ModificationEntity; import org.gridsuite.modification.server.entities.ModificationGroupEntity; import org.gridsuite.modification.server.entities.TabularModificationEntity; -import org.gridsuite.modification.server.entities.equipment.modification.GeneratorModificationEntity; -import org.gridsuite.modification.server.entities.equipment.modification.LoadModificationEntity; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @@ -198,7 +196,7 @@ public ModificationInfos getModificationInfo(UUID modificationUuid) { throw new NetworkModificationException(MODIFICATION_NOT_FOUND, modificationUuid.toString()); } ModificationEntity modificationEntity = optionalModificationEntity.get(); - if(modificationEntity instanceof TabularModificationEntity tabularModificationEntity) { + if (modificationEntity instanceof TabularModificationEntity tabularModificationEntity) { List entities = new ArrayList<>(); List ids = modificationRepository.findSubModificationsIds(modificationUuid); switch (tabularModificationEntity.getModificationType()) { From 244b52a3f044ed6583f70bef03a93ab4720c6aa0 Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Mon, 22 Jan 2024 16:38:01 +0100 Subject: [PATCH 05/24] clean Signed-off-by: Etienne Homer --- .../server/entities/TabularModificationEntity.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java b/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java index d7b08e33f..52304798f 100644 --- a/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java +++ b/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java @@ -60,10 +60,10 @@ public void update(@NonNull ModificationInfos modificationInfos) { private void assignAttributes(TabularModificationInfos tabularModificationInfos) { modificationType = tabularModificationInfos.getModificationType(); if (modifications == null) { - modifications = tabularModificationInfos.getModifications().stream().map(modificationInfos -> modificationInfos.toEntity()).toList(); + modifications = tabularModificationInfos.getModifications().stream().map(ModificationInfos::toEntity).toList(); } else { modifications.clear(); - modifications.addAll(tabularModificationInfos.getModifications().stream().map(modificationInfos -> modificationInfos.toEntity()).toList()); + modifications.addAll(tabularModificationInfos.getModifications().stream().map(ModificationInfos::toEntity).toList()); } } } From 475b4fd8f777e60a302b5115d328b671c064ac67 Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Fri, 26 Jan 2024 14:37:37 +0100 Subject: [PATCH 06/24] Extends to all tabular modifs Signed-off-by: Etienne Homer --- .../EquipmentModificationRepositories.java | 109 ++++++++++++++++++ .../NetworkModificationRepository.java | 21 +--- .../BatteryModificationRepository.java | 21 ++++ .../GeneratorModificationRepository.java | 3 +- .../LineModificationRepository.java | 21 ++++ .../LoadModificationRepository.java | 2 +- ...huntCompensatorModificationRepository.java | 20 ++++ .../SubstationModificationRepository.java | 21 ++++ ...ingsTransformerModificationRepository.java | 21 ++++ .../VoltageLevelModificationRepository.java | 21 ++++ 10 files changed, 242 insertions(+), 18 deletions(-) create mode 100644 src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java create mode 100644 src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java rename src/main/java/org/gridsuite/modification/server/repositories/{ => equipmentmodification}/GeneratorModificationRepository.java (94%) create mode 100644 src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java rename src/main/java/org/gridsuite/modification/server/repositories/{ => equipmentmodification}/LoadModificationRepository.java (89%) create mode 100644 src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java create mode 100644 src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java create mode 100644 src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java create mode 100644 src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java diff --git a/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java b/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java new file mode 100644 index 000000000..483aa05ff --- /dev/null +++ b/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java @@ -0,0 +1,109 @@ +package org.gridsuite.modification.server.repositories; + +import org.gridsuite.modification.server.ModificationType; +import org.gridsuite.modification.server.entities.ModificationEntity; +import org.gridsuite.modification.server.repositories.equipmentmodification.*; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +@Repository +public class EquipmentModificationRepositories { + + private final BatteryModificationRepository batteryModificationRepository; + + private final GeneratorModificationRepository generatorModificationRepository; + + private final LineModificationRepository lineModificationRepository; + + private final LoadModificationRepository loadModificationRepository; + + private final ShuntCompensatorModificationRepository shuntCompensatorModificationRepository; + + private final SubstationModificationRepository substationModificationRepository; + + private final TwoWindingsTransformerModificationRepository twoWindingsTransformerModificationRepository; + + private final VoltageLevelModificationRepository voltageLevelModificationRepository; + + public EquipmentModificationRepositories( + BatteryModificationRepository batteryModificationRepository, + GeneratorModificationRepository generatorModificationRepository, + LineModificationRepository lineModificationRepository, + LoadModificationRepository loadModificationRepository, + ShuntCompensatorModificationRepository shuntCompensatorModificationRepository, + SubstationModificationRepository substationModificationRepository, + TwoWindingsTransformerModificationRepository twoWindingsTransformerModificationRepository, + VoltageLevelModificationRepository voltageLevelModificationRepository + ) { + this.batteryModificationRepository = batteryModificationRepository; + this.generatorModificationRepository = generatorModificationRepository; + this.lineModificationRepository = lineModificationRepository; + this.loadModificationRepository = loadModificationRepository; + this.shuntCompensatorModificationRepository = shuntCompensatorModificationRepository; + this.substationModificationRepository = substationModificationRepository; + this.twoWindingsTransformerModificationRepository = twoWindingsTransformerModificationRepository; + this.voltageLevelModificationRepository = voltageLevelModificationRepository; + } + + @Transactional // To have the 2 delete in the same transaction (atomic) + public void deleteAll() { + batteryModificationRepository.deleteAll(); + generatorModificationRepository.deleteAll(); + lineModificationRepository.deleteAll(); + loadModificationRepository.deleteAll(); + shuntCompensatorModificationRepository.deleteAll(); + substationModificationRepository.deleteAll(); + twoWindingsTransformerModificationRepository.deleteAll(); + voltageLevelModificationRepository.deleteAll(); + } + + public GeneratorModificationRepository getGeneratorModificationRepository() { + return generatorModificationRepository; + } + + public List findSubEntities (ModificationType modificationType, List ids) { + List subEntities = new ArrayList<>(); + switch (modificationType) { + case GENERATOR_MODIFICATION -> subEntities.addAll(generatorModificationRepository.findAllWithReactiveCapabilityCurvePointsByIdIn(ids)); + default -> subEntities.addAll(getRepositoryByModificationType(modificationType).findAllById(ids)); + } + return subEntities; + } + + public JpaRepository getRepositoryByModificationType(ModificationType modificationType) { + switch (modificationType) { + case BATTERY_MODIFICATION -> { + return batteryModificationRepository; + } + case GENERATOR_MODIFICATION -> { + return generatorModificationRepository; + } + case LINE_MODIFICATION -> { + return lineModificationRepository; + } + case LOAD_MODIFICATION -> { + return loadModificationRepository; + } + case SHUNT_COMPENSATOR_MODIFICATION -> { + return shuntCompensatorModificationRepository; + } + case SUBSTATION_MODIFICATION -> { + return substationModificationRepository; + } + case TWO_WINDINGS_TRANSFORMER_MODIFICATION -> { + return twoWindingsTransformerModificationRepository; + } + case VOLTAGE_LEVEL_MODIFICATION -> { + return voltageLevelModificationRepository; + } + default -> { + return null; + } + } + } +} diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index ef929786e..23a369e95 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -32,25 +32,21 @@ public class NetworkModificationRepository { private final ModificationRepository modificationRepository; - private final GeneratorModificationRepository generatorModificationRepository; - - private final LoadModificationRepository loadModificationRepository; + private final EquipmentModificationRepositories equipmentModificationRepositories; private static final String MODIFICATION_NOT_FOUND_MESSAGE = "Modification (%s) not found"; - public NetworkModificationRepository(ModificationGroupRepository modificationGroupRepository, ModificationRepository modificationRepository, GeneratorModificationRepository generatorModificationRepository, LoadModificationRepository loadModificationRepository) { + public NetworkModificationRepository(ModificationGroupRepository modificationGroupRepository, ModificationRepository modificationRepository, EquipmentModificationRepositories equipmentModificationRepositories) { this.modificationGroupRepository = modificationGroupRepository; this.modificationRepository = modificationRepository; - this.generatorModificationRepository = generatorModificationRepository; - this.loadModificationRepository = loadModificationRepository; + this.equipmentModificationRepositories = equipmentModificationRepositories; } @Transactional // To have the 2 delete in the same transaction (atomic) public void deleteAll() { modificationRepository.deleteAll(); modificationGroupRepository.deleteAll(); - generatorModificationRepository.deleteAll(); - loadModificationRepository.deleteAll(); + equipmentModificationRepositories.deleteAll(); } @Transactional // To have all create in the same transaction (atomic) @@ -162,7 +158,7 @@ public List getModificationsMetadata(UUID groupUuid, boolean public TabularModificationEntity loadTabularModificationSubEntities(TabularModificationEntity tabularModificationEntity) { switch (tabularModificationEntity.getModificationType()) { case GENERATOR_MODIFICATION: - generatorModificationRepository.findAllWithReactiveCapabilityCurvePointsByIdIn(tabularModificationEntity.getModifications().stream().map(ModificationEntity::getId).toList()); + equipmentModificationRepositories.getGeneratorModificationRepository().findAllWithReactiveCapabilityCurvePointsByIdIn(tabularModificationEntity.getModifications().stream().map(ModificationEntity::getId).toList()); break; default: break; @@ -197,13 +193,8 @@ public ModificationInfos getModificationInfo(UUID modificationUuid) { } ModificationEntity modificationEntity = optionalModificationEntity.get(); if (modificationEntity instanceof TabularModificationEntity tabularModificationEntity) { - List entities = new ArrayList<>(); List ids = modificationRepository.findSubModificationsIds(modificationUuid); - switch (tabularModificationEntity.getModificationType()) { - case GENERATOR_MODIFICATION -> entities.addAll(generatorModificationRepository.findAllWithReactiveCapabilityCurvePointsByIdIn(ids)); - case LOAD_MODIFICATION -> entities.addAll(loadModificationRepository.findAllById(ids)); - } - tabularModificationEntity.setModifications(entities); + tabularModificationEntity.setModifications(equipmentModificationRepositories.findSubEntities(tabularModificationEntity.getModificationType(), ids)); return getModificationInfos(tabularModificationEntity); } return getModificationInfos(modificationEntity); diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java new file mode 100644 index 000000000..ccbf9c0d6 --- /dev/null +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java @@ -0,0 +1,21 @@ +/* + Copyright (c) 2024, RTE (http://www.rte-france.com) + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.gridsuite.modification.server.repositories.equipmentmodification; + +import org.gridsuite.modification.server.entities.equipment.modification.BatteryModificationEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.UUID; + +/** + * @author Etienne Homer + */ +@Repository +public interface BatteryModificationRepository extends JpaRepository { + +} diff --git a/src/main/java/org/gridsuite/modification/server/repositories/GeneratorModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java similarity index 94% rename from src/main/java/org/gridsuite/modification/server/repositories/GeneratorModificationRepository.java rename to src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java index 9e0a1ecc2..be34c7d0e 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/GeneratorModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java @@ -4,7 +4,7 @@ License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -package org.gridsuite.modification.server.repositories; +package org.gridsuite.modification.server.repositories.equipmentmodification; import org.gridsuite.modification.server.entities.TabularModificationEntity; import org.gridsuite.modification.server.entities.equipment.modification.GeneratorModificationEntity; @@ -27,5 +27,4 @@ public interface GeneratorModificationRepository extends JpaRepository findAllWithReactiveCapabilityCurvePointsById(UUID id); - } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java new file mode 100644 index 000000000..f73f5cabe --- /dev/null +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java @@ -0,0 +1,21 @@ +/* + Copyright (c) 2024, RTE (http://www.rte-france.com) + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.gridsuite.modification.server.repositories.equipmentmodification; + +import org.gridsuite.modification.server.entities.equipment.modification.LineModificationEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.UUID; + +/** + * @author Etienne Homer + */ +@Repository +public interface LineModificationRepository extends JpaRepository { + +} diff --git a/src/main/java/org/gridsuite/modification/server/repositories/LoadModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java similarity index 89% rename from src/main/java/org/gridsuite/modification/server/repositories/LoadModificationRepository.java rename to src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java index 8e36cc3fa..2be3c3d89 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/LoadModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java @@ -4,7 +4,7 @@ License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -package org.gridsuite.modification.server.repositories; +package org.gridsuite.modification.server.repositories.equipmentmodification; import org.gridsuite.modification.server.entities.equipment.modification.LoadModificationEntity; import org.springframework.data.jpa.repository.JpaRepository; diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java new file mode 100644 index 000000000..e2cde9296 --- /dev/null +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java @@ -0,0 +1,20 @@ +/* + Copyright (c) 2024, RTE (http://www.rte-france.com) + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.gridsuite.modification.server.repositories.equipmentmodification; + +import org.gridsuite.modification.server.entities.equipment.modification.ShuntCompensatorModificationEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.UUID; + +/** + * @author Etienne Homer + */ +@Repository +public interface ShuntCompensatorModificationRepository extends JpaRepository { +} diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java new file mode 100644 index 000000000..f6294e366 --- /dev/null +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java @@ -0,0 +1,21 @@ +/* + Copyright (c) 2024, RTE (http://www.rte-france.com) + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.gridsuite.modification.server.repositories.equipmentmodification; + +import org.gridsuite.modification.server.entities.equipment.modification.SubstationModificationEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.UUID; + +/** + * @author Etienne Homer + */ +@Repository +public interface SubstationModificationRepository extends JpaRepository { + +} diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java new file mode 100644 index 000000000..f3d58bb78 --- /dev/null +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java @@ -0,0 +1,21 @@ +/* + Copyright (c) 2024, RTE (http://www.rte-france.com) + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.gridsuite.modification.server.repositories.equipmentmodification; + +import org.gridsuite.modification.server.entities.equipment.modification.TwoWindingsTransformerModificationEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.UUID; + +/** + * @author Etienne Homer + */ +@Repository +public interface TwoWindingsTransformerModificationRepository extends JpaRepository { + +} diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java new file mode 100644 index 000000000..eb8a31ccd --- /dev/null +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java @@ -0,0 +1,21 @@ +/* + Copyright (c) 2024, RTE (http://www.rte-france.com) + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.gridsuite.modification.server.repositories.equipmentmodification; + +import org.gridsuite.modification.server.entities.equipment.modification.VoltageLevelModificationEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.UUID; + +/** + * @author Etienne Homer + */ +@Repository +public interface VoltageLevelModificationRepository extends JpaRepository { + +} From 316342d0f9648388d92b005394993707f1d8004c Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Fri, 26 Jan 2024 14:39:11 +0100 Subject: [PATCH 07/24] clean Signed-off-by: Etienne Homer --- .../server/repositories/EquipmentModificationRepositories.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java b/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java index 483aa05ff..e85d0bce4 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java @@ -66,7 +66,7 @@ public GeneratorModificationRepository getGeneratorModificationRepository() { return generatorModificationRepository; } - public List findSubEntities (ModificationType modificationType, List ids) { + public List findSubEntities(ModificationType modificationType, List ids) { List subEntities = new ArrayList<>(); switch (modificationType) { case GENERATOR_MODIFICATION -> subEntities.addAll(generatorModificationRepository.findAllWithReactiveCapabilityCurvePointsByIdIn(ids)); From 6a662eec3e2403108da8dd68811d70cc4138441f Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Thu, 1 Feb 2024 11:30:25 +0100 Subject: [PATCH 08/24] Add cast to native query Signed-off-by: Etienne Homer --- .../server/repositories/ModificationRepository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java index 40a5efafa..bdf6c5d22 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java @@ -27,7 +27,7 @@ public interface ModificationRepository extends JpaRepository findAllBaseByGroupId(UUID uuid); - @Query(value = "SELECT modifications_id from tabular_modification_modifications where tabular_modification_entity_id= ?1", nativeQuery = true) + @Query(value = "SELECT cast(modifications_id as varchar) from tabular_modification_modifications where tabular_modification_entity_id= ?1", nativeQuery = true) List findSubModificationsIds(UUID uuid); Integer countByGroupIdAndStashed(UUID groupId, boolean stashed); From 7881819ced4acdbee4e68f9715ad893698b1706a Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Thu, 1 Feb 2024 11:41:05 +0100 Subject: [PATCH 09/24] Change requests count in UT Signed-off-by: Etienne Homer --- .../TabularGeneratorModificationsTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/gridsuite/modification/server/modifications/tabularmodifications/TabularGeneratorModificationsTest.java b/src/test/java/org/gridsuite/modification/server/modifications/tabularmodifications/TabularGeneratorModificationsTest.java index c115d2488..926beb7a2 100644 --- a/src/test/java/org/gridsuite/modification/server/modifications/tabularmodifications/TabularGeneratorModificationsTest.java +++ b/src/test/java/org/gridsuite/modification/server/modifications/tabularmodifications/TabularGeneratorModificationsTest.java @@ -101,7 +101,7 @@ public void testCheckSqlRequestsCount() throws Exception { mockMvc.perform(get("/v1/network-modifications/{uuid}", modificationUuid)).andExpectAll( status().isOk(), content().contentType(MediaType.APPLICATION_JSON)) .andReturn(); - assertSelectCount(3); + assertSelectCount(4); modifications = List.of( GeneratorModificationInfos.builder().equipmentId("idGenerator").maxActivePower(new AttributeModification<>(300., OperationType.SET)).build(), @@ -119,7 +119,7 @@ public void testCheckSqlRequestsCount() throws Exception { status().isOk(), content().contentType(MediaType.APPLICATION_JSON)) .andReturn(); // We check that the request count is not dependent on the number of sub modifications of the tabular modification (the JPA N+1 problem is correctly solved) - assertSelectCount(3); + assertSelectCount(4); reset(); // We get the modifications of the group (so the 2 tabular modifications) From 44f33e43f0dc5dc650f655889a52db4257a1e65c Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Thu, 1 Feb 2024 13:19:34 +0100 Subject: [PATCH 10/24] Add parametrized type Signed-off-by: Etienne Homer --- .../server/repositories/EquipmentModificationRepositories.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java b/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java index e85d0bce4..f6d068ff9 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java @@ -75,7 +75,7 @@ public List findSubEntities(ModificationType modificationTyp return subEntities; } - public JpaRepository getRepositoryByModificationType(ModificationType modificationType) { + public JpaRepository getRepositoryByModificationType(ModificationType modificationType) { switch (modificationType) { case BATTERY_MODIFICATION -> { return batteryModificationRepository; From bb9be4551d1c2f6ccf33b7c8a9f6ec09eef85c69 Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Thu, 1 Feb 2024 13:23:59 +0100 Subject: [PATCH 11/24] clean Signed-off-by: Etienne Homer --- .../equipmentmodification/GeneratorModificationRepository.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java index be34c7d0e..4979d987c 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java @@ -24,7 +24,4 @@ public interface GeneratorModificationRepository extends JpaRepository findAllWithReactiveCapabilityCurvePointsByIdIn(List ids); - - @EntityGraph(attributePaths = {"modifications", "modifications.reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD) - Optional findAllWithReactiveCapabilityCurvePointsById(UUID id); } From 7a5ecc949cec1b3b02566d24e237d7d114ec70a4 Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Thu, 1 Feb 2024 13:27:18 +0100 Subject: [PATCH 12/24] Clean Signed-off-by: Etienne Homer --- .../equipmentmodification/GeneratorModificationRepository.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java index 4979d987c..a72f70c55 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java @@ -6,14 +6,12 @@ */ package org.gridsuite.modification.server.repositories.equipmentmodification; -import org.gridsuite.modification.server.entities.TabularModificationEntity; import org.gridsuite.modification.server.entities.equipment.modification.GeneratorModificationEntity; import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; -import java.util.Optional; import java.util.UUID; /** From 1c1131140e539452f02711d999e146a1fd0d389a Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Thu, 1 Feb 2024 17:46:47 +0100 Subject: [PATCH 13/24] save Signed-off-by: Etienne Homer --- .../EquipmentModificationRepositories.java | 11 +++++++++++ .../server/repositories/ModificationRepository.java | 6 ++++++ .../repositories/NetworkModificationRepository.java | 9 ++++++++- .../LineModificationRepository.java | 7 ++++++- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java b/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java index f6d068ff9..5178537f5 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java @@ -2,6 +2,7 @@ import org.gridsuite.modification.server.ModificationType; import org.gridsuite.modification.server.entities.ModificationEntity; +import org.gridsuite.modification.server.entities.TabularModificationEntity; import org.gridsuite.modification.server.repositories.equipmentmodification.*; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -14,6 +15,8 @@ @Repository public class EquipmentModificationRepositories { + private final ModificationRepository modificationRepository; + private final BatteryModificationRepository batteryModificationRepository; private final GeneratorModificationRepository generatorModificationRepository; @@ -31,6 +34,7 @@ public class EquipmentModificationRepositories { private final VoltageLevelModificationRepository voltageLevelModificationRepository; public EquipmentModificationRepositories( + ModificationRepository modificationRepository, BatteryModificationRepository batteryModificationRepository, GeneratorModificationRepository generatorModificationRepository, LineModificationRepository lineModificationRepository, @@ -40,6 +44,7 @@ public EquipmentModificationRepositories( TwoWindingsTransformerModificationRepository twoWindingsTransformerModificationRepository, VoltageLevelModificationRepository voltageLevelModificationRepository ) { + this.modificationRepository = modificationRepository; this.batteryModificationRepository = batteryModificationRepository; this.generatorModificationRepository = generatorModificationRepository; this.lineModificationRepository = lineModificationRepository; @@ -52,6 +57,7 @@ public EquipmentModificationRepositories( @Transactional // To have the 2 delete in the same transaction (atomic) public void deleteAll() { + modificationRepository.deleteAll(); batteryModificationRepository.deleteAll(); generatorModificationRepository.deleteAll(); lineModificationRepository.deleteAll(); @@ -106,4 +112,9 @@ public JpaRepository getRepositoryByModifica } } } + + public void deleteTabularModification(TabularModificationEntity tabularModificationEntity) { + modificationRepository.deleteModificationByIds(modificationRepository.findSubModificationsIds(tabularModificationEntity.getId())); +// getRepositoryByModificationType(tabularModificationEntity.getModificationType()).deleteSubModificationsByIds(modificationRepository.findSubModificationsIds(tabularModificationEntity.getId())); + } } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java index bdf6c5d22..5592d0c47 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java @@ -11,6 +11,7 @@ import org.gridsuite.modification.server.entities.ModificationEntity; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @@ -31,4 +32,9 @@ public interface ModificationRepository extends JpaRepository findSubModificationsIds(UUID uuid); Integer countByGroupIdAndStashed(UUID groupId, boolean stashed); + + @Modifying + @Query(value = "DELETE FROM modification WHERE id IN ?1", nativeQuery = true) + void deleteModificationByIds(List ids); + } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index 23a369e95..a83672de7 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -205,7 +205,14 @@ public void deleteModificationGroup(UUID groupUuid, boolean errorOnGroupNotFound try { ModificationGroupEntity groupEntity = getModificationGroup(groupUuid); if (!groupEntity.getModifications().isEmpty()) { - modificationRepository.deleteAll(groupEntity.getModifications().stream().filter(Objects::nonNull).collect(Collectors.toList())); + groupEntity.getModifications().forEach(modificationEntity -> { + if (modificationEntity instanceof TabularModificationEntity tabularModificationEntity) { + equipmentModificationRepositories.deleteTabularModification(tabularModificationEntity); + } else { + modificationRepository.delete(modificationEntity); + } + }); +// modificationRepository.deleteAll(groupEntity.getModifications().stream().filter(Objects::nonNull).collect(Collectors.toList())); } this.modificationGroupRepository.delete(groupEntity); } catch (NetworkModificationException e) { diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java index f73f5cabe..9e4f33b05 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java @@ -8,8 +8,11 @@ import org.gridsuite.modification.server.entities.equipment.modification.LineModificationEntity; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.UUID; /** @@ -17,5 +20,7 @@ */ @Repository public interface LineModificationRepository extends JpaRepository { - + @Modifying + @Query(value = "DELETE FROM line_modification WHERE id IN ?1", nativeQuery = true) + void deleteSubModificationsByIds(List ids); } From 30e6b6894ad33fcefe77adfcb5d5ab32234112f3 Mon Sep 17 00:00:00 2001 From: Etienne Date: Thu, 1 Feb 2024 19:43:13 +0100 Subject: [PATCH 14/24] optim delete Signed-off-by: Etienne --- .../repositories/EquipmentModificationRepositories.java | 2 +- .../BatteryModificationRepository.java | 8 +++++++- .../EquipmentModificationRepository.java | 8 ++++++++ .../GeneratorModificationRepository.java | 8 +++++++- .../LineModificationRepository.java | 2 +- .../LoadModificationRepository.java | 9 +++++++-- .../ShuntCompensatorModificationRepository.java | 8 +++++++- .../SubstationModificationRepository.java | 9 +++++++-- .../TwoWindingsTransformerModificationRepository.java | 9 +++++++-- .../VoltageLevelModificationRepository.java | 9 +++++++-- 10 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/EquipmentModificationRepository.java diff --git a/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java b/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java index 5178537f5..809883f66 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java @@ -115,6 +115,6 @@ public JpaRepository getRepositoryByModifica public void deleteTabularModification(TabularModificationEntity tabularModificationEntity) { modificationRepository.deleteModificationByIds(modificationRepository.findSubModificationsIds(tabularModificationEntity.getId())); -// getRepositoryByModificationType(tabularModificationEntity.getModificationType()).deleteSubModificationsByIds(modificationRepository.findSubModificationsIds(tabularModificationEntity.getId())); + ((EquipmentModificationRepository) getRepositoryByModificationType(tabularModificationEntity.getModificationType())).deleteSubModificationsByIds(modificationRepository.findSubModificationsIds(tabularModificationEntity.getId())); } } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java index ccbf9c0d6..2d27ba51f 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java @@ -8,14 +8,20 @@ import org.gridsuite.modification.server.entities.equipment.modification.BatteryModificationEntity; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.UUID; /** * @author Etienne Homer */ @Repository -public interface BatteryModificationRepository extends JpaRepository { +public interface BatteryModificationRepository extends JpaRepository, EquipmentModificationRepository { + @Modifying + @Query(value = "DELETE FROM battery_modification WHERE id IN ?1", nativeQuery = true) + void deleteSubModificationsByIds(List ids); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/EquipmentModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/EquipmentModificationRepository.java new file mode 100644 index 000000000..05812c03d --- /dev/null +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/EquipmentModificationRepository.java @@ -0,0 +1,8 @@ +package org.gridsuite.modification.server.repositories.equipmentmodification; + +import java.util.List; +import java.util.UUID; + +public interface EquipmentModificationRepository { + void deleteSubModificationsByIds(List ids); +} diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java index a72f70c55..a402af778 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java @@ -9,6 +9,8 @@ import org.gridsuite.modification.server.entities.equipment.modification.GeneratorModificationEntity; import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.List; @@ -18,8 +20,12 @@ * @author Etienne Homer */ @Repository -public interface GeneratorModificationRepository extends JpaRepository { +public interface GeneratorModificationRepository extends JpaRepository, EquipmentModificationRepository { @EntityGraph(attributePaths = {"reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD) List findAllWithReactiveCapabilityCurvePointsByIdIn(List ids); + + @Modifying + @Query(value = "DELETE FROM generator_modification WHERE id IN ?1", nativeQuery = true) + void deleteSubModificationsByIds(List ids); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java index 9e4f33b05..99b7a5877 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java @@ -19,7 +19,7 @@ * @author Etienne Homer */ @Repository -public interface LineModificationRepository extends JpaRepository { +public interface LineModificationRepository extends JpaRepository, EquipmentModificationRepository { @Modifying @Query(value = "DELETE FROM line_modification WHERE id IN ?1", nativeQuery = true) void deleteSubModificationsByIds(List ids); diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java index 2be3c3d89..64702347c 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java @@ -8,14 +8,19 @@ import org.gridsuite.modification.server.entities.equipment.modification.LoadModificationEntity; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.UUID; /** * @author Etienne Homer */ @Repository -public interface LoadModificationRepository extends JpaRepository { - +public interface LoadModificationRepository extends JpaRepository, EquipmentModificationRepository { + @Modifying + @Query(value = "DELETE FROM load_modification WHERE id IN ?1", nativeQuery = true) + void deleteSubModificationsByIds(List ids); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java index e2cde9296..a572e1272 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java @@ -8,13 +8,19 @@ import org.gridsuite.modification.server.entities.equipment.modification.ShuntCompensatorModificationEntity; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.UUID; /** * @author Etienne Homer */ @Repository -public interface ShuntCompensatorModificationRepository extends JpaRepository { +public interface ShuntCompensatorModificationRepository extends JpaRepository, EquipmentModificationRepository { + @Modifying + @Query(value = "DELETE FROM shunt_compensator_modification WHERE id IN ?1", nativeQuery = true) + void deleteSubModificationsByIds(List ids); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java index f6294e366..9eaca8dc4 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java @@ -8,14 +8,19 @@ import org.gridsuite.modification.server.entities.equipment.modification.SubstationModificationEntity; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.UUID; /** * @author Etienne Homer */ @Repository -public interface SubstationModificationRepository extends JpaRepository { - +public interface SubstationModificationRepository extends JpaRepository, EquipmentModificationRepository { + @Modifying + @Query(value = "DELETE FROM substation_modification WHERE id IN ?1", nativeQuery = true) + void deleteSubModificationsByIds(List ids); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java index f3d58bb78..cba41f5af 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java @@ -8,14 +8,19 @@ import org.gridsuite.modification.server.entities.equipment.modification.TwoWindingsTransformerModificationEntity; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.UUID; /** * @author Etienne Homer */ @Repository -public interface TwoWindingsTransformerModificationRepository extends JpaRepository { - +public interface TwoWindingsTransformerModificationRepository extends JpaRepository, EquipmentModificationRepository { + @Modifying + @Query(value = "DELETE FROM two_windings_transformer_modification WHERE id IN ?1", nativeQuery = true) + void deleteSubModificationsByIds(List ids); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java index eb8a31ccd..f65b0978a 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java @@ -8,14 +8,19 @@ import org.gridsuite.modification.server.entities.equipment.modification.VoltageLevelModificationEntity; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.UUID; /** * @author Etienne Homer */ @Repository -public interface VoltageLevelModificationRepository extends JpaRepository { - +public interface VoltageLevelModificationRepository extends JpaRepository, EquipmentModificationRepository { + @Modifying + @Query(value = "DELETE FROM voltage_level_modification WHERE id IN ?1", nativeQuery = true) + void deleteSubModificationsByIds(List ids); } From 0dae6e80a199d501f66383b668244a77e5dea1f3 Mon Sep 17 00:00:00 2001 From: Etienne Date: Thu, 1 Feb 2024 19:54:51 +0100 Subject: [PATCH 15/24] clean commented code Signed-off-by: Etienne --- .../server/repositories/NetworkModificationRepository.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index a83672de7..3cb8bb24f 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -212,8 +212,7 @@ public void deleteModificationGroup(UUID groupUuid, boolean errorOnGroupNotFound modificationRepository.delete(modificationEntity); } }); -// modificationRepository.deleteAll(groupEntity.getModifications().stream().filter(Objects::nonNull).collect(Collectors.toList())); - } + }git this.modificationGroupRepository.delete(groupEntity); } catch (NetworkModificationException e) { if (e.getType() == MODIFICATION_GROUP_NOT_FOUND && !errorOnGroupNotFound) { From 6c8db8f9364c7abedd2fdbfa40f6b8c4ca524dc1 Mon Sep 17 00:00:00 2001 From: Etienne Date: Thu, 1 Feb 2024 19:56:19 +0100 Subject: [PATCH 16/24] clean Signed-off-by: Etienne --- .../server/repositories/NetworkModificationRepository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index 3cb8bb24f..089c3bd51 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -212,7 +212,7 @@ public void deleteModificationGroup(UUID groupUuid, boolean errorOnGroupNotFound modificationRepository.delete(modificationEntity); } }); - }git + } this.modificationGroupRepository.delete(groupEntity); } catch (NetworkModificationException e) { if (e.getType() == MODIFICATION_GROUP_NOT_FOUND && !errorOnGroupNotFound) { From b01f8435e43f4a5ace20b640c6ddc0420b66588c Mon Sep 17 00:00:00 2001 From: Etienne Date: Thu, 1 Feb 2024 20:18:28 +0100 Subject: [PATCH 17/24] tmp Signed-off-by: Etienne --- .../NetworkModificationRepository.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index 089c3bd51..bf5d27807 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -206,11 +206,7 @@ public void deleteModificationGroup(UUID groupUuid, boolean errorOnGroupNotFound ModificationGroupEntity groupEntity = getModificationGroup(groupUuid); if (!groupEntity.getModifications().isEmpty()) { groupEntity.getModifications().forEach(modificationEntity -> { - if (modificationEntity instanceof TabularModificationEntity tabularModificationEntity) { - equipmentModificationRepositories.deleteTabularModification(tabularModificationEntity); - } else { - modificationRepository.delete(modificationEntity); - } + deleteModification(modificationEntity); }); } this.modificationGroupRepository.delete(groupEntity); @@ -222,6 +218,14 @@ public void deleteModificationGroup(UUID groupUuid, boolean errorOnGroupNotFound } } + private void deleteModification(ModificationEntity modificationEntity) { + if (modificationEntity instanceof TabularModificationEntity tabularModificationEntity) { + equipmentModificationRepositories.deleteTabularModification(tabularModificationEntity); + } else { + modificationRepository.delete(modificationEntity); + } + } + @Transactional public int deleteModifications(UUID groupUuid, boolean onlyStashed) { ModificationGroupEntity groupEntity = getModificationGroup(groupUuid); @@ -235,7 +239,7 @@ public int deleteModifications(UUID groupUuid, boolean onlyStashed) { } modifications.forEach(groupEntity::removeModification); int count = modifications.size(); - this.modificationRepository.deleteAll(modifications); + modifications.forEach(modificationEntity -> deleteModification(modificationEntity)); return count; } From f7c8ce717625b86549dc593187df9c5f1ac7917f Mon Sep 17 00:00:00 2001 From: Etienne Date: Thu, 1 Feb 2024 20:27:45 +0100 Subject: [PATCH 18/24] clean Signed-off-by: Etienne --- .../server/repositories/NetworkModificationRepository.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index bf5d27807..272a7b777 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -205,9 +205,7 @@ public void deleteModificationGroup(UUID groupUuid, boolean errorOnGroupNotFound try { ModificationGroupEntity groupEntity = getModificationGroup(groupUuid); if (!groupEntity.getModifications().isEmpty()) { - groupEntity.getModifications().forEach(modificationEntity -> { - deleteModification(modificationEntity); - }); + groupEntity.getModifications().forEach(this::deleteModification); } this.modificationGroupRepository.delete(groupEntity); } catch (NetworkModificationException e) { @@ -239,7 +237,7 @@ public int deleteModifications(UUID groupUuid, boolean onlyStashed) { } modifications.forEach(groupEntity::removeModification); int count = modifications.size(); - modifications.forEach(modificationEntity -> deleteModification(modificationEntity)); + modifications.forEach(this::deleteModification); return count; } From 09b3b12d547a8b947e799de7dcea55e9a7436379 Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Fri, 2 Feb 2024 10:59:49 +0100 Subject: [PATCH 19/24] native queries Signed-off-by: Etienne Homer --- .../repositories/EquipmentModificationRepositories.java | 3 ++- .../server/repositories/ModificationRepository.java | 3 +++ .../server/repositories/NetworkModificationRepository.java | 7 ++++--- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java b/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java index 809883f66..60f1bb870 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java @@ -114,7 +114,8 @@ public JpaRepository getRepositoryByModifica } public void deleteTabularModification(TabularModificationEntity tabularModificationEntity) { - modificationRepository.deleteModificationByIds(modificationRepository.findSubModificationsIds(tabularModificationEntity.getId())); ((EquipmentModificationRepository) getRepositoryByModificationType(tabularModificationEntity.getModificationType())).deleteSubModificationsByIds(modificationRepository.findSubModificationsIds(tabularModificationEntity.getId())); + modificationRepository.deleteTabularModificationInJoinTableByIds(tabularModificationEntity.getId()); + modificationRepository.deleteModificationByIds(modificationRepository.findSubModificationsIds(tabularModificationEntity.getId())); } } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java index 5592d0c47..e74ebcb47 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java @@ -37,4 +37,7 @@ public interface ModificationRepository extends JpaRepository ids); + @Modifying + @Query(value = "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?1", nativeQuery = true) + void deleteTabularModificationInJoinTableByIds(UUID id); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index 272a7b777..dd16b77d2 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -219,9 +219,8 @@ public void deleteModificationGroup(UUID groupUuid, boolean errorOnGroupNotFound private void deleteModification(ModificationEntity modificationEntity) { if (modificationEntity instanceof TabularModificationEntity tabularModificationEntity) { equipmentModificationRepositories.deleteTabularModification(tabularModificationEntity); - } else { - modificationRepository.delete(modificationEntity); } + modificationRepository.delete(modificationEntity); } @Transactional @@ -248,8 +247,10 @@ public int deleteModifications(UUID groupUuid, List uuids) { .filter(m -> uuids.contains(m.getId())) .collect(Collectors.toList()); modifications.forEach(groupEntity::removeModification); + int count = modifications.size(); - this.modificationRepository.deleteAll(modifications); +// this.modificationRepository.deleteAll(modifications); + modifications.forEach(this::deleteModification); return count; } From 123b6d7a41a7da6e5e2d5cdb14a992f23aa1f2c1 Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Fri, 2 Feb 2024 16:41:48 +0100 Subject: [PATCH 20/24] Disable triggers before delete on modification table Signed-off-by: Etienne Homer --- .../EquipmentModificationRepositories.java | 7 +++---- .../repositories/ModificationRepository.java | 15 ++++++++++++++- .../NetworkModificationRepository.java | 1 - .../BatteryModificationRepository.java | 12 +++++++++--- .../EquipmentModificationRepository.java | 2 +- .../GeneratorModificationRepository.java | 15 +++++++++++++-- .../LineModificationRepository.java | 11 +++++++++-- .../LoadModificationRepository.java | 11 +++++++++-- .../ShuntCompensatorModificationRepository.java | 11 +++++++++-- .../SubstationModificationRepository.java | 11 +++++++++-- ...WindingsTransformerModificationRepository.java | 11 +++++++++-- .../VoltageLevelModificationRepository.java | 11 +++++++++-- 12 files changed, 94 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java b/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java index 60f1bb870..80b26272c 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/EquipmentModificationRepositories.java @@ -55,7 +55,7 @@ public EquipmentModificationRepositories( this.voltageLevelModificationRepository = voltageLevelModificationRepository; } - @Transactional // To have the 2 delete in the same transaction (atomic) + @Transactional public void deleteAll() { modificationRepository.deleteAll(); batteryModificationRepository.deleteAll(); @@ -114,8 +114,7 @@ public JpaRepository getRepositoryByModifica } public void deleteTabularModification(TabularModificationEntity tabularModificationEntity) { - ((EquipmentModificationRepository) getRepositoryByModificationType(tabularModificationEntity.getModificationType())).deleteSubModificationsByIds(modificationRepository.findSubModificationsIds(tabularModificationEntity.getId())); - modificationRepository.deleteTabularModificationInJoinTableByIds(tabularModificationEntity.getId()); - modificationRepository.deleteModificationByIds(modificationRepository.findSubModificationsIds(tabularModificationEntity.getId())); + List subModificationsIds = modificationRepository.findSubModificationsIds(tabularModificationEntity.getId()); + ((EquipmentModificationRepository) getRepositoryByModificationType(tabularModificationEntity.getModificationType())).deleteSubModificationsByIds(subModificationsIds, tabularModificationEntity.getId()); } } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java index e74ebcb47..1e1384ea9 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java @@ -34,10 +34,23 @@ public interface ModificationRepository extends JpaRepository ids); @Modifying @Query(value = "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?1", nativeQuery = true) void deleteTabularModificationInJoinTableByIds(UUID id); + +// @Modifying +// @Query(value = "ALTER TABLE modification DISABLE TRIGGER ALL;", nativeQuery = true) +// void disableTrigger(); +// +// @Modifying +// @Query(value = "ALTER TABLE modification ENABLE TRIGGER ALL;", nativeQuery = true) +// void enableTrigger(); + } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index dd16b77d2..0810a7406 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -249,7 +249,6 @@ public int deleteModifications(UUID groupUuid, List uuids) { modifications.forEach(groupEntity::removeModification); int count = modifications.size(); -// this.modificationRepository.deleteAll(modifications); modifications.forEach(this::deleteModification); return count; } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java index 2d27ba51f..2cc86af95 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java @@ -20,8 +20,14 @@ */ @Repository public interface BatteryModificationRepository extends JpaRepository, EquipmentModificationRepository { - + @Override @Modifying - @Query(value = "DELETE FROM battery_modification WHERE id IN ?1", nativeQuery = true) - void deleteSubModificationsByIds(List ids); + @Query(value = "BEGIN;" + + "ALTER TABLE modification DISABLE TRIGGER ALL;" + + "DELETE FROM battery_modification WHERE id IN ?1 ;" + + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + + "DELETE FROM modification WHERE id IN ?1 ;" + + "ALTER TABLE modification ENABLE TRIGGER ALL;" + + "COMMIT;", nativeQuery = true) + void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/EquipmentModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/EquipmentModificationRepository.java index 05812c03d..9fa0dc206 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/EquipmentModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/EquipmentModificationRepository.java @@ -4,5 +4,5 @@ import java.util.UUID; public interface EquipmentModificationRepository { - void deleteSubModificationsByIds(List ids); + void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java index a402af778..a36e9b38a 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java @@ -25,7 +25,18 @@ public interface GeneratorModificationRepository extends JpaRepository findAllWithReactiveCapabilityCurvePointsByIdIn(List ids); + // To disable trigger is a practice and maybe in production, the postgres user of the application will not be allowed to change the schema. + // This code is just an example of how fixing the following problem : + // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. + // But as the 'modification' table is referenced up to now by 40 foreign key constraints, all the triggers make the delete statement execution quite slow (about 3-4 seconds). + @Override @Modifying - @Query(value = "DELETE FROM generator_modification WHERE id IN ?1", nativeQuery = true) - void deleteSubModificationsByIds(List ids); + @Query(value = "BEGIN;" + + "ALTER TABLE modification DISABLE TRIGGER ALL;" + + "DELETE FROM generator_modification WHERE id IN ?1 ;" + + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + + "DELETE FROM modification WHERE id IN ?1 ;" + + "ALTER TABLE modification ENABLE TRIGGER ALL;" + + "COMMIT;", nativeQuery = true) + void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java index 99b7a5877..fc28db8db 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java @@ -20,7 +20,14 @@ */ @Repository public interface LineModificationRepository extends JpaRepository, EquipmentModificationRepository { + @Override @Modifying - @Query(value = "DELETE FROM line_modification WHERE id IN ?1", nativeQuery = true) - void deleteSubModificationsByIds(List ids); + @Query(value = "BEGIN;" + + "ALTER TABLE modification DISABLE TRIGGER ALL;" + + "DELETE FROM line_modification WHERE id IN ?1 ;" + + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + + "DELETE FROM modification WHERE id IN ?1 ;" + + "ALTER TABLE modification ENABLE TRIGGER ALL;" + + "COMMIT;", nativeQuery = true) + void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java index 64702347c..8519e987f 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java @@ -20,7 +20,14 @@ */ @Repository public interface LoadModificationRepository extends JpaRepository, EquipmentModificationRepository { + @Override @Modifying - @Query(value = "DELETE FROM load_modification WHERE id IN ?1", nativeQuery = true) - void deleteSubModificationsByIds(List ids); + @Query(value = "BEGIN;" + + "ALTER TABLE modification DISABLE TRIGGER ALL;" + + "DELETE FROM load_modification WHERE id IN ?1 ;" + + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + + "DELETE FROM modification WHERE id IN ?1 ;" + + "ALTER TABLE modification ENABLE TRIGGER ALL;" + + "COMMIT;", nativeQuery = true) + void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java index a572e1272..acede0d23 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java @@ -20,7 +20,14 @@ */ @Repository public interface ShuntCompensatorModificationRepository extends JpaRepository, EquipmentModificationRepository { + @Override @Modifying - @Query(value = "DELETE FROM shunt_compensator_modification WHERE id IN ?1", nativeQuery = true) - void deleteSubModificationsByIds(List ids); + @Query(value = "BEGIN;" + + "ALTER TABLE modification DISABLE TRIGGER ALL;" + + "DELETE FROM shunt_compensator_modification WHERE id IN ?1 ;" + + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + + "DELETE FROM modification WHERE id IN ?1 ;" + + "ALTER TABLE modification ENABLE TRIGGER ALL;" + + "COMMIT;", nativeQuery = true) + void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java index 9eaca8dc4..9649f0bbf 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java @@ -20,7 +20,14 @@ */ @Repository public interface SubstationModificationRepository extends JpaRepository, EquipmentModificationRepository { + @Override @Modifying - @Query(value = "DELETE FROM substation_modification WHERE id IN ?1", nativeQuery = true) - void deleteSubModificationsByIds(List ids); + @Query(value = "BEGIN;" + + "ALTER TABLE modification DISABLE TRIGGER ALL;" + + "DELETE FROM substation_modification WHERE id IN ?1 ;" + + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + + "DELETE FROM modification WHERE id IN ?1 ;" + + "ALTER TABLE modification ENABLE TRIGGER ALL;" + + "COMMIT;", nativeQuery = true) + void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java index cba41f5af..660d8b5b8 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java @@ -20,7 +20,14 @@ */ @Repository public interface TwoWindingsTransformerModificationRepository extends JpaRepository, EquipmentModificationRepository { + @Override @Modifying - @Query(value = "DELETE FROM two_windings_transformer_modification WHERE id IN ?1", nativeQuery = true) - void deleteSubModificationsByIds(List ids); + @Query(value = "BEGIN;" + + "ALTER TABLE modification DISABLE TRIGGER ALL;" + + "DELETE FROM two_windings_transformer_modification WHERE id IN ?1 ;" + + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + + "DELETE FROM modification WHERE id IN ?1 ;" + + "ALTER TABLE modification ENABLE TRIGGER ALL;" + + "COMMIT;", nativeQuery = true) + void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java index f65b0978a..77adedd47 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java @@ -20,7 +20,14 @@ */ @Repository public interface VoltageLevelModificationRepository extends JpaRepository, EquipmentModificationRepository { + @Override @Modifying - @Query(value = "DELETE FROM voltage_level_modification WHERE id IN ?1", nativeQuery = true) - void deleteSubModificationsByIds(List ids); + @Query(value = "BEGIN;" + + "ALTER TABLE modification DISABLE TRIGGER ALL;" + + "DELETE FROM voltage_level_modification WHERE id IN ?1 ;" + + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + + "DELETE FROM modification WHERE id IN ?1 ;" + + "ALTER TABLE modification ENABLE TRIGGER ALL;" + + "COMMIT;", nativeQuery = true) + void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } From 4f461561eb6c8d6791b2ee343b21999f2fe161b1 Mon Sep 17 00:00:00 2001 From: Etienne Date: Sat, 3 Feb 2024 17:42:43 +0100 Subject: [PATCH 21/24] clean comment Signed-off-by: Etienne --- .../GeneratorModificationRepository.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java index a36e9b38a..0d657d61a 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java @@ -25,10 +25,10 @@ public interface GeneratorModificationRepository extends JpaRepository findAllWithReactiveCapabilityCurvePointsByIdIn(List ids); - // To disable trigger is a practice and maybe in production, the postgres user of the application will not be allowed to change the schema. + // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. // This code is just an example of how fixing the following problem : // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. - // But as the 'modification' table is referenced up to now by 40 foreign key constraints, all the triggers make the delete statement execution quite slow (about 3-4 seconds). + // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + From 4a8e6d7a5adb36ef29640155cfd9814398e16bc2 Mon Sep 17 00:00:00 2001 From: Etienne Date: Sun, 4 Feb 2024 10:08:24 +0100 Subject: [PATCH 22/24] add comment Signed-off-by: Etienne --- .../equipmentmodification/BatteryModificationRepository.java | 4 ++++ .../equipmentmodification/LineModificationRepository.java | 4 ++++ .../equipmentmodification/LoadModificationRepository.java | 4 ++++ .../ShuntCompensatorModificationRepository.java | 4 ++++ .../SubstationModificationRepository.java | 4 ++++ .../TwoWindingsTransformerModificationRepository.java | 4 ++++ .../VoltageLevelModificationRepository.java | 4 ++++ 7 files changed, 28 insertions(+) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java index 2cc86af95..ca05c77c8 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java @@ -20,6 +20,10 @@ */ @Repository public interface BatteryModificationRepository extends JpaRepository, EquipmentModificationRepository { + // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. + // This code is just an example of how fixing the following problem : + // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. + // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java index fc28db8db..dd343a104 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java @@ -20,6 +20,10 @@ */ @Repository public interface LineModificationRepository extends JpaRepository, EquipmentModificationRepository { + // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. + // This code is just an example of how fixing the following problem : + // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. + // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java index 8519e987f..aa764eafa 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java @@ -20,6 +20,10 @@ */ @Repository public interface LoadModificationRepository extends JpaRepository, EquipmentModificationRepository { + // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. + // This code is just an example of how fixing the following problem : + // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. + // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java index acede0d23..82959fbee 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java @@ -20,6 +20,10 @@ */ @Repository public interface ShuntCompensatorModificationRepository extends JpaRepository, EquipmentModificationRepository { + // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. + // This code is just an example of how fixing the following problem : + // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. + // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java index 9649f0bbf..e9b53339c 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java @@ -20,6 +20,10 @@ */ @Repository public interface SubstationModificationRepository extends JpaRepository, EquipmentModificationRepository { + // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. + // This code is just an example of how fixing the following problem : + // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. + // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java index 660d8b5b8..df26c64bd 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java @@ -20,6 +20,10 @@ */ @Repository public interface TwoWindingsTransformerModificationRepository extends JpaRepository, EquipmentModificationRepository { + // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. + // This code is just an example of how fixing the following problem : + // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. + // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java index 77adedd47..fce613b37 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java @@ -20,6 +20,10 @@ */ @Repository public interface VoltageLevelModificationRepository extends JpaRepository, EquipmentModificationRepository { + // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. + // This code is just an example of how fixing the following problem : + // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. + // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + From 528523a4d5c220cf4470883a80fe84f5d3def838 Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Wed, 21 Feb 2024 11:39:40 +0100 Subject: [PATCH 23/24] clean DISABLE TRIGGER Signed-off-by: Etienne Homer --- .../repositories/ModificationRepository.java | 20 ------------------- .../BatteryModificationRepository.java | 6 ------ .../GeneratorModificationRepository.java | 6 ------ .../LineModificationRepository.java | 6 ------ .../LoadModificationRepository.java | 6 ------ ...huntCompensatorModificationRepository.java | 6 ------ .../SubstationModificationRepository.java | 6 ------ ...ingsTransformerModificationRepository.java | 6 ------ .../VoltageLevelModificationRepository.java | 6 ------ 9 files changed, 68 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java index 1e1384ea9..9c632c833 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java @@ -33,24 +33,4 @@ public interface ModificationRepository extends JpaRepository ids); - - @Modifying - @Query(value = "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?1", nativeQuery = true) - void deleteTabularModificationInJoinTableByIds(UUID id); - -// @Modifying -// @Query(value = "ALTER TABLE modification DISABLE TRIGGER ALL;", nativeQuery = true) -// void disableTrigger(); -// -// @Modifying -// @Query(value = "ALTER TABLE modification ENABLE TRIGGER ALL;", nativeQuery = true) -// void enableTrigger(); - } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java index ca05c77c8..ff25aff66 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/BatteryModificationRepository.java @@ -20,18 +20,12 @@ */ @Repository public interface BatteryModificationRepository extends JpaRepository, EquipmentModificationRepository { - // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. - // This code is just an example of how fixing the following problem : - // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. - // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + - "ALTER TABLE modification DISABLE TRIGGER ALL;" + "DELETE FROM battery_modification WHERE id IN ?1 ;" + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + "DELETE FROM modification WHERE id IN ?1 ;" + - "ALTER TABLE modification ENABLE TRIGGER ALL;" + "COMMIT;", nativeQuery = true) void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java index 0d657d61a..0611c4964 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/GeneratorModificationRepository.java @@ -25,18 +25,12 @@ public interface GeneratorModificationRepository extends JpaRepository findAllWithReactiveCapabilityCurvePointsByIdIn(List ids); - // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. - // This code is just an example of how fixing the following problem : - // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. - // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + - "ALTER TABLE modification DISABLE TRIGGER ALL;" + "DELETE FROM generator_modification WHERE id IN ?1 ;" + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + "DELETE FROM modification WHERE id IN ?1 ;" + - "ALTER TABLE modification ENABLE TRIGGER ALL;" + "COMMIT;", nativeQuery = true) void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java index dd343a104..a3f1467d1 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LineModificationRepository.java @@ -20,18 +20,12 @@ */ @Repository public interface LineModificationRepository extends JpaRepository, EquipmentModificationRepository { - // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. - // This code is just an example of how fixing the following problem : - // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. - // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + - "ALTER TABLE modification DISABLE TRIGGER ALL;" + "DELETE FROM line_modification WHERE id IN ?1 ;" + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + "DELETE FROM modification WHERE id IN ?1 ;" + - "ALTER TABLE modification ENABLE TRIGGER ALL;" + "COMMIT;", nativeQuery = true) void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java index aa764eafa..2a679778b 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/LoadModificationRepository.java @@ -20,18 +20,12 @@ */ @Repository public interface LoadModificationRepository extends JpaRepository, EquipmentModificationRepository { - // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. - // This code is just an example of how fixing the following problem : - // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. - // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + - "ALTER TABLE modification DISABLE TRIGGER ALL;" + "DELETE FROM load_modification WHERE id IN ?1 ;" + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + "DELETE FROM modification WHERE id IN ?1 ;" + - "ALTER TABLE modification ENABLE TRIGGER ALL;" + "COMMIT;", nativeQuery = true) void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java index 82959fbee..d290b5018 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/ShuntCompensatorModificationRepository.java @@ -20,18 +20,12 @@ */ @Repository public interface ShuntCompensatorModificationRepository extends JpaRepository, EquipmentModificationRepository { - // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. - // This code is just an example of how fixing the following problem : - // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. - // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + - "ALTER TABLE modification DISABLE TRIGGER ALL;" + "DELETE FROM shunt_compensator_modification WHERE id IN ?1 ;" + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + "DELETE FROM modification WHERE id IN ?1 ;" + - "ALTER TABLE modification ENABLE TRIGGER ALL;" + "COMMIT;", nativeQuery = true) void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java index e9b53339c..2f4b9fd0c 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/SubstationModificationRepository.java @@ -20,18 +20,12 @@ */ @Repository public interface SubstationModificationRepository extends JpaRepository, EquipmentModificationRepository { - // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. - // This code is just an example of how fixing the following problem : - // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. - // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + - "ALTER TABLE modification DISABLE TRIGGER ALL;" + "DELETE FROM substation_modification WHERE id IN ?1 ;" + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + "DELETE FROM modification WHERE id IN ?1 ;" + - "ALTER TABLE modification ENABLE TRIGGER ALL;" + "COMMIT;", nativeQuery = true) void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java index df26c64bd..6f874c716 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/TwoWindingsTransformerModificationRepository.java @@ -20,18 +20,12 @@ */ @Repository public interface TwoWindingsTransformerModificationRepository extends JpaRepository, EquipmentModificationRepository { - // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. - // This code is just an example of how fixing the following problem : - // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. - // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + - "ALTER TABLE modification DISABLE TRIGGER ALL;" + "DELETE FROM two_windings_transformer_modification WHERE id IN ?1 ;" + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + "DELETE FROM modification WHERE id IN ?1 ;" + - "ALTER TABLE modification ENABLE TRIGGER ALL;" + "COMMIT;", nativeQuery = true) void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java index fce613b37..65578f4b7 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/equipmentmodification/VoltageLevelModificationRepository.java @@ -20,18 +20,12 @@ */ @Repository public interface VoltageLevelModificationRepository extends JpaRepository, EquipmentModificationRepository { - // To disable trigger is a bad practice and in production, the postgres user of the application will not be allowed to change the schema. - // This code is just an example of how fixing the following problem : - // When we want to delete a tabular modification, we need to execute a delete statement with thousands of ids on the 'modification' table. - // But as the 'modification' table is referenced up to now by 40 foreign key constraints (it will be about 300 later), all the triggers make the delete statement execution quite slow (about 3-4 seconds). @Override @Modifying @Query(value = "BEGIN;" + - "ALTER TABLE modification DISABLE TRIGGER ALL;" + "DELETE FROM voltage_level_modification WHERE id IN ?1 ;" + "DELETE FROM tabular_modification_modifications WHERE tabular_modification_entity_id = ?2 ;" + "DELETE FROM modification WHERE id IN ?1 ;" + - "ALTER TABLE modification ENABLE TRIGGER ALL;" + "COMMIT;", nativeQuery = true) void deleteSubModificationsByIds(List subModificationIds, UUID tabularModificationId); } From e56d5e4abb3666743bc8d087e6f2c459981fb236 Mon Sep 17 00:00:00 2001 From: Etienne Homer Date: Wed, 21 Feb 2024 11:45:09 +0100 Subject: [PATCH 24/24] Clean Signed-off-by: Etienne Homer --- .../modification/server/entities/TabularModificationEntity.java | 1 - .../modification/server/repositories/ModificationRepository.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java b/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java index 52304798f..e33463645 100644 --- a/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java +++ b/src/main/java/org/gridsuite/modification/server/entities/TabularModificationEntity.java @@ -19,7 +19,6 @@ * @author Etienne Homer */ @NoArgsConstructor -@AllArgsConstructor @Getter @Setter @Entity diff --git a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java index 9c632c833..63ef246de 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java @@ -11,7 +11,6 @@ import org.gridsuite.modification.server.entities.ModificationEntity; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository;