Search in sources :

Example 16 with Constraint

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

the class DbIndexTest method testNamedRelationIndex.

@Test
public void testNamedRelationIndex() {
    URI id = URIUtil.createId(Project.class);
    URI pid0 = URIUtil.createId(TenantOrg.class);
    URI pid1 = URIUtil.createId(TenantOrg.class);
    String lbl0 = "abcd1234";
    String lbl1 = "efgh5678";
    Constraint constraint0 = ContainmentPrefixConstraint.Factory.getProjectUnderTenantConstraint(pid0, lbl0);
    Constraint constraint1 = ContainmentPrefixConstraint.Factory.getProjectUnderTenantConstraint(pid1, lbl1);
    {
        Project obj = new Project();
        obj.setId(id);
        obj.setLabel(lbl0);
        obj.setTenantOrg(new NamedURI(pid0, lbl0));
        _dbClient.createObject(obj);
    }
    verifyContain(constraint0, id, 1);
    verifyContain(constraint1, null, 0);
    {
        Project obj = _dbClient.queryObject(Project.class, id);
        obj.setLabel(lbl1);
        obj.setTenantOrg(new NamedURI(pid1, lbl1));
        _dbClient.persistObject(obj);
    }
    verifyContain(constraint1, id, 1);
    verifyContain(constraint0, null, 0);
}
Also used : 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) URI(java.net.URI) Test(org.junit.Test)

Example 17 with Constraint

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

the class GeoTest method testQueryByConstraintWithPageinate.

private void testQueryByConstraintWithPageinate() throws Exception {
    Constraint constraint = PrefixConstraint.Factory.getConstraint(VirtualArray.class, "label", "fo");
    URI startId = null;
    int count = 0;
    while (true) {
        URIQueryResultList ret = new URIQueryResultList();
        geoClient.queryByConstraint(constraint, ret, startId, 3);
        Iterator<URI> it = ret.iterator();
        if (!it.hasNext()) {
            // reach the end
            break;
        }
        while (it.hasNext()) {
            startId = it.next();
            VirtualArray obj = geoClient.queryObject(VirtualArray.class, startId);
            if (obj.getInactive() == false) {
                count++;
            }
        }
    }
    Assert.assertEquals(virtualArrayCount, count);
}
Also used : Constraint(com.emc.storageos.db.client.constraint.Constraint) PrefixConstraint(com.emc.storageos.db.client.constraint.PrefixConstraint) AlternateIdConstraint(com.emc.storageos.db.client.constraint.AlternateIdConstraint) URI(java.net.URI) Constraint(com.emc.storageos.db.client.constraint.Constraint) PrefixConstraint(com.emc.storageos.db.client.constraint.PrefixConstraint) AlternateIdConstraint(com.emc.storageos.db.client.constraint.AlternateIdConstraint) URIQueryResultList(com.emc.storageos.db.client.constraint.URIQueryResultList)

Example 18 with Constraint

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

the class TaskUtils method findPendingTasksForResource.

/**
 * returns pending tasks for a resource
 *
 * @param dbClient
 * @param resourceId
 * @return
 */
public static Iterator<Task> findPendingTasksForResource(DbClient dbClient, URI resourceId, URI tenantId) {
    Constraint constraint = AggregatedConstraint.Factory.getAggregationConstraint(Task.class, "tenant", tenantId.toString(), "taskStatus");
    AggregationQueryResultList queryResults = new AggregationQueryResultList();
    dbClient.queryByConstraint(constraint, queryResults);
    Iterator<AggregationQueryResultList.AggregatedEntry> it = queryResults.iterator();
    List<URI> pendingTasks = new ArrayList<URI>();
    while (it.hasNext()) {
        AggregationQueryResultList.AggregatedEntry entry = it.next();
        if (entry.getValue().equals(Task.Status.pending.name())) {
            pendingTasks.add(entry.getId());
        }
    }
    List<Task> pendingTasksForResource = new ArrayList<Task>();
    Iterator<Task> pendingItr = dbClient.queryIterativeObjects(Task.class, pendingTasks);
    while (pendingItr.hasNext()) {
        Task task = pendingItr.next();
        if (task.getResource().getURI().equals(resourceId)) {
            pendingTasksForResource.add(task);
        }
    }
    return pendingTasksForResource.iterator();
}
Also used : Task(com.emc.storageos.db.client.model.Task) AggregatedConstraint(com.emc.storageos.db.client.constraint.AggregatedConstraint) Constraint(com.emc.storageos.db.client.constraint.Constraint) ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint) AlternateIdConstraint(com.emc.storageos.db.client.constraint.AlternateIdConstraint) AggregationQueryResultList(com.emc.storageos.db.client.constraint.AggregationQueryResultList) ArrayList(java.util.ArrayList) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI)

