List of usage examples for java.util BitSet nextSetBit
public int nextSetBit(int fromIndex)
From source file:edu.uci.ics.hyracks.control.cc.scheduler.ActivityClusterPlanner.java
private Map<TaskId, List<Pair<TaskId, ConnectorDescriptorId>>> computeTaskConnectivity(JobRun jobRun, Map<ActivityId, ActivityPlan> activityPlanMap, Set<ActivityId> activities) { Map<TaskId, List<Pair<TaskId, ConnectorDescriptorId>>> taskConnectivity = new HashMap<TaskId, List<Pair<TaskId, ConnectorDescriptorId>>>(); ActivityClusterGraph acg = jobRun.getActivityClusterGraph(); BitSet targetBitmap = new BitSet(); for (ActivityId ac1 : activities) { ActivityCluster ac = acg.getActivityMap().get(ac1); Task[] ac1TaskStates = activityPlanMap.get(ac1).getTasks(); int nProducers = ac1TaskStates.length; List<IConnectorDescriptor> outputConns = ac.getActivityOutputMap().get(ac1); if (outputConns != null) { for (IConnectorDescriptor c : outputConns) { ConnectorDescriptorId cdId = c.getConnectorId(); ActivityId ac2 = ac.getConsumerActivity(cdId); Task[] ac2TaskStates = activityPlanMap.get(ac2).getTasks(); int nConsumers = ac2TaskStates.length; if (c.allProducersToAllConsumers()) { List<Pair<TaskId, ConnectorDescriptorId>> cInfoList = new ArrayList<Pair<TaskId, ConnectorDescriptorId>>(); for (int j = 0; j < nConsumers; j++) { TaskId targetTID = ac2TaskStates[j].getTaskId(); cInfoList.add(Pair.<TaskId, ConnectorDescriptorId>of(targetTID, cdId)); }//from w ww .j ava 2 s. c om for (int i = 0; i < nProducers; ++i) { taskConnectivity.put(ac1TaskStates[i].getTaskId(), cInfoList); } } else { for (int i = 0; i < nProducers; ++i) { c.indicateTargetPartitions(nProducers, nConsumers, i, targetBitmap); List<Pair<TaskId, ConnectorDescriptorId>> cInfoList = taskConnectivity .get(ac1TaskStates[i].getTaskId()); if (cInfoList == null) { cInfoList = new ArrayList<Pair<TaskId, ConnectorDescriptorId>>(); taskConnectivity.put(ac1TaskStates[i].getTaskId(), cInfoList); } for (int j = targetBitmap.nextSetBit(0); j >= 0; j = targetBitmap.nextSetBit(j + 1)) { TaskId targetTID = ac2TaskStates[j].getTaskId(); cInfoList.add(Pair.<TaskId, ConnectorDescriptorId>of(targetTID, cdId)); } } } } } } return taskConnectivity; }
From source file:org.apache.tez.dag.app.rm.DagAwareYarnTaskScheduler.java
@GuardedBy("this") @Nullable//from ww w . j ava2s . co m private Collection<ContainerId> maybePreempt(Resource freeResources) { if (preemptionPercentage == 0 || numHeartbeats - lastPreemptionHeartbeat < numHeartbeatsBetweenPreemptions) { return null; } if (!requestTracker.isPreemptionDeadlineExpired() && requestTracker.fitsHighestPriorityRequest(freeResources)) { if (numHeartbeats % 50 == 1) { LOG.info("Highest priority request fits in free resources {}", freeResources); } return null; } int numIdleContainers = idleTracker.getNumContainers(); if (numIdleContainers > 0) { if (numHeartbeats % 50 == 1) { LOG.info("Avoiding preemption since there are {} idle containers", numIdleContainers); } return null; } BitSet blocked = requestTracker.createVertexBlockedSet(); if (!blocked.intersects(assignedVertices)) { if (numHeartbeats % 50 == 1) { LOG.info( "Avoiding preemption since there are no descendants of the highest priority requests running"); } return null; } Resource preemptLeft = requestTracker.getAmountToPreempt(preemptionPercentage); if (!resourceCalculator.anyAvailable(preemptLeft)) { if (numHeartbeats % 50 == 1) { LOG.info("Avoiding preemption since amount to preempt is {}", preemptLeft); } return null; } PriorityQueue<HeldContainer> candidates = new PriorityQueue<>(11, PREEMPT_ORDER_COMPARATOR); blocked.and(assignedVertices); for (int i = blocked.nextSetBit(0); i >= 0; i = blocked.nextSetBit(i + 1)) { Collection<HeldContainer> containers = vertexAssignments.get(i); if (containers != null) { candidates.addAll(containers); } else { LOG.error("Vertex {} in assignedVertices but no assignments?", i); } } ArrayList<ContainerId> preemptedContainers = new ArrayList<>(); HeldContainer hc; while ((hc = candidates.poll()) != null) { LOG.info("Preempting container {} currently allocated to task {}", hc.getId(), hc.getAssignedTask()); preemptedContainers.add(hc.getId()); resourceCalculator.deductFrom(preemptLeft, hc.getCapability()); if (!resourceCalculator.anyAvailable(preemptLeft)) { break; } } return preemptedContainers; }
From source file:org.apache.hadoop.hive.ql.optimizer.ConstantPropagateProcFactory.java
private static ExprNodeDesc shortcutFunction(GenericUDF udf, List<ExprNodeDesc> newExprs, Operator<? extends Serializable> op) throws UDFArgumentException { if (udf instanceof GenericUDFOPEqual) { assert newExprs.size() == 2; boolean foundUDFInFirst = false; ExprNodeGenericFuncDesc caseOrWhenexpr = null; if (newExprs.get(0) instanceof ExprNodeGenericFuncDesc) { caseOrWhenexpr = (ExprNodeGenericFuncDesc) newExprs.get(0); if (caseOrWhenexpr.getGenericUDF() instanceof GenericUDFWhen || caseOrWhenexpr.getGenericUDF() instanceof GenericUDFCase) { foundUDFInFirst = true;//w w w.j a va2 s. c o m } } if (!foundUDFInFirst && newExprs.get(1) instanceof ExprNodeGenericFuncDesc) { caseOrWhenexpr = (ExprNodeGenericFuncDesc) newExprs.get(1); if (!(caseOrWhenexpr.getGenericUDF() instanceof GenericUDFWhen || caseOrWhenexpr.getGenericUDF() instanceof GenericUDFCase)) { return null; } } if (null == caseOrWhenexpr) { // we didn't find case or when udf return null; } GenericUDF childUDF = caseOrWhenexpr.getGenericUDF(); List<ExprNodeDesc> children = caseOrWhenexpr.getChildren(); int i; if (childUDF instanceof GenericUDFWhen) { for (i = 1; i < children.size(); i += 2) { children.set(i, ExprNodeGenericFuncDesc.newInstance(new GenericUDFOPEqual(), Lists.newArrayList(children.get(i), newExprs.get(foundUDFInFirst ? 1 : 0)))); } if (children.size() % 2 == 1) { i = children.size() - 1; children.set(i, ExprNodeGenericFuncDesc.newInstance(new GenericUDFOPEqual(), Lists.newArrayList(children.get(i), newExprs.get(foundUDFInFirst ? 1 : 0)))); } // after constant folding of child expression the return type of UDFWhen might have changed, // so recreate the expression ExprNodeGenericFuncDesc newCaseOrWhenExpr = ExprNodeGenericFuncDesc.newInstance(childUDF, caseOrWhenexpr.getFuncText(), children); return newCaseOrWhenExpr; } else if (childUDF instanceof GenericUDFCase) { for (i = 2; i < children.size(); i += 2) { children.set(i, ExprNodeGenericFuncDesc.newInstance(new GenericUDFOPEqual(), Lists.newArrayList(children.get(i), newExprs.get(foundUDFInFirst ? 1 : 0)))); } if (children.size() % 2 == 0) { i = children.size() - 1; children.set(i, ExprNodeGenericFuncDesc.newInstance(new GenericUDFOPEqual(), Lists.newArrayList(children.get(i), newExprs.get(foundUDFInFirst ? 1 : 0)))); } // after constant folding of child expression the return type of UDFCase might have changed, // so recreate the expression ExprNodeGenericFuncDesc newCaseOrWhenExpr = ExprNodeGenericFuncDesc.newInstance(childUDF, caseOrWhenexpr.getFuncText(), children); return newCaseOrWhenExpr; } else { // cant happen return null; } } if (udf instanceof GenericUDFOPAnd) { final BitSet positionsToRemove = new BitSet(); final List<ExprNodeDesc> notNullExprs = new ArrayList<ExprNodeDesc>(); final List<Integer> notNullExprsPositions = new ArrayList<Integer>(); final List<ExprNodeDesc> compareExprs = new ArrayList<ExprNodeDesc>(); for (int i = 0; i < newExprs.size(); i++) { ExprNodeDesc childExpr = newExprs.get(i); if (childExpr instanceof ExprNodeConstantDesc) { ExprNodeConstantDesc c = (ExprNodeConstantDesc) childExpr; if (Boolean.TRUE.equals(c.getValue())) { // if true, prune it positionsToRemove.set(i); } else { // if false, return false return childExpr; } } else if (childExpr instanceof ExprNodeGenericFuncDesc && ((ExprNodeGenericFuncDesc) childExpr).getGenericUDF() instanceof GenericUDFOPNotNull && childExpr.getChildren().get(0) instanceof ExprNodeColumnDesc) { notNullExprs.add(childExpr.getChildren().get(0)); notNullExprsPositions.add(i); } else if (childExpr instanceof ExprNodeGenericFuncDesc && ((ExprNodeGenericFuncDesc) childExpr).getGenericUDF() instanceof GenericUDFBaseCompare && !(((ExprNodeGenericFuncDesc) childExpr).getGenericUDF() instanceof GenericUDFOPNotEqual) && childExpr.getChildren().size() == 2) { // Try to fold (key <op> 86) and (key is not null) to (key <op> 86) // where <op> can be "=", ">=", "<=", ">", "<". // Note: (key <> 86) and (key is not null) cannot be folded ExprNodeColumnDesc colDesc = ExprNodeDescUtils.getColumnExpr(childExpr.getChildren().get(0)); if (null == colDesc) { colDesc = ExprNodeDescUtils.getColumnExpr(childExpr.getChildren().get(1)); } if (colDesc != null) { compareExprs.add(colDesc); } } } // Try to fold (key = 86) and (key is not null) to (key = 86) for (int i = 0; i < notNullExprs.size(); i++) { for (ExprNodeDesc other : compareExprs) { if (notNullExprs.get(i).isSame(other)) { positionsToRemove.set(notNullExprsPositions.get(i)); break; } } } // Remove unnecessary expressions int pos = 0; int removed = 0; while ((pos = positionsToRemove.nextSetBit(pos)) != -1) { newExprs.remove(pos - removed); pos++; removed++; } if (newExprs.size() == 0) { return new ExprNodeConstantDesc(TypeInfoFactory.booleanTypeInfo, Boolean.TRUE); } if (newExprs.size() == 1) { return newExprs.get(0); } } if (udf instanceof GenericUDFOPOr) { final BitSet positionsToRemove = new BitSet(); for (int i = 0; i < newExprs.size(); i++) { ExprNodeDesc childExpr = newExprs.get(i); if (childExpr instanceof ExprNodeConstantDesc) { ExprNodeConstantDesc c = (ExprNodeConstantDesc) childExpr; if (Boolean.FALSE.equals(c.getValue())) { // if false, prune it positionsToRemove.set(i); } else if (Boolean.TRUE.equals(c.getValue())) { // if true return true return childExpr; } } } int pos = 0; int removed = 0; while ((pos = positionsToRemove.nextSetBit(pos)) != -1) { newExprs.remove(pos - removed); pos++; removed++; } if (newExprs.size() == 0) { return new ExprNodeConstantDesc(TypeInfoFactory.booleanTypeInfo, Boolean.FALSE); } if (newExprs.size() == 1) { return newExprs.get(0); } } if (udf instanceof GenericUDFWhen) { if (!(newExprs.size() == 2 || newExprs.size() == 3)) { // In general, when can have unlimited # of branches, // we currently only handle either 1 or 2 branch. return null; } ExprNodeDesc thenExpr = newExprs.get(1); ExprNodeDesc elseExpr = newExprs.size() == 3 ? newExprs.get(2) : new ExprNodeConstantDesc(newExprs.get(1).getTypeInfo(), null); ExprNodeDesc whenExpr = newExprs.get(0); if (whenExpr instanceof ExprNodeConstantDesc) { Boolean whenVal = (Boolean) ((ExprNodeConstantDesc) whenExpr).getValue(); return (whenVal == null || Boolean.FALSE.equals(whenVal)) ? elseExpr : thenExpr; } if (thenExpr instanceof ExprNodeConstantDesc && elseExpr instanceof ExprNodeConstantDesc) { ExprNodeConstantDesc constThen = (ExprNodeConstantDesc) thenExpr; ExprNodeConstantDesc constElse = (ExprNodeConstantDesc) elseExpr; Object thenVal = constThen.getValue(); Object elseVal = constElse.getValue(); if (thenVal == null) { if (elseVal == null) { // both branches are null. return thenExpr; } else if (op instanceof FilterOperator) { // we can still fold, since here null is equivalent to false. return Boolean.TRUE.equals(elseVal) ? ExprNodeGenericFuncDesc.newInstance(new GenericUDFOPNot(), newExprs.subList(0, 1)) : Boolean.FALSE.equals(elseVal) ? elseExpr : null; } else { // can't do much, expression is not in context of filter, so we can't treat null as equivalent to false here. return null; } } else if (elseVal == null && op instanceof FilterOperator) { return Boolean.TRUE.equals(thenVal) ? whenExpr : Boolean.FALSE.equals(thenVal) ? thenExpr : null; } else if (thenVal.equals(elseVal)) { return thenExpr; } else if (thenVal instanceof Boolean && elseVal instanceof Boolean) { List<ExprNodeDesc> children = new ArrayList<>(); children.add(whenExpr); children.add(new ExprNodeConstantDesc(false)); ExprNodeGenericFuncDesc func = ExprNodeGenericFuncDesc.newInstance(new GenericUDFNvl(), children); if (Boolean.TRUE.equals(thenVal)) { return func; } else { List<ExprNodeDesc> exprs = new ArrayList<>(); exprs.add(func); return ExprNodeGenericFuncDesc.newInstance(new GenericUDFOPNot(), exprs); } } else { return null; } } } if (udf instanceof GenericUDFCase) { // HIVE-9644 Attempt to fold expression like : // where (case ss_sold_date when '1998-01-01' then 1=1 else null=1 end); // where ss_sold_date= '1998-01-01' ; if (!(newExprs.size() == 3 || newExprs.size() == 4)) { // In general case can have unlimited # of branches, // we currently only handle either 1 or 2 branch. return null; } ExprNodeDesc thenExpr = newExprs.get(2); ExprNodeDesc elseExpr = newExprs.size() == 4 ? newExprs.get(3) : new ExprNodeConstantDesc(newExprs.get(2).getTypeInfo(), null); if (thenExpr instanceof ExprNodeConstantDesc && elseExpr instanceof ExprNodeConstantDesc) { ExprNodeConstantDesc constThen = (ExprNodeConstantDesc) thenExpr; ExprNodeConstantDesc constElse = (ExprNodeConstantDesc) elseExpr; Object thenVal = constThen.getValue(); Object elseVal = constElse.getValue(); if (thenVal == null) { if (null == elseVal) { return thenExpr; } else if (op instanceof FilterOperator) { return Boolean.TRUE.equals(elseVal) ? ExprNodeGenericFuncDesc.newInstance(new GenericUDFOPNotEqual(), newExprs.subList(0, 2)) : Boolean.FALSE.equals(elseVal) ? elseExpr : null; } else { return null; } } else if (null == elseVal && op instanceof FilterOperator) { return Boolean.TRUE.equals(thenVal) ? ExprNodeGenericFuncDesc.newInstance(new GenericUDFOPEqual(), newExprs.subList(0, 2)) : Boolean.FALSE.equals(thenVal) ? thenExpr : null; } else if (thenVal.equals(elseVal)) { return thenExpr; } else if (thenVal instanceof Boolean && elseVal instanceof Boolean) { ExprNodeGenericFuncDesc equal = ExprNodeGenericFuncDesc.newInstance(new GenericUDFOPEqual(), newExprs.subList(0, 2)); List<ExprNodeDesc> children = new ArrayList<>(); children.add(equal); children.add(new ExprNodeConstantDesc(false)); ExprNodeGenericFuncDesc func = ExprNodeGenericFuncDesc.newInstance(new GenericUDFNvl(), children); if (Boolean.TRUE.equals(thenVal)) { return func; } else { List<ExprNodeDesc> exprs = new ArrayList<>(); exprs.add(func); return ExprNodeGenericFuncDesc.newInstance(new GenericUDFOPNot(), exprs); } } else { return null; } } } if (udf instanceof GenericUDFUnixTimeStamp) { if (newExprs.size() >= 1) { // unix_timestamp(args) -> to_unix_timestamp(args) return ExprNodeGenericFuncDesc.newInstance(new GenericUDFToUnixTimeStamp(), newExprs); } } return null; }
From source file:org.alfresco.module.org_alfresco_module_rm.capability.RMAfterInvocationProvider.java
private Object[] decide(Authentication authentication, Object object, ConfigAttributeDefinition config, Object[] returnedObject) { // Assumption: value is not null BitSet incudedSet = new BitSet(returnedObject.length); List<ConfigAttributeDefintion> supportedDefinitions = extractSupportedDefinitions(config); if (supportedDefinitions.size() == 0) { return returnedObject; }/* w w w . java2s. com*/ for (int i = 0, l = returnedObject.length; i < l; i++) { Object current = returnedObject[i]; int parentReadCheck = checkRead(getParentReadCheckNode(current)); int childReadChek = checkRead(getChildReadCheckNode(current)); for (ConfigAttributeDefintion cad : supportedDefinitions) { incudedSet.set(i, true); NodeRef testNodeRef = null; if (cad.parent) { if (StoreRef.class.isAssignableFrom(current.getClass())) { testNodeRef = null; } else if (NodeRef.class.isAssignableFrom(current.getClass())) { testNodeRef = nodeService.getPrimaryParent((NodeRef) current).getParentRef(); } else if (ChildAssociationRef.class.isAssignableFrom(current.getClass())) { testNodeRef = ((ChildAssociationRef) current).getParentRef(); } else if (PermissionCheckValue.class.isAssignableFrom(current.getClass())) { NodeRef nodeRef = ((PermissionCheckValue) current).getNodeRef(); testNodeRef = nodeService.getPrimaryParent(nodeRef).getParentRef(); } else { throw new ACLEntryVoterException( "The specified parameter is recognized: " + current.getClass()); } } else { if (StoreRef.class.isAssignableFrom(current.getClass())) { testNodeRef = nodeService.getRootNode((StoreRef) current); } else if (NodeRef.class.isAssignableFrom(current.getClass())) { testNodeRef = (NodeRef) current; } else if (ChildAssociationRef.class.isAssignableFrom(current.getClass())) { testNodeRef = ((ChildAssociationRef) current).getChildRef(); } else if (PermissionCheckValue.class.isAssignableFrom(current.getClass())) { testNodeRef = ((PermissionCheckValue) current).getNodeRef(); } else { throw new ACLEntryVoterException( "The specified parameter is recognized: " + current.getClass()); } } if (logger.isDebugEnabled()) { logger.debug("\t" + cad.typeString + " test on " + testNodeRef + " from " + current.getClass().getName()); } if (isUnfiltered(testNodeRef)) { continue; } int readCheck = childReadChek; if (cad.parent) { readCheck = parentReadCheck; } if (incudedSet.get(i) && (testNodeRef != null) && (readCheck != AccessDecisionVoter.ACCESS_GRANTED)) { incudedSet.set(i, false); } } } if (incudedSet.cardinality() == returnedObject.length) { return returnedObject; } else { Object[] answer = new Object[incudedSet.cardinality()]; for (int i = incudedSet.nextSetBit(0), p = 0; i >= 0; i = incudedSet.nextSetBit(++i), p++) { answer[p] = returnedObject[i]; } return answer; } }
From source file:org.alfresco.repo.security.permissions.impl.acegi.ACLEntryAfterInvocationProvider.java
@SuppressWarnings("rawtypes") private Object[] decide(Authentication authentication, Object object, ConfigAttributeDefinition config, Object[] returnedObject) throws AccessDeniedException { // Assumption: value is not null BitSet incudedSet = new BitSet(returnedObject.length); List<ConfigAttributeDefintion> supportedDefinitions = extractSupportedDefinitions(config); if (supportedDefinitions.size() == 0) { return returnedObject; }/* w w w . j av a 2s. co m*/ for (int i = 0, l = returnedObject.length; i < l; i++) { Object current = returnedObject[i]; for (ConfigAttributeDefintion cad : supportedDefinitions) { incudedSet.set(i, true); NodeRef testNodeRef = null; if (cad.typeString.equals(AFTER_ACL_NODE)) { if (StoreRef.class.isAssignableFrom(current.getClass())) { testNodeRef = nodeService.getRootNode((StoreRef) current); } else if (NodeRef.class.isAssignableFrom(current.getClass())) { testNodeRef = (NodeRef) current; } else if (ChildAssociationRef.class.isAssignableFrom(current.getClass())) { testNodeRef = ((ChildAssociationRef) current).getChildRef(); } else if (Pair.class.isAssignableFrom(current.getClass())) { testNodeRef = (NodeRef) ((Pair) current).getSecond(); } else if (PermissionCheckValue.class.isAssignableFrom(current.getClass())) { testNodeRef = ((PermissionCheckValue) current).getNodeRef(); } else { throw new ACLEntryVoterException( "The specified parameter is recognized: " + current.getClass()); } } else if (cad.typeString.equals(AFTER_ACL_PARENT)) { if (StoreRef.class.isAssignableFrom(current.getClass())) { testNodeRef = null; } else if (NodeRef.class.isAssignableFrom(current.getClass())) { testNodeRef = nodeService.getPrimaryParent((NodeRef) current).getParentRef(); } else if (ChildAssociationRef.class.isAssignableFrom(current.getClass())) { testNodeRef = ((ChildAssociationRef) current).getParentRef(); } else if (Pair.class.isAssignableFrom(current.getClass())) { testNodeRef = (NodeRef) ((Pair) current).getSecond(); } else if (PermissionCheckValue.class.isAssignableFrom(current.getClass())) { NodeRef nodeRef = ((PermissionCheckValue) current).getNodeRef(); testNodeRef = nodeService.getPrimaryParent(nodeRef).getParentRef(); } else { throw new ACLEntryVoterException( "The specified parameter is recognized: " + current.getClass()); } } if (log.isDebugEnabled()) { log.debug("\t" + cad.typeString + " test on " + testNodeRef + " from " + current.getClass().getName()); } if (isUnfiltered(testNodeRef)) { continue; } if (incudedSet.get(i) && (testNodeRef != null) && (permissionService.hasPermission(testNodeRef, cad.required.toString()) == AccessStatus.DENIED)) { incudedSet.set(i, false); } } } if (incudedSet.cardinality() == returnedObject.length) { return returnedObject; } else { Object[] answer = new Object[incudedSet.cardinality()]; for (int i = incudedSet.nextSetBit(0), p = 0; i >= 0; i = incudedSet.nextSetBit(++i), p++) { answer[p] = returnedObject[i]; } return answer; } }
From source file:com.cohort.util.String2.java
/** * This displays the contents of a bitSet as a String. * * @param bitSet/*from w w w . j a va2 s . c o m*/ * @return the corresponding String (the 'true' bits, comma separated) */ public static String toString(BitSet bitSet) { if (bitSet == null) return null; StringBuilder sb = new StringBuilder(1024); String separator = ""; int i = bitSet.nextSetBit(0); while (i >= 0) { sb.append(separator + i); separator = ", "; i = bitSet.nextSetBit(i + 1); } return sb.toString(); }