49 changed files with 55 additions and 990 deletions
@ -1,26 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2022 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.dao.util; |
|
||||
|
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|
||||
|
|
||||
import java.lang.annotation.Retention; |
|
||||
import java.lang.annotation.RetentionPolicy; |
|
||||
|
|
||||
@Retention(RetentionPolicy.RUNTIME) |
|
||||
@ConditionalOnProperty(prefix = "spring.jpa", value = "database-platform", havingValue = "org.hibernate.dialect.HSQLDialect") |
|
||||
public @interface HsqlDao { |
|
||||
} |
|
||||
@ -1,26 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2022 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.dao.util; |
|
||||
|
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|
||||
|
|
||||
import java.lang.annotation.Retention; |
|
||||
import java.lang.annotation.RetentionPolicy; |
|
||||
|
|
||||
@Retention(RetentionPolicy.RUNTIME) |
|
||||
@ConditionalOnProperty(prefix = "spring.jpa", value = "database-platform", havingValue = "org.hibernate.dialect.PostgreSQL10Dialect") |
|
||||
public @interface PsqlDao { |
|
||||
} |
|
||||
@ -1,27 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2022 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.dao.util; |
|
||||
|
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; |
|
||||
|
|
||||
import java.lang.annotation.Retention; |
|
||||
import java.lang.annotation.RetentionPolicy; |
|
||||
|
|
||||
@Retention(RetentionPolicy.RUNTIME) |
|
||||
@ConditionalOnExpression("('${database.ts_latest.type}'=='sql' || '${database.ts_latest.type}'=='timescale') " + |
|
||||
"&& '${spring.jpa.database-platform}'=='org.hibernate.dialect.PostgreSQL10Dialect'") |
|
||||
public @interface PsqlTsLatestAnyDao { |
|
||||
} |
|
||||
@ -1,37 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2022 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.dao; |
|
||||
|
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan; |
|
||||
import org.springframework.context.annotation.ComponentScan; |
|
||||
import org.springframework.context.annotation.Configuration; |
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement; |
|
||||
import org.thingsboard.server.dao.util.HsqlDao; |
|
||||
import org.thingsboard.server.dao.util.SqlTsLatestDao; |
|
||||
import org.thingsboard.server.dao.util.TbAutoConfiguration; |
|
||||
|
|
||||
@Configuration |
|
||||
@TbAutoConfiguration |
|
||||
@ComponentScan({"org.thingsboard.server.dao.sqlts.hsql"}) |
|
||||
@EnableJpaRepositories({"org.thingsboard.server.dao.sqlts.insert.latest.hsql", "org.thingsboard.server.dao.sqlts.latest"}) |
|
||||
@EntityScan({"org.thingsboard.server.dao.model.sqlts.latest"}) |
|
||||
@EnableTransactionManagement |
|
||||
@SqlTsLatestDao |
|
||||
@HsqlDao |
|
||||
public class HsqlTsLatestDaoConfig { |
|
||||
|
|
||||
} |
|
||||
@ -1,37 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2022 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.dao; |
|
||||
|
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan; |
|
||||
import org.springframework.context.annotation.ComponentScan; |
|
||||
import org.springframework.context.annotation.Configuration; |
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement; |
|
||||
import org.thingsboard.server.dao.util.PsqlDao; |
|
||||
import org.thingsboard.server.dao.util.SqlTsDao; |
|
||||
import org.thingsboard.server.dao.util.TbAutoConfiguration; |
|
||||
|
|
||||
@Configuration |
|
||||
@TbAutoConfiguration |
|
||||
@ComponentScan({"org.thingsboard.server.dao.sqlts.psql", "org.thingsboard.server.dao.sqlts.insert.psql"}) |
|
||||
@EnableJpaRepositories({"org.thingsboard.server.dao.sqlts.ts", "org.thingsboard.server.dao.sqlts.insert.psql"}) |
|
||||
@EntityScan({"org.thingsboard.server.dao.model.sqlts.ts"}) |
|
||||
@EnableTransactionManagement |
|
||||
@PsqlDao |
|
||||
@SqlTsDao |
|
||||
public class PsqlTsDaoConfig { |
|
||||
|
|
||||
} |
|
||||
@ -1,76 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2022 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.dao.sql.attributes; |
|
||||
|
|
||||
import org.springframework.stereotype.Repository; |
|
||||
import org.springframework.transaction.annotation.Transactional; |
|
||||
import org.thingsboard.server.dao.model.sql.AttributeKvEntity; |
|
||||
import org.thingsboard.server.dao.util.HsqlDao; |
|
||||
|
|
||||
import java.sql.Types; |
|
||||
import java.util.List; |
|
||||
|
|
||||
@HsqlDao |
|
||||
@Repository |
|
||||
@Transactional |
|
||||
public class HsqlAttributesInsertRepository extends AttributeKvInsertRepository { |
|
||||
|
|
||||
private static final String INSERT_OR_UPDATE = |
|
||||
"MERGE INTO attribute_kv USING(VALUES ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) " + |
|
||||
"A (entity_type, entity_id, attribute_type, attribute_key, str_v, long_v, dbl_v, bool_v, json_v, last_update_ts) " + |
|
||||
"ON (attribute_kv.entity_type=A.entity_type " + |
|
||||
"AND attribute_kv.entity_id=A.entity_id " + |
|
||||
"AND attribute_kv.attribute_type=A.attribute_type " + |
|
||||
"AND attribute_kv.attribute_key=A.attribute_key) " + |
|
||||
"WHEN MATCHED THEN UPDATE SET attribute_kv.str_v = A.str_v, attribute_kv.long_v = A.long_v, attribute_kv.dbl_v = A.dbl_v, attribute_kv.bool_v = A.bool_v, attribute_kv.json_v = A.json_v, attribute_kv.last_update_ts = A.last_update_ts " + |
|
||||
"WHEN NOT MATCHED THEN INSERT (entity_type, entity_id, attribute_type, attribute_key, str_v, long_v, dbl_v, bool_v, json_v, last_update_ts) " + |
|
||||
"VALUES (A.entity_type, A.entity_id, A.attribute_type, A.attribute_key, A.str_v, A.long_v, A.dbl_v, A.bool_v, A.json_v, A.last_update_ts)"; |
|
||||
|
|
||||
@Override |
|
||||
protected void saveOrUpdate(List<AttributeKvEntity> entities) { |
|
||||
entities.forEach(entity -> { |
|
||||
jdbcTemplate.update(INSERT_OR_UPDATE, ps -> { |
|
||||
ps.setString(1, entity.getId().getEntityType().name()); |
|
||||
ps.setObject(2, entity.getId().getEntityId()); |
|
||||
ps.setString(3, entity.getId().getAttributeType()); |
|
||||
ps.setString(4, entity.getId().getAttributeKey()); |
|
||||
ps.setString(5, entity.getStrValue()); |
|
||||
|
|
||||
if (entity.getLongValue() != null) { |
|
||||
ps.setLong(6, entity.getLongValue()); |
|
||||
} else { |
|
||||
ps.setNull(6, Types.BIGINT); |
|
||||
} |
|
||||
|
|
||||
if (entity.getDoubleValue() != null) { |
|
||||
ps.setDouble(7, entity.getDoubleValue()); |
|
||||
} else { |
|
||||
ps.setNull(7, Types.DOUBLE); |
|
||||
} |
|
||||
|
|
||||
if (entity.getBooleanValue() != null) { |
|
||||
ps.setBoolean(8, entity.getBooleanValue()); |
|
||||
} else { |
|
||||
ps.setNull(8, Types.BOOLEAN); |
|
||||
} |
|
||||
|
|
||||
ps.setString(9, entity.getJsonValue()); |
|
||||
|
|
||||
ps.setLong(10, entity.getLastUpdateTs()); |
|
||||
}); |
|
||||
}); |
|
||||
} |
|
||||
} |
|
||||
@ -1,65 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2022 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.dao.sql.component; |
|
||||
|
|
||||
import org.springframework.stereotype.Repository; |
|
||||
import org.thingsboard.server.dao.model.sql.ComponentDescriptorEntity; |
|
||||
import org.thingsboard.server.dao.util.HsqlDao; |
|
||||
|
|
||||
import javax.persistence.Query; |
|
||||
|
|
||||
@HsqlDao |
|
||||
@Repository |
|
||||
public class HsqlComponentDescriptorInsertRepository extends AbstractComponentDescriptorInsertRepository { |
|
||||
|
|
||||
private static final String P_KEY_CONFLICT_STATEMENT = "(component_descriptor.id=UUID(I.id))"; |
|
||||
private static final String UNQ_KEY_CONFLICT_STATEMENT = "(component_descriptor.clazz=I.clazz)"; |
|
||||
|
|
||||
private static final String INSERT_OR_UPDATE_ON_P_KEY_CONFLICT = getInsertString(P_KEY_CONFLICT_STATEMENT); |
|
||||
private static final String INSERT_OR_UPDATE_ON_UNQ_KEY_CONFLICT = getInsertString(UNQ_KEY_CONFLICT_STATEMENT); |
|
||||
|
|
||||
@Override |
|
||||
public ComponentDescriptorEntity saveOrUpdate(ComponentDescriptorEntity entity) { |
|
||||
return saveAndGet(entity, INSERT_OR_UPDATE_ON_P_KEY_CONFLICT, INSERT_OR_UPDATE_ON_UNQ_KEY_CONFLICT); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected Query getQuery(ComponentDescriptorEntity entity, String query) { |
|
||||
return entityManager.createNativeQuery(query, ComponentDescriptorEntity.class) |
|
||||
.setParameter("id", entity.getUuid().toString()) |
|
||||
.setParameter("created_time", entity.getCreatedTime()) |
|
||||
.setParameter("actions", entity.getActions()) |
|
||||
.setParameter("clazz", entity.getClazz()) |
|
||||
.setParameter("configuration_descriptor", entity.getConfigurationDescriptor().toString()) |
|
||||
.setParameter("name", entity.getName()) |
|
||||
.setParameter("scope", entity.getScope().name()) |
|
||||
.setParameter("search_text", entity.getSearchText()) |
|
||||
.setParameter("type", entity.getType().name()); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected ComponentDescriptorEntity doProcessSaveOrUpdate(ComponentDescriptorEntity entity, String query) { |
|
||||
getQuery(entity, query).executeUpdate(); |
|
||||
return entityManager.find(ComponentDescriptorEntity.class, entity.getUuid()); |
|
||||
} |
|
||||
|
|
||||
private static String getInsertString(String conflictStatement) { |
|
||||
return "MERGE INTO component_descriptor USING (VALUES :id, :created_time, :actions, :clazz, :configuration_descriptor, :name, :scope, :search_text, :type) I (id, created_time, actions, clazz, configuration_descriptor, name, scope, search_text, type) ON " |
|
||||
+ conflictStatement |
|
||||
+ " WHEN MATCHED THEN UPDATE SET component_descriptor.id = UUID(I.id), component_descriptor.actions = I.actions, component_descriptor.clazz = I.clazz, component_descriptor.configuration_descriptor = I.configuration_descriptor, component_descriptor.name = I.name, component_descriptor.scope = I.scope, component_descriptor.search_text = I.search_text, component_descriptor.type = I.type" + |
|
||||
" WHEN NOT MATCHED THEN INSERT (id, created_time, actions, clazz, configuration_descriptor, name, scope, search_text, type) VALUES (UUID(I.id), I.created_time, I.actions, I.clazz, I.configuration_descriptor, I.name, I.scope, I.search_text, I.type)"; |
|
||||
} |
|
||||
} |
|
||||
@ -1,63 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2022 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.dao.sql.relation; |
|
||||
|
|
||||
import org.springframework.stereotype.Repository; |
|
||||
import org.springframework.transaction.annotation.Transactional; |
|
||||
import org.thingsboard.server.dao.model.sql.RelationCompositeKey; |
|
||||
import org.thingsboard.server.dao.model.sql.RelationEntity; |
|
||||
import org.thingsboard.server.dao.util.HsqlDao; |
|
||||
|
|
||||
import javax.persistence.Query; |
|
||||
|
|
||||
@HsqlDao |
|
||||
@Repository |
|
||||
@Transactional |
|
||||
public class HsqlRelationInsertRepository extends AbstractRelationInsertRepository implements RelationInsertRepository { |
|
||||
|
|
||||
private static final String INSERT_ON_CONFLICT_DO_UPDATE = "MERGE INTO relation USING (VALUES :fromId, :fromType, :toId, :toType, :relationTypeGroup, :relationType, :additionalInfo) R " + |
|
||||
"(from_id, from_type, to_id, to_type, relation_type_group, relation_type, additional_info) " + |
|
||||
"ON (relation.from_id = UUID(R.from_id) AND relation.from_type = R.from_type AND relation.relation_type_group = R.relation_type_group AND relation.relation_type = R.relation_type AND relation.to_id = UUID(R.to_id) AND relation.to_type = R.to_type) " + |
|
||||
"WHEN MATCHED THEN UPDATE SET relation.additional_info = R.additional_info " + |
|
||||
"WHEN NOT MATCHED THEN INSERT (from_id, from_type, to_id, to_type, relation_type_group, relation_type, additional_info) VALUES (UUID(R.from_id), R.from_type, UUID(R.to_id), R.to_type, R.relation_type_group, R.relation_type, R.additional_info)"; |
|
||||
|
|
||||
protected Query getQuery(RelationEntity entity, String query) { |
|
||||
Query nativeQuery = entityManager.createNativeQuery(query, RelationEntity.class); |
|
||||
if (entity.getAdditionalInfo() == null) { |
|
||||
nativeQuery.setParameter("additionalInfo", null); |
|
||||
} else { |
|
||||
nativeQuery.setParameter("additionalInfo", entity.getAdditionalInfo().toString()); |
|
||||
} |
|
||||
return nativeQuery |
|
||||
.setParameter("fromId", entity.getFromId().toString()) |
|
||||
.setParameter("fromType", entity.getFromType()) |
|
||||
.setParameter("toId", entity.getToId().toString()) |
|
||||
.setParameter("toType", entity.getToType()) |
|
||||
.setParameter("relationTypeGroup", entity.getRelationTypeGroup()) |
|
||||
.setParameter("relationType", entity.getRelationType()); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public RelationEntity saveOrUpdate(RelationEntity entity) { |
|
||||
return processSaveOrUpdate(entity); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected RelationEntity processSaveOrUpdate(RelationEntity entity) { |
|
||||
getQuery(entity, INSERT_ON_CONFLICT_DO_UPDATE).executeUpdate(); |
|
||||
return entityManager.find(RelationEntity.class, new RelationCompositeKey(entity.toData())); |
|
||||
} |
|
||||
} |
|
||||
@ -1,62 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2022 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.dao.sqlts.hsql; |
|
||||
|
|
||||
import com.google.common.util.concurrent.Futures; |
|
||||
import com.google.common.util.concurrent.ListenableFuture; |
|
||||
import com.google.common.util.concurrent.MoreExecutors; |
|
||||
import lombok.extern.slf4j.Slf4j; |
|
||||
import org.springframework.stereotype.Component; |
|
||||
import org.thingsboard.server.common.data.id.EntityId; |
|
||||
import org.thingsboard.server.common.data.id.TenantId; |
|
||||
import org.thingsboard.server.common.data.kv.TsKvEntry; |
|
||||
import org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity; |
|
||||
import org.thingsboard.server.dao.sqlts.AbstractChunkedAggregationTimeseriesDao; |
|
||||
import org.thingsboard.server.dao.timeseries.TimeseriesDao; |
|
||||
import org.thingsboard.server.dao.util.HsqlDao; |
|
||||
import org.thingsboard.server.dao.util.SqlTsDao; |
|
||||
|
|
||||
|
|
||||
@Component |
|
||||
@Slf4j |
|
||||
@SqlTsDao |
|
||||
@HsqlDao |
|
||||
public class JpaHsqlTimeseriesDao extends AbstractChunkedAggregationTimeseriesDao implements TimeseriesDao { |
|
||||
|
|
||||
@Override |
|
||||
public ListenableFuture<Integer> save(TenantId tenantId, EntityId entityId, TsKvEntry tsKvEntry, long ttl) { |
|
||||
int dataPointDays = getDataPointDays(tsKvEntry, computeTtl(ttl)); |
|
||||
String strKey = tsKvEntry.getKey(); |
|
||||
Integer keyId = getOrSaveKeyId(strKey); |
|
||||
TsKvEntity entity = new TsKvEntity(); |
|
||||
entity.setEntityId(entityId.getId()); |
|
||||
entity.setTs(tsKvEntry.getTs()); |
|
||||
entity.setKey(keyId); |
|
||||
entity.setStrValue(tsKvEntry.getStrValue().orElse(null)); |
|
||||
entity.setDoubleValue(tsKvEntry.getDoubleValue().orElse(null)); |
|
||||
entity.setLongValue(tsKvEntry.getLongValue().orElse(null)); |
|
||||
entity.setBooleanValue(tsKvEntry.getBooleanValue().orElse(null)); |
|
||||
entity.setJsonValue(tsKvEntry.getJsonValue().orElse(null)); |
|
||||
log.trace("Saving entity: {}", entity); |
|
||||
return Futures.transform(tsQueue.add(entity), v -> dataPointDays, MoreExecutors.directExecutor()); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public void cleanup(long systemTtl) { |
|
||||
|
|
||||
} |
|
||||
|
|
||||
} |
|
||||
@ -1,87 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2022 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.dao.sqlts.insert.hsql; |
|
||||
|
|
||||
import org.springframework.jdbc.core.BatchPreparedStatementSetter; |
|
||||
import org.springframework.stereotype.Repository; |
|
||||
import org.springframework.transaction.annotation.Transactional; |
|
||||
import org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity; |
|
||||
import org.thingsboard.server.dao.sqlts.insert.AbstractInsertRepository; |
|
||||
import org.thingsboard.server.dao.sqlts.insert.InsertTsRepository; |
|
||||
import org.thingsboard.server.dao.util.HsqlDao; |
|
||||
import org.thingsboard.server.dao.util.SqlTsDao; |
|
||||
|
|
||||
import java.sql.PreparedStatement; |
|
||||
import java.sql.SQLException; |
|
||||
import java.sql.Types; |
|
||||
import java.util.List; |
|
||||
|
|
||||
@SqlTsDao |
|
||||
@HsqlDao |
|
||||
@Repository |
|
||||
@Transactional |
|
||||
public class HsqlInsertTsRepository extends AbstractInsertRepository implements InsertTsRepository<TsKvEntity> { |
|
||||
|
|
||||
private static final String INSERT_OR_UPDATE = |
|
||||
"MERGE INTO ts_kv USING(VALUES ?, ?, ?, ?, ?, ?, ?, ?) " + |
|
||||
"T (entity_id, key, ts, bool_v, str_v, long_v, dbl_v, json_v) " + |
|
||||
"ON (ts_kv.entity_id=T.entity_id " + |
|
||||
"AND ts_kv.key=T.key " + |
|
||||
"AND ts_kv.ts=T.ts) " + |
|
||||
"WHEN MATCHED THEN UPDATE SET ts_kv.bool_v = T.bool_v, ts_kv.str_v = T.str_v, ts_kv.long_v = T.long_v, ts_kv.dbl_v = T.dbl_v ,ts_kv.json_v = T.json_v " + |
|
||||
"WHEN NOT MATCHED THEN INSERT (entity_id, key, ts, bool_v, str_v, long_v, dbl_v, json_v) " + |
|
||||
"VALUES (T.entity_id, T.key, T.ts, T.bool_v, T.str_v, T.long_v, T.dbl_v, T.json_v);"; |
|
||||
|
|
||||
@Override |
|
||||
public void saveOrUpdate(List<TsKvEntity> entities) { |
|
||||
jdbcTemplate.batchUpdate(INSERT_OR_UPDATE, new BatchPreparedStatementSetter() { |
|
||||
@Override |
|
||||
public void setValues(PreparedStatement ps, int i) throws SQLException { |
|
||||
TsKvEntity tsKvEntity = entities.get(i); |
|
||||
ps.setObject(1, tsKvEntity.getEntityId()); |
|
||||
ps.setInt(2, tsKvEntity.getKey()); |
|
||||
ps.setLong(3, tsKvEntity.getTs()); |
|
||||
|
|
||||
if (tsKvEntity.getBooleanValue() != null) { |
|
||||
ps.setBoolean(4, tsKvEntity.getBooleanValue()); |
|
||||
} else { |
|
||||
ps.setNull(4, Types.BOOLEAN); |
|
||||
} |
|
||||
|
|
||||
ps.setString(5, tsKvEntity.getStrValue()); |
|
||||
|
|
||||
if (tsKvEntity.getLongValue() != null) { |
|
||||
ps.setLong(6, tsKvEntity.getLongValue()); |
|
||||
} else { |
|
||||
ps.setNull(6, Types.BIGINT); |
|
||||
} |
|
||||
|
|
||||
if (tsKvEntity.getDoubleValue() != null) { |
|
||||
ps.setDouble(7, tsKvEntity.getDoubleValue()); |
|
||||
} else { |
|
||||
ps.setNull(7, Types.DOUBLE); |
|
||||
} |
|
||||
|
|
||||
ps.setString(8, tsKvEntity.getJsonValue()); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public int getBatchSize() { |
|
||||
return entities.size(); |
|
||||
} |
|
||||
}); |
|
||||
} |
|
||||
} |
|
||||
@ -1,85 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2022 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.dao.sqlts.insert.latest.hsql; |
|
||||
|
|
||||
import org.springframework.jdbc.core.BatchPreparedStatementSetter; |
|
||||
import org.springframework.stereotype.Repository; |
|
||||
import org.springframework.transaction.annotation.Transactional; |
|
||||
import org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity; |
|
||||
import org.thingsboard.server.dao.sqlts.insert.AbstractInsertRepository; |
|
||||
import org.thingsboard.server.dao.sqlts.insert.latest.InsertLatestTsRepository; |
|
||||
import org.thingsboard.server.dao.util.HsqlDao; |
|
||||
import org.thingsboard.server.dao.util.SqlTsLatestDao; |
|
||||
|
|
||||
import java.sql.PreparedStatement; |
|
||||
import java.sql.SQLException; |
|
||||
import java.sql.Types; |
|
||||
import java.util.List; |
|
||||
|
|
||||
@SqlTsLatestDao |
|
||||
@HsqlDao |
|
||||
@Repository |
|
||||
@Transactional |
|
||||
public class HsqlLatestInsertTsRepository extends AbstractInsertRepository implements InsertLatestTsRepository { |
|
||||
|
|
||||
private static final String INSERT_OR_UPDATE = |
|
||||
"MERGE INTO ts_kv_latest USING(VALUES ?, ?, ?, ?, ?, ?, ?, ?) " + |
|
||||
"T (entity_id, key, ts, bool_v, str_v, long_v, dbl_v, json_v) " + |
|
||||
"ON (ts_kv_latest.entity_id=T.entity_id " + |
|
||||
"AND ts_kv_latest.key=T.key) " + |
|
||||
"WHEN MATCHED THEN UPDATE SET ts_kv_latest.ts = T.ts, ts_kv_latest.bool_v = T.bool_v, ts_kv_latest.str_v = T.str_v, ts_kv_latest.long_v = T.long_v, ts_kv_latest.dbl_v = T.dbl_v, ts_kv_latest.json_v = T.json_v " + |
|
||||
"WHEN NOT MATCHED THEN INSERT (entity_id, key, ts, bool_v, str_v, long_v, dbl_v, json_v) " + |
|
||||
"VALUES (T.entity_id, T.key, T.ts, T.bool_v, T.str_v, T.long_v, T.dbl_v, T.json_v);"; |
|
||||
|
|
||||
@Override |
|
||||
public void saveOrUpdate(List<TsKvLatestEntity> entities) { |
|
||||
jdbcTemplate.batchUpdate(INSERT_OR_UPDATE, new BatchPreparedStatementSetter() { |
|
||||
@Override |
|
||||
public void setValues(PreparedStatement ps, int i) throws SQLException { |
|
||||
ps.setObject(1, entities.get(i).getEntityId()); |
|
||||
ps.setInt(2, entities.get(i).getKey()); |
|
||||
ps.setLong(3, entities.get(i).getTs()); |
|
||||
|
|
||||
if (entities.get(i).getBooleanValue() != null) { |
|
||||
ps.setBoolean(4, entities.get(i).getBooleanValue()); |
|
||||
} else { |
|
||||
ps.setNull(4, Types.BOOLEAN); |
|
||||
} |
|
||||
|
|
||||
ps.setString(5, entities.get(i).getStrValue()); |
|
||||
|
|
||||
if (entities.get(i).getLongValue() != null) { |
|
||||
ps.setLong(6, entities.get(i).getLongValue()); |
|
||||
} else { |
|
||||
ps.setNull(6, Types.BIGINT); |
|
||||
} |
|
||||
|
|
||||
if (entities.get(i).getDoubleValue() != null) { |
|
||||
ps.setDouble(7, entities.get(i).getDoubleValue()); |
|
||||
} else { |
|
||||
ps.setNull(7, Types.DOUBLE); |
|
||||
} |
|
||||
|
|
||||
ps.setString(8, entities.get(i).getJsonValue()); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public int getBatchSize() { |
|
||||
return entities.size(); |
|
||||
} |
|
||||
}); |
|
||||
} |
|
||||
} |
|
||||
@ -1,40 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2022 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.dao.util; |
|
||||
|
|
||||
import org.springframework.jdbc.core.JdbcTemplate; |
|
||||
|
|
||||
import java.sql.DriverManager; |
|
||||
|
|
||||
public class DaoTestUtil { |
|
||||
private static final String POSTGRES_DRIVER_CLASS = "org.postgresql.Driver"; |
|
||||
private static final String H2_DRIVER_CLASS = "org.hsqldb.jdbc.JDBCDriver"; |
|
||||
|
|
||||
|
|
||||
public static SqlDbType getSqlDbType(JdbcTemplate template){ |
|
||||
try { |
|
||||
String driverName = DriverManager.getDriver(template.getDataSource().getConnection().getMetaData().getURL()).getClass().getName(); |
|
||||
if (POSTGRES_DRIVER_CLASS.equals(driverName)) { |
|
||||
return SqlDbType.POSTGRES; |
|
||||
} else if (H2_DRIVER_CLASS.equals(driverName)) { |
|
||||
return SqlDbType.H2; |
|
||||
} |
|
||||
} catch (Exception e) { |
|
||||
e.printStackTrace(); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
} |
|
||||
@ -1,20 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2022 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.dao.util; |
|
||||
|
|
||||
public enum SqlDbType { |
|
||||
POSTGRES, H2; |
|
||||
} |
|
||||
@ -1,61 +0,0 @@ |
|||||
# |
|
||||
# Copyright © 2016-2022 The Thingsboard Authors |
|
||||
# |
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
# you may not use this file except in compliance with the License. |
|
||||
# You may obtain a copy of the License at |
|
||||
# |
|
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
|
||||
# |
|
||||
# Unless required by applicable law or agreed to in writing, software |
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
# See the License for the specific language governing permissions and |
|
||||
# limitations under the License. |
|
||||
# |
|
||||
|
|
||||
FROM thingsboard/openjdk11 |
|
||||
|
|
||||
COPY logback.xml ${pkg.name}.conf start-db.sh stop-db.sh start-tb.sh upgrade-tb.sh install-tb.sh ${pkg.name}.deb /tmp/ |
|
||||
|
|
||||
RUN chmod a+x /tmp/*.sh \ |
|
||||
&& mv /tmp/start-tb.sh /usr/bin \ |
|
||||
&& mv /tmp/upgrade-tb.sh /usr/bin \ |
|
||||
&& mv /tmp/install-tb.sh /usr/bin \ |
|
||||
&& mv /tmp/start-db.sh /usr/bin \ |
|
||||
&& mv /tmp/stop-db.sh /usr/bin |
|
||||
|
|
||||
RUN dpkg -i /tmp/${pkg.name}.deb |
|
||||
RUN rm /tmp/${pkg.name}.deb |
|
||||
|
|
||||
RUN systemctl --no-reload disable --now ${pkg.name}.service > /dev/null 2>&1 || : |
|
||||
|
|
||||
RUN mv /tmp/logback.xml ${pkg.installFolder}/conf \ |
|
||||
&& mv /tmp/${pkg.name}.conf ${pkg.installFolder}/conf |
|
||||
|
|
||||
ENV DATA_FOLDER=/data |
|
||||
|
|
||||
ENV HTTP_BIND_PORT=9090 |
|
||||
ENV DATABASE_TS_TYPE=sql |
|
||||
|
|
||||
ENV SPRING_JPA_DATABASE_PLATFORM=org.hibernate.dialect.HSQLDialect |
|
||||
ENV SPRING_DRIVER_CLASS_NAME=org.hsqldb.jdbc.JDBCDriver |
|
||||
ENV SPRING_DATASOURCE_URL=jdbc:hsqldb:file:/data/db/thingsboardDb;sql.enforce_size=false;hsqldb.log_size=5 |
|
||||
ENV SPRING_DATASOURCE_USERNAME=sa |
|
||||
ENV SPRING_DATASOURCE_PASSWORD= |
|
||||
|
|
||||
RUN mkdir -p /data |
|
||||
RUN chown -R ${pkg.user}:${pkg.user} /data |
|
||||
|
|
||||
RUN chmod 555 ${pkg.installFolder}/bin/${pkg.name}.jar |
|
||||
|
|
||||
USER ${pkg.user} |
|
||||
|
|
||||
EXPOSE 9090 |
|
||||
EXPOSE 1883 |
|
||||
EXPOSE 5683/udp |
|
||||
EXPOSE 5685/udp |
|
||||
|
|
||||
VOLUME ["/data"] |
|
||||
|
|
||||
CMD ["start-tb.sh"] |
|
||||
@ -1,18 +0,0 @@ |
|||||
#!/bin/bash |
|
||||
# |
|
||||
# Copyright © 2016-2022 The Thingsboard Authors |
|
||||
# |
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
# you may not use this file except in compliance with the License. |
|
||||
# You may obtain a copy of the License at |
|
||||
# |
|
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
|
||||
# |
|
||||
# Unless required by applicable law or agreed to in writing, software |
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
# See the License for the specific language governing permissions and |
|
||||
# limitations under the License. |
|
||||
# |
|
||||
|
|
||||
# Do nothing |
|
||||
@ -1,18 +0,0 @@ |
|||||
#!/bin/bash |
|
||||
# |
|
||||
# Copyright © 2016-2022 The Thingsboard Authors |
|
||||
# |
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
# you may not use this file except in compliance with the License. |
|
||||
# You may obtain a copy of the License at |
|
||||
# |
|
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
|
||||
# |
|
||||
# Unless required by applicable law or agreed to in writing, software |
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
# See the License for the specific language governing permissions and |
|
||||
# limitations under the License. |
|
||||
# |
|
||||
|
|
||||
# Do nothing |
|
||||
Loading…
Reference in new issue