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);
}
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);
}
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();
}
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);
}
}
}
}
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);
}
}
}
Aggregations