Example 19 with Constraint

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

the class Joiner method joinBid2A.

/**
 * Join A to B where B's id is in A's joinToField
 * Here the assumption is that we have already computed the necessary A's,
 * either by previous joins or selections on A columns, as the normal case.
 * If the number of potential As is less than the number of potential Bs,
 * we will just find all the Bs in the As by iterating.
 * Otherwise if the number of potential Bs ls less than the number of potential As,
 * we use a containment constraint to find the A's containing each B and eliminate
 * any A's found that are not in the A result set.
 *
 * @param jc
 */
private void joinBid2A(JClass jc, Set<URI> selectionUris) {
    JClass joinToClass = lookupAlias(jc.joinToAlias);
    String joinToField = jc.getJoinToField();
    Set<URI> bURIs = selectionUris;
    if (bURIs == null) {
        bURIs = engine.queryByType(jc.getClazz());
    }
    jc.setCacheValid(true);
    // Determine if A's joinToField is indexed.
    boolean aIndexed = joinToClass.getMetaData().isIndexed(joinToField);
    if (!aIndexed || joinToClass.getUris().size() < bURIs.size()) {
        // Common case; A is already bounded smaller than B universe.
        // Therefore we iterate through the As and select only the corresponding Bs.
        Iterator aIter = joinToClass.iterator(engine);
        while (aIter.hasNext()) {
            DataObject object = (DataObject) aIter.next();
            Method method = getGettr(joinToClass, joinToField);
            if (method == null) {
                throw new JoinerException("Cannot find gettr for join: " + jc.getField());
            }
            Object values = null;
            try {
                values = method.invoke(object);
            } catch (Exception ex) {
                log.warn("failed to invoke {} ", method.getName());
            }
            for (URI uri : bURIs) {
                if (uriInObject(uri, values)) {
                    DataObject bobj = engine.queryObject(jc.getClazz(), uri);
                    if (testSelections(jc, bobj) == false) {
                        continue;
                    }
                    jc.addToJoinMap(object.getId(), uri);
                    jc.getUris().add(uri);
                    jc.addToCache(bobj);
                }
            }
        }
    } else {
        // So use a constraint query.
        for (URI bURI : bURIs) {
            Constraint constraint = joinToClass.getMetaData().buildConstraint(bURI, joinToClass.getClazz(), joinToField);
            Set<URI> aURIs = engine.queryByConstraint(constraint);
            for (URI aURI : aURIs) {
                // If aURI is not in the result set of A, then skip it
                if (!joinToClass.getUris().contains(aURI)) {
                    continue;
                }
                DataObject object = (DataObject) engine.queryObject(jc.getClazz(), bURI);
                if (testSelections(jc, object) == false) {
                    continue;
                }
                jc.addToJoinMap(aURI, bURI);
                jc.addToCache(object);
                jc.getUris().add(bURI);
            }
        }
    }
}
Also used : DataObject(com.emc.storageos.db.client.model.DataObject) Constraint(com.emc.storageos.db.client.constraint.Constraint) Iterator(java.util.Iterator) DataObject(com.emc.storageos.db.client.model.DataObject) Method(java.lang.reflect.Method) URI(java.net.URI)

