From abed6e2a164bd1b20770b3dc8c48a4e81d0216bb Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Fri, 6 Dec 2019 11:45:38 +0200 Subject: [PATCH] refactored --- .../src/main/resources/thingsboard.yml | 6 +- .../timescale/TimescaleInsertRepository.java | 124 +++++------------- 2 files changed, 36 insertions(+), 94 deletions(-) diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml index 8be9a8a1c5..fcf62a659b 100644 --- a/application/src/main/resources/thingsboard.yml +++ b/application/src/main/resources/thingsboard.yml @@ -213,9 +213,9 @@ sql: batch_max_delay: "${SQL_TS_LATEST_BATCH_MAX_DELAY_MS:100}" stats_print_interval_ms: "${SQL_TS_LATEST_BATCH_STATS_PRINT_MS:10000}" ts_timescale: - batch_size: "${SQL_TS_TIMESCALE_BATCH_SIZE:10000}" - batch_max_delay: "${SQL_TS_TIMESCALE_BATCH_MAX_DELAY_MS:100}" - stats_print_interval_ms: "${SQL_TS_TIMESCALE_BATCH_STATS_PRINT_MS:10000}" + batch_size: "${SQL_TS_TIMESCALE_BATCH_SIZE:10000}" + batch_max_delay: "${SQL_TS_TIMESCALE_BATCH_MAX_DELAY_MS:100}" + stats_print_interval_ms: "${SQL_TS_TIMESCALE_BATCH_STATS_PRINT_MS:10000}" # Specify whether to remove null characters from strValue of attributes and timeseries before insert remove_null_chars: "${SQL_REMOVE_NULL_CHARS:true}" diff --git a/dao/src/main/java/org/thingsboard/server/dao/sqlts/timescale/TimescaleInsertRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sqlts/timescale/TimescaleInsertRepository.java index 8493703275..b47c79cb98 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sqlts/timescale/TimescaleInsertRepository.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sqlts/timescale/TimescaleInsertRepository.java @@ -17,9 +17,7 @@ package org.thingsboard.server.dao.sqlts.timescale; import org.springframework.jdbc.core.BatchPreparedStatementSetter; import org.springframework.stereotype.Repository; -import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.annotation.Transactional; -import org.springframework.transaction.support.TransactionCallbackWithoutResult; import org.thingsboard.server.dao.model.sqlts.timescale.TimescaleTsKvEntity; import org.thingsboard.server.dao.sqlts.AbstractTimeseriesInsertRepository; import org.thingsboard.server.dao.util.PsqlDao; @@ -28,7 +26,6 @@ import org.thingsboard.server.dao.util.TimescaleDBTsDao; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Types; -import java.util.ArrayList; import java.util.List; @TimescaleDBTsDao @@ -57,101 +54,46 @@ public class TimescaleInsertRepository extends AbstractTimeseriesInsertRepositor @Override public void saveOrUpdate(List entities) { - transactionTemplate.execute(new TransactionCallbackWithoutResult() { + jdbcTemplate.batchUpdate(INSERT_OR_UPDATE, new BatchPreparedStatementSetter() { @Override - protected void doInTransactionWithoutResult(TransactionStatus status) { - int[] result = jdbcTemplate.batchUpdate(BATCH_UPDATE, new BatchPreparedStatementSetter() { - @Override - public void setValues(PreparedStatement ps, int i) throws SQLException { + public void setValues(PreparedStatement ps, int i) throws SQLException { + ps.setString(1, entities.get(i).getTenantId()); + ps.setString(2, entities.get(i).getEntityId()); + ps.setString(3, entities.get(i).getKey()); + ps.setLong(4, entities.get(i).getTs()); - if (entities.get(i).getBooleanValue() != null) { - ps.setBoolean(1, entities.get(i).getBooleanValue()); - } else { - ps.setNull(1, Types.BOOLEAN); - } - - ps.setString(2, replaceNullChars(entities.get(i).getStrValue())); - - if (entities.get(i).getLongValue() != null) { - ps.setLong(3, entities.get(i).getLongValue()); - } else { - ps.setNull(3, Types.BIGINT); - } - - if (entities.get(i).getDoubleValue() != null) { - ps.setDouble(4, entities.get(i).getDoubleValue()); - } else { - ps.setNull(4, Types.DOUBLE); - } - - ps.setString(5, entities.get(i).getTenantId()); - ps.setString(6, entities.get(i).getEntityId()); - ps.setString(7, entities.get(i).getKey()); - ps.setLong(8, entities.get(i).getTs()); - } - - @Override - public int getBatchSize() { - return entities.size(); - } - }); - - int updatedCount = 0; - for (int i = 0; i < result.length; i++) { - if (result[i] == 0) { - updatedCount++; - } + if (entities.get(i).getBooleanValue() != null) { + ps.setBoolean(5, entities.get(i).getBooleanValue()); + ps.setBoolean(9, entities.get(i).getBooleanValue()); + } else { + ps.setNull(5, Types.BOOLEAN); + ps.setNull(9, Types.BOOLEAN); } - List insertEntities = new ArrayList<>(updatedCount); - for (int i = 0; i < result.length; i++) { - if (result[i] == 0) { - insertEntities.add(entities.get(i)); - } + ps.setString(6, replaceNullChars(entities.get(i).getStrValue())); + ps.setString(10, replaceNullChars(entities.get(i).getStrValue())); + + + if (entities.get(i).getLongValue() != null) { + ps.setLong(7, entities.get(i).getLongValue()); + ps.setLong(11, entities.get(i).getLongValue()); + } else { + ps.setNull(7, Types.BIGINT); + ps.setNull(11, Types.BIGINT); } - jdbcTemplate.batchUpdate(INSERT_OR_UPDATE, new BatchPreparedStatementSetter() { - @Override - public void setValues(PreparedStatement ps, int i) throws SQLException { - ps.setString(1, entities.get(i).getTenantId()); - ps.setString(2, entities.get(i).getEntityId()); - ps.setString(3, entities.get(i).getKey()); - ps.setLong(4, entities.get(i).getTs()); + if (entities.get(i).getDoubleValue() != null) { + ps.setDouble(8, entities.get(i).getDoubleValue()); + ps.setDouble(12, entities.get(i).getDoubleValue()); + } else { + ps.setNull(8, Types.DOUBLE); + ps.setNull(12, Types.DOUBLE); + } + } - if (entities.get(i).getBooleanValue() != null) { - ps.setBoolean(5, entities.get(i).getBooleanValue()); - ps.setBoolean(9, entities.get(i).getBooleanValue()); - } else { - ps.setNull(5, Types.BOOLEAN); - ps.setNull(9, Types.BOOLEAN); - } - - ps.setString(6, replaceNullChars(entities.get(i).getStrValue())); - ps.setString(10, replaceNullChars(entities.get(i).getStrValue())); - - - if (entities.get(i).getLongValue() != null) { - ps.setLong(7, entities.get(i).getLongValue()); - ps.setLong(11, entities.get(i).getLongValue()); - } else { - ps.setNull(7, Types.BIGINT); - ps.setNull(11, Types.BIGINT); - } - - if (entities.get(i).getDoubleValue() != null) { - ps.setDouble(8, entities.get(i).getDoubleValue()); - ps.setDouble(12, entities.get(i).getDoubleValue()); - } else { - ps.setNull(8, Types.DOUBLE); - ps.setNull(12, Types.DOUBLE); - } - } - - @Override - public int getBatchSize() { - return insertEntities.size(); - } - }); + @Override + public int getBatchSize() { + return entities.size(); } }); }