Skip to content

Commit

Permalink
Do not fetch the entire entity if only the id is needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
amporsim committed Sep 13, 2023
1 parent 2a6445e commit 3c05838
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ public CaseInstanceMigrationValidationResult validateMigrateCaseInstancesOfCaseD
CmmnModel newModel = CaseDefinitionUtil.getCmmnModel(caseDefinition.getId());

CaseInstanceEntityManager caseInstanceEntityManager = cmmnEngineConfiguration.getCaseInstanceEntityManager();
List<CaseInstance> caseInstances = caseInstanceEntityManager.findByCriteria(
List<String> caseInstanceIds = caseInstanceEntityManager.findIdsByCriteria(
new CaseInstanceQueryImpl(commandContext, cmmnEngineConfiguration).caseDefinitionId(caseDefinitionId));

for (CaseInstance caseInstance : caseInstances) {
doValidateCaseInstanceMigration(caseInstance.getId(), newModel, document, validationResult, commandContext);
for (String caseInstanceId : caseInstanceIds) {
doValidateCaseInstanceMigration(caseInstanceId, newModel, document, validationResult, commandContext);
}
}
}
Expand Down Expand Up @@ -497,9 +497,10 @@ public Batch batchMigrateHistoricCaseInstancesOfCaseDefinition(String caseDefini
CaseDefinition caseDefinition = resolveCaseDefinition(document, commandContext);

CmmnEngineConfiguration engineConfiguration = CommandContextUtil.getCmmnEngineConfiguration();
HistoricCaseInstanceQueryImpl historicCaseInstanceQuery = new HistoricCaseInstanceQueryImpl(commandContext, cmmnEngineConfiguration).caseDefinitionId(caseDefinitionId).finished();
List<HistoricCaseInstance> historicCaseInstances = engineConfiguration.getHistoricCaseInstanceEntityManager()
.findByCriteria(historicCaseInstanceQuery);
HistoricCaseInstanceQueryImpl historicCaseInstanceQuery = new HistoricCaseInstanceQueryImpl(commandContext, cmmnEngineConfiguration).caseDefinitionId(
caseDefinitionId).finished();
List<String> historicCaseInstanceIds = engineConfiguration.getHistoricCaseInstanceEntityManager()
.findIdsByCriteria(historicCaseInstanceQuery);

BatchService batchService = engineConfiguration.getBatchServiceConfiguration().getBatchService();
Batch batch = batchService.createBatchBuilder().batchType(Batch.CASE_MIGRATION_TYPE)
Expand All @@ -510,20 +511,20 @@ public Batch batchMigrateHistoricCaseInstancesOfCaseDefinition(String caseDefini
.create();

JobService jobService = engineConfiguration.getJobServiceConfiguration().getJobService();
for (HistoricCaseInstance historicCaseInstance : historicCaseInstances) {
for (String historicCaseInstanceId : historicCaseInstanceIds) {
BatchPart batchPart = batchService.createBatchPart(batch, CaseInstanceBatchMigrationResult.STATUS_WAITING,
historicCaseInstance.getId(), null, ScopeTypes.CMMN);
historicCaseInstanceId, null, ScopeTypes.CMMN);

JobEntity job = jobService.createJob();
job.setJobHandlerType(HistoricCaseInstanceMigrationJobHandler.TYPE);
job.setScopeId(historicCaseInstance.getId());
job.setScopeId(historicCaseInstanceId);
job.setScopeType(ScopeTypes.CMMN);
job.setJobHandlerConfiguration(HistoricCaseInstanceMigrationJobHandler.getHandlerCfgForBatchPartId(batchPart.getId()));
jobService.createAsyncJob(job, false);
jobService.scheduleAsyncJob(job);
}

if (!historicCaseInstances.isEmpty()) {
if (!historicCaseInstanceIds.isEmpty()) {
TimerJobService timerJobService = engineConfiguration.getJobServiceConfiguration().getTimerJobService();
TimerJobEntity timerJob = timerJobService.createTimerJob();
timerJob.setJobType(JobEntity.JOB_TYPE_TIMER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@

import java.util.List;

import org.flowable.cmmn.api.history.HistoricCaseInstance;
import org.flowable.cmmn.api.history.HistoricMilestoneInstance;
import org.flowable.cmmn.api.repository.CaseDefinition;
import org.flowable.cmmn.api.repository.CaseDefinitionQuery;
import org.flowable.cmmn.api.runtime.CaseInstance;
import org.flowable.cmmn.engine.CmmnEngineConfiguration;
import org.flowable.cmmn.engine.impl.history.CmmnHistoryHelper;
import org.flowable.cmmn.engine.impl.history.HistoricCaseInstanceQueryImpl;
Expand Down Expand Up @@ -90,22 +88,23 @@ public CaseDefinition findCaseDefinitionByKeyAndVersionAndTenantId(String caseDe

@Override
public void deleteCaseDefinitionAndRelatedData(String caseDefinitionId, boolean cascadeHistory) {

// Case instances
CaseInstanceEntityManager caseInstanceEntityManager = getCaseInstanceEntityManager();
CommandContext commandContext = Context.getCommandContext();
List<CaseInstance> caseInstances = caseInstanceEntityManager.findByCriteria(
List<String> caseInstanceIds = caseInstanceEntityManager.findIdsByCriteria(
new CaseInstanceQueryImpl(commandContext, engineConfiguration).caseDefinitionId(caseDefinitionId));
for (CaseInstance caseInstance : caseInstances) {
caseInstanceEntityManager.delete(caseInstance.getId(), true, null);
for (String caseInstanceId : caseInstanceIds) {
caseInstanceEntityManager.delete(caseInstanceId, true, null);
}

if (cascadeHistory) {
engineConfiguration.getTaskServiceConfiguration().getHistoricTaskService().deleteHistoricTaskLogEntriesForScopeDefinition(ScopeTypes.CMMN, caseDefinitionId);
engineConfiguration.getTaskServiceConfiguration().getHistoricTaskService()
.deleteHistoricTaskLogEntriesForScopeDefinition(ScopeTypes.CMMN, caseDefinitionId);

HistoricIdentityLinkEntityManager historicIdentityLinkEntityManager = getHistoricIdentityLinkEntityManager();
historicIdentityLinkEntityManager.deleteHistoricIdentityLinksByScopeDefinitionIdAndScopeType(caseDefinitionId, ScopeTypes.CMMN);

// Historic milestone
HistoricMilestoneInstanceEntityManager historicMilestoneInstanceEntityManager = getHistoricMilestoneInstanceEntityManager();
List<HistoricMilestoneInstance> historicMilestoneInstances = historicMilestoneInstanceEntityManager
Expand All @@ -117,7 +116,7 @@ public void deleteCaseDefinitionAndRelatedData(String caseDefinitionId, boolean
// Historic tasks
HistoricTaskInstanceEntityManager historicTaskInstanceEntityManager = getHistoricTaskInstanceEntityManager();
List<HistoricTaskInstance> historicTaskInstances = historicTaskInstanceEntityManager
.findHistoricTaskInstancesByQueryCriteria(new HistoricTaskInstanceQueryImpl().scopeDefinitionId(caseDefinitionId).scopeType(ScopeTypes.CMMN));
.findHistoricTaskInstancesByQueryCriteria(new HistoricTaskInstanceQueryImpl().scopeDefinitionId(caseDefinitionId).scopeType(ScopeTypes.CMMN));
for (HistoricTaskInstance historicTaskInstance : historicTaskInstances) {
TaskHelper.deleteHistoricTask(historicTaskInstance.getId(), engineConfiguration);
}
Expand All @@ -128,10 +127,10 @@ public void deleteCaseDefinitionAndRelatedData(String caseDefinitionId, boolean
.forEach(p -> historicPlanItemInstanceEntityManager.delete(p.getId()));

HistoricCaseInstanceEntityManager historicCaseInstanceEntityManager = getHistoricCaseInstanceEntityManager();
List<HistoricCaseInstance> historicCaseInstanceEntities = historicCaseInstanceEntityManager
.findByCriteria(new HistoricCaseInstanceQueryImpl().caseDefinitionId(caseDefinitionId));
for (HistoricCaseInstance historicCaseInstanceEntity : historicCaseInstanceEntities) {
CmmnHistoryHelper.deleteHistoricCaseInstance(engineConfiguration, historicCaseInstanceEntity.getId());
List<String> historicCaseInstanceIds = historicCaseInstanceEntityManager
.findIdsByCriteria(new HistoricCaseInstanceQueryImpl().caseDefinitionId(caseDefinitionId));
for (String historicCaseInstanceId : historicCaseInstanceIds) {
CmmnHistoryHelper.deleteHistoricCaseInstance(engineConfiguration, historicCaseInstanceId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ public interface CaseInstanceEntityManager extends EntityManager<CaseInstanceEnt

List<CaseInstance> findByCriteria(CaseInstanceQuery query);

List<String> findIdsByCriteria(CaseInstanceQuery query);

List<CaseInstance> findWithVariablesByCriteria(CaseInstanceQuery query);

long countByCriteria(CaseInstanceQuery query);

void delete(String caseInstanceId, boolean cascade, String deleteReason);

void updateCaseInstanceBusinessKey(CaseInstanceEntity caseInstanceEntity, String businessKey);

void updateCaseInstanceBusinessStatus(CaseInstanceEntity caseInstanceEntity, String businessStatus);

void updateLockTime(String caseInstanceId, String lockOwner, Date lockTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public List<CaseInstance> findByCriteria(CaseInstanceQuery query) {
return dataManager.findByCriteria((CaseInstanceQueryImpl) query);
}

@Override
public List<String> findIdsByCriteria(CaseInstanceQuery query) {
return dataManager.findIdsByCriteria((CaseInstanceQueryImpl) query);
}

@Override
public List<CaseInstance> findWithVariablesByCriteria(CaseInstanceQuery query) {
return dataManager.findWithVariablesByCriteria((CaseInstanceQueryImpl) query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@
public interface HistoricCaseInstanceEntityManager extends EntityManager<HistoricCaseInstanceEntity> {

HistoricCaseInstanceEntity create(CaseInstance caseInstance);

HistoricCaseInstanceQuery createHistoricCaseInstanceQuery();

List<HistoricCaseInstanceEntity> findHistoricCaseInstancesByCaseDefinitionId(String caseDefinitionId);

List<String> findHistoricCaseInstanceIdsByParentIds(Collection<String> caseInstanceIds);

List<HistoricCaseInstance> findByCriteria(HistoricCaseInstanceQuery query);

List<String> findIdsByCriteria(HistoricCaseInstanceQuery query);

List<HistoricCaseInstance> findWithVariablesByQueryCriteria(HistoricCaseInstanceQuery query);

long countByCriteria(HistoricCaseInstanceQuery query);

void deleteHistoricCaseInstances(HistoricCaseInstanceQueryImpl historicCaseInstanceQuery);

void bulkDeleteHistoricCaseInstances(Collection<String> caseInstanceIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public List<HistoricCaseInstance> findByCriteria(HistoricCaseInstanceQuery query
return dataManager.findByCriteria((HistoricCaseInstanceQueryImpl) query);
}

@Override
public List<String> findIdsByCriteria(HistoricCaseInstanceQuery query) {
return dataManager.findIdsByCriteria((HistoricCaseInstanceQueryImpl) query);
}

@Override
@SuppressWarnings("unchecked")
public List<HistoricCaseInstance> findWithVariablesByQueryCriteria(HistoricCaseInstanceQuery query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public interface CaseInstanceDataManager extends DataManager<CaseInstanceEntity>

List<CaseInstance> findByCriteria(CaseInstanceQueryImpl query);

List<String> findIdsByCriteria(CaseInstanceQueryImpl query);

List<CaseInstance> findWithVariablesByCriteria(CaseInstanceQueryImpl query);

long countByCriteria(CaseInstanceQueryImpl query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@
public interface HistoricCaseInstanceDataManager extends DataManager<HistoricCaseInstanceEntity> {

HistoricCaseInstanceEntity create(CaseInstance caseInstance);

List<HistoricCaseInstanceEntity> findHistoricCaseInstancesByCaseDefinitionId(String caseDefinitionId);

List<String> findHistoricCaseInstanceIdsByParentIds(Collection<String> caseInstanceIds);

List<HistoricCaseInstance> findByCriteria(HistoricCaseInstanceQueryImpl query);


List<String> findIdsByCriteria(HistoricCaseInstanceQueryImpl query);

long countByCriteria(HistoricCaseInstanceQueryImpl query);

List<HistoricCaseInstance> findWithVariablesByQueryCriteria(HistoricCaseInstanceQueryImpl historicCaseInstanceQuery);

void deleteByCaseDefinitionId(String caseDefinitionId);

void deleteHistoricCaseInstances(HistoricCaseInstanceQueryImpl historicCaseInstanceQuery);

void bulkDeleteHistoricCaseInstances(Collection<String> caseInstanceIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ public List<CaseInstance> findByCriteria(CaseInstanceQueryImpl query) {
return getDbSqlSession().selectListNoCacheLoadAndStore("selectCaseInstancesByQueryCriteria", query, getManagedEntityClass());
}

@Override
@SuppressWarnings("unchecked")
public List<String> findIdsByCriteria(CaseInstanceQueryImpl query) {
// Not going through cache as the case instance should always be loaded with all related plan item instances
// when not doing a query call
setSafeInValueLists(query);
return getDbSqlSession().selectListNoCacheLoadAndStore("selectCaseInstanceIdsByQueryCriteria", query, getManagedEntityClass());
}

@SuppressWarnings("unchecked")
@Override
public List<CaseInstance> findWithVariablesByCriteria(CaseInstanceQueryImpl query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public List<HistoricCaseInstance> findByCriteria(HistoricCaseInstanceQueryImpl q
return getDbSqlSession().selectList("selectHistoricCaseInstancesByQueryCriteria", query, getManagedEntityClass());
}

@Override
@SuppressWarnings("unchecked")
public List<String> findIdsByCriteria(HistoricCaseInstanceQueryImpl query) {
setSafeInValueLists(query);
return getDbSqlSession().selectList("selectHistoricCaseInstanceIdsByQueryCriteria", query, getManagedEntityClass());
}

@Override
public long countByCriteria(HistoricCaseInstanceQueryImpl query) {
setSafeInValueLists(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,26 @@
<select id="selectCaseInstancesByQueryCriteria" parameterType="org.flowable.cmmn.engine.impl.runtime.CaseInstanceQueryImpl" resultMap="caseInstanceResultMap">
<if test="needsPaging">${limitBefore}</if>
select RES.* <if test="needsPaging">${limitBetween}</if>,
CASE_DEF.KEY_ as CaseDefinitionKey,
CASE_DEF.NAME_ as CaseDefinitionName,
CASE_DEF.VERSION_ as CaseDefinitionVersion,
CASE_DEF.DEPLOYMENT_ID_ as CaseDefinitionDeploymentId
CASE_DEF.KEY_ as CaseDefinitionKey,
CASE_DEF.NAME_ as CaseDefinitionName,
CASE_DEF.VERSION_ as CaseDefinitionVersion,
CASE_DEF.DEPLOYMENT_ID_ as CaseDefinitionDeploymentId
from ${prefix}ACT_CMMN_RU_CASE_INST RES
<!-- Doing an inner join on the definition table is OK, since it is a 1:1 relationship -->
inner join ${prefix}ACT_CMMN_CASEDEF CASE_DEF on RES.CASE_DEF_ID_ = CASE_DEF.ID_
<include refid="commonSelectCaseInstancesByQueryCriteriaSql" />
<include refid="commonSelectCaseInstancesByQueryCriteriaSql"/>
${orderBy}
<if test="needsPaging">${limitAfter}</if>
</select>

<select id="selectCaseInstanceIdsByQueryCriteria" parameterType="org.flowable.cmmn.engine.impl.runtime.CaseInstanceQueryImpl" resultType="string">
<if test="needsPaging">${limitBefore}</if>
select RES.ID_
<if test="needsPaging">${limitBetween}</if>
from ${prefix}ACT_CMMN_RU_CASE_INST RES
<!-- Doing an inner join on the definition table is OK, since it is a 1:1 relationship -->
inner join ${prefix}ACT_CMMN_CASEDEF CASE_DEF on RES.CASE_DEF_ID_ = CASE_DEF.ID_
<include refid="commonSelectCaseInstancesByQueryCriteriaSql"/>
${orderBy}
<if test="needsPaging">${limitAfter}</if>
</select>
Expand All @@ -304,7 +316,7 @@
select count(distinct RES.ID_) from ${prefix}ACT_CMMN_RU_CASE_INST RES
<!-- Doing an inner join on the definition table is OK, since it is a 1:1 relationship -->
inner join ${prefix}ACT_CMMN_CASEDEF CASE_DEF on RES.CASE_DEF_ID_ = CASE_DEF.ID_
<include refid="commonSelectCaseInstancesByQueryCriteriaSql" />
<include refid="commonSelectCaseInstancesByQueryCriteriaSql"/>
</select>

<select id="selectCaseInstanceWithVariablesByQueryCriteria"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,33 @@
</foreach>
</select>

<select id="selectHistoricCaseInstancesByQueryCriteria" parameterType="org.flowable.cmmn.engine.impl.history.HistoricCaseInstanceQueryImpl" resultMap="historicCaseInstanceResultMap">
<select id="selectHistoricCaseInstancesByQueryCriteria" parameterType="org.flowable.cmmn.engine.impl.history.HistoricCaseInstanceQueryImpl"
resultMap="historicCaseInstanceResultMap">
<if test="needsPaging">${limitBefore}</if>
select RES.* <if test="needsPaging">${limitBetween}</if>,
CASE_DEF.KEY_ as CaseDefinitionKey,
CASE_DEF.NAME_ as CaseDefinitionName,
CASE_DEF.VERSION_ as CaseDefinitionVersion,
CASE_DEF.DEPLOYMENT_ID_ as CaseDefinitionDeploymentId
<include refid="selectHistoricCaseInstancesByQueryCriteriaSql" />
CASE_DEF.KEY_ as CaseDefinitionKey,
CASE_DEF.NAME_ as CaseDefinitionName,
CASE_DEF.VERSION_ as CaseDefinitionVersion,
CASE_DEF.DEPLOYMENT_ID_ as CaseDefinitionDeploymentId
<include refid="selectHistoricCaseInstancesByQueryCriteriaSql"/>
${orderBy}
<if test="needsPaging">${limitAfter}</if>
</select>

<select id="selectHistoricCaseInstanceIdsByQueryCriteria" parameterType="org.flowable.cmmn.engine.impl.history.HistoricCaseInstanceQueryImpl"
resultType="string">
<if test="needsPaging">${limitBefore}</if>
select RES.ID_
<if test="needsPaging">${limitBetween}</if>
<include refid="selectHistoricCaseInstancesByQueryCriteriaSql"/>
${orderBy}
<if test="needsPaging">${limitAfter}</if>
</select>

<select id="selectHistoricCaseInstanceCountByQueryCriteria" parameterType="org.flowable.cmmn.engine.impl.history.HistoricCaseInstanceQueryImpl" resultType="long">
<select id="selectHistoricCaseInstanceCountByQueryCriteria" parameterType="org.flowable.cmmn.engine.impl.history.HistoricCaseInstanceQueryImpl"
resultType="long">
select count(distinct RES.ID_)
<include refid="selectHistoricCaseInstancesByQueryCriteriaSql" />
<include refid="selectHistoricCaseInstancesByQueryCriteriaSql"/>
</select>

<select id="selectHistoricCaseInstancesWithVariablesByQueryCriteria"
Expand Down

0 comments on commit 3c05838

Please sign in to comment.