Skip to content

Commit b50c665

Browse files
bcubkfmbenhassine
authored andcommitted
Add method StepExecutionDao.countStepExecutions to eliminate for-loop in SimpleJobRepository.getStepExecutionCount
Resolves #3657
1 parent 56fbc27 commit b50c665

File tree

7 files changed

+97
-19
lines changed

7 files changed

+97
-19
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2019 the original author or authors.
2+
* Copyright 2006-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,6 +61,7 @@
6161
* @author Robert Kasanicky
6262
* @author David Turanski
6363
* @author Mahmoud Ben Hassine
64+
* @author Baris Cubukcuoglu
6465
*
6566
* @see StepExecutionDao
6667
*/
@@ -105,6 +106,14 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
105106
private static final String CURRENT_VERSION_STEP_EXECUTION = "SELECT VERSION FROM %PREFIX%STEP_EXECUTION WHERE " +
106107
"STEP_EXECUTION_ID=?";
107108

109+
private static final String COUNT_STEP_EXECUTIONS = "SELECT COUNT(*) " +
110+
" from %PREFIX%JOB_EXECUTION JE, %PREFIX%STEP_EXECUTION SE" +
111+
" where " +
112+
" SE.JOB_EXECUTION_ID in (SELECT JOB_EXECUTION_ID from %PREFIX%JOB_EXECUTION " +
113+
"where JE.JOB_INSTANCE_ID = ?)" +
114+
" and SE.JOB_EXECUTION_ID = JE.JOB_EXECUTION_ID " +
115+
" and SE.STEP_NAME = ?";
116+
108117
private int exitMessageLength = DEFAULT_EXIT_MESSAGE_LENGTH;
109118

110119
private DataFieldMaxValueIncrementer stepExecutionIncrementer;
@@ -347,6 +356,11 @@ public void addStepExecutions(JobExecution jobExecution) {
347356
jobExecution.getId());
348357
}
349358

359+
@Override
360+
public int countStepExecutions(JobInstance jobInstance, String stepName) {
361+
return getJdbcTemplate().queryForObject(getQuery(COUNT_STEP_EXECUTIONS), new Object[] { jobInstance.getInstanceId(), stepName }, Integer.class);
362+
}
363+
350364
private static class StepExecutionRowMapper implements RowMapper<StepExecution> {
351365

352366
private final JobExecution jobExecution;

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/MapStepExecutionDao.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2019 the original author or authors.
2+
* Copyright 2006-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -172,4 +172,17 @@ public void saveStepExecutions(Collection<StepExecution> stepExecutions) {
172172
saveStepExecution(stepExecution);
173173
}
174174
}
175+
176+
@Override
177+
public int countStepExecutions(JobInstance jobInstance, String stepName) {
178+
int count = 0;
179+
180+
for (StepExecution stepExecution : executionsByStepExecutionId.values()) {
181+
if (stepExecution.getStepName().equals(stepName) && stepExecution.getJobExecution().getJobInstance()
182+
.getInstanceId() == jobInstance.getInstanceId()) {
183+
count++;
184+
}
185+
}
186+
return count;
187+
}
175188
}

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/StepExecutionDao.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2019 the original author or authors.
2+
* Copyright 2006-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -86,4 +86,15 @@ default StepExecution getLastStepExecution(JobInstance jobInstance, String stepN
8686
*/
8787
void addStepExecutions(JobExecution jobExecution);
8888

89+
/**
90+
* Counts all the {@link StepExecution} for a given step name.
91+
*
92+
* @param jobInstance the parent {@link JobInstance}
93+
* @param stepName the name of the step
94+
* @since 4.3
95+
* @return the count of {@link StepExecution}s for a given step
96+
*/
97+
default int countStepExecutions(JobInstance jobInstance, String stepName) {
98+
throw new UnsupportedOperationException();
99+
}
89100
}

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2019 the original author or authors.
2+
* Copyright 2006-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,6 +51,7 @@
5151
* @author Robert Kasanicky
5252
* @author David Turanski
5353
* @author Mahmoud Ben Hassine
54+
* @author Baris Cubukcuoglu
5455
*
5556
* @see JobRepository
5657
* @see JobInstanceDao
@@ -235,17 +236,7 @@ public StepExecution getLastStepExecution(JobInstance jobInstance, String stepNa
235236
*/
236237
@Override
237238
public int getStepExecutionCount(JobInstance jobInstance, String stepName) {
238-
int count = 0;
239-
List<JobExecution> jobExecutions = jobExecutionDao.findJobExecutions(jobInstance);
240-
for (JobExecution jobExecution : jobExecutions) {
241-
stepExecutionDao.addStepExecutions(jobExecution);
242-
for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
243-
if (stepName.equals(stepExecution.getStepName())) {
244-
count++;
245-
}
246-
}
247-
}
248-
return count;
239+
return stepExecutionDao.countStepExecutions(jobInstance, stepName);
249240
}
250241

