Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne Homer <[email protected]>
  • Loading branch information
etiennehomer committed Mar 27, 2024
1 parent 7d5566a commit 2afb69d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@
import javax.sql.DataSource;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.time.Instant;
import java.util.*;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -693,66 +691,21 @@ private <T extends IdentifiableAttributes> List<Resource<T>> getIdentifiablesInV
return getIdentifiablesInContainer(networkUuid, variantNum, voltageLevelId, tableMapping.getVoltageLevelIdColumns(), tableMapping);
}

public <T extends IdentifiableAttributes & Contained> void updateIdentifiables2(UUID networkUuid, List<Resource<T>> resources,
TableMapping tableMapping, String columnToAddToWhereClause) {
AtomicReference<Long> startTime = new AtomicReference<>();
startTime.set(System.nanoTime());
try (var connection = dataSource.getConnection()) {
try (PreparedStatement preparedStmt = connection.prepareStatement(QueryCatalog.buildUpdateIdentifiableQuery(tableMapping.getTable(), tableMapping.getColumnsMapping().keySet(), columnToAddToWhereClause))) {
List<Object> values = new ArrayList<>(4 + tableMapping.getColumnsMapping().size());
for (List<Resource<T>> subResources : Lists.partition(resources, BATCH_SIZE)) {
for (Resource<T> resource : subResources) {
T attributes = resource.getAttributes();
values.clear();
for (var e : tableMapping.getColumnsMapping().entrySet()) {
String columnName = e.getKey();
var mapping = e.getValue();
if (!columnName.equals(columnToAddToWhereClause)) {
values.add(mapping.get(attributes));
}
}
values.add(networkUuid);
values.add(resource.getVariantNum());
values.add(resource.getId());
values.add(resource.getAttributes().getContainerIds().iterator().next());
bindValues(preparedStmt, values);
preparedStmt.addBatch();
}
preparedStmt.executeBatch();
}
}
} catch (SQLException e) {
throw new UncheckedSqlException(e);
}
LOGGER.info("UPDATE IDENTIFIABLE {}ms", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime.get()));
}

public <T extends IdentifiableAttributes & Contained> void updateIdentifiables(UUID networkUuid, List<Resource<T>> resources,
TableMapping tableMapping, String columnToAddToWhereClause) {
AtomicReference<Long> startTime = new AtomicReference<>();
startTime.set(System.nanoTime());
try (var connection = dataSource.getConnection()) {
for (List<Resource<T>> subResources : Lists.partition(resources, UPDATE_BATCH_SIZE)) {
for (List<Resource<T>> subResources : Lists.partition(resources, BATCH_SIZE)) {
List<Object> values = new ArrayList<>(4 + tableMapping.getColumnsMapping().size());
try (PreparedStatement preparedStmt = connection.prepareStatement(QueryCatalog.buildMultiRowsUpdateIdentifiableQuery(tableMapping.getTable(), tableMapping.getColumnsMapping().keySet(), columnToAddToWhereClause, subResources.size()))) {
for (Resource<T> resource : subResources) {
T attributes = resource.getAttributes();
for (var e : tableMapping.getColumnsMapping().entrySet()) {
String columnName = e.getKey();
var mapping = e.getValue();
if (!columnName.equalsIgnoreCase(columnToAddToWhereClause)) {
Object value = mapping.get(attributes);
if (value == null) {
if (e.getValue().getClassR() == Double.class) {
values.add(Double.NaN);
} else if (e.getValue().getClassR() == Integer.class) {
values.add(0);
} else {
values.add(mapping.get(attributes));
}
} else {
values.add(mapping.get(attributes));
}
if (!columnName.equals(columnToAddToWhereClause)) {
values.add(mapping.get(attributes));
}
}
values.add(networkUuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public static String buildMultiRowsUpdateIdentifiableQuery(String tableName, Set
.append(" T2." + VARIANT_NUM_COLUMN).append(" = T1." + VARIANT_NUM_COLUMN + " and\n")
.append(" T2." + ID_COLUMN).append(" = T1." + ID_COLUMN + " and\n");
if (columnToAddToWhereClause != null) {
query.append(" T2." + columnToAddToWhereClause + "::text").append(" = T1." + columnToAddToWhereClause + "::text");
query.append(" T2." + columnToAddToWhereClause).append(" = T1." + columnToAddToWhereClause);
}
query.append(";");
return query.toString();
Expand Down

0 comments on commit 2afb69d

Please sign in to comment.