16
16
// under the License.
17
17
package com .cloud .host .dao ;
18
18
19
+ import java .util .ArrayList ;
19
20
import java .util .List ;
20
21
22
+ import org .apache .cloudstack .api .response .HostTagResponse ;
21
23
import org .apache .cloudstack .framework .config .ConfigKey ;
22
24
import org .apache .cloudstack .framework .config .Configurable ;
25
+ import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
26
+ import org .apache .commons .lang3 .StringUtils ;
23
27
import org .springframework .stereotype .Component ;
24
28
25
29
import com .cloud .host .HostTagVO ;
30
34
import com .cloud .utils .db .TransactionLegacy ;
31
35
import com .cloud .utils .db .SearchCriteria .Func ;
32
36
37
+ import javax .inject .Inject ;
38
+
33
39
@ Component
34
40
public class HostTagsDaoImpl extends GenericDaoBase <HostTagVO , Long > implements HostTagsDao , Configurable {
35
41
protected final SearchBuilder <HostTagVO > HostSearch ;
36
42
protected final GenericSearchBuilder <HostTagVO , String > DistinctImplictTagsSearch ;
43
+ private final SearchBuilder <HostTagVO > stSearch ;
44
+ private final SearchBuilder <HostTagVO > tagIdsearch ;
45
+ private final SearchBuilder <HostTagVO > ImplicitTagsSearch ;
46
+
47
+ @ Inject
48
+ private ConfigurationDao _configDao ;
37
49
38
50
public HostTagsDaoImpl () {
39
51
HostSearch = createSearchBuilder ();
40
52
HostSearch .and ("hostId" , HostSearch .entity ().getHostId (), SearchCriteria .Op .EQ );
53
+ HostSearch .and ("isImplicit" , HostSearch .entity ().getIsImplicit (), SearchCriteria .Op .EQ );
41
54
HostSearch .and ("isTagARule" , HostSearch .entity ().getIsTagARule (), SearchCriteria .Op .EQ );
42
55
HostSearch .done ();
43
56
@@ -46,6 +59,19 @@ public HostTagsDaoImpl() {
46
59
DistinctImplictTagsSearch .and ("hostIds" , DistinctImplictTagsSearch .entity ().getHostId (), SearchCriteria .Op .IN );
47
60
DistinctImplictTagsSearch .and ("implicitTags" , DistinctImplictTagsSearch .entity ().getTag (), SearchCriteria .Op .IN );
48
61
DistinctImplictTagsSearch .done ();
62
+
63
+ stSearch = createSearchBuilder ();
64
+ stSearch .and ("idIN" , stSearch .entity ().getId (), SearchCriteria .Op .IN );
65
+ stSearch .done ();
66
+
67
+ tagIdsearch = createSearchBuilder ();
68
+ tagIdsearch .and ("id" , tagIdsearch .entity ().getId (), SearchCriteria .Op .EQ );
69
+ tagIdsearch .done ();
70
+
71
+ ImplicitTagsSearch = createSearchBuilder ();
72
+ ImplicitTagsSearch .and ("hostId" , ImplicitTagsSearch .entity ().getHostId (), SearchCriteria .Op .EQ );
73
+ ImplicitTagsSearch .and ("isImplicit" , ImplicitTagsSearch .entity ().getIsImplicit (), SearchCriteria .Op .EQ );
74
+ ImplicitTagsSearch .done ();
49
75
}
50
76
51
77
@ Override
@@ -74,6 +100,36 @@ public void deleteTags(long hostId) {
74
100
txn .commit ();
75
101
}
76
102
103
+ @ Override
104
+ public boolean updateImplicitTags (long hostId , List <String > hostTags ) {
105
+ TransactionLegacy txn = TransactionLegacy .currentTxn ();
106
+ txn .start ();
107
+ SearchCriteria <HostTagVO > sc = ImplicitTagsSearch .create ();
108
+ sc .setParameters ("hostId" , hostId );
109
+ sc .setParameters ("isImplicit" , true );
110
+ boolean expunged = expunge (sc ) > 0 ;
111
+ boolean persisted = false ;
112
+ for (String tag : hostTags ) {
113
+ if (StringUtils .isNotBlank (tag )) {
114
+ HostTagVO vo = new HostTagVO (hostId , tag .trim ());
115
+ vo .setIsImplicit (true );
116
+ persist (vo );
117
+ persisted = true ;
118
+ }
119
+ }
120
+ txn .commit ();
121
+ return expunged || persisted ;
122
+ }
123
+
124
+ @ Override
125
+ public List <HostTagVO > getExplicitHostTags (long hostId ) {
126
+ SearchCriteria <HostTagVO > sc = ImplicitTagsSearch .create ();
127
+ sc .setParameters ("hostId" , hostId );
128
+ sc .setParameters ("isImplicit" , false );
129
+
130
+ return search (sc , null );
131
+ }
132
+
77
133
@ Override
78
134
public List <HostTagVO > findHostRuleTags () {
79
135
SearchCriteria <HostTagVO > sc = HostSearch .create ();
@@ -89,6 +145,7 @@ public void persist(long hostId, List<String> hostTags, Boolean isTagARule) {
89
145
txn .start ();
90
146
SearchCriteria <HostTagVO > sc = HostSearch .create ();
91
147
sc .setParameters ("hostId" , hostId );
148
+ sc .setParameters ("isImplicit" , false );
92
149
expunge (sc );
93
150
94
151
for (String tag : hostTags ) {
@@ -110,4 +167,72 @@ public ConfigKey<?>[] getConfigKeys() {
110
167
public String getConfigComponentName () {
111
168
return HostTagsDaoImpl .class .getSimpleName ();
112
169
}
170
+
171
+ @ Override
172
+ public HostTagResponse newHostTagResponse (HostTagVO tag ) {
173
+ HostTagResponse tagResponse = new HostTagResponse ();
174
+
175
+ tagResponse .setName (tag .getTag ());
176
+ tagResponse .setHostId (tag .getHostId ());
177
+ tagResponse .setImplicit (tag .getIsImplicit ());
178
+
179
+ tagResponse .setObjectName ("hosttag" );
180
+
181
+ return tagResponse ;
182
+ }
183
+
184
+ @ Override
185
+ public List <HostTagVO > searchByIds (Long ... tagIds ) {
186
+ String batchCfg = _configDao .getValue ("detail.batch.query.size" );
187
+
188
+ final int detailsBatchSize = batchCfg != null ? Integer .parseInt (batchCfg ) : 2000 ;
189
+
190
+ // query details by batches
191
+ List <HostTagVO > tagList = new ArrayList <>();
192
+ int curr_index = 0 ;
193
+
194
+ if (tagIds .length > detailsBatchSize ) {
195
+ while ((curr_index + detailsBatchSize ) <= tagIds .length ) {
196
+ Long [] ids = new Long [detailsBatchSize ];
197
+
198
+ for (int k = 0 , j = curr_index ; j < curr_index + detailsBatchSize ; j ++, k ++) {
199
+ ids [k ] = tagIds [j ];
200
+ }
201
+
202
+ SearchCriteria <HostTagVO > sc = stSearch .create ();
203
+
204
+ sc .setParameters ("idIN" , (Object [])ids );
205
+
206
+ List <HostTagVO > vms = searchIncludingRemoved (sc , null , null , false );
207
+
208
+ if (vms != null ) {
209
+ tagList .addAll (vms );
210
+ }
211
+
212
+ curr_index += detailsBatchSize ;
213
+ }
214
+ }
215
+
216
+ if (curr_index < tagIds .length ) {
217
+ int batch_size = (tagIds .length - curr_index );
218
+ // set the ids value
219
+ Long [] ids = new Long [batch_size ];
220
+
221
+ for (int k = 0 , j = curr_index ; j < curr_index + batch_size ; j ++, k ++) {
222
+ ids [k ] = tagIds [j ];
223
+ }
224
+
225
+ SearchCriteria <HostTagVO > sc = stSearch .create ();
226
+
227
+ sc .setParameters ("idIN" , (Object [])ids );
228
+
229
+ List <HostTagVO > tags = searchIncludingRemoved (sc , null , null , false );
230
+
231
+ if (tags != null ) {
232
+ tagList .addAll (tags );
233
+ }
234
+ }
235
+
236
+ return tagList ;
237
+ }
113
238
}
0 commit comments