Search in sources :

Example 1 with ContainmentConstraintImpl

use of com.emc.storageos.db.client.constraint.impl.ContainmentConstraintImpl in project coprhd-controller by CoprHD.

the class DbIndexTest method verifyRelationDbIndex.

private void verifyRelationDbIndex(DataObject obj, ColumnField field, Object val, boolean indexByKey, DbClient client) {
    switch(field.getType()) {
        case Primitive:
            {
                ContainmentConstraint constraint = new ContainmentConstraintImpl((URI) val, obj.getClass(), field);
                verifyContain(constraint, obj.getId(), -1, client);
            }
            break;
        case TrackingMap:
            for (String key : ((AbstractChangeTrackingMap<String>) val).keySet()) {
                ContainmentConstraint constraint = new ContainmentConstraintImpl(URI.create(key), obj.getClass(), field);
                verifyContain(constraint, obj.getId(), -1, client);
            }
            break;
        case TrackingSet:
            for (String key : (AbstractChangeTrackingSet<String>) val) {
                ContainmentConstraint constraint = new ContainmentConstraintImpl(URI.create(key), obj.getClass(), field);
                verifyContain(constraint, obj.getId(), -1, client);
            }
            break;
        case Id:
        case NamedURI:
        case NestedObject:
        case TrackingSetMap:
        default:
            throw new IllegalArgumentException(String.format("Field type %s is not supported by RelationDbIndex", field.getType().toString()));
    }
}
Also used : ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint) ContainmentConstraintImpl(com.emc.storageos.db.client.constraint.impl.ContainmentConstraintImpl) URI(java.net.URI)

Example 2 with ContainmentConstraintImpl

use of com.emc.storageos.db.client.constraint.impl.ContainmentConstraintImpl in project coprhd-controller by CoprHD.

the class DbDependencyPurger method purge.

private <T extends DataObject> void purge(T dataObj, Set<URI> visited) throws DatabaseException {
    if (dataObj == null || visited.contains(dataObj.getId())) {
        return;
    }
    visited.add(dataObj.getId());
    Class<? extends DataObject> type = dataObj.getClass();
    List<DependencyTracker.Dependency> dependencyList = _dependencyTracker.getDependencies(type);
    try {
        for (DependencyTracker.Dependency dependence : dependencyList) {
            // Query relational index to see if any dependents exist
            Class<? extends DataObject> childType = dependence.getType();
            ColumnField childField = dependence.getColumnField();
            ContainmentConstraint constraint = new ContainmentConstraintImpl(dataObj.getId(), childType, childField);
            URIQueryResultList list = new URIQueryResultList();
            _dbClient.queryByConstraint(constraint, list);
            if (!list.iterator().hasNext()) {
                continue;
            }
            // used to remove efficiently identical URIs from the list.
            HashSet<URI> childSet = new HashSet();
            while (list.iterator().hasNext()) {
                childSet.clear();
                while (list.iterator().hasNext()) {
                    childSet.add(list.iterator().next());
                    if (childSet.size() >= MAX_PERSISTENCE_LIMIT) {
                        break;
                    }
                }
                List<URI> childUriList = new ArrayList(childSet);
                List<? extends DataObject> children = _dbClient.queryObjectField(childType, childField.getName(), childUriList);
                List<DataObject> decommissionedChildren = new ArrayList();
                for (DataObject childObj : children) {
                    switch(childField.getType()) {
                        case TrackingSet:
                        case TrackingMap:
                            // assume @indexByKey is set
                            java.beans.PropertyDescriptor pd = childField.getPropertyDescriptor();
                            Object fieldValue = pd.getReadMethod().invoke(childObj);
                            boolean deactivateChildObj = false;
                            if (fieldValue != null) {
                                // should be always true.
                                if (AbstractChangeTrackingMap.class.isAssignableFrom(pd.getPropertyType())) {
                                    AbstractChangeTrackingMap<?> trackingMap = (AbstractChangeTrackingMap<?>) fieldValue;
                                    trackingMap.remove(dataObj.getId().toString());
                                    if (trackingMap.isEmpty() && childField.deactivateIfEmpty()) {
                                        deactivateChildObj = true;
                                    }
                                } else if (AbstractChangeTrackingSet.class.isAssignableFrom(pd.getPropertyType())) {
                                    AbstractChangeTrackingSet trackingSet = (AbstractChangeTrackingSet) fieldValue;
                                    trackingSet.remove(dataObj.getId().toString());
                                    if (trackingSet.isEmpty() && childField.deactivateIfEmpty()) {
                                        deactivateChildObj = true;
                                    }
                                }
                                if (deactivateChildObj) {
                                    purge(childObj, visited);
                                    childObj.setInactive(true);
                                    _log.info("Deactivated db_object: type = {}, id = {}", childType.toString(), childObj.getId());
                                }
                            }
                            break;
                        default:
                            {
                                purge(childObj, visited);
                                childObj.setInactive(true);
                                _log.info("Deactivated db_object: type = {}, id = {}", childType.toString(), childObj.getId());
                            }
                    }
                    decommissionedChildren.add(childObj);
                }
                _dbClient.persistObject(decommissionedChildren);
            }
        }
    }// should never get here....
     catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException e) {
        _log.error("Unexpected purge error", e);
        throw DatabaseException.fatals.purgeFailed(e);
    }
}
Also used : ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint) ArrayList(java.util.ArrayList) URI(java.net.URI) URIQueryResultList(com.emc.storageos.db.client.constraint.URIQueryResultList) HashSet(java.util.HashSet) ContainmentConstraintImpl(com.emc.storageos.db.client.constraint.impl.ContainmentConstraintImpl) ColumnField(com.emc.storageos.db.client.impl.ColumnField) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataObject(com.emc.storageos.db.client.model.DataObject) AbstractChangeTrackingMap(com.emc.storageos.db.client.model.AbstractChangeTrackingMap) DataObject(com.emc.storageos.db.client.model.DataObject) AbstractChangeTrackingSet(com.emc.storageos.db.client.model.AbstractChangeTrackingSet)

