Search in sources :

Example 6 with ColumnField

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

the class DataObjectUtils method getPropertyValue.

/**
 * This function returns the property value for an object of {@link DataObject} subtype.
 *
 * @param clzz the object class that should be a subtype of {@link DataObject}
 * @param dataObject the instance of clzz
 * @param property the string name of the property
 * @return the value of the property
 */
public static <T extends DataObject> Object getPropertyValue(Class<T> clzz, DataObject dataObject, String property) {
    try {
        DataObjectType doType = TypeMap.getDoType(clzz);
        ColumnField field = doType.getColumnField(property);
        return field.getPropertyDescriptor().getReadMethod().invoke(dataObject);
    } catch (Exception ex) {
        throw DatabaseException.fatals.failedToReadPropertyValue(clzz, property, ex);
    }
}
Also used : ColumnField(com.emc.storageos.db.client.impl.ColumnField) DataObjectType(com.emc.storageos.db.client.impl.DataObjectType) DatabaseException(com.emc.storageos.db.exceptions.DatabaseException)

Example 7 with ColumnField

use of com.emc.storageos.db.client.impl.ColumnField 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 8 with ColumnField

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

the class DbCli method printFieldsByCf.

/**
 * Print the fields' info of column family.
 *
 * @Param cfName
 */
public void printFieldsByCf(String cfName) {
    // fill in type from cfName
    final Class clazz = getClassFromCFName(cfName);
    if (clazz == null) {
        return;
    }
    if (DataObject.class.isAssignableFrom(clazz)) {
        DataObjectType doType = TypeMap.getDoType(clazz);
        System.out.println(String.format("Column Family: %s", doType.getCF().getName()));
        Collection<ColumnField> cfs = doType.getColumnFields();
        Iterator it = cfs.iterator();
        while (it.hasNext()) {
            ColumnField field = (ColumnField) it.next();
            System.out.println(String.format("\tfield=%-30s\ttype=%s", field.getName(), field.getPropertyDescriptor().getPropertyType().toString().substring(6)));
        }
    }
}
Also used : ColumnField(com.emc.storageos.db.client.impl.ColumnField) Iterator(java.util.Iterator) DataObjectType(com.emc.storageos.db.client.impl.DataObjectType)

Example 9 with ColumnField

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

the class PersistingChangesTest method getIndexRecordCount.

private int getIndexRecordCount(String fieldName) throws ConnectionException {
    Keyspace keyspace = ((DbClientTest.DbClientImplUnitTester) dbClient).getLocalContext().getKeyspace();
    DataObjectType doType = TypeMap.getDoType(Volume.class);
    ColumnField field = doType.getColumnField(fieldName);
    ColumnFamilyQuery<String, IndexColumnName> query = keyspace.prepareQuery(field.getIndexCF());
    OperationResult<Rows<String, IndexColumnName>> result = query.getAllRows().execute();
    int count = 0;
    for (Row<String, IndexColumnName> row : result.getResult()) {
        _log.debug("{}, RowKey={}, Columns Size={}", ++count, row.getKey(), row.getColumns().size());
        for (Column<IndexColumnName> column : row.getColumns()) {
            _log.debug("\t, Column Name={}, String Value={}.", column.getName(), column.getStringValue());
        }
    }
    return count;
}
Also used : IndexColumnName(com.emc.storageos.db.client.impl.IndexColumnName) Keyspace(com.netflix.astyanax.Keyspace) ColumnField(com.emc.storageos.db.client.impl.ColumnField) DataObjectType(com.emc.storageos.db.client.impl.DataObjectType) com.emc.storageos.db.client.constraint(com.emc.storageos.db.client.constraint) DbClientTest(com.emc.storageos.db.server.DbClientTest) Rows(com.netflix.astyanax.model.Rows)

Example 10 with ColumnField

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

the class DbIndexTest method apply.