Example 20 with Constraint

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

the class Joiner method joinAid2B.

/**
 * Join B to A where A's ID is in B's "field"
 * B's field must be indexed.
 * Here we find only the Bs that contain A in the specified field using
 * a containment constraint (i.e. the id of A is contained in B's field).
 *
 * @param jc
 */
private void joinAid2B(JClass jc, Set<URI> selectionUris) {
    JClass joinToClass = lookupAlias(jc.joinToAlias);
    Set<URI> joinToUris = joinToClass.getUris();
    boolean manualMatch = false;
    jc.setCacheValid(true);
    for (URI aURI : joinToUris) {
        Set<URI> bURIs = null;
        if (jc.getMetaData().isIndexed(jc.getField())) {
            Constraint constraint = jc.getMetaData().buildConstraint(aURI, jc.getClazz(), jc.getField());
            bURIs = engine.queryByConstraint(constraint);
        } else {
            log.info(String.format("Joiner suboptimal query %s.%s should be indexed to join to %s", jc.getClazz().getSimpleName(), jc.getField(), joinToClass.getClazz().getSimpleName()));
            bURIs = engine.queryByType(jc.getClazz());
            manualMatch = true;
        }
        for (URI bURI : bURIs) {
            // Skip any objects that will not pass selection index
            if (selectionUris != null && !selectionUris.contains(bURI)) {
                continue;
            }
            DataObject object = (DataObject) engine.queryObject(jc.getClazz(), bURI);
            // If the join field was not indexed, manually match A's UID against the B field.
            if (manualMatch) {
                Method method = getGettr(jc, jc.getField());
                if (method == null) {
                    throw new JoinerException("Cannot find gettr for join: " + jc.getField());
                }
                Object values = null;
                try {
                    values = method.invoke(object);
                } catch (Exception ex) {
                    log.warn("failed to invoke method {}", method.getName());
                }
                if (!uriInObject(aURI, values)) {
                    continue;
                }
            }
            if (testSelections(jc, object) == false) {
                continue;
            }
            jc.addToJoinMap(aURI, bURI);
            jc.addToCache(object);
            jc.getUris().add(bURI);
        }
    }
}
Also used : DataObject(com.emc.storageos.db.client.model.DataObject) Constraint(com.emc.storageos.db.client.constraint.Constraint) DataObject(com.emc.storageos.db.client.model.DataObject) Method(java.lang.reflect.Method) URI(java.net.URI)

Aggregations

Constraint (com.emc.storageos.db.client.constraint.Constraint)34 ContainmentConstraint (com.emc.storageos.db.client.constraint.ContainmentConstraint)25 AlternateIdConstraint (com.emc.storageos.db.client.constraint.AlternateIdConstraint)24 URI (java.net.URI)23 PrefixConstraint (com.emc.storageos.db.client.constraint.PrefixConstraint)18 ContainmentPermissionsConstraint (com.emc.storageos.db.client.constraint.ContainmentPermissionsConstraint)16 ContainmentPrefixConstraint (com.emc.storageos.db.client.constraint.ContainmentPrefixConstraint)16 URIQueryResultList (com.emc.storageos.db.client.constraint.URIQueryResultList)14 AggregatedConstraint (com.emc.storageos.db.client.constraint.AggregatedConstraint)13 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)8 NamedURI (com.emc.storageos.db.client.model.NamedURI)7 Volume (com.emc.storageos.db.client.model.Volume)5 AggregationQueryResultList (com.emc.storageos.db.client.constraint.AggregationQueryResultList)4 Project (com.emc.storageos.db.client.model.Project)4 ProtectionSet (com.emc.storageos.db.client.model.ProtectionSet)4 BlockSnapshot (com.emc.storageos.db.client.model.BlockSnapshot)3 DataObject (com.emc.storageos.db.client.model.DataObject)3 TenantOrg (com.emc.storageos.db.client.model.TenantOrg)3 BlockSnapshotSession (com.emc.storageos.db.client.model.BlockSnapshotSession)2