1717package com .cloud .api .query .dao ;
1818
1919import java .text .DecimalFormat ;
20+ import java .util .ArrayList ;
2021import java .util .Date ;
2122import java .util .EnumSet ;
2223import java .util .Iterator ;
2930
3031import com .cloud .api .ApiDBUtils ;
3132import com .cloud .api .query .vo .HostJoinVO ;
33+ import com .cloud .configuration .dao .ConfigurationDao ;
3234import com .cloud .host .Host ;
3335import com .cloud .host .HostStats ;
3436
3537import org .apache .cloudstack .api .ApiConstants .HostDetails ;
3638import org .apache .cloudstack .api .response .HostResponse ;
3739import com .cloud .storage .StorageStats ;
40+ import com .cloud .utils .component .Inject ;
3841import com .cloud .utils .db .GenericDaoBase ;
3942import com .cloud .utils .db .SearchBuilder ;
4043import com .cloud .utils .db .SearchCriteria ;
4447public class HostJoinDaoImpl extends GenericDaoBase <HostJoinVO , Long > implements HostJoinDao {
4548 public static final Logger s_logger = Logger .getLogger (HostJoinDaoImpl .class );
4649
47- private SearchBuilder <HostJoinVO > vrSearch ;
50+ @ Inject
51+ private ConfigurationDao _configDao ;
4852
49- private SearchBuilder <HostJoinVO > vrIdSearch ;
53+ private SearchBuilder <HostJoinVO > hostSearch ;
54+
55+ private SearchBuilder <HostJoinVO > hostIdSearch ;
5056
5157
5258 protected HostJoinDaoImpl () {
5359
54- vrSearch = createSearchBuilder ();
55- vrSearch .and ("idIN" , vrSearch .entity ().getId (), SearchCriteria .Op .IN );
56- vrSearch .done ();
60+ hostSearch = createSearchBuilder ();
61+ hostSearch .and ("idIN" , hostSearch .entity ().getId (), SearchCriteria .Op .IN );
62+ hostSearch .done ();
5763
58- vrIdSearch = createSearchBuilder ();
59- vrIdSearch .and ("id" , vrIdSearch .entity ().getId (), SearchCriteria .Op .EQ );
60- vrIdSearch .done ();
64+ hostIdSearch = createSearchBuilder ();
65+ hostIdSearch .and ("id" , hostIdSearch .entity ().getId (), SearchCriteria .Op .EQ );
66+ hostIdSearch .done ();
6167
6268 this ._count = "select count(distinct id) from host_view WHERE " ;
6369 }
@@ -206,7 +212,7 @@ public HostResponse setHostResponse(HostResponse response, HostJoinVO host) {
206212
207213 @ Override
208214 public List <HostJoinVO > newHostView (Host host ) {
209- SearchCriteria <HostJoinVO > sc = vrIdSearch .create ();
215+ SearchCriteria <HostJoinVO > sc = hostIdSearch .create ();
210216 sc .setParameters ("id" , host .getId ());
211217 return searchIncludingRemoved (sc , null , null , false );
212218
@@ -215,10 +221,47 @@ public List<HostJoinVO> newHostView(Host host) {
215221
216222
217223 @ Override
218- public List <HostJoinVO > searchByIds (Long ... ids ) {
219- SearchCriteria <HostJoinVO > sc = vrSearch .create ();
220- sc .setParameters ("idIN" , ids );
221- return searchIncludingRemoved (sc , null , null , false );
224+ public List <HostJoinVO > searchByIds (Long ... hostIds ) {
225+ // set detail batch query size
226+ int DETAILS_BATCH_SIZE = 2000 ;
227+ String batchCfg = _configDao .getValue ("detail.batch.query.size" );
228+ if ( batchCfg != null ){
229+ DETAILS_BATCH_SIZE = Integer .parseInt (batchCfg );
230+ }
231+ // query details by batches
232+ List <HostJoinVO > uvList = new ArrayList <HostJoinVO >();
233+ // query details by batches
234+ int curr_index = 0 ;
235+ if ( hostIds .length > DETAILS_BATCH_SIZE ){
236+ while ( (curr_index + DETAILS_BATCH_SIZE ) <= hostIds .length ) {
237+ Long [] ids = new Long [DETAILS_BATCH_SIZE ];
238+ for (int k = 0 , j = curr_index ; j < curr_index + DETAILS_BATCH_SIZE ; j ++, k ++) {
239+ ids [k ] = hostIds [j ];
240+ }
241+ SearchCriteria <HostJoinVO > sc = hostSearch .create ();
242+ sc .setParameters ("idIN" , ids );
243+ List <HostJoinVO > vms = searchIncludingRemoved (sc , null , null , false );
244+ if (vms != null ) {
245+ uvList .addAll (vms );
246+ }
247+ curr_index += DETAILS_BATCH_SIZE ;
248+ }
249+ }
250+ if (curr_index < hostIds .length ) {
251+ int batch_size = (hostIds .length - curr_index );
252+ // set the ids value
253+ Long [] ids = new Long [batch_size ];
254+ for (int k = 0 , j = curr_index ; j < curr_index + batch_size ; j ++, k ++) {
255+ ids [k ] = hostIds [j ];
256+ }
257+ SearchCriteria <HostJoinVO > sc = hostSearch .create ();
258+ sc .setParameters ("idIN" , ids );
259+ List <HostJoinVO > vms = searchIncludingRemoved (sc , null , null , false );
260+ if (vms != null ) {
261+ uvList .addAll (vms );
262+ }
263+ }
264+ return uvList ;
222265 }
223266
224267
0 commit comments