Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Optim delete tabular modif native query #430

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.gridsuite.modification.server.repositories;

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;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Repository
public class EquipmentModificationRepositories {

private final ModificationRepository modificationRepository;

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(
ModificationRepository modificationRepository,
BatteryModificationRepository batteryModificationRepository,
GeneratorModificationRepository generatorModificationRepository,
LineModificationRepository lineModificationRepository,
LoadModificationRepository loadModificationRepository,
ShuntCompensatorModificationRepository shuntCompensatorModificationRepository,
SubstationModificationRepository substationModificationRepository,
TwoWindingsTransformerModificationRepository twoWindingsTransformerModificationRepository,
VoltageLevelModificationRepository voltageLevelModificationRepository
) {
this.modificationRepository = modificationRepository;
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
public void deleteAll() {
modificationRepository.deleteAll();
batteryModificationRepository.deleteAll();
generatorModificationRepository.deleteAll();
lineModificationRepository.deleteAll();
loadModificationRepository.deleteAll();
shuntCompensatorModificationRepository.deleteAll();
substationModificationRepository.deleteAll();
twoWindingsTransformerModificationRepository.deleteAll();
voltageLevelModificationRepository.deleteAll();
}

public GeneratorModificationRepository getGeneratorModificationRepository() {
return generatorModificationRepository;
}

public List<ModificationEntity> findSubEntities(ModificationType modificationType, List<UUID> ids) {
List<ModificationEntity> subEntities = new ArrayList<>();
switch (modificationType) {
case GENERATOR_MODIFICATION -> subEntities.addAll(generatorModificationRepository.findAllWithReactiveCapabilityCurvePointsByIdIn(ids));
default -> subEntities.addAll(getRepositoryByModificationType(modificationType).findAllById(ids));
}
return subEntities;
}

public JpaRepository<? extends ModificationEntity, UUID> 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;
}
}
}

