Skip to content

Commit

Permalink
Use join instead of views (apache#8321)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishesh92 authored Mar 18, 2024
1 parent ffd5972 commit 0043540
Show file tree
Hide file tree
Showing 41 changed files with 1,592 additions and 575 deletions.
2 changes: 1 addition & 1 deletion api/src/main/java/com/cloud/offering/ServiceOffering.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ enum StorageType {

boolean getDefaultUse();

String getSystemVmType();
String getVmType();

String getDeploymentPlanner();

Expand Down
4 changes: 3 additions & 1 deletion api/src/main/java/org/apache/cloudstack/acl/RoleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public interface RoleService {
* Moreover, we will check if the requested role is of 'Admin' type; roles with 'Admin' type should only be visible to 'root admins'.
* Therefore, if a non-'root admin' user tries to search for an 'Admin' role, this method will return null.
*/
Role findRole(Long id, boolean removePrivateRoles);
Role findRole(Long id, boolean ignorePrivateRoles);

List<Role> findRoles(List<Long> ids, boolean ignorePrivateRoles);

Role findRole(Long id);

Expand Down
14 changes: 14 additions & 0 deletions api/src/main/java/org/apache/cloudstack/api/InternalIdentity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,18 @@

public interface InternalIdentity extends Serializable {
long getId();

/*
Helper method to add conditions in joins where some column name is equal to a string value
*/
default Object setString(String str) {
return null;
}

/*
Helper method to add conditions in joins where some column name is equal to a long value
*/
default Object setLong(Long l) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public ServiceOfferingVO(ServiceOfferingVO offering) {
limitCpuUse = offering.getLimitCpuUse();
volatileVm = offering.isVolatileVm();
hostTag = offering.getHostTag();
vmType = offering.getSystemVmType();
vmType = offering.getVmType();
systemUse = offering.isSystemUse();
dynamicScalingEnabled = offering.isDynamicScalingEnabled();
diskOfferingStrictness = offering.diskOfferingStrictness;
Expand Down Expand Up @@ -278,7 +278,7 @@ public String getHostTag() {
}

@Override
public String getSystemVmType() {
public String getVmType() {
return vmType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ public interface RoleDao extends GenericDao<RoleVO, Long> {
Pair<List<RoleVO>, Integer> findAllByRoleType(RoleType type, Long offset, Long limit, boolean showPrivateRole);

Pair<List<RoleVO>, Integer> listAllRoles(Long startIndex, Long limit, boolean showPrivateRole);

List<RoleVO> searchByIds(Long... ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

@Component
public class RoleDaoImpl extends GenericDaoBase<RoleVO, Long> implements RoleDao {

private final SearchBuilder<RoleVO> RoleByIdsSearch;
private final SearchBuilder<RoleVO> RoleByNameSearch;
private final SearchBuilder<RoleVO> RoleByTypeSearch;
private final SearchBuilder<RoleVO> RoleByNameAndTypeSearch;
Expand All @@ -40,6 +43,10 @@ public class RoleDaoImpl extends GenericDaoBase<RoleVO, Long> implements RoleDao
public RoleDaoImpl() {
super();

RoleByIdsSearch = createSearchBuilder();
RoleByIdsSearch.and("idIN", RoleByIdsSearch.entity().getId(), SearchCriteria.Op.IN);
RoleByIdsSearch.done();

RoleByNameSearch = createSearchBuilder();
RoleByNameSearch.and("roleName", RoleByNameSearch.entity().getName(), SearchCriteria.Op.LIKE);
RoleByNameSearch.and("isPublicRole", RoleByNameSearch.entity().isPublicRole(), SearchCriteria.Op.EQ);
Expand Down Expand Up @@ -116,6 +123,16 @@ public Pair<List<RoleVO>, Integer> listAllRoles(Long startIndex, Long limit, boo
return searchAndCount(sc, new Filter(RoleVO.class, "id", true, startIndex, limit));
}

@Override
public List<RoleVO> searchByIds(Long... ids) {
if (ids == null || ids.length == 0) {
return Collections.emptyList();
}
SearchCriteria<RoleVO> sc = RoleByIdsSearch.create();
sc.setParameters("idIN", ids);
return listBy(sc);
}

public void filterPrivateRolesIfNeeded(SearchCriteria<RoleVO> sc, boolean showPrivateRole) {
if (!showPrivateRole) {
sc.setParameters("isPublicRole", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String getValue() {
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ public interface ImageStoreDao extends GenericDao<ImageStoreVO, Long> {
ImageStoreVO findOneByZoneAndProtocol(long zoneId, String protocol);

List<ImageStoreVO> listImageStoresByZoneIds(Long... zoneIds);

List<ImageStoreVO> listByIds(List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
*/
package org.apache.cloudstack.storage.datastore.db;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.naming.ConfigurationException;

import com.cloud.utils.db.Filter;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
Expand All @@ -44,6 +46,7 @@ public class ImageStoreDaoImpl extends GenericDaoBase<ImageStoreVO, Long> implem
private SearchBuilder<ImageStoreVO> zoneProtocolSearch;

private SearchBuilder<ImageStoreVO> zonesInSearch;
private SearchBuilder<ImageStoreVO> IdsSearch;

public ImageStoreDaoImpl() {
super();
Expand All @@ -62,6 +65,9 @@ public ImageStoreDaoImpl() {
zonesInSearch.and("zonesIn", zonesInSearch.entity().getDcId(), SearchCriteria.Op.IN);
zonesInSearch.done();

IdsSearch = createSearchBuilder();
IdsSearch.and("ids", IdsSearch.entity().getId(), SearchCriteria.Op.IN);
IdsSearch.done();
}
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
Expand Down Expand Up @@ -206,4 +212,14 @@ public List<ImageStoreVO> listImageStoresByZoneIds(Long... zoneIds) {
sc.setParametersIfNotNull("zonesIn", zoneIds);
return listBy(sc);
}

@Override
public List<ImageStoreVO> listByIds(List<Long> ids) {
if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList();
}
SearchCriteria<ImageStoreVO> sc = IdsSearch.create();
sc.setParameters("ids", ids.toArray());
return listBy(sc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.storage.ScopeType;
import com.cloud.storage.StoragePoolStatus;
import com.cloud.utils.Pair;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDao;

/**
Expand Down Expand Up @@ -139,4 +141,10 @@ public interface PrimaryDataStoreDao extends GenericDao<StoragePoolVO, Long> {
List<StoragePoolVO> findPoolsByStorageType(String storageType);

List<StoragePoolVO> listStoragePoolsWithActiveVolumesByOfferingId(long offeringid);

Pair<List<Long>, Integer> searchForIdsAndCount(Long storagePoolId, String storagePoolName, Long zoneId,
String path, Long podId, Long clusterId, String address, ScopeType scopeType, StoragePoolStatus status,
String keyword, Filter searchFilter);

List<StoragePoolVO> listByIds(List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.naming.ConfigurationException;

import com.cloud.utils.Pair;
import com.cloud.utils.db.Filter;
import org.apache.commons.collections.CollectionUtils;

import com.cloud.host.Status;
Expand Down Expand Up @@ -57,6 +61,7 @@ public class PrimaryDataStoreDaoImpl extends GenericDaoBase<StoragePoolVO, Long>
private final SearchBuilder<StoragePoolVO> DcLocalStorageSearch;
private final GenericSearchBuilder<StoragePoolVO, Long> StatusCountSearch;
private final SearchBuilder<StoragePoolVO> ClustersSearch;
private final SearchBuilder<StoragePoolVO> IdsSearch;

@Inject
private StoragePoolDetailsDao _detailsDao;
Expand Down Expand Up @@ -143,6 +148,11 @@ public PrimaryDataStoreDaoImpl() {
ClustersSearch = createSearchBuilder();
ClustersSearch.and("clusterIds", ClustersSearch.entity().getClusterId(), Op.IN);
ClustersSearch.and("status", ClustersSearch.entity().getStatus(), Op.EQ);
ClustersSearch.done();

IdsSearch = createSearchBuilder();
IdsSearch.and("ids", IdsSearch.entity().getId(), SearchCriteria.Op.IN);
IdsSearch.done();

}

Expand Down Expand Up @@ -669,4 +679,92 @@ public List<StoragePoolVO> listStoragePoolsWithActiveVolumesByOfferingId(long of
throw new CloudRuntimeException("Caught: " + sql, e);
}
}

@Override
public Pair<List<Long>, Integer> searchForIdsAndCount(Long storagePoolId, String storagePoolName, Long zoneId,
String path, Long podId, Long clusterId, String address, ScopeType scopeType, StoragePoolStatus status,
String keyword, Filter searchFilter) {
SearchCriteria<StoragePoolVO> sc = createStoragePoolSearchCriteria(storagePoolId, storagePoolName, zoneId, path, podId, clusterId, address, scopeType, status, keyword);
Pair<List<StoragePoolVO>, Integer> uniquePair = searchAndCount(sc, searchFilter);
List<Long> idList = uniquePair.first().stream().map(StoragePoolVO::getId).collect(Collectors.toList());
return new Pair<>(idList, uniquePair.second());
}

@Override
public List<StoragePoolVO> listByIds(List<Long> ids) {
if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList();
}
SearchCriteria<StoragePoolVO> sc = IdsSearch.create();
sc.setParameters("ids", ids.toArray());
return listBy(sc);
}

private SearchCriteria<StoragePoolVO> createStoragePoolSearchCriteria(Long storagePoolId, String storagePoolName,
Long zoneId, String path, Long podId, Long clusterId, String address, ScopeType scopeType,
StoragePoolStatus status, String keyword) {
SearchBuilder<StoragePoolVO> sb = createSearchBuilder();
sb.select(null, SearchCriteria.Func.DISTINCT, sb.entity().getId()); // select distinct
// ids
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
sb.and("path", sb.entity().getPath(), SearchCriteria.Op.EQ);
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
sb.and("clusterId", sb.entity().getClusterId(), SearchCriteria.Op.EQ);
sb.and("hostAddress", sb.entity().getHostAddress(), SearchCriteria.Op.EQ);
sb.and("scope", sb.entity().getScope(), SearchCriteria.Op.EQ);
sb.and("status", sb.entity().getStatus(), SearchCriteria.Op.EQ);
sb.and("parent", sb.entity().getParent(), SearchCriteria.Op.EQ);

SearchCriteria<StoragePoolVO> sc = sb.create();

if (keyword != null) {
SearchCriteria<StoragePoolVO> ssc = createSearchCriteria();
ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
ssc.addOr("poolType", SearchCriteria.Op.LIKE, "%" + keyword + "%");

sc.addAnd("name", SearchCriteria.Op.SC, ssc);
}

if (storagePoolId != null) {
sc.setParameters("id", storagePoolId);
}

if (storagePoolName != null) {
sc.setParameters("name", storagePoolName);
}

if (path != null) {
sc.setParameters("path", path);
}
if (zoneId != null) {
sc.setParameters("dataCenterId", zoneId);
}
if (podId != null) {
SearchCriteria<StoragePoolVO> ssc = createSearchCriteria();
ssc.addOr("podId", SearchCriteria.Op.EQ, podId);
ssc.addOr("podId", SearchCriteria.Op.NULL);

sc.addAnd("podId", SearchCriteria.Op.SC, ssc);
}
if (address != null) {
sc.setParameters("hostAddress", address);
}
if (clusterId != null) {
SearchCriteria<StoragePoolVO> ssc = createSearchCriteria();
ssc.addOr("clusterId", SearchCriteria.Op.EQ, clusterId);
ssc.addOr("clusterId", SearchCriteria.Op.NULL);

sc.addAnd("clusterId", SearchCriteria.Op.SC, ssc);
}
if (scopeType != null) {
sc.setParameters("scope", scopeType.toString());
}
if (status != null) {
sc.setParameters("status", status.toString());
}
sc.setParameters("parent", 0);
return sc;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ SELECT
FROM
`cloud`.`service_offering`
INNER JOIN
`cloud`.`disk_offering_view` AS `disk_offering` ON service_offering.disk_offering_id = disk_offering.id
`cloud`.`disk_offering` ON service_offering.disk_offering_id = disk_offering.id AND `disk_offering`.`state`='Active'
LEFT JOIN
`cloud`.`service_offering_details` AS `domain_details` ON `domain_details`.`service_offering_id` = `service_offering`.`id` AND `domain_details`.`name`='domainid'
LEFT JOIN
Expand Down
9 changes: 9 additions & 0 deletions framework/db/src/main/java/com/cloud/utils/db/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public int setFalse(int value) {

protected String table;
protected String columnName;
protected Object value;
protected Field field;
protected int flags;
protected Column column;
Expand All @@ -100,6 +101,10 @@ public Attribute(String table, String columnName) {
this.column = null;
}

public Attribute(Object value) {
this.value = value;
}

protected void setupColumnInfo(Class<?> clazz, AttributeOverride[] overrides, String tableName, boolean isEmbedded, boolean isId) {
flags = Flag.Selectable.setTrue(flags);
GeneratedValue gv = field.getAnnotation(GeneratedValue.class);
Expand Down Expand Up @@ -214,6 +219,10 @@ public Field getField() {
return field;
}

public Object getValue() {
return value;
}

public Object get(Object entity) {
try {
return field.get(entity);
Expand Down
2 changes: 2 additions & 0 deletions framework/db/src/main/java/com/cloud/utils/db/GenericDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,6 @@ public interface GenericDao<T, ID extends Serializable> {
Pair<List<T>, Integer> searchAndDistinctCount(final SearchCriteria<T> sc, final Filter filter, final String[] distinctColumns);

Integer countAll();

List<T> findByUuids(String... uuidArray);
}
Loading

0 comments on commit 0043540

Please sign in to comment.