Implement JpaPartitionedAbstractDao
This commit is contained in:
		
							parent
							
								
									8fe4947e1e
								
							
						
					
					
						commit
						2d7c1a3cd5
					
				@ -380,7 +380,7 @@ public abstract class BaseController {
 | 
			
		||||
            if (!logControllerErrorStackTrace) { // not to log the error twice
 | 
			
		||||
                log.warn("Database error: {} - {}", errorType, ExceptionUtils.getRootCauseMessage(exception));
 | 
			
		||||
            }
 | 
			
		||||
            return new ThingsboardException("Database error: " + errorType, ThingsboardErrorCode.GENERAL);
 | 
			
		||||
            return new ThingsboardException("Database error", ThingsboardErrorCode.GENERAL);
 | 
			
		||||
        }
 | 
			
		||||
        return new ThingsboardException(exception.getMessage(), exception, ThingsboardErrorCode.GENERAL);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -43,9 +43,6 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D>
 | 
			
		||||
        extends JpaAbstractDaoListeningExecutorService
 | 
			
		||||
        implements Dao<D> {
 | 
			
		||||
 | 
			
		||||
    @PersistenceContext
 | 
			
		||||
    private EntityManager entityManager;
 | 
			
		||||
 | 
			
		||||
    protected abstract Class<E> getEntityClass();
 | 
			
		||||
 | 
			
		||||
    protected abstract JpaRepository<E, UUID> getRepository();
 | 
			
		||||
@ -67,18 +64,14 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D>
 | 
			
		||||
            entity.setUuid(uuid);
 | 
			
		||||
            entity.setCreatedTime(Uuids.unixTimestamp(uuid));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (isPartitioned()) {
 | 
			
		||||
            createPartition(entity);
 | 
			
		||||
        }
 | 
			
		||||
        if (isNew) {
 | 
			
		||||
            entityManager.persist(entity);
 | 
			
		||||
        } else {
 | 
			
		||||
            entity = entityManager.merge(entity);
 | 
			
		||||
        }
 | 
			
		||||
        entity = doSave(entity, isNew);
 | 
			
		||||
        return DaoUtil.getData(entity);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected E doSave(E entity, boolean isNew) {
 | 
			
		||||
        return getRepository().save(entity);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @Transactional
 | 
			
		||||
    public D saveAndFlush(TenantId tenantId, D domain) {
 | 
			
		||||
@ -132,11 +125,4 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D>
 | 
			
		||||
        return DaoUtil.convertDataList(entities);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean isPartitioned() {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void createPartition(E entity) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,43 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2023 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;
 | 
			
		||||
 | 
			
		||||
import org.thingsboard.server.dao.model.BaseEntity;
 | 
			
		||||
import org.thingsboard.server.dao.util.SqlDao;
 | 
			
		||||
 | 
			
		||||
import javax.persistence.EntityManager;
 | 
			
		||||
import javax.persistence.PersistenceContext;
 | 
			
		||||
 | 
			
		||||
@SqlDao
 | 
			
		||||
public abstract class JpaPartitionedAbstractDao<E extends BaseEntity<D>, D> extends JpaAbstractDao<E, D> {
 | 
			
		||||
 | 
			
		||||
    @PersistenceContext
 | 
			
		||||
    private EntityManager entityManager;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected E doSave(E entity, boolean isNew) {
 | 
			
		||||
        createPartition(entity);
 | 
			
		||||
        if (isNew) {
 | 
			
		||||
            entityManager.persist(entity);
 | 
			
		||||
        } else {
 | 
			
		||||
            entity = entityManager.merge(entity);
 | 
			
		||||
        }
 | 
			
		||||
        return entity;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public abstract void createPartition(E entity);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -31,7 +31,7 @@ import org.thingsboard.server.common.data.page.PageLink;
 | 
			
		||||
import org.thingsboard.server.dao.DaoUtil;
 | 
			
		||||
import org.thingsboard.server.dao.alarm.AlarmCommentDao;
 | 
			
		||||
import org.thingsboard.server.dao.model.sql.AlarmCommentEntity;
 | 
			
		||||
import org.thingsboard.server.dao.sql.JpaAbstractDao;
 | 
			
		||||
import org.thingsboard.server.dao.sql.JpaPartitionedAbstractDao;
 | 
			
		||||
import org.thingsboard.server.dao.sqlts.insert.sql.SqlPartitioningRepository;
 | 
			
		||||
import org.thingsboard.server.dao.util.SqlDao;
 | 
			
		||||
 | 
			
		||||
@ -44,7 +44,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.ALARM_COMMENT_TABL
 | 
			
		||||
@Component
 | 
			
		||||
@SqlDao
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
public class JpaAlarmCommentDao extends JpaAbstractDao<AlarmCommentEntity, AlarmComment> implements AlarmCommentDao {
 | 
			
		||||
public class JpaAlarmCommentDao extends JpaPartitionedAbstractDao<AlarmCommentEntity, AlarmComment> implements AlarmCommentDao {
 | 
			
		||||
    private final SqlPartitioningRepository partitioningRepository;
 | 
			
		||||
    @Value("${sql.alarm_comments.partition_size:168}")
 | 
			
		||||
    private int partitionSizeInHours;
 | 
			
		||||
@ -53,7 +53,7 @@ public class JpaAlarmCommentDao extends JpaAbstractDao<AlarmCommentEntity, Alarm
 | 
			
		||||
    private AlarmCommentRepository alarmCommentRepository;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public PageData<AlarmCommentInfo> findAlarmComments(TenantId tenantId, AlarmId id, PageLink pageLink){
 | 
			
		||||
    public PageData<AlarmCommentInfo> findAlarmComments(TenantId tenantId, AlarmId id, PageLink pageLink) {
 | 
			
		||||
        log.trace("Try to find alarm comments by alarm id using [{}]", id);
 | 
			
		||||
        return DaoUtil.toPageData(
 | 
			
		||||
                alarmCommentRepository.findAllByAlarmId(id.getId(), DaoUtil.toPageable(pageLink)));
 | 
			
		||||
@ -71,11 +71,6 @@ public class JpaAlarmCommentDao extends JpaAbstractDao<AlarmCommentEntity, Alarm
 | 
			
		||||
        return findByIdAsync(tenantId, key);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isPartitioned() {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void createPartition(AlarmCommentEntity entity) {
 | 
			
		||||
        partitioningRepository.createPartitionIfNotExists(ALARM_COMMENT_TABLE_NAME, entity.getCreatedTime(), TimeUnit.HOURS.toMillis(partitionSizeInHours));
 | 
			
		||||
 | 
			
		||||
@ -32,7 +32,7 @@ import org.thingsboard.server.dao.DaoUtil;
 | 
			
		||||
import org.thingsboard.server.dao.audit.AuditLogDao;
 | 
			
		||||
import org.thingsboard.server.dao.model.ModelConstants;
 | 
			
		||||
import org.thingsboard.server.dao.model.sql.AuditLogEntity;
 | 
			
		||||
import org.thingsboard.server.dao.sql.JpaAbstractDao;
 | 
			
		||||
import org.thingsboard.server.dao.sql.JpaPartitionedAbstractDao;
 | 
			
		||||
import org.thingsboard.server.dao.sqlts.insert.sql.SqlPartitioningRepository;
 | 
			
		||||
import org.thingsboard.server.dao.util.SqlDao;
 | 
			
		||||
 | 
			
		||||
@ -44,7 +44,7 @@ import java.util.concurrent.TimeUnit;
 | 
			
		||||
@SqlDao
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class JpaAuditLogDao extends JpaAbstractDao<AuditLogEntity, AuditLog> implements AuditLogDao {
 | 
			
		||||
public class JpaAuditLogDao extends JpaPartitionedAbstractDao<AuditLogEntity, AuditLog> implements AuditLogDao {
 | 
			
		||||
 | 
			
		||||
    private final AuditLogRepository auditLogRepository;
 | 
			
		||||
    private final SqlPartitioningRepository partitioningRepository;
 | 
			
		||||
@ -158,11 +158,6 @@ public class JpaAuditLogDao extends JpaAbstractDao<AuditLogEntity, AuditLog> imp
 | 
			
		||||
        jdbcTemplate.update("CALL migrate_audit_logs(?, ?, ?)", startTime, endTime, partitionSizeInMs);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isPartitioned() {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void createPartition(AuditLogEntity entity) {
 | 
			
		||||
        partitioningRepository.createPartitionIfNotExists(TABLE_NAME, entity.getCreatedTime(), TimeUnit.HOURS.toMillis(partitionSizeInHours));
 | 
			
		||||
 | 
			
		||||
@ -35,7 +35,7 @@ import org.thingsboard.server.dao.DaoUtil;
 | 
			
		||||
import org.thingsboard.server.dao.edge.EdgeEventDao;
 | 
			
		||||
import org.thingsboard.server.dao.model.ModelConstants;
 | 
			
		||||
import org.thingsboard.server.dao.model.sql.EdgeEventEntity;
 | 
			
		||||
import org.thingsboard.server.dao.sql.JpaAbstractDao;
 | 
			
		||||
import org.thingsboard.server.dao.sql.JpaPartitionedAbstractDao;
 | 
			
		||||
import org.thingsboard.server.dao.sql.ScheduledLogExecutorComponent;
 | 
			
		||||
import org.thingsboard.server.dao.sql.TbSqlBlockingQueueParams;
 | 
			
		||||
import org.thingsboard.server.dao.sql.TbSqlBlockingQueueWrapper;
 | 
			
		||||
@ -57,7 +57,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
 | 
			
		||||
@SqlDao
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class JpaBaseEdgeEventDao extends JpaAbstractDao<EdgeEventEntity, EdgeEvent> implements EdgeEventDao {
 | 
			
		||||
public class JpaBaseEdgeEventDao extends JpaPartitionedAbstractDao<EdgeEventEntity, EdgeEvent> implements EdgeEventDao {
 | 
			
		||||
 | 
			
		||||
    private final UUID systemTenantId = NULL_UUID;
 | 
			
		||||
 | 
			
		||||
@ -228,11 +228,6 @@ public class JpaBaseEdgeEventDao extends JpaAbstractDao<EdgeEventEntity, EdgeEve
 | 
			
		||||
        jdbcTemplate.update("CALL migrate_edge_event(?, ?, ?)", startTime, endTime, partitionSIzeInMs);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isPartitioned() {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void createPartition(EdgeEventEntity entity) {
 | 
			
		||||
        partitioningRepository.createPartitionIfNotExists(TABLE_NAME, entity.getCreatedTime(), TimeUnit.HOURS.toMillis(partitionSizeInHours));
 | 
			
		||||
 | 
			
		||||
@ -32,7 +32,7 @@ import org.thingsboard.server.dao.DaoUtil;
 | 
			
		||||
import org.thingsboard.server.dao.model.ModelConstants;
 | 
			
		||||
import org.thingsboard.server.dao.model.sql.NotificationEntity;
 | 
			
		||||
import org.thingsboard.server.dao.notification.NotificationDao;
 | 
			
		||||
import org.thingsboard.server.dao.sql.JpaAbstractDao;
 | 
			
		||||
import org.thingsboard.server.dao.sql.JpaPartitionedAbstractDao;
 | 
			
		||||
import org.thingsboard.server.dao.sqlts.insert.sql.SqlPartitioningRepository;
 | 
			
		||||
import org.thingsboard.server.dao.util.SqlDao;
 | 
			
		||||
 | 
			
		||||
@ -42,7 +42,7 @@ import java.util.concurrent.TimeUnit;
 | 
			
		||||
@Component
 | 
			
		||||
@SqlDao
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
public class JpaNotificationDao extends JpaAbstractDao<NotificationEntity, Notification> implements NotificationDao {
 | 
			
		||||
public class JpaNotificationDao extends JpaPartitionedAbstractDao<NotificationEntity, Notification> implements NotificationDao {
 | 
			
		||||
 | 
			
		||||
    private final NotificationRepository notificationRepository;
 | 
			
		||||
    private final SqlPartitioningRepository partitioningRepository;
 | 
			
		||||
@ -100,11 +100,6 @@ public class JpaNotificationDao extends JpaAbstractDao<NotificationEntity, Notif
 | 
			
		||||
        notificationRepository.deleteByRecipientId(recipientId.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isPartitioned() {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void createPartition(NotificationEntity entity) {
 | 
			
		||||
        partitioningRepository.createPartitionIfNotExists(ModelConstants.NOTIFICATION_TABLE_NAME,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user