refactored

This commit is contained in:
YevhenBondarenko 2019-12-06 11:45:38 +02:00 committed by Andrew Shvayka
parent 7f2c36952c
commit abed6e2a16
2 changed files with 36 additions and 94 deletions

View File

@ -213,9 +213,9 @@ sql:
batch_max_delay: "${SQL_TS_LATEST_BATCH_MAX_DELAY_MS:100}" batch_max_delay: "${SQL_TS_LATEST_BATCH_MAX_DELAY_MS:100}"
stats_print_interval_ms: "${SQL_TS_LATEST_BATCH_STATS_PRINT_MS:10000}" stats_print_interval_ms: "${SQL_TS_LATEST_BATCH_STATS_PRINT_MS:10000}"
ts_timescale: ts_timescale:
batch_size: "${SQL_TS_TIMESCALE_BATCH_SIZE:10000}" batch_size: "${SQL_TS_TIMESCALE_BATCH_SIZE:10000}"
batch_max_delay: "${SQL_TS_TIMESCALE_BATCH_MAX_DELAY_MS:100}" batch_max_delay: "${SQL_TS_TIMESCALE_BATCH_MAX_DELAY_MS:100}"
stats_print_interval_ms: "${SQL_TS_TIMESCALE_BATCH_STATS_PRINT_MS:10000}" 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 # Specify whether to remove null characters from strValue of attributes and timeseries before insert
remove_null_chars: "${SQL_REMOVE_NULL_CHARS:true}" remove_null_chars: "${SQL_REMOVE_NULL_CHARS:true}"

View File

@ -17,9 +17,7 @@ package org.thingsboard.server.dao.sqlts.timescale;
import org.springframework.jdbc.core.BatchPreparedStatementSetter; import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional; 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.model.sqlts.timescale.TimescaleTsKvEntity;
import org.thingsboard.server.dao.sqlts.AbstractTimeseriesInsertRepository; import org.thingsboard.server.dao.sqlts.AbstractTimeseriesInsertRepository;
import org.thingsboard.server.dao.util.PsqlDao; 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.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Types; import java.sql.Types;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@TimescaleDBTsDao @TimescaleDBTsDao
@ -57,101 +54,46 @@ public class TimescaleInsertRepository extends AbstractTimeseriesInsertRepositor
@Override @Override
public void saveOrUpdate(List<TimescaleTsKvEntity> entities) { public void saveOrUpdate(List<TimescaleTsKvEntity> entities) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() { jdbcTemplate.batchUpdate(INSERT_OR_UPDATE, new BatchPreparedStatementSetter() {
@Override @Override
protected void doInTransactionWithoutResult(TransactionStatus status) { public void setValues(PreparedStatement ps, int i) throws SQLException {
int[] result = jdbcTemplate.batchUpdate(BATCH_UPDATE, new BatchPreparedStatementSetter() { ps.setString(1, entities.get(i).getTenantId());
@Override ps.setString(2, entities.get(i).getEntityId());
public void setValues(PreparedStatement ps, int i) throws SQLException { ps.setString(3, entities.get(i).getKey());
ps.setLong(4, entities.get(i).getTs());
if (entities.get(i).getBooleanValue() != null) { if (entities.get(i).getBooleanValue() != null) {
ps.setBoolean(1, entities.get(i).getBooleanValue()); ps.setBoolean(5, entities.get(i).getBooleanValue());
} else { ps.setBoolean(9, entities.get(i).getBooleanValue());
ps.setNull(1, Types.BOOLEAN); } else {
} ps.setNull(5, Types.BOOLEAN);
ps.setNull(9, 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++;
}
} }
List<TimescaleTsKvEntity> insertEntities = new ArrayList<>(updatedCount); ps.setString(6, replaceNullChars(entities.get(i).getStrValue()));
for (int i = 0; i < result.length; i++) { ps.setString(10, replaceNullChars(entities.get(i).getStrValue()));
if (result[i] == 0) {
insertEntities.add(entities.get(i));
} 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() { if (entities.get(i).getDoubleValue() != null) {
@Override ps.setDouble(8, entities.get(i).getDoubleValue());
public void setValues(PreparedStatement ps, int i) throws SQLException { ps.setDouble(12, entities.get(i).getDoubleValue());
ps.setString(1, entities.get(i).getTenantId()); } else {
ps.setString(2, entities.get(i).getEntityId()); ps.setNull(8, Types.DOUBLE);
ps.setString(3, entities.get(i).getKey()); ps.setNull(12, Types.DOUBLE);
ps.setLong(4, entities.get(i).getTs()); }
}
if (entities.get(i).getBooleanValue() != null) { @Override
ps.setBoolean(5, entities.get(i).getBooleanValue()); public int getBatchSize() {
ps.setBoolean(9, entities.get(i).getBooleanValue()); return entities.size();
} 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();
}
});
} }
}); });
} }