Skip to content

Commit

Permalink
using projection and update result without finding by jpa
Browse files Browse the repository at this point in the history
Signed-off-by: Thang PHAM <[email protected]>
  • Loading branch information
thangqp committed Nov 18, 2024
1 parent f295c86 commit 87f095b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
12 changes: 12 additions & 0 deletions src/main/java/org/gridsuite/ds/server/model/ResultEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
@Entity
public class ResultEntity implements Serializable {

public interface WithoutOutputState {
DynamicSimulationStatus getStatus();

UUID getTimeSeriesId();

UUID getTimeLineId();
}

public interface OutputStateOnly {
byte[] getOutputState();
}

public ResultEntity(UUID id, UUID timeSeriesId, UUID timeLineId, DynamicSimulationStatus status, byte[] outputState) {
this.id = id;
this.timeSeriesId = timeSeriesId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,34 @@
*/
package org.gridsuite.ds.server.repository;

import org.gridsuite.ds.server.dto.DynamicSimulationStatus;
import org.gridsuite.ds.server.model.ResultEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
* @author Abdelsalem Hedhili <abdelsalem.hedhili at rte-france.com>
*/
@Repository
public interface ResultRepository extends JpaRepository<ResultEntity, UUID> {
<T> Optional<T> findById(UUID id, Class<T> type);

<T> List<T> findBy(Class<T> type);

@Modifying
@Query("UPDATE ResultEntity r SET r.status = :status WHERE r.id IN :resultUuids")
int updateStatus(@Param("resultUuids") List<UUID> resultUuids, @Param("status") DynamicSimulationStatus status);

@Modifying
@Query("UPDATE ResultEntity r SET r.status = :status, r.timeSeriesId = :timeSeriesId, r.timeLineId = :timeLineId, r.outputState = :outputState" +
" WHERE r.id = :resultUuid")
int updateResult(@Param("resultUuid") UUID resultUuid, @Param("timeSeriesId") UUID timeSeriesId, @Param("timeLineId") UUID timeLineId,
@Param("status") DynamicSimulationStatus status, @Param("outputState") byte[] outputState);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.*;

import static org.gridsuite.ds.server.DynamicSimulationException.Type.RESULT_UUID_NOT_FOUND;

Expand All @@ -36,33 +33,28 @@ public DynamicSimulationResultService(ResultRepository resultRepository, TimeSer

public UUID getTimeSeriesId(UUID resultUuid) {
Objects.requireNonNull(resultUuid);
return resultRepository.findById(resultUuid)
return resultRepository.findById(resultUuid, ResultEntity.WithoutOutputState.class)
.orElseThrow(() -> new DynamicSimulationException(RESULT_UUID_NOT_FOUND, MSG_RESULT_UUID_NOT_FOUND + resultUuid))
.getTimeSeriesId();
}

public UUID getTimeLineId(UUID resultUuid) {
Objects.requireNonNull(resultUuid);
return resultRepository.findById(resultUuid)
return resultRepository.findById(resultUuid, ResultEntity.WithoutOutputState.class)
.orElseThrow(() -> new DynamicSimulationException(RESULT_UUID_NOT_FOUND, MSG_RESULT_UUID_NOT_FOUND + resultUuid))
.getTimeLineId();
}

public byte[] getOutputState(UUID resultUuid) {
Objects.requireNonNull(resultUuid);
return resultRepository.findById(resultUuid)
return resultRepository.findById(resultUuid, ResultEntity.OutputStateOnly.class)
.orElseThrow(() -> new DynamicSimulationException(RESULT_UUID_NOT_FOUND, MSG_RESULT_UUID_NOT_FOUND + resultUuid))
.getOutputState();
}

@Transactional
public List<UUID> updateStatus(List<UUID> resultUuids, DynamicSimulationStatus status) {
// find result entities
List<ResultEntity> resultEntities = resultRepository.findAllById(resultUuids);
// set entity with new values
resultEntities.forEach(resultEntity -> resultEntity.setStatus(status));
// save entities into database
return resultRepository.saveAllAndFlush(resultEntities).stream().map(ResultEntity::getId).toList();
return resultRepository.updateStatus(resultUuids, status) > 0 ? resultUuids : Collections.emptyList();
}

@Transactional
Expand All @@ -76,15 +68,11 @@ public void updateResult(UUID resultUuid, List<TimeSeries<?, ?>> timeSeries, Lis
.map(TimeSeriesGroupInfos::getId)
.orElse(null);

LOGGER.info("Update dynamic simulation [resultUuid={}, timeSeriesUuid={}, timeLineUuid={}, status={}",
LOGGER.debug("Update dynamic simulation [resultUuid={}, timeSeriesUuid={}, timeLineUuid={}, status={}",
resultUuid, timeSeriesUuid, timeLineUuid, status);

// update time-series/timeline uuids and result status to the db
ResultEntity resultEntity = resultRepository.findById(resultUuid).orElseThrow();
resultEntity.setTimeSeriesId(timeSeriesUuid);
resultEntity.setTimeLineId(timeLineUuid);
resultEntity.setStatus(status);
resultEntity.setOutputState(outputState);
// update time-series/timeline uuids, status and outputState to the db
resultRepository.updateResult(resultUuid, timeSeriesUuid, timeLineUuid, status, outputState);
}

@Override
Expand All @@ -99,7 +87,7 @@ public void insertStatus(List<UUID> resultUuids, DynamicSimulationStatus status)
@Transactional
public void delete(UUID resultUuid) {
Objects.requireNonNull(resultUuid);
ResultEntity resultEntity = resultRepository.findById(resultUuid).orElse(null);
ResultEntity.WithoutOutputState resultEntity = resultRepository.findById(resultUuid, ResultEntity.WithoutOutputState.class).orElse(null);
if (resultEntity == null) {
return;
}
Expand All @@ -114,22 +102,22 @@ public void delete(UUID resultUuid) {
@Override
@Transactional
public void deleteAll() {
List<ResultEntity> resultEntities = resultRepository.findAll();
List<ResultEntity.WithoutOutputState> resultEntities = resultRepository.findBy(ResultEntity.WithoutOutputState.class);

// call time series client to delete time-series and timeline
for (ResultEntity resultEntity : resultEntities) {
for (ResultEntity.WithoutOutputState resultEntity : resultEntities) {
timeSeriesClient.deleteTimeSeriesGroup(resultEntity.getTimeSeriesId());
timeSeriesClient.deleteTimeSeriesGroup(resultEntity.getTimeLineId());
}

// then delete all results in local db
resultRepository.deleteAllById(resultEntities.stream().map(ResultEntity::getId).toList());
resultRepository.deleteAll();
}

@Override
public DynamicSimulationStatus findStatus(UUID resultUuid) {
Objects.requireNonNull(resultUuid);
return resultRepository.findById(resultUuid)
return resultRepository.findById(resultUuid, ResultEntity.WithoutOutputState.class)
.orElseThrow(() -> new DynamicSimulationException(RESULT_UUID_NOT_FOUND, MSG_RESULT_UUID_NOT_FOUND + resultUuid))
.getStatus();
}
Expand Down

0 comments on commit 87f095b

Please sign in to comment.