refactored
This commit is contained in:
parent
7f2c36952c
commit
abed6e2a16
@ -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,59 +54,6 @@ public class TimescaleInsertRepository extends AbstractTimeseriesInsertRepositor
|
||||
|
||||
@Override
|
||||
public void saveOrUpdate(List<TimescaleTsKvEntity> entities) {
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
int[] result = jdbcTemplate.batchUpdate(BATCH_UPDATE, new BatchPreparedStatementSetter() {
|
||||
@Override
|
||||
public void setValues(PreparedStatement ps, int i) throws SQLException {
|
||||
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
||||
List<TimescaleTsKvEntity> insertEntities = new ArrayList<>(updatedCount);
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
if (result[i] == 0) {
|
||||
insertEntities.add(entities.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
jdbcTemplate.batchUpdate(INSERT_OR_UPDATE, new BatchPreparedStatementSetter() {
|
||||
@Override
|
||||
public void setValues(PreparedStatement ps, int i) throws SQLException {
|
||||
@ -149,9 +93,7 @@ public class TimescaleInsertRepository extends AbstractTimeseriesInsertRepositor
|
||||
|
||||
@Override
|
||||
public int getBatchSize() {
|
||||
return insertEntities.size();
|
||||
}
|
||||
});
|
||||
return entities.size();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user