Skip to content

Commit ffb1541

Browse files
committed
Split SQL in order to improve global performance of request that retreive all queue entries by tag. #2579
1 parent 6aebd00 commit ffb1541

File tree

6 files changed

+309
-299
lines changed

6 files changed

+309
-299
lines changed

source/src/main/java/org/cerberus/core/crud/dao/ITestCaseExecutionQueueDAO.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,17 @@ public interface ITestCaseExecutionQueueDAO {
4949

5050
/**
5151
* @param tag
52-
* @param start
53-
* @param amount
54-
* @param sort
55-
* @param searchTerm
56-
* @param individualSearch
5752
* @return
5853
* @throws CerberusException
5954
*/
60-
public AnswerList<TestCaseExecutionQueue> readByTagByCriteria(String tag, int start, int amount, String sort, String searchTerm, Map<String, List<String>> individualSearch) throws CerberusException;
55+
public AnswerList<Long> readMaxIdListByTag(String tag) throws CerberusException;
56+
57+
/**
58+
* @param queueIDList
59+
* @return
60+
* @throws CerberusException
61+
*/
62+
public AnswerList<TestCaseExecutionQueue> readByQueueIdList(List<Long> queueIDList) throws CerberusException;
6163

6264
/**
6365
* Read TestCaseExecutionInQueue By Tag
@@ -300,8 +302,8 @@ public interface ITestCaseExecutionQueueDAO {
300302
AnswerItem<Integer> updateToPausedPendingRecord(String tag, String user, String comment);
301303

302304
/**
303-
* This method removes queue State from -PAUSED any queue entry that belong to
304-
* tag and that has been paused (*-PAUSED)
305+
* This method removes queue State from -PAUSED any queue entry that belong
306+
* to tag and that has been paused (*-PAUSED)
305307
*
306308
* @param tag
307309
* @param user

source/src/main/java/org/cerberus/core/crud/dao/impl/TestCaseExecutionQueueDAO.java

+74-49
Original file line numberDiff line numberDiff line change
@@ -185,59 +185,26 @@ public AnswerItem<TestCaseExecutionQueue> readByKey(long queueid) {
185185
}
186186

187187
@Override
188-
public AnswerList<TestCaseExecutionQueue> readByTagByCriteria(String tag, int start, int amount, String sort, String searchTerm, Map<String, List<String>> individualSearch) throws CerberusException {
188+
public AnswerList<Long> readMaxIdListByTag(String tag) throws CerberusException {
189189
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
190-
AnswerList<TestCaseExecutionQueue> answer = new AnswerList<>();
191-
List<String> individalColumnSearchValues = new ArrayList<>();
192-
190+
AnswerList<Long> answer = new AnswerList<>();
193191
final StringBuilder query = new StringBuilder();
194192

195-
query.append("SELECT * FROM testcaseexecutionqueue exq ");
196-
query.append("left join testcase tec on exq.Test = tec.Test and exq.TestCase = tec.TestCase ");
197-
query.append("left join application app on tec.application = app.application ");
198-
query.append("where exq.ID IN ");
199-
query.append("(select MAX(exq.ID) from testcaseexecutionqueue exq ");
200-
193+
query.append("SELECT MAX(exq.ID) from testcaseexecutionqueue exq ");
201194
query.append("where 1=1 ");
202195
if (!StringUtil.isEmptyOrNull(tag)) {
203196
query.append("and exq.tag = ? ");
204197
}
205-
206-
query.append("group by exq.test, exq.testcase, exq.Environment, exq.Browser, exq.Country) ");
207-
if (!StringUtil.isEmptyOrNull(searchTerm)) {
208-
query.append("and (exq.`test` like ? ");
209-
query.append(" or exq.`testCase` like ? ");
210-
query.append(" or tec.`application` like ? ");
211-
query.append(" or tec.`bugs` like ? ");
212-
query.append(" or tec.`priority` like ? ");
213-
query.append(" or tec.`description` like ? )");
214-
}
215-
if (individualSearch != null && !individualSearch.isEmpty()) {
216-
query.append(" and ( 1=1 ");
217-
for (Map.Entry<String, List<String>> entry : individualSearch.entrySet()) {
218-
query.append(" and ");
219-
query.append(SqlUtil.getInSQLClauseForPreparedStatement(entry.getKey(), entry.getValue()));
220-
individalColumnSearchValues.addAll(entry.getValue());
221-
}
222-
query.append(" ) ");
223-
}
224-
225-
if (!StringUtil.isEmptyOrNull(sort)) {
226-
query.append(" order by ").append(sort);
227-
}
228-
229-
if ((amount <= 0) || (amount >= MAX_ROW_SELECTED)) {
230-
query.append(" limit ").append(start).append(" , ").append(MAX_ROW_SELECTED);
231-
} else {
232-
query.append(" limit ").append(start).append(" , ").append(amount);
233-
}
198+
query.append("group by exq.test, exq.testcase, exq.Environment, exq.Browser, exq.Country ");
199+
query.append(" limit ").append(MAX_ROW_SELECTED);
234200

235201
// Debug message on SQL.
236202
if (LOG.isDebugEnabled()) {
237203
LOG.debug("SQL : " + query.toString());
204+
LOG.debug("SQL.param.tag : " + tag);
238205
}
239206

240-
List<TestCaseExecutionQueue> testCaseExecutionInQueueList = new ArrayList<>();
207+
List<Long> testCaseExecutionInQueueList = new ArrayList<>();
241208
Connection connection = this.databaseSpring.connect();
242209
try {
243210
PreparedStatement preStat = connection.prepareStatement(query.toString());
@@ -246,17 +213,75 @@ public AnswerList<TestCaseExecutionQueue> readByTagByCriteria(String tag, int st
246213
if (!StringUtil.isEmptyOrNull(tag)) {
247214
preStat.setString(i++, tag);
248215
}
249-
if (!StringUtil.isEmptyOrNull(searchTerm)) {
250-
preStat.setString(i++, "%" + searchTerm + "%");
251-
preStat.setString(i++, "%" + searchTerm + "%");
252-
preStat.setString(i++, "%" + searchTerm + "%");
253-
preStat.setString(i++, "%" + searchTerm + "%");
254-
preStat.setString(i++, "%" + searchTerm + "%");
255-
preStat.setString(i++, "%" + searchTerm + "%");
216+
217+
try {
218+
ResultSet resultSet = preStat.executeQuery();
219+
try {
220+
while (resultSet.next()) {
221+
testCaseExecutionInQueueList.add(resultSet.getLong(1));
222+
}
223+
msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseExecutionInQueue").replace("%OPERATION%", "SELECT"));
224+
answer.setDataList(testCaseExecutionInQueueList);
225+
} catch (SQLException exception) {
226+
LOG.error("Unable to execute query : " + exception.toString());
227+
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
228+
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
229+
testCaseExecutionInQueueList = null;
230+
} finally {
231+
resultSet.close();
232+
}
233+
} catch (SQLException exception) {
234+
LOG.error("Unable to execute query : " + exception.toString());
235+
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
236+
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
237+
testCaseExecutionInQueueList = null;
238+
} finally {
239+
preStat.close();
256240
}
257-
for (String individualColumnSearchValue : individalColumnSearchValues) {
258-
preStat.setString(i++, individualColumnSearchValue);
241+
} catch (SQLException exception) {
242+
LOG.error("Unable to execute query : " + exception.toString());
243+
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
244+
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
245+
testCaseExecutionInQueueList = null;
246+
} finally {
247+
try {
248+
if (connection != null) {
249+
connection.close();
250+
}
251+
} catch (SQLException e) {
252+
LOG.warn(e.toString());
253+
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
254+
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to retrieve the list of entries!"));
259255
}
256+
}
257+
answer.setResultMessage(msg);
258+
return answer;
259+
}
260+
261+
@Override
262+
public AnswerList<TestCaseExecutionQueue> readByQueueIdList(List<Long> queueIDList) throws CerberusException {
263+
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
264+
AnswerList<TestCaseExecutionQueue> answer = new AnswerList<>();
265+
266+
final StringBuilder query = new StringBuilder();
267+
268+
query.append("SELECT * FROM testcaseexecutionqueue exq ");
269+
query.append("left join testcase tec on exq.Test = tec.Test and exq.TestCase = tec.TestCase ");
270+
query.append("left join application app on tec.application = app.application ");
271+
query.append("where ");
272+
query.append(SqlUtil.createWhereInClauseLong("exq.ID", queueIDList, "", ""));
273+
query.append(" limit 10000");
274+
275+
// Debug message on SQL.
276+
if (LOG.isDebugEnabled()) {
277+
LOG.debug("SQL : " + query.toString());
278+
}
279+
280+
List<TestCaseExecutionQueue> testCaseExecutionInQueueList = new ArrayList<>();
281+
Connection connection = this.databaseSpring.connect();
282+
283+
try {
284+
PreparedStatement preStat = connection.prepareStatement(query.toString());
260285

261286
try {
262287
ResultSet resultSet = preStat.executeQuery();

source/src/main/java/org/cerberus/core/crud/service/ITestCaseExecutionQueueService.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,10 @@ public interface ITestCaseExecutionQueueService {
5656

5757
/**
5858
* @param tag
59-
* @param start
60-
* @param amount
61-
* @param sort
62-
* @param searchTerm
63-
* @param individualSearch
6459
* @return
6560
* @throws CerberusException
6661
*/
67-
AnswerList<TestCaseExecutionQueue> readByTagByCriteria(String tag, int start, int amount, String sort, String searchTerm, Map<String, List<String>> individualSearch) throws CerberusException;
62+
AnswerList<TestCaseExecutionQueue> readMaxIdByTag(String tag) throws CerberusException;
6863

6964
/**
7065
* @param tag tag to filter.

source/src/main/java/org/cerberus/core/servlet/crud/testexecution/CreateTestCaseExecutionQueue.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
202202
Map<String, TestCaseExecutionQueue> queueAlreadyInsertedInTag = new HashMap<>();
203203
if (StringUtil.isNotEmptyOrNull(tag)) {
204204
LOG.debug("We don't have the list of all already inserted entries. Let's get it from tag value : " + tag);
205-
List<TestCaseExecutionQueue> queueFromTag = executionQueueService.convert(executionQueueService.readByTagByCriteria(tag, 0, 0, null, null, null));
205+
List<TestCaseExecutionQueue> queueFromTag = executionQueueService.convert(executionQueueService.readMaxIdByTag(tag));
206206
for (TestCaseExecutionQueue tceQueue : queueFromTag) {
207207
queueAlreadyInsertedInTag.put(executionQueueService.getUniqKey(tceQueue.getTest(), tceQueue.getTestCase(), tceQueue.getCountry(), tceQueue.getEnvironment()), tceQueue);
208208
}

0 commit comments

Comments
 (0)