Example 3 with ContainmentConstraintImpl

use of com.emc.storageos.db.client.constraint.impl.ContainmentConstraintImpl in project coprhd-controller by CoprHD.

the class DependencyChecker method checkDependencies.

/**
 * checks to see if any references exist for this uri
 * uses dependency list created from relational indices
 *
 * @param uri id of the DataObject
 * @param type DataObject class name
 * @param onlyActive if true, checks for active references only (expensive)
 * @param excludeTypes optional list of classes that can be excluded as dependency
 * @return null if no references exist on this uri, return the type of the dependency if exist
 */
public String checkDependencies(URI uri, Class<? extends DataObject> type, boolean onlyActive, List<Class<? extends DataObject>> excludeTypes) {
    List<DependencyTracker.Dependency> dependencies = _dependencyTracker.getDependencies(type);
    // no dependencies - nothing to do
    if (dependencies.isEmpty()) {
        return null;
    }
    for (DependencyTracker.Dependency dependency : dependencies) {
        if (excludeTypes != null) {
            if (excludeTypes.contains(dependency.getType())) {
                continue;
            }
        }
        // Query relational index to see if any dependents exist
        ContainmentConstraint constraint = new ContainmentConstraintImpl(uri, dependency.getType(), dependency.getColumnField());
        URIQueryResultList list = new URIQueryResultList();
        _dbClient.queryByConstraint(constraint, list);
        if (list.iterator().hasNext()) {
            if (!onlyActive || checkIfAnyActive(list, dependency.getType())) {
                _log.info("{}: active references of type {} found", uri.toString(), dependency.getType().getSimpleName());
                return dependency.getType().getSimpleName();
            }
        }
    }
    return null;
}
Also used : ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint) ContainmentConstraintImpl(com.emc.storageos.db.client.constraint.impl.ContainmentConstraintImpl) URIQueryResultList(com.emc.storageos.db.client.constraint.URIQueryResultList)

Example 4 with ContainmentConstraintImpl

use of com.emc.storageos.db.client.constraint.impl.ContainmentConstraintImpl in project coprhd-controller by CoprHD.

the class InternalDbClientImpl method getReferUris.

public List<URI> getReferUris(URI targetUri, Class<? extends DataObject> type, Dependency dependency) {
    List<URI> references = new ArrayList<>();
    if (targetUri == null) {
        return references;
    }
    ContainmentConstraint constraint = new ContainmentConstraintImpl(targetUri, dependency.getType(), dependency.getColumnField());
    URIQueryResultList result = new URIQueryResultList();
    this.queryByConstraint(constraint, result);
    Iterator<URI> resultIt = result.iterator();
    if (resultIt.hasNext()) {
        references.add(resultIt.next());
    }
    return references;
}
Also used : ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint) ContainmentConstraintImpl(com.emc.storageos.db.client.constraint.impl.ContainmentConstraintImpl) ArrayList(java.util.ArrayList) URI(java.net.URI) URIQueryResultList(com.emc.storageos.db.client.constraint.URIQueryResultList)

Example 5 with ContainmentConstraintImpl

use of com.emc.storageos.db.client.constraint.impl.ContainmentConstraintImpl in project coprhd-controller by CoprHD.

the class ModelClientImpl method findBy.

@Override
public <T extends DataObject> List<NamedElement> findBy(Class<T> clazz, String columnField, URI id) throws DatabaseException {
    LOG.debug("findBy({}, {}, {})", new Object[] { clazz, columnField, id });
    DataObjectType doType = TypeMap.getDoType(clazz);
    ColumnField field = doType.getColumnField(columnField);
    ContainmentConstraint constraint = new ContainmentConstraintImpl(id, clazz, field);
    return queryNamedElementsByConstraint(constraint);
}
Also used : ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint) ContainmentConstraintImpl(com.emc.storageos.db.client.constraint.impl.ContainmentConstraintImpl)

Aggregations

ContainmentConstraint (com.emc.storageos.db.client.constraint.ContainmentConstraint)5 ContainmentConstraintImpl (com.emc.storageos.db.client.constraint.impl.ContainmentConstraintImpl)5 URIQueryResultList (com.emc.storageos.db.client.constraint.URIQueryResultList)3 URI (java.net.URI)3 ArrayList (java.util.ArrayList)2 ColumnField (com.emc.storageos.db.client.impl.ColumnField)1 AbstractChangeTrackingMap (com.emc.storageos.db.client.model.AbstractChangeTrackingMap)1 AbstractChangeTrackingSet (com.emc.storageos.db.client.model.AbstractChangeTrackingSet)1 DataObject (com.emc.storageos.db.client.model.DataObject)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashSet (java.util.HashSet)1