|
16 | 16 | // under the License. |
17 | 17 | package com.cloud.event.dao; |
18 | 18 |
|
| 19 | +import java.sql.PreparedStatement; |
| 20 | +import java.sql.SQLException; |
19 | 21 | import java.util.Date; |
20 | 22 | import java.util.List; |
| 23 | +import java.util.stream.Collectors; |
21 | 24 |
|
22 | 25 |
|
| 26 | +import com.cloud.utils.db.DB; |
| 27 | +import com.cloud.utils.db.Filter; |
| 28 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 29 | +import org.apache.commons.collections4.CollectionUtils; |
23 | 30 | import org.springframework.stereotype.Component; |
24 | 31 |
|
25 | | -import com.cloud.event.Event.State; |
26 | 32 | import com.cloud.event.EventVO; |
27 | | -import com.cloud.utils.db.Filter; |
28 | 33 | import com.cloud.utils.db.GenericDaoBase; |
29 | 34 | import com.cloud.utils.db.SearchBuilder; |
30 | 35 | import com.cloud.utils.db.SearchCriteria; |
|
33 | 38 |
|
34 | 39 | @Component |
35 | 40 | public class EventDaoImpl extends GenericDaoBase<EventVO, Long> implements EventDao { |
36 | | - protected final SearchBuilder<EventVO> CompletedEventSearch; |
| 41 | + |
37 | 42 | protected final SearchBuilder<EventVO> ToArchiveOrDeleteEventSearch; |
38 | 43 |
|
39 | 44 | public EventDaoImpl() { |
40 | | - CompletedEventSearch = createSearchBuilder(); |
41 | | - CompletedEventSearch.and("state", CompletedEventSearch.entity().getState(), SearchCriteria.Op.EQ); |
42 | | - CompletedEventSearch.and("startId", CompletedEventSearch.entity().getStartId(), SearchCriteria.Op.EQ); |
43 | | - CompletedEventSearch.and("archived", CompletedEventSearch.entity().getArchived(), Op.EQ); |
44 | | - CompletedEventSearch.done(); |
45 | | - |
46 | 45 | ToArchiveOrDeleteEventSearch = createSearchBuilder(); |
| 46 | + ToArchiveOrDeleteEventSearch.select("id", SearchCriteria.Func.NATIVE, ToArchiveOrDeleteEventSearch.entity().getId()); |
47 | 47 | ToArchiveOrDeleteEventSearch.and("id", ToArchiveOrDeleteEventSearch.entity().getId(), Op.IN); |
48 | 48 | ToArchiveOrDeleteEventSearch.and("type", ToArchiveOrDeleteEventSearch.entity().getType(), Op.EQ); |
49 | | - ToArchiveOrDeleteEventSearch.and("accountIds", ToArchiveOrDeleteEventSearch.entity().getAccountId(), Op.IN); |
| 49 | + ToArchiveOrDeleteEventSearch.and("accountId", ToArchiveOrDeleteEventSearch.entity().getAccountId(), Op.EQ); |
| 50 | + ToArchiveOrDeleteEventSearch.and("domainIds", ToArchiveOrDeleteEventSearch.entity().getDomainId(), Op.IN); |
50 | 51 | ToArchiveOrDeleteEventSearch.and("createdDateB", ToArchiveOrDeleteEventSearch.entity().getCreateDate(), Op.BETWEEN); |
51 | 52 | ToArchiveOrDeleteEventSearch.and("createdDateL", ToArchiveOrDeleteEventSearch.entity().getCreateDate(), Op.LTEQ); |
| 53 | + ToArchiveOrDeleteEventSearch.and("createdDateLT", ToArchiveOrDeleteEventSearch.entity().getCreateDate(), Op.LT); |
52 | 54 | ToArchiveOrDeleteEventSearch.and("archived", ToArchiveOrDeleteEventSearch.entity().getArchived(), Op.EQ); |
53 | 55 | ToArchiveOrDeleteEventSearch.done(); |
54 | 56 | } |
55 | 57 |
|
56 | | - @Override |
57 | | - public List<EventVO> searchAllEvents(SearchCriteria<EventVO> sc, Filter filter) { |
58 | | - return listIncludingRemovedBy(sc, filter); |
59 | | - } |
60 | | - |
61 | | - @Override |
62 | | - public List<EventVO> listOlderEvents(Date oldTime) { |
63 | | - if (oldTime == null) |
64 | | - return null; |
65 | | - SearchCriteria<EventVO> sc = createSearchCriteria(); |
66 | | - sc.addAnd("createDate", SearchCriteria.Op.LT, oldTime); |
67 | | - sc.addAnd("archived", SearchCriteria.Op.EQ, false); |
68 | | - return listIncludingRemovedBy(sc, null); |
69 | | - } |
70 | | - |
71 | | - @Override |
72 | | - public EventVO findCompletedEvent(long startId) { |
73 | | - SearchCriteria<EventVO> sc = CompletedEventSearch.create(); |
74 | | - sc.setParameters("state", State.Completed); |
75 | | - sc.setParameters("startId", startId); |
76 | | - sc.setParameters("archived", false); |
77 | | - return findOneIncludingRemovedBy(sc); |
78 | | - } |
79 | | - |
80 | | - @Override |
81 | | - public List<EventVO> listToArchiveOrDeleteEvents(List<Long> ids, String type, Date startDate, Date endDate, List<Long> accountIds) { |
| 58 | + private SearchCriteria<EventVO> createEventSearchCriteria(List<Long> ids, String type, Date startDate, Date endDate, |
| 59 | + Date limitDate, Long accountId, List<Long> domainIds) { |
82 | 60 | SearchCriteria<EventVO> sc = ToArchiveOrDeleteEventSearch.create(); |
83 | | - if (ids != null) { |
84 | | - sc.setParameters("id", ids.toArray(new Object[ids.size()])); |
| 61 | + |
| 62 | + if (CollectionUtils.isNotEmpty(ids)) { |
| 63 | + sc.setParameters("id", ids.toArray(new Object[0])); |
85 | 64 | } |
86 | | - if (type != null) { |
87 | | - sc.setParameters("type", type); |
| 65 | + if (CollectionUtils.isNotEmpty(domainIds)) { |
| 66 | + sc.setParameters("domainIds", domainIds.toArray(new Object[0])); |
88 | 67 | } |
89 | 68 | if (startDate != null && endDate != null) { |
90 | 69 | sc.setParameters("createdDateB", startDate, endDate); |
91 | 70 | } else if (endDate != null) { |
92 | 71 | sc.setParameters("createdDateL", endDate); |
93 | 72 | } |
94 | | - if (accountIds != null && !accountIds.isEmpty()) { |
95 | | - sc.setParameters("accountIds", accountIds.toArray(new Object[accountIds.size()])); |
96 | | - } |
| 73 | + sc.setParametersIfNotNull("accountId", accountId); |
| 74 | + sc.setParametersIfNotNull("createdDateLT", limitDate); |
| 75 | + sc.setParametersIfNotNull("type", type); |
97 | 76 | sc.setParameters("archived", false); |
98 | | - return search(sc, null); |
| 77 | + |
| 78 | + return sc; |
99 | 79 | } |
100 | 80 |
|
101 | 81 | @Override |
102 | | - public void archiveEvents(List<EventVO> events) { |
103 | | - if (events != null && !events.isEmpty()) { |
104 | | - TransactionLegacy txn = TransactionLegacy.currentTxn(); |
105 | | - txn.start(); |
106 | | - for (EventVO event : events) { |
107 | | - event = lockRow(event.getId(), true); |
108 | | - event.setArchived(true); |
109 | | - update(event.getId(), event); |
110 | | - txn.commit(); |
| 82 | + public long archiveEvents(List<Long> ids, String type, Date startDate, Date endDate, Long accountId, List<Long> domainIds, |
| 83 | + long limitPerQuery) { |
| 84 | + SearchCriteria<EventVO> sc = createEventSearchCriteria(ids, type, startDate, endDate, null, accountId, domainIds); |
| 85 | + Filter filter = null; |
| 86 | + if (limitPerQuery > 0) { |
| 87 | + filter = new Filter(limitPerQuery); |
| 88 | + } |
| 89 | + |
| 90 | + long archived; |
| 91 | + long totalArchived = 0L; |
| 92 | + |
| 93 | + do { |
| 94 | + List<EventVO> events = search(sc, filter); |
| 95 | + if (events.isEmpty()) { |
| 96 | + break; |
111 | 97 | } |
112 | | - txn.close(); |
| 98 | + |
| 99 | + archived = archiveEventsInternal(events); |
| 100 | + totalArchived += archived; |
| 101 | + } while (limitPerQuery > 0 && archived >= limitPerQuery); |
| 102 | + |
| 103 | + return totalArchived; |
| 104 | + } |
| 105 | + |
| 106 | + @DB |
| 107 | + private long archiveEventsInternal(List<EventVO> events) { |
| 108 | + final String idsAsString = events.stream() |
| 109 | + .map(e -> Long.toString(e.getId())) |
| 110 | + .collect(Collectors.joining(",")); |
| 111 | + final String query = String.format("UPDATE event SET archived=true WHERE id IN (%s)", idsAsString); |
| 112 | + |
| 113 | + try (TransactionLegacy txn = TransactionLegacy.currentTxn(); |
| 114 | + PreparedStatement pstmt = txn.prepareStatement(query)) { |
| 115 | + return pstmt.executeUpdate(); |
| 116 | + } catch (SQLException e) { |
| 117 | + throw new CloudRuntimeException(e); |
113 | 118 | } |
114 | 119 | } |
| 120 | + |
| 121 | + @Override |
| 122 | + public long purgeAll(List<Long> ids, Date startDate, Date endDate, Date limitDate, String type, Long accountId, |
| 123 | + List<Long> domainIds, long limitPerQuery) { |
| 124 | + SearchCriteria<EventVO> sc = createEventSearchCriteria(ids, type, startDate, endDate, limitDate, accountId, domainIds); |
| 125 | + return batchExpunge(sc, limitPerQuery); |
| 126 | + } |
115 | 127 | } |
0 commit comments