Skip to content

Commit

Permalink
Don't use sorting for case and process history cleanup and job and ti…
Browse files Browse the repository at this point in the history
…mer due queries
  • Loading branch information
tijsrademakers committed Nov 8, 2024
1 parent 7d9cafa commit 4cf7b44
Show file tree
Hide file tree
Showing 25 changed files with 247 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,16 @@ public interface HistoricCaseInstanceQuery extends Query<HistoricCaseInstanceQue
* Instruct localization to fallback to more general locales including the default locale of the JVM if the specified locale is not found.
*/
HistoricCaseInstanceQuery withLocalizationFallback();

/**
* Perform the query without applying sorting parameters. By default sorting will be applied.
*/
HistoricCaseInstanceQuery withoutSorting();

/**
* Return only the id value of the case instances, to reduce any additional instance data to be returned.
*/
HistoricCaseInstanceQuery returnIdsOnly();

HistoricCaseInstanceQuery orderByCaseInstanceId();
HistoricCaseInstanceQuery orderByCaseInstanceName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void execute(JobEntity job, String configuration, VariableScope variableS

HistoricCaseInstanceQuery query = config.getQuery();
// In the synchronous deletion, we are always deleting the first elements
List<HistoricCaseInstance> historicCaseInstances = query.listPage(0, batchSize);
List<HistoricCaseInstance> historicCaseInstances = query.withoutSorting().returnIdsOnly().listPage(0, batchSize);
ObjectNode resultNode = engineConfiguration.getObjectMapper().createObjectNode();
Set<String> caseInstanceIdsToDelete = new HashSet<>();
for (HistoricCaseInstance historicCaseInstance : historicCaseInstances) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public class HistoricCaseInstanceQueryImpl extends AbstractVariableQueryImpl<His
protected boolean inOrStatement;
protected String locale;
protected boolean withLocalizationFallback;
protected boolean withoutSorting;
protected boolean returnIdsOnly;

public HistoricCaseInstanceQueryImpl() {
}
Expand Down Expand Up @@ -824,8 +826,16 @@ public long executeCount(CommandContext commandContext) {
@Override
public List<HistoricCaseInstance> executeList(CommandContext commandContext) {
ensureVariablesInitialized();

if (withoutSorting) {
setIgnoreOrderBy();
}

List<HistoricCaseInstance> results;
if (includeCaseVariables) {
if (returnIdsOnly) {
results = cmmnEngineConfiguration.getHistoricCaseInstanceEntityManager().findIdsByCriteria(this);

} else if (includeCaseVariables) {
results = cmmnEngineConfiguration.getHistoricCaseInstanceEntityManager().findWithVariablesByQueryCriteria(this);

if (caseInstanceId != null) {
Expand Down Expand Up @@ -1175,6 +1185,18 @@ public HistoricCaseInstanceQuery withLocalizationFallback() {
this.withLocalizationFallback = true;
return this;
}

@Override
public HistoricCaseInstanceQuery withoutSorting() {
this.withoutSorting = true;
return this;
}

@Override
public HistoricCaseInstanceQuery returnIdsOnly() {
this.returnIdsOnly = true;
return this;
}

public String getCaseDefinitionId() {
return caseDefinitionId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public interface HistoricCaseInstanceEntityManager extends EntityManager<Histori
List<HistoricCaseInstance> findByCriteria(HistoricCaseInstanceQuery query);

List<HistoricCaseInstance> findWithVariablesByQueryCriteria(HistoricCaseInstanceQuery query);

List<HistoricCaseInstance> findIdsByCriteria(HistoricCaseInstanceQuery query);

long countByCriteria(HistoricCaseInstanceQuery query);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public List<HistoricCaseInstance> findByCriteria(HistoricCaseInstanceQuery query
public List<HistoricCaseInstance> findWithVariablesByQueryCriteria(HistoricCaseInstanceQuery query) {
return dataManager.findWithVariablesByQueryCriteria((HistoricCaseInstanceQueryImpl) query);
}

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

@Override
public long countByCriteria(HistoricCaseInstanceQuery query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public interface HistoricCaseInstanceDataManager extends DataManager<HistoricCas
long countByCriteria(HistoricCaseInstanceQueryImpl query);

List<HistoricCaseInstance> findWithVariablesByQueryCriteria(HistoricCaseInstanceQueryImpl historicCaseInstanceQuery);

List<HistoricCaseInstance> findIdsByCriteria(HistoricCaseInstanceQueryImpl query);

void deleteByCaseDefinitionId(String caseDefinitionId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public List<HistoricCaseInstance> findWithVariablesByQueryCriteria(HistoricCaseI
return getDbSqlSession().selectList("selectHistoricCaseInstancesWithVariablesByQueryCriteria", historicCaseInstanceQuery, getManagedEntityClass());
}

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

@Override
public void deleteByCaseDefinitionId(String caseDefinitionId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@
<result property="caseDefinitionVersion" column="CaseDefinitionVersion" jdbcType="INTEGER" />
<result property="caseDefinitionDeploymentId" column="CaseDefinitionDeploymentId" jdbcType="VARCHAR" />
</resultMap>

<resultMap id="historicCaseInstanceIdResultMap" type="org.flowable.cmmn.engine.impl.persistence.entity.HistoricCaseInstanceEntityImpl">
<id property="id" column="ID_" jdbcType="VARCHAR" />
</resultMap>

<resultMap id="historicCaseInstanceResultMapWithVariables"
type="org.flowable.cmmn.engine.impl.persistence.entity.HistoricCaseInstanceEntityImpl"
Expand Down Expand Up @@ -247,6 +251,13 @@
select count(distinct RES.ID_)
<include refid="selectHistoricCaseInstancesByQueryCriteriaSql" />
</select>

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

<select id="selectHistoricCaseInstancesWithVariablesByQueryCriteria"
parameterType="org.flowable.cmmn.engine.impl.history.HistoricCaseInstanceQueryImpl"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2025,4 +2025,42 @@ public void testQueryByParentScopeId() {
);
}

@Test
public void getCaseInstanceIdByCaseDefinitionKey() {
CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
.caseDefinitionKey("oneTaskCase")
.start();

if (CmmnHistoryTestHelper.isHistoryLevelAtLeast(HistoryLevel.ACTIVITY, cmmnEngineConfiguration)) {
HistoricCaseInstance historicCaseInstance = cmmnHistoryService.createHistoricCaseInstanceQuery().caseDefinitionKey("oneTaskCase").withoutSorting().returnIdsOnly().singleResult();
assertThat(historicCaseInstance.getId()).isEqualTo(caseInstance.getId());
assertThat(historicCaseInstance.getCaseDefinitionId()).isNull();

historicCaseInstance = cmmnHistoryService.createHistoricCaseInstanceQuery().caseDefinitionKey("oneTaskCase").withoutSorting().singleResult();
assertThat(historicCaseInstance.getId()).isEqualTo(caseInstance.getId());
assertThat(historicCaseInstance.getCaseDefinitionId()).isNotNull();

historicCaseInstance = cmmnHistoryService.createHistoricCaseInstanceQuery()
.or()
.caseDefinitionKey("oneTaskCase")
.caseInstanceId("undefined")
.endOr()
.withoutSorting()
.returnIdsOnly()
.singleResult();
assertThat(historicCaseInstance.getId()).isEqualTo(caseInstance.getId());
assertThat(historicCaseInstance.getCaseDefinitionId()).isNull();

historicCaseInstance = cmmnHistoryService.createHistoricCaseInstanceQuery()
.or()
.caseDefinitionKey("oneTaskCase")
.caseInstanceId("undefined")
.endOr()
.withoutSorting()
.singleResult();

assertThat(historicCaseInstance.getId()).isEqualTo(caseInstance.getId());
assertThat(historicCaseInstance.getCaseDefinitionId()).isNotNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,8 @@ public void testDeleteHistoricInstancesWithAllQueryOptions() throws InvocationTa
methodsToIgnore.add("includeCaseVariables");
methodsToIgnore.add("locale");
methodsToIgnore.add("withLocalizationFallback");
methodsToIgnore.add("returnIdsOnly");
methodsToIgnore.add("withoutSorting");
methodsToIgnore.add("asc");
methodsToIgnore.add("desc");
methodsToIgnore.add("or");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public NullHandlingOnOrder getNullHandlingOnOrder() {
protected Collection<OrderBy> orderByCollection;
protected OrderBy defaultOrderBy = DEFAULT_ORDER_BY;
protected QueryProperty orderProperty;
// Only needed for DB2 and MSSQL queries for the over statement
protected boolean ignoreOrderBy;
protected String nullHandlingColumn;
protected NullHandlingOnOrder nullHandlingOnOrder;
protected ResultType resultType;
Expand Down Expand Up @@ -138,6 +140,10 @@ public boolean hasOrderBy() {

return defaultOrderBy != null;
}

public void setIgnoreOrderBy() {
this.ignoreOrderBy = true;
}

// This is used for the SQL Server and DB2 order by in a window function / over
@SuppressWarnings("unused")
Expand All @@ -146,6 +152,10 @@ public String getOrderByForWindow() {
}

protected String buildOrderBy() {
if (ignoreOrderBy) {
return "";
}

Collection<OrderBy> orderBy = getOrderByCollectionSafe();

if (orderBy.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,4 +582,14 @@ public interface HistoricProcessInstanceQuery extends Query<HistoricProcessInsta
* Instruct localization to fallback to more general locales including the default locale of the JVM if the specified locale is not found.
*/
HistoricProcessInstanceQuery withLocalizationFallback();

/**
* Perform the query without applying sorting parameters. By default sorting will be applied.
*/
HistoricProcessInstanceQuery withoutSorting();

/**
* Return only the id value of the process instances, to reduce any additional instance data to be returned.
*/
HistoricProcessInstanceQuery returnIdsOnly();
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public class HistoricProcessInstanceQueryImpl extends AbstractVariableQueryImpl<
protected String referenceType;
protected String locale;
protected boolean withLocalizationFallback;
protected boolean withoutSorting;
protected boolean returnIdsOnly;
protected List<HistoricProcessInstanceQueryImpl> orQueryObjects = new ArrayList<>();
protected HistoricProcessInstanceQueryImpl currentOrQueryObject;
protected boolean inOrStatement;
Expand Down Expand Up @@ -990,7 +992,19 @@ public HistoricProcessInstanceQuery locale(String locale) {

@Override
public HistoricProcessInstanceQuery withLocalizationFallback() {
withLocalizationFallback = true;
this.withLocalizationFallback = true;
return this;
}

@Override
public HistoricProcessInstanceQuery withoutSorting() {
this.withoutSorting = true;
return this;
}

@Override
public HistoricProcessInstanceQuery returnIdsOnly() {
this.returnIdsOnly = true;
return this;
}

Expand Down Expand Up @@ -1070,13 +1084,21 @@ public long executeCount(CommandContext commandContext) {
@Override
public List<HistoricProcessInstance> executeList(CommandContext commandContext) {
ensureVariablesInitialized();

if (withoutSorting) {
setIgnoreOrderBy();
}

List<HistoricProcessInstance> results = null;

if (processEngineConfiguration.getHistoricProcessInstanceQueryInterceptor() != null) {
processEngineConfiguration.getHistoricProcessInstanceQueryInterceptor().beforeHistoricProcessInstanceQueryExecute(this);
}

if (includeProcessVariables) {
if (returnIdsOnly) {
results = processEngineConfiguration.getHistoricProcessInstanceEntityManager().findHistoricProcessInstanceIdsByQueryCriteria(this);

} else if (includeProcessVariables) {
results = processEngineConfiguration.getHistoricProcessInstanceEntityManager().findHistoricProcessInstancesAndVariablesByQueryCriteria(this);

if (processInstanceId != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void execute(JobEntity job, String configuration, VariableScope variableS

HistoricProcessInstanceQuery query = config.getQuery();
// In the synchronous deletion, we are always deleting the first elements
List<HistoricProcessInstance> historicProcessInstances = query.listPage(0, batchSize);
List<HistoricProcessInstance> historicProcessInstances = query.withoutSorting().returnIdsOnly().listPage(0, batchSize);
ObjectNode resultNode = engineConfiguration.getObjectMapper().createObjectNode();
Set<String> processInstanceIdsToDelete = new HashSet<>();
for (HistoricProcessInstance historicProcessInstance : historicProcessInstances) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public interface HistoricProcessInstanceEntityManager extends EntityManager<Hist

List<HistoricProcessInstance> findHistoricProcessInstancesAndVariablesByQueryCriteria(HistoricProcessInstanceQueryImpl historicProcessInstanceQuery);

List<HistoricProcessInstance> findHistoricProcessInstanceIdsByQueryCriteria(HistoricProcessInstanceQueryImpl historicProcessInstanceQuery);

List<HistoricProcessInstance> findHistoricProcessInstancesByNativeQuery(Map<String, Object> parameterMap);

List<HistoricProcessInstance> findHistoricProcessInstancesBySuperProcessInstanceId(String historicProcessInstanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@ public long findHistoricProcessInstanceCountByQueryCriteria(HistoricProcessInsta
}

@Override
@SuppressWarnings("unchecked")
public List<HistoricProcessInstance> findHistoricProcessInstancesByQueryCriteria(HistoricProcessInstanceQueryImpl historicProcessInstanceQuery) {
return dataManager.findHistoricProcessInstancesByQueryCriteria(historicProcessInstanceQuery);
}

@Override
@SuppressWarnings("unchecked")
public List<HistoricProcessInstance> findHistoricProcessInstancesAndVariablesByQueryCriteria(HistoricProcessInstanceQueryImpl historicProcessInstanceQuery) {
return dataManager.findHistoricProcessInstancesAndVariablesByQueryCriteria(historicProcessInstanceQuery);
}

@Override
public List<HistoricProcessInstance> findHistoricProcessInstanceIdsByQueryCriteria(HistoricProcessInstanceQueryImpl historicProcessInstanceQuery) {
return dataManager.findHistoricProcessInstanceIdsByQueryCriteria(historicProcessInstanceQuery);
}

@Override
public List<HistoricProcessInstance> findHistoricProcessInstancesByNativeQuery(Map<String, Object> parameterMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public interface HistoricProcessInstanceDataManager extends DataManager<Historic
List<HistoricProcessInstance> findHistoricProcessInstancesByQueryCriteria(HistoricProcessInstanceQueryImpl historicProcessInstanceQuery);

List<HistoricProcessInstance> findHistoricProcessInstancesAndVariablesByQueryCriteria(HistoricProcessInstanceQueryImpl historicProcessInstanceQuery);

List<HistoricProcessInstance> findHistoricProcessInstanceIdsByQueryCriteria(HistoricProcessInstanceQueryImpl historicProcessInstanceQuery);

List<HistoricProcessInstance> findHistoricProcessInstancesByNativeQuery(Map<String, Object> parameterMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ public List<HistoricProcessInstance> findHistoricProcessInstancesAndVariablesByQ
setSafeInValueLists(historicProcessInstanceQuery);
return getDbSqlSession().selectList("selectHistoricProcessInstancesWithVariablesByQueryCriteria", historicProcessInstanceQuery, getManagedEntityClass());
}

@Override
@SuppressWarnings("unchecked")
public List<HistoricProcessInstance> findHistoricProcessInstanceIdsByQueryCriteria(HistoricProcessInstanceQueryImpl historicProcessInstanceQuery) {
setSafeInValueLists(historicProcessInstanceQuery);
return getDbSqlSession().selectList("selectHistoricProcessInstanceIdsByQueryCriteria", historicProcessInstanceQuery, getManagedEntityClass());
}

@Override
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@
<result property="propagatedStageInstanceId" column="PROPAGATED_STAGE_INST_ID_" jdbcType="NVARCHAR" />
</resultMap>

<resultMap id="historicProcessInstanceIdResultMap" type="org.flowable.engine.impl.persistence.entity.HistoricProcessInstanceEntityImpl">
<id property="id" column="ID_" jdbcType="NVARCHAR" />
</resultMap>

<resultMap id="historicProcessInstanceAndVariablesResultMap" type="org.flowable.engine.impl.persistence.entity.HistoricProcessInstanceEntityImpl">
<id property="id" column="ID_" jdbcType="NVARCHAR" />
<result property="revision" column="REV_" jdbcType="INTEGER"/>
Expand Down Expand Up @@ -382,14 +386,21 @@
select count(distinct RES.ID_)
<include refid="selectHistoricProcessInstancesByQueryCriteriaSql"/>
</select>

<select id="selectHistoricProcessInstanceIdsByQueryCriteria" parameterType="org.flowable.engine.impl.HistoricProcessInstanceQueryImpl" resultMap="historicProcessInstanceIdResultMap">
<if test="needsPaging">${limitBefore}</if>
SELECT RES.ID_ <if test="needsPaging">${limitBetween}</if>
<include refid="selectHistoricProcessInstancesByQueryCriteriaSql"/>
<if test="needsPaging">${limitAfter}</if>
</select>

<sql id="selectHistoricProcessInstancesByQueryCriteriaSql">
from ${prefix}ACT_HI_PROCINST RES
<!--
Doing a join on the definition table is OK, since it is a 1:1 relationship.
This has to be an outer join because historic data might exists, but definitions might have been deleted
-->
left outer join ${prefix}ACT_RE_PROCDEF DEF on RES.PROC_DEF_ID_ = DEF.ID_
left outer join ${prefix}ACT_RE_PROCDEF DEF on RES.PROC_DEF_ID_ = DEF.ID_
<include refid="commonSelectHistoricProcessInstancesByQueryCriteriaSql"/>
</sql>

Expand Down
Loading

0 comments on commit 4cf7b44

Please sign in to comment.