// Returns a list of fields touched
public static void apply(DataObject obj, DataObjectType doType, Object[] ops) {
    try {
        for (int i = 0; i < ops.length; i++) {
            if (!(ops[i] instanceof String)) {
                throw new IllegalArgumentException();
            }
            String op = (String) ops[i];
            // Get name of the field
            String fieldName = op.substring(0, op.length() - 1);
            op = op.substring(op.length() - 1, op.length());
            // Get type of the field
            ColumnField field = doType.getColumnField(fieldName);
            PropertyDescriptor propDesc = field.getPropertyDescriptor();
            switch(field.getType()) {
                case Primitive:
                case NamedURI:
                case Id:
                case NestedObject:
                    if (op.charAt(0) != '=') {
                        throw new IllegalArgumentException();
                    }
                    propDesc.getWriteMethod().invoke(obj, ops[++i]);
                    break;
                case TrackingSet:
                    if (op.charAt(0) == '<') {
                        AbstractChangeTrackingSet set = (AbstractChangeTrackingSet) propDesc.getReadMethod().invoke(obj);
                        if (set == null) {
                            set = (AbstractChangeTrackingSet) propDesc.getPropertyType().getConstructor(new Class<?>[0]).newInstance(new Object[0]);
                            propDesc.getWriteMethod().invoke(obj, set);
                        }
                        set.add(ops[++i]);
                    } else if (op.charAt(0) == '>') {
                        AbstractChangeTrackingSet set = (AbstractChangeTrackingSet) propDesc.getReadMethod().invoke(obj);
                        set.remove(ops[++i]);
                    } else if (op.charAt(0) == '=') {
                        propDesc.getWriteMethod().invoke(obj, ops[++i]);
                    }
                    break;
                case TrackingMap:
                    if (op.charAt(0) == '<') {
                        AbstractChangeTrackingMap map = (AbstractChangeTrackingMap) propDesc.getReadMethod().invoke(obj);
                        if (map == null) {
                            map = (AbstractChangeTrackingMap) propDesc.getPropertyType().getConstructor(new Class<?>[0]).newInstance(new Object[0]);
                            propDesc.getWriteMethod().invoke(obj, map);
                        }
                        map.put((String) ops[i + 1], ops[i + 2]);
                        i += 2;
                    } else if (op.charAt(0) == '>') {
                        AbstractChangeTrackingMap map = (AbstractChangeTrackingMap) propDesc.getReadMethod().invoke(obj);
                        map.remove((String) ops[++i]);
                    } else if (op.charAt(0) == '=') {
                        propDesc.getWriteMethod().invoke(obj, ops[++i]);
                    }
                    break;
                case TrackingSetMap:
                    if (op.charAt(0) == '<') {
                        AbstractChangeTrackingSetMap setMap = (AbstractChangeTrackingSetMap) propDesc.getReadMethod().invoke(obj);
                        if (setMap == null) {
                            setMap = (AbstractChangeTrackingSetMap) propDesc.getPropertyType().getConstructor(new Class<?>[0]).newInstance(new Object[0]);
                            propDesc.getWriteMethod().invoke(obj, setMap);
                        }
                        setMap.put((String) ops[i + 1], ops[i + 2]);
                        i += 2;
                    } else if (op.charAt(0) == '>') {
                        AbstractChangeTrackingSetMap setMap = (AbstractChangeTrackingSetMap) propDesc.getReadMethod().invoke(obj);
                        setMap.remove((String) ops[i + 1], ops[i + 2]);
                        i += 2;
                    } else if (op.charAt(0) == '=') {
                        propDesc.getWriteMethod().invoke(obj, ops[++i]);
                    }
                    break;
                default:
                    break;
            }
        }
    } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException | SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) ColumnField(com.emc.storageos.db.client.impl.ColumnField) PrefixConstraint(com.emc.storageos.db.client.constraint.PrefixConstraint) AlternateIdConstraint(com.emc.storageos.db.client.constraint.AlternateIdConstraint) ContainmentPrefixConstraint(com.emc.storageos.db.client.constraint.ContainmentPrefixConstraint) ContainmentPermissionsConstraint(com.emc.storageos.db.client.constraint.ContainmentPermissionsConstraint) Constraint(com.emc.storageos.db.client.constraint.Constraint) ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ColumnField (com.emc.storageos.db.client.impl.ColumnField)13 DataObjectType (com.emc.storageos.db.client.impl.DataObjectType)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 ContainmentConstraint (com.emc.storageos.db.client.constraint.ContainmentConstraint)3 ArrayList (java.util.ArrayList)3 DbIndex (com.emc.storageos.db.client.impl.DbIndex)2 IndexColumnName (com.emc.storageos.db.client.impl.IndexColumnName)2 DataObject (com.emc.storageos.db.client.model.DataObject)2 MigrationCallbackException (com.emc.storageos.svcs.errorhandling.resources.MigrationCallbackException)2 JsonObject (com.google.gson.JsonObject)2 Rows (com.netflix.astyanax.model.Rows)2 HashMap (java.util.HashMap)2 com.emc.storageos.db.client.constraint (com.emc.storageos.db.client.constraint)1 AlternateIdConstraint (com.emc.storageos.db.client.constraint.AlternateIdConstraint)1 Constraint (com.emc.storageos.db.client.constraint.Constraint)1 ContainmentPermissionsConstraint (com.emc.storageos.db.client.constraint.ContainmentPermissionsConstraint)1 ContainmentPrefixConstraint (com.emc.storageos.db.client.constraint.ContainmentPrefixConstraint)1 PrefixConstraint (com.emc.storageos.db.client.constraint.PrefixConstraint)1 URIQueryResultList (com.emc.storageos.db.client.constraint.URIQueryResultList)1 ContainmentConstraintImpl (com.emc.storageos.db.client.constraint.impl.ContainmentConstraintImpl)1