public void deleteTabularModification(TabularModificationEntity tabularModificationEntity) {
List<UUID> subModificationsIds = modificationRepository.findSubModificationsIds(tabularModificationEntity.getId());
((EquipmentModificationRepository) getRepositoryByModificationType(tabularModificationEntity.getModificationType())).deleteSubModificationsByIds(subModificationsIds, tabularModificationEntity.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,11 +27,9 @@ public interface ModificationRepository extends JpaRepository<ModificationEntity
@Query(value = "SELECT new ModificationEntity(m.id, m.type, m.date, m.stashed, m.messageType, m.messageValues) FROM ModificationEntity m WHERE m.group.id = ?1 order by m.modificationsOrder")
List<ModificationEntity> findAllBaseByGroupId(UUID uuid);

@EntityGraph(attributePaths = {"modifications", "modifications.reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD)
Optional<TabularModificationEntity> findAllWithReactiveCapabilityCurvePointsById(UUID id);

@EntityGraph(attributePaths = {"reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD)
Set<GeneratorModificationEntity> findAllReactiveCapabilityCurvePointsByIdIn(List<UUID> ids);
@Query(value = "SELECT cast(modifications_id as varchar) from tabular_modification_modifications where tabular_modification_entity_id= ?1", nativeQuery = true)
List<UUID> findSubModificationsIds(UUID uuid);

Integer countByGroupIdAndStashed(UUID groupId, boolean stashed);

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,21 @@ public class NetworkModificationRepository {

private final ModificationRepository modificationRepository;

private final EquipmentModificationRepositories equipmentModificationRepositories;

private static final String MODIFICATION_NOT_FOUND_MESSAGE = "Modification (%s) not found";

public NetworkModificationRepository(ModificationGroupRepository modificationGroupRepository, ModificationRepository modificationRepository) {
public NetworkModificationRepository(ModificationGroupRepository modificationGroupRepository, ModificationRepository modificationRepository, EquipmentModificationRepositories equipmentModificationRepositories) {
this.modificationGroupRepository = modificationGroupRepository;
this.modificationRepository = modificationRepository;
this.equipmentModificationRepositories = equipmentModificationRepositories;
}

@Transactional // To have the 2 delete in the same transaction (atomic)
public void deleteAll() {
modificationRepository.deleteAll();
modificationGroupRepository.deleteAll();
equipmentModificationRepositories.deleteAll();
}

@Transactional // To have all create in the same transaction (atomic)
Expand Down Expand Up @@ -151,14 +155,10 @@ public List<ModificationInfos> getModificationsMetadata(UUID groupUuid, boolean
}
}

public TabularModificationEntity loadTabularModificationSubEntities(ModificationEntity modificationEntity) {
TabularModificationEntity tabularModificationEntity = (TabularModificationEntity) modificationEntity;
public TabularModificationEntity loadTabularModificationSubEntities(TabularModificationEntity tabularModificationEntity) {
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());
equipmentModificationRepositories.getGeneratorModificationRepository().findAllWithReactiveCapabilityCurvePointsByIdIn(tabularModificationEntity.getModifications().stream().map(ModificationEntity::getId).toList());
break;
default:
break;
Expand All @@ -167,8 +167,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();
}
Expand All @@ -191,15 +191,21 @@ 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<UUID> ids = modificationRepository.findSubModificationsIds(modificationUuid);
tabularModificationEntity.setModifications(equipmentModificationRepositories.findSubEntities(tabularModificationEntity.getModificationType(), ids));
return getModificationInfos(tabularModificationEntity);
}
return getModificationInfos(modificationEntity);
}

@Transactional // To have the 2 delete in the same transaction (atomic)
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(this::deleteModification);
}
this.modificationGroupRepository.delete(groupEntity);
} catch (NetworkModificationException e) {
Expand All @@ -210,6 +216,13 @@ public void deleteModificationGroup(UUID groupUuid, boolean errorOnGroupNotFound
}
}

private void deleteModification(ModificationEntity modificationEntity) {
if (modificationEntity instanceof TabularModificationEntity tabularModificationEntity) {
equipmentModificationRepositories.deleteTabularModification(tabularModificationEntity);
}
modificationRepository.delete(modificationEntity);
}

@Transactional
public int deleteModifications(UUID groupUuid, boolean onlyStashed) {
ModificationGroupEntity groupEntity = getModificationGroup(groupUuid);
Expand All @@ -223,7 +236,7 @@ public int deleteModifications(UUID groupUuid, boolean onlyStashed) {
}
modifications.forEach(groupEntity::removeModification);
int count = modifications.size();
this.modificationRepository.deleteAll(modifications);
modifications.forEach(this::deleteModification);
return count;
}

Expand All @@ -234,8 +247,9 @@ public int deleteModifications(UUID groupUuid, List<UUID> uuids) {
.filter(m -> uuids.contains(m.getId()))
.collect(Collectors.toList());
modifications.forEach(groupEntity::removeModification);

int count = modifications.size();
this.modificationRepository.deleteAll(modifications);
modifications.forEach(this::deleteModification);
return count;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
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.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 <etienne.homer at rte-france.com>
*/
@Repository
public interface BatteryModificationRepository extends JpaRepository<BatteryModificationEntity, UUID>, EquipmentModificationRepository {
@Override
@Modifying
@Query(value = "BEGIN;" +
"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 ;" +
"COMMIT;", nativeQuery = true)
void deleteSubModificationsByIds(List<UUID> subModificationIds, UUID tabularModificationId);
}
Original file line number Diff line number Diff line change
@@ -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<UUID> subModificationIds, UUID tabularModificationId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
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.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;
import java.util.UUID;

/**
* @author Etienne Homer <etienne.homer at rte-france.com>
*/
@Repository
public interface GeneratorModificationRepository extends JpaRepository<GeneratorModificationEntity, UUID>, EquipmentModificationRepository {

@EntityGraph(attributePaths = {"reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD)
List<GeneratorModificationEntity> findAllWithReactiveCapabilityCurvePointsByIdIn(List<UUID> ids);

@Override
@Modifying
@Query(value = "BEGIN;" +
"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 ;" +
"COMMIT;", nativeQuery = true)
void deleteSubModificationsByIds(List<UUID> subModificationIds, UUID tabularModificationId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
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.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 <etienne.homer at rte-france.com>
*/
@Repository
public interface LineModificationRepository extends JpaRepository<LineModificationEntity, UUID>, EquipmentModificationRepository {
@Override
@Modifying
@Query(value = "BEGIN;" +
"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 ;" +
"COMMIT;", nativeQuery = true)
void deleteSubModificationsByIds(List<UUID> subModificationIds, UUID tabularModificationId);
}
Loading