1616// under the License.
1717package com .cloud .host .dao ;
1818
19+ import java .util .ArrayList ;
1920import java .util .List ;
2021
22+ import org .apache .cloudstack .api .response .HostTagResponse ;
2123import org .apache .cloudstack .framework .config .ConfigKey ;
2224import org .apache .cloudstack .framework .config .Configurable ;
25+ import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
26+ import org .apache .commons .lang3 .StringUtils ;
2327import org .springframework .stereotype .Component ;
2428
2529import com .cloud .host .HostTagVO ;
3034import com .cloud .utils .db .TransactionLegacy ;
3135import com .cloud .utils .db .SearchCriteria .Func ;
3236
37+ import javax .inject .Inject ;
38+
3339@ Component
3440public class HostTagsDaoImpl extends GenericDaoBase <HostTagVO , Long > implements HostTagsDao , Configurable {
3541 protected final SearchBuilder <HostTagVO > HostSearch ;
3642 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 ;
3749
3850 public HostTagsDaoImpl () {
3951 HostSearch = createSearchBuilder ();
4052 HostSearch .and ("hostId" , HostSearch .entity ().getHostId (), SearchCriteria .Op .EQ );
53+ HostSearch .and ("isImplicit" , HostSearch .entity ().getIsImplicit (), SearchCriteria .Op .EQ );
4154 HostSearch .and ("isTagARule" , HostSearch .entity ().getIsTagARule (), SearchCriteria .Op .EQ );
4255 HostSearch .done ();
4356
@@ -46,6 +59,19 @@ public HostTagsDaoImpl() {
4659 DistinctImplictTagsSearch .and ("hostIds" , DistinctImplictTagsSearch .entity ().getHostId (), SearchCriteria .Op .IN );
4760 DistinctImplictTagsSearch .and ("implicitTags" , DistinctImplictTagsSearch .entity ().getTag (), SearchCriteria .Op .IN );
4861 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 ();
4975 }
5076
5177 @ Override
@@ -74,6 +100,36 @@ public void deleteTags(long hostId) {
74100 txn .commit ();
75101 }
76102
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+
77133 @ Override
78134 public List <HostTagVO > findHostRuleTags () {
79135 SearchCriteria <HostTagVO > sc = HostSearch .create ();
@@ -89,6 +145,7 @@ public void persist(long hostId, List<String> hostTags, Boolean isTagARule) {
89145 txn .start ();
90146 SearchCriteria <HostTagVO > sc = HostSearch .create ();
91147 sc .setParameters ("hostId" , hostId );
148+ sc .setParameters ("isImplicit" , false );
92149 expunge (sc );
93150
94151 for (String tag : hostTags ) {
@@ -110,4 +167,72 @@ public ConfigKey<?>[] getConfigKeys() {
110167 public String getConfigComponentName () {
111168 return HostTagsDaoImpl .class .getSimpleName ();
112169 }
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+ }
113238}
0 commit comments