DaanHoogland commented on code in PR #6751:
URL: https://github.com/apache/cloudstack/pull/6751#discussion_r985384073


##########
engine/storage/src/main/java/org/apache/cloudstack/storage/image/db/SnapshotDataStoreDaoImpl.java:
##########
@@ -45,130 +45,84 @@
 import com.cloud.utils.db.GenericDaoBase;
 import com.cloud.utils.db.SearchBuilder;
 import com.cloud.utils.db.SearchCriteria;
-import com.cloud.utils.db.SearchCriteria.Op;
 import com.cloud.utils.db.TransactionLegacy;
 import com.cloud.utils.db.UpdateBuilder;
 
 @Component
 public class SnapshotDataStoreDaoImpl extends 
GenericDaoBase<SnapshotDataStoreVO, Long> implements SnapshotDataStoreDao {
     private static final Logger s_logger = 
Logger.getLogger(SnapshotDataStoreDaoImpl.class);
-    private SearchBuilder<SnapshotDataStoreVO> updateStateSearch;
-    private SearchBuilder<SnapshotDataStoreVO> storeSearch;
-    private SearchBuilder<SnapshotDataStoreVO> storeStateSearch;
-    private SearchBuilder<SnapshotDataStoreVO> destroyedSearch;
-    private SearchBuilder<SnapshotDataStoreVO> cacheSearch;
-    private SearchBuilder<SnapshotDataStoreVO> storeSnapshotSearch;
-    private SearchBuilder<SnapshotDataStoreVO> snapshotIdSearch;
-    private SearchBuilder<SnapshotDataStoreVO> volumeIdSearch;
-    private SearchBuilder<SnapshotDataStoreVO> volumeIdAndStateReadySearch;
-    private SearchBuilder<SnapshotDataStoreVO> volumeSearch;
+    private static final String STORE_ID = "store_id";
+    private static final String STORE_ROLE = "store_role";
+    private static final String STATE = "state";
+    private static final String REF_CNT = "ref_cnt";
+    private static final String ID = "id";
+    private static final String UPDATED_COUNT = "updatedCount";
+    private static final String SNAPSHOT_ID = "snapshot_id";
+    private static final String VOLUME_ID = "volume_id";
+    private static final String CREATED = "created";
+    private SearchBuilder<SnapshotDataStoreVO> 
searchFilteringStoreIdEqStoreRoleEqStateNeqRefCntNeq;
+    private SearchBuilder<SnapshotDataStoreVO> 
searchFilteringStoreIdEqStateEqStoreRoleEqIdEqUpdateCountEqSnapshotIdEqVolumeIdEq;
     private SearchBuilder<SnapshotDataStoreVO> stateSearch;
-    private SearchBuilder<SnapshotDataStoreVO> parentSnapshotSearch;
-    private SearchBuilder<SnapshotVO> snapshotVOSearch;
+    protected SearchBuilder<SnapshotVO> snapshotVOSearch;
     private SearchBuilder<SnapshotDataStoreVO> snapshotCreatedSearch;
-    protected SearchBuilder<SnapshotDataStoreVO> snapshotSearch;
 
-    public static ArrayList<Hypervisor.HypervisorType> 
hypervisorsSupportingSnapshotsChaining = new 
ArrayList<Hypervisor.HypervisorType>();
+    protected static final List<Hypervisor.HypervisorType> 
HYPERVISORS_SUPPORTING_SNAPSHOTS_CHAINING = 
List.of(Hypervisor.HypervisorType.XenServer);
 
     @Inject
-    private SnapshotDao _snapshotDao;
+    protected SnapshotDao snapshotDao;
 
-    private final String findLatestSnapshot = "select store_id, store_role, 
snapshot_id from cloud.snapshot_store_ref where " +
+    private static final String FIND_OLDEST_OR_LATEST_SNAPSHOT = "select 
store_id, store_role, snapshot_id from cloud.snapshot_store_ref where " +
             " store_role = ? and volume_id = ? and state = 'Ready'" +
-            " order by created DESC " +
-            " limit 1";
-    private final String findOldestSnapshot = "select store_id, store_role, 
snapshot_id from cloud.snapshot_store_ref where " +
-            " store_role = ? and volume_id = ? and state = 'Ready'" +
-            " order by created ASC " +
+            " order by created %s " +
             " limit 1";
 
     @Override
     public boolean configure(String name, Map<String, Object> params) throws 
ConfigurationException {
         super.configure(name, params);
 
-        // Note that snapshot_store_ref stores snapshots on primary as well as
-        // those on secondary, so we need to
-        // use (store_id, store_role) to search
-        storeSearch = createSearchBuilder();
-        storeSearch.and("store_id", storeSearch.entity().getDataStoreId(), 
SearchCriteria.Op.EQ);
-        storeSearch.and("store_role", storeSearch.entity().getRole(), 
SearchCriteria.Op.EQ);
-        storeSearch.and("state", storeSearch.entity().getState(), 
SearchCriteria.Op.NEQ);
-        storeSearch.done();
-
-        storeStateSearch = createSearchBuilder();
-        storeStateSearch.and("store_id", 
storeStateSearch.entity().getDataStoreId(), SearchCriteria.Op.EQ);
-        storeStateSearch.and("state", storeStateSearch.entity().getState(), 
SearchCriteria.Op.EQ);
-        storeStateSearch.done();
-
-        destroyedSearch = createSearchBuilder();
-        destroyedSearch.and("store_id", 
destroyedSearch.entity().getDataStoreId(), SearchCriteria.Op.EQ);
-        destroyedSearch.and("store_role", destroyedSearch.entity().getRole(), 
SearchCriteria.Op.EQ);
-        destroyedSearch.and("state", destroyedSearch.entity().getState(), 
SearchCriteria.Op.EQ);
-        destroyedSearch.done();
-
-        cacheSearch = createSearchBuilder();
-        cacheSearch.and("store_id", cacheSearch.entity().getDataStoreId(), 
SearchCriteria.Op.EQ);
-        cacheSearch.and("store_role", cacheSearch.entity().getRole(), 
SearchCriteria.Op.EQ);
-        cacheSearch.and("state", cacheSearch.entity().getState(), 
SearchCriteria.Op.NEQ);
-        cacheSearch.and("ref_cnt", cacheSearch.entity().getRefCnt(), 
SearchCriteria.Op.NEQ);
-        cacheSearch.done();
-
-        updateStateSearch = this.createSearchBuilder();
-        updateStateSearch.and("id", updateStateSearch.entity().getId(), Op.EQ);
-        updateStateSearch.and("state", updateStateSearch.entity().getState(), 
Op.EQ);
-        updateStateSearch.and("updatedCount", 
updateStateSearch.entity().getUpdatedCount(), Op.EQ);
-        updateStateSearch.done();
-
-        snapshotSearch = createSearchBuilder();
-        snapshotSearch.and("snapshot_id", 
snapshotSearch.entity().getSnapshotId(), SearchCriteria.Op.EQ);
-        snapshotSearch.and("store_role", snapshotSearch.entity().getRole(), 
SearchCriteria.Op.EQ);
-        snapshotSearch.and("state", snapshotSearch.entity().getState(), 
SearchCriteria.Op.EQ);
-        snapshotSearch.done();
-
-        storeSnapshotSearch = createSearchBuilder();
-        storeSnapshotSearch.and("snapshot_id", 
storeSnapshotSearch.entity().getSnapshotId(), SearchCriteria.Op.EQ);
-        storeSnapshotSearch.and("store_id", 
storeSnapshotSearch.entity().getDataStoreId(), SearchCriteria.Op.EQ);
-        storeSnapshotSearch.and("store_role", 
storeSnapshotSearch.entity().getRole(), SearchCriteria.Op.EQ);
-        storeSnapshotSearch.and("state", 
storeSnapshotSearch.entity().getState(), SearchCriteria.Op.EQ);
-        storeSnapshotSearch.done();
-
-        snapshotIdSearch = createSearchBuilder();
-        snapshotIdSearch.and("snapshot_id", 
snapshotIdSearch.entity().getSnapshotId(), SearchCriteria.Op.EQ);
-        snapshotIdSearch.done();
-
-        volumeIdSearch = createSearchBuilder();
-        volumeIdSearch.and("volume_id", volumeIdSearch.entity().getVolumeId(), 
SearchCriteria.Op.EQ);
-        volumeIdSearch.done();
-
-        volumeSearch = createSearchBuilder();
-        volumeSearch.and("volume_id", volumeSearch.entity().getVolumeId(), 
SearchCriteria.Op.EQ);
-        volumeSearch.and("store_role", volumeSearch.entity().getRole(), 
SearchCriteria.Op.EQ);
-        volumeSearch.and("snapshot_id", volumeSearch.entity().getSnapshotId(), 
SearchCriteria.Op.EQ);
-        volumeSearch.done();
-
-        volumeIdAndStateReadySearch = createSearchBuilder();
-        volumeIdAndStateReadySearch.and("volume_id", 
volumeIdAndStateReadySearch.entity().getVolumeId(), SearchCriteria.Op.EQ);
-        volumeIdAndStateReadySearch.and("state", 
volumeIdAndStateReadySearch.entity().getState(), SearchCriteria.Op.EQ);
-        volumeIdAndStateReadySearch.done();
+        searchFilteringStoreIdEqStoreRoleEqStateNeqRefCntNeq = 
createSearchBuilder();
+        searchFilteringStoreIdEqStoreRoleEqStateNeqRefCntNeq.and(STORE_ID, 
searchFilteringStoreIdEqStoreRoleEqStateNeqRefCntNeq.entity().getDataStoreId(), 
SearchCriteria.Op.EQ);
+        searchFilteringStoreIdEqStoreRoleEqStateNeqRefCntNeq.and(STORE_ROLE, 
searchFilteringStoreIdEqStoreRoleEqStateNeqRefCntNeq.entity().getRole(), 
SearchCriteria.Op.EQ);
+        searchFilteringStoreIdEqStoreRoleEqStateNeqRefCntNeq.and(STATE, 
searchFilteringStoreIdEqStoreRoleEqStateNeqRefCntNeq.entity().getState(), 
SearchCriteria.Op.NEQ);
+        searchFilteringStoreIdEqStoreRoleEqStateNeqRefCntNeq.and(REF_CNT 
searchFilteringStoreIdEqStoreRoleEqStateNeqRefCntNeq.entity().getRefCnt(), 
SearchCriteria.Op.NEQ);

Review Comment:
   ```suggestion
           searchFilteringStoreIdEqStoreRoleEqStateNeqRefCntNeq.and(REF_CNT, 
searchFilteringStoreIdEqStoreRoleEqStateNeqRefCntNeq.entity().getRefCnt(), 
SearchCriteria.Op.NEQ);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to