251242
/**

spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDaoTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2013 the original author or authors.
2+
* Copyright 2008-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.batch.core.repository.dao;
1717

18+
import static org.junit.Assert.assertEquals;
1819
import static org.junit.Assert.assertTrue;
1920

2021
import org.junit.Test;
@@ -75,4 +76,16 @@ public void testTruncateExitDescription() {
7576
.length() < stepExecution.getExitStatus().getExitDescription().length());
7677
}
7778

79+
@Transactional
80+
@Test
81+
public void testCountStepExecutions() {
82+
// Given
83+
dao.saveStepExecution(stepExecution);
84+
85+
// When
86+
int result = dao.countStepExecutions(jobInstance, stepExecution.getStepName());
87+
88+
// Then
89+
assertEquals(1, result);
90+
}
7891
}

spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/MapStepExecutionDaoTests.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2012 the original author or authors.
2+
* Copyright 2008-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -117,4 +117,26 @@ public void testAddStepExecutions() {
117117
assertEquals(BatchStatus.COMPLETED, jobStepExecution.getStatus());
118118
}
119119

120+
@Test
121+
public void testCountStepExecutions() {
122+
// Given
123+
StepExecutionDao tested = new MapStepExecutionDao();
124+
JobExecution jobExecution = new JobExecution(jobInstance, 88L, null, null);
125+
126+
StepExecution firstStepExecution = new StepExecution("Step one", jobExecution);
127+
firstStepExecution.setStatus(BatchStatus.STARTED);
128+
129+
tested.saveStepExecution(firstStepExecution);
130+
131+
StepExecution secondStepExecution = new StepExecution("Step two", jobExecution);
132+
secondStepExecution.setStatus(BatchStatus.STARTED);
133+
134+
tested.saveStepExecution(secondStepExecution);
135+
136+
// When
137+
int result = tested.countStepExecutions(jobInstance, firstStepExecution.getStepName());
138+
139+
// Then
140+
assertEquals(1, result);
141+
}
120142
}

spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2018 the original author or authors.
2+
* Copyright 2006-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,11 @@
1616

1717
package org.springframework.batch.core.repository.support;
1818

19-
import static org.junit.Assert.assertFalse;
2019
import static org.junit.Assert.assertNotNull;
2120
import static org.junit.Assert.assertTrue;
21+
import static org.junit.Assert.assertFalse;
2222
import static org.junit.Assert.fail;
23+
import static org.junit.Assert.assertEquals;
2324
import static org.mockito.Mockito.mock;
2425
import static org.mockito.Mockito.verify;
2526
import static org.mockito.Mockito.when;
@@ -56,6 +57,7 @@
5657
* @author Lucas Ward
5758
* @author Will Schipp
5859
* @author Dimitrios Liapis
60+
* @author Baris Cubukcuoglu
5961
*
6062
*/
6163
public class SimpleJobRepositoryTests {
@@ -272,4 +274,16 @@ public void testCreateJobExecutionAlreadyComplete() throws Exception {
272274
jobRepository.createJobExecution("foo", new JobParameters());
273275
}
274276

277+
@Test
278+
public void testGetStepExecutionCount() {
279+
// Given
280+
int expectedResult = 1;
281+
when(stepExecutionDao.countStepExecutions(jobInstance, "stepName")).thenReturn(expectedResult);
282+
283+
// When
284+
int actualResult = jobRepository.getStepExecutionCount(jobInstance, "stepName");
285+
286+
// Then
287+
assertEquals(expectedResult, actualResult);
288+
}
275289
}

0 commit comments

Comments
 (0)