Java Code Examples for java.util.Deque#poll()

The following examples show how to use java.util.Deque#poll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PullBuffer.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public void doCleanWaitQueue(Deque<DelayRequest> waitQueue) {
    while (!waitQueue.isEmpty()) {
        DelayRequest r = waitQueue.peek();
        if (r == null) break;
        if (r.isExpired()) {
            r.timeout();
        }
        if (r.isFinished()) {
            r = waitQueue.poll();
            if (r != null && !r.isFinished()) {
                waitQueue.addFirst(r);
            }
        } else {
            break;
        }
    }
}
 
Example 2
Source File: TreeView.java    From ThinkMap with Apache License 2.0 6 votes vote down vote up
/**
 * 添加所有的NoteView
 */
private void addNoteViews() {
    if (mTreeModel != null) {
        NodeModel<String> rootNode = mTreeModel.getRootNode();
        Deque<NodeModel<String>> deque = new ArrayDeque<>();
        deque.add(rootNode);
        while (!deque.isEmpty()) {
            NodeModel<String> poll = deque.poll();

            addNodeViewToGroup(poll);

            LinkedList<NodeModel<String>> childNodes = poll.getChildNodes();
            for (NodeModel<String> ch : childNodes) {
                deque.push(ch);
            }
        }
    }
}
 
Example 3
Source File: AmbientBrightnessStatsTracker.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private AmbientBrightnessDayStats getOrCreateDayStats(
        Deque<AmbientBrightnessDayStats> userStats, LocalDate localDate) {
    AmbientBrightnessDayStats lastBrightnessStats = userStats.peekLast();
    if (lastBrightnessStats != null && lastBrightnessStats.getLocalDate().equals(
            localDate)) {
        return lastBrightnessStats;
    } else {
        AmbientBrightnessDayStats dayStats = new AmbientBrightnessDayStats(localDate,
                BUCKET_BOUNDARIES_FOR_NEW_STATS);
        if (userStats.size() == MAX_DAYS_TO_TRACK) {
            userStats.poll();
        }
        userStats.offer(dayStats);
        return dayStats;
    }
}
 
Example 4
Source File: PredicatedHandlersParser.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
private static List<Token> readArrayType(final String string, final Deque<Token> tokens, String expectedEndToken) {
    final List<Token> values = new ArrayList<>();
    Token token = tokens.poll();
    if(token.getToken().equals(expectedEndToken)) {
        return Collections.emptyList();
    }
    while (token != null) {
        Token commaOrEnd = tokens.poll();
        values.add(token);
        if (commaOrEnd.getToken().equals(expectedEndToken)) {
            return values;
        } else if (!commaOrEnd.getToken().equals(",")) {
            throw error(string, commaOrEnd.getPosition(), "expected either , or " + expectedEndToken);
        }
        token = tokens.poll();
    }
    throw error(string, string.length(), "unexpected end of input in array");
}
 
Example 5
Source File: BFSAdjacencyMatrix.java    From algorithmic-programming with MIT License 5 votes vote down vote up
public int[] printNodesInBFSOrder(int graph[][], int startingNode) {

        // to track visited nodes
        Set<Integer> visitedSet = new HashSet<>();

        Deque<Integer> queue = new LinkedList<>();
        queue.add(startingNode);
        visitedSet.add(startingNode);

        int result[] = new int[graph.length];
        int runner = 0;

        while (!queue.isEmpty()) {

            int current = queue.poll();
            result[runner ++] = current;

            for (int i=0; i<graph[current].length; i++) {
                // check if edge exists
                if (graph[current][i] == 1) {
                    // check if already visited
                    if (visitedSet.add(i)) {
                        queue.add(i);
                    }
                }
            }

        }

        return result;
    }
 
Example 6
Source File: ConverterPagerAdapter.java    From power-adapters with Apache License 2.0 5 votes vote down vote up
@Nullable
View get(@NonNull Object itemViewType) {
    Deque<View> views = mViews.get(itemViewType);
    if (views == null) {
        return null;
    }
    return views.poll();
}
 
Example 7
Source File: PullBuffer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void offer(UpstreamJob job) {
    buffer.offer(job);

    if (!job.canProcessDelayRequest()) { // current thread do not process delay request.
        return;
    }

    String topic = job.getTopic();
    Deque<DelayRequest> waitQueue = waitQueueMap.get(topic);
    if (waitQueue == null) {
        waitQueue = waitQueueMap.get(EMPTY_TOPIC);
    }
    if (waitQueue == null) return;

    while (true) {
        DelayRequest request = waitQueue.poll();
        if (request == null) break;
        if (request.isFinished()) continue;
        synchronized (request) { // lock this request so that timeout-checker won't process it.
            if (request.isFinished()) continue;
            List<Message> messages = pull(request.getContext(), request.getRequest().getMaxBatchSize());
            if (CollectionUtils.isNotEmpty(messages)) {
                request.response(messages);
            } else {
                waitQueue.addFirst(request);
            }
        }
        break;
    }
}
 
Example 8
Source File: BinarySearchTreeTest.java    From Algorithms with MIT License 5 votes vote down vote up
static void levelOrder(List<Integer> lst, TestTreeNode node) {

    Deque<TestTreeNode> q = new ArrayDeque<>();
    if (node != null) q.offer(node);

    while (!q.isEmpty()) {

      node = q.poll();
      lst.add(node.data);
      if (node.left != null) q.offer(node.left);
      if (node.right != null) q.offer(node.right);
    }
  }
 
Example 9
Source File: AsyncSink.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
private void drainOnOperatorThread() {
  Deque<Message> batchOfCompletedFutures = completed.drainAll();
  Reductions reductions = this.reductions.get();
  Message message;
  while ((message = batchOfCompletedFutures.poll()) != null) {
    backPressureValve.notifyAsyncOperationCompleted(message.target());
    reductions.enqueue(message);
  }
  reductions.processEnvelopes();
}
 
Example 10
Source File: LinkedDequeTest.java    From concurrentlinkedhashmap with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "warmedDeque")
public void poll_toEmpty(Deque<SimpleLinkedValue> deque) {
  SimpleLinkedValue value;
  while ((value = deque.poll()) != null) {
    assertThat(deque.contains(value), is(false));
  }
  assertThat(deque, is(emptyCollection()));
}
 
Example 11
Source File: Dependencies.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find the dependencies of a class, using the current
 * {@link Dependencies#getFinder finder} and
 * {@link Dependencies#getFilter filter}.
 * The search may optionally include the transitive closure of all the
 * filtered dependencies, by also searching in the classes named in those
 * dependencies.
 * @param classFinder a finder to locate class files
 * @param rootClassNames the names of the root classes from which to begin
 *      searching
 * @param transitiveClosure whether or not to also search those classes
 *      named in any filtered dependencies that are found.
 * @param recorder a recorder for handling the results
 * @throws ClassFileNotFoundException if a required class file cannot be found
 * @throws ClassFileError if an error occurs while processing a class file,
 *      such as an error in the internal class file structure.
 */
public void findAllDependencies(
        ClassFileReader classFinder, Set<String> rootClassNames,
        boolean transitiveClosure, Recorder recorder)
        throws ClassFileNotFoundException {
    Set<String> doneClasses = new HashSet<String>();

    getFinder();  // ensure initialized
    getFilter();  // ensure initialized

    // Work queue of names of classfiles to be searched.
    // Entries will be unique, and for classes that do not yet have
    // dependencies in the results map.
    Deque<String> deque = new LinkedList<String>(rootClassNames);

    String className;
    while ((className = deque.poll()) != null) {
        assert (!doneClasses.contains(className));
        doneClasses.add(className);

        ClassFile cf = classFinder.getClassFile(className);

        // The following code just applies the filter to the dependencies
        // followed for the transitive closure.
        for (Dependency d: finder.findDependencies(cf)) {
            recorder.addDependency(d);
            if (transitiveClosure && filter.accepts(d)) {
                String cn = d.getTarget().getClassName();
                if (!doneClasses.contains(cn))
                    deque.add(cn);
            }
        }
    }
}
 
Example 12
Source File: PredicatedHandlersParser.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
private static Node handleSingleArrayValue(final String string, final Token builder, final Deque<Token> tokens, String endChar) {
    List<Token> array = readArrayType(string, tokens, "}");
    Token close = tokens.poll();
    if (!close.getToken().equals(endChar)) {
        throw error(string, close.getPosition(), "expected " + endChar);
    }
    return new ExpressionNode(builder, Collections.<String, Node>singletonMap(null, new ArrayNode(builder, array)));
}
 
Example 13
Source File: PluginManager.java    From plugin-installation-manager-tool with MIT License 4 votes vote down vote up
private Map<String, Plugin> resolveRecursiveDependencies(Plugin plugin, @CheckForNull Map<String, Plugin> topLevelDependencies) {
    Deque<Plugin> queue = new LinkedList<>();
    Map<String, Plugin> recursiveDependencies = new HashMap<>();
    queue.add(plugin);
    recursiveDependencies.put(plugin.getName(), plugin);

    while (queue.size() != 0) {
        Plugin dependency = queue.poll();

        if (!dependency.isDependenciesSpecified()) {
            dependency.setDependencies(resolveDirectDependencies(dependency));
        }

        for (Plugin p : dependency.getDependencies()) {
            String dependencyName = p.getName();
            Plugin pinnedPlugin = topLevelDependencies != null ? topLevelDependencies.get(dependencyName) : null;

            // See https://github.com/jenkinsci/plugin-installation-manager-tool/pull/102
            if (pinnedPlugin != null) { // There is a top-level plugin with the same ID
                if (pinnedPlugin.getVersion().isNewerThanOrEqualTo(p.getVersion())) {
                    if (verbose) {
                        logVerbose(String.format("Skipping dependency %s:%s and its sub-dependencies, because there is a higher version defined on the top level - %s:%s",
                                p.getName(), p.getVersion(), pinnedPlugin.getName(), pinnedPlugin.getVersion()));
                    }
                    continue;
                } else {
                    String message = String.format("Plugin %s:%s depends on %s:%s, but there is an older version defined on the top level - %s:%s",
                            plugin.getName(), plugin.getVersion(), p.getName(), p.getVersion(), pinnedPlugin.getName(), pinnedPlugin.getVersion());
                    // TODO(oleg_nenashev): Should be an error by default, but it is not how the tests are written now
                    // throw new PluginDependencyStrategyException(message);
                    logVerbose(message);
                }
            }

            if (!recursiveDependencies.containsKey(dependencyName)) {
                recursiveDependencies.put(dependencyName, p);
                queue.add(p);
            } else {
                Plugin existingDependency = recursiveDependencies.get(dependencyName);
                if (existingDependency.getVersion().isOlderThan(p.getVersion())) {
                    outputPluginReplacementInfo(existingDependency, p);
                    queue.add(p); //in case the higher version contains dependencies the lower version didn't have
                    recursiveDependencies.replace(dependencyName, existingDependency, p);
                }
            }
        }
    }
    return recursiveDependencies;
}
 
Example 14
Source File: ElementSelectorPanel.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void doInitialExpansion( int howMuch ) {
    
    Node root = getExplorerManager().getRootContext();
    Node[] subNodes = root.getChildren().getNodes(true);
    
    if ( subNodes == null ) {
        return;
    }
    Node toSelect = null;
    
    int row = 0;

    boolean oldScroll = elementView.getScrollsOnExpand();
    elementView.setScrollsOnExpand( false );
    
    for( int i = 0; subNodes != null && i < (howMuch == - 1 || howMuch > subNodes.length ? subNodes.length : howMuch ) ; i++ ) {                    
         elementView.expandNode(subNodes[i]);
        Node[] ssn = subNodes[i].getChildren().getNodes( true );
        row += ssn.length;
        if ( toSelect == null ) {                
            if ( ssn.length > 0 ) {
                toSelect = ssn[0];
            }                    
        }
    }
    Node toSelect2 = null;
    // extend the expansion to all initially selected nodes
    Deque<Node> toProcess = new ArrayDeque<>(Arrays.asList(subNodes));
    while (!toProcess.isEmpty()) {
        Node n = toProcess.poll();
        ElementNode.Description desc = getDescription(n);
        if (desc == null) {
            continue;
        }
        if (desc.isSelected() && toSelect2 == null) {
            // respect initial selection, if some is done
            toSelect2 = n;
        }
        if (desc.hasSelection(false)) {
            elementView.expandNode(n);
            toProcess.addAll(Arrays.asList(n.getChildren().getNodes(true)));
        }
    }
    if (toSelect2 != null) {
        toSelect = toSelect2;
    }
    
    elementView.setScrollsOnExpand( oldScroll );
    
    try  {
        if (toSelect != null ) {
            getExplorerManager().setSelectedNodes(new org.openide.nodes.Node[]{toSelect});
        }
    }
    catch (PropertyVetoException ex) {
        // Ignore
    }
}
 
Example 15
Source File: Expressionator.java    From jackcess with Apache License 2.0 4 votes vote down vote up
private static void parseObjectRefExpression(Token firstTok, TokBuf buf) {

    // object references may be joined by '.' or '!'. access syntac docs claim
    // object identifiers can be formatted like:
    //     "[Collection name]![Object name].[Property name]"
    // However, in practice, they only ever seem to be (at most) two levels
    // and only use '.'.  Apparently '!' is actually a special late-bind
    // operator (not sure it makes a difference for this code?), see:
    // http://bytecomb.com/the-bang-exclamation-operator-in-vba/
    Deque<String> objNames = new LinkedList<String>();
    objNames.add(firstTok.getValueStr());

    Token t = null;
    boolean atSep = false;
    while((t = buf.peekNext()) != null) {
      if(!atSep) {
        if(isObjNameSep(t)) {
          buf.next();
          atSep = true;
          continue;
        }
      } else {
        if((t.getType() == TokenType.OBJ_NAME) ||
           (t.getType() == TokenType.STRING)) {
          buf.next();
          // always insert at beginning of list so names are in reverse order
          objNames.addFirst(t.getValueStr());
          atSep = false;
          continue;
        }
      }
      break;
    }

    int numNames = objNames.size();
    if(atSep || (numNames > 3)) {
      throw new ParseException("Invalid object reference " + buf);
    }

    // names are in reverse order
    String propName = null;
    if(numNames == 3) {
      propName = objNames.poll();
    }
    String objName = objNames.poll();
    String collectionName = objNames.poll();

    buf.setPendingExpr(
        new EObjValue(new Identifier(collectionName, objName, propName)));
  }
 
Example 16
Source File: StoredObjectsTreeModelTest.java    From swift-explorer with Apache License 2.0 4 votes vote down vote up
private void verifyTreeStructure (StoredObjectsTreeModel treeModel)
{
	// First, let's check the root
	TreeNode root = (TreeNode) treeModel.getRoot() ;
	
	assertTrue (root.isRoot()) ;
	assertTrue (root.getContainer() == rootContainer) ;
	assertTrue (treeModel.getChildCount(root) == numberOfChildren) ;
	
	// a "depth-first search" method to check all the nodes
	// (Note the tree was generated using a "breadth-first search")
	Deque<Pair<Integer, TreeNode> > stack = new  ArrayDeque<Pair<Integer, TreeNode> > () ;
	stack.add(Pair.newPair(0, root)) ;
	while (!stack.isEmpty())
	{
		Pair<Integer, TreeNode>  currPairLevelTreeNode = stack.poll() ;
		
		TreeNode currTreeNode = currPairLevelTreeNode.getO() ;
		int level = currPairLevelTreeNode.getL() + 1 ;
		
		// check the number of children
		assertTrue (treeModel.getChildCount(currTreeNode) == numberOfChildren) ;
		
		// check that no node, apart from the root, is marked as root.
		if (level > 1)
			assertFalse (currTreeNode.isRoot()) ;
		
		String previousChildName = null ;
		for (int i = 0 ; i < numberOfChildren ; i++)
		{
			TreeNode currChildTreeNode = (TreeNode) treeModel.getChild(currTreeNode, i) ;
			String currChildName = currChildTreeNode.getObjectName() ;
			
			// check the base name
			String baseName = buildBaseNodeName ((currTreeNode.isRoot())?(null):(currTreeNode.getObjectName()), level) ;
			assertTrue (currChildName.startsWith(baseName)) ;
			
			// The name must be lexicographically ordered in the tree
			// Note that in the init method, the list is shuffled, just to be sure...
			if (previousChildName != null)
				assertTrue (previousChildName.compareTo(currChildName) < 0) ;
			previousChildName = currChildName ;
			
 		if (level <  numberOfLevel)
 			stack.add(Pair.newPair(level, currChildTreeNode)) ;
 		else
 			// here, it must be a leaf
 			assertTrue (treeModel.isLeaf(currChildTreeNode)) ;
		}
	}
}
 
Example 17
Source File: PredicatedHandlersParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
static Node parse(final String string, Deque<Token> tokens, boolean topLevel) {

        //shunting yard algorithm
        //gets rid or parentheses and fixes up operator ordering
        Deque<Token> operatorStack = new ArrayDeque<>();

        Deque<Node> output = new ArrayDeque<>();
        List<Node> blocks = new ArrayList<>();


        while (!tokens.isEmpty()) {
            Token token = tokens.poll();
            if(token.getToken().equals("{")) {
                output.push(parse(string, tokens, false));
            } else if(token.getToken().equals("}")) {
                if(topLevel) {
                    throw error(string, token.getPosition(), "Unexpected token");
                }
                break;
            } else if(token.getToken().equals("\n") || token.getToken().equals(";")) {
                handleLineEnd(string, operatorStack, output, blocks);
            } else if (isSpecialChar(token.getToken())) {
                if (token.getToken().equals("(")) {
                    operatorStack.push(token);
                } else if (token.getToken().equals(")")) {
                    for (; ; ) {
                        Token op = operatorStack.pop();
                        if (op == null) {
                            throw error(string, token.getPosition(), "Unexpected end of input");
                        } else if (op.getToken().equals("(")) {
                            break;
                        } else {
                            output.push(new OperatorNode(op));
                        }
                    }
                } else {
                    output.push(new OperatorNode(token));
                }
            } else {
                if (isOperator(token.getToken()) && !token.getToken().equals(ELSE)) {
                    int prec = precedence(token.getToken());
                    Token top = operatorStack.peek();
                    while (top != null) {
                        if (top.getToken().equals("(")) {
                            break;
                        }
                        int exitingPrec = precedence(top.getToken());
                        if (prec <= exitingPrec) {
                            output.push(new OperatorNode(operatorStack.pop()));
                        } else {
                            break;
                        }
                        top = operatorStack.peek();
                    }
                    operatorStack.push(token);
                } else {
                    output.push(parseExpression(string, token, tokens));
                }
            }
        }
        handleLineEnd(string, operatorStack, output, blocks);
        if(blocks.size() == 1) {
            return blocks.get(0);
        } else {
            return new BlockNode(new Token("", 0), blocks);
        }
    }
 
Example 18
Source File: FisExporter.java    From jfuzzylite with GNU General Public License v3.0 4 votes vote down vote up
/**
 Returns a string representation for the Rule in the Fuzzy Inference System
 format

 @param rule is the rule
 @param engine is the engine in which the rule is registered
 @return a string representation for the rule in the Fuzzy Inference System
 format
 */
public String exportRule(Rule rule, Engine engine) {
    List<Proposition> propositions = new ArrayList<Proposition>();
    List<Operator> operators = new ArrayList<Operator>();
    Deque<Expression> queue = new ArrayDeque<Expression>();

    queue.offer(rule.getAntecedent().getExpression());
    while (!queue.isEmpty()) {
        Expression front = queue.poll();
        if (front instanceof Operator) {
            Operator op = (Operator) front;
            queue.offer(op.getLeft());
            queue.offer(op.getRight());
            operators.add(op);
        } else if (front instanceof Proposition) {
            propositions.add((Proposition) front);
        } else {
            throw new RuntimeException(String.format(
                    "[export error] unexpected class <%s>", front.getClass().getName()));
        }
    }
    boolean equalOperators = true;
    for (int i = 0; i < operators.size() - 1; ++i) {
        if (!operators.get(i).getName().equals(operators.get(i + 1).getName())) {
            equalOperators = false;
            break;
        }
    }
    if (!equalOperators) {
        throw new RuntimeException("[export error] "
                + "fis files do not support rules with different connectors "
                + "(i.e. ['and', 'or']). All connectors within a rule must be the same");
    }
    List<Variable> inputVariables = new ArrayList<Variable>();
    List<Variable> outputVariables = new ArrayList<Variable>();
    for (InputVariable inputVariable : engine.getInputVariables()) {
        inputVariables.add(inputVariable);
    }
    for (OutputVariable outputVariable : engine.getOutputVariables()) {
        outputVariables.add(outputVariable);
    }

    StringBuilder result = new StringBuilder();
    result.append(translate(propositions, inputVariables)).append(", ");
    result.append(translate(rule.getConsequent().getConclusions(), outputVariables));
    result.append(String.format("(%s)", Op.str(rule.getWeight())));
    String connector;
    if (operators.isEmpty()) {
        connector = "1";
    } else if (Rule.FL_AND.equals(operators.get(0).getName())) {
        connector = "1";
    } else if (Rule.FL_OR.equals(operators.get(0).getName())) {
        connector = "2";
    } else {
        connector = operators.get(0).getName();
    }
    result.append(" : ").append(connector);
    return result.toString();
}
 
Example 19
Source File: PivotFacetProcessor.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Recursive function to compute all the pivot counts for the values under the specified field
 */
protected List<NamedList<Object>> doPivots(NamedList<Integer> superFacets,
                                           String field, String subField,
                                           Deque<String> fnames, Deque<String> vnames,
                                           ParsedParams parsed, List<StatsField> statsFields,
                                           List<FacetComponent.FacetBase> facetQueries, List<RangeFacetRequest> facetRanges)
    throws IOException {

  boolean isShard = rb.req.getParams().getBool(ShardParams.IS_SHARD, false);

  SolrIndexSearcher searcher = rb.req.getSearcher();
  // TODO: optimize to avoid converting to an external string and then having to convert back to internal below
  SchemaField sfield = searcher.getSchema().getField(field);
  FieldType ftype = sfield.getType();

  String nextField = fnames.poll();

  // re-usable BytesRefBuilder for conversion of term values to Objects
  BytesRefBuilder termval = new BytesRefBuilder(); 

  List<NamedList<Object>> values = new ArrayList<>( superFacets.size() );
  for (Map.Entry<String, Integer> kv : superFacets) {
    // Only sub-facet if parent facet has positive count - still may not be any values for the sub-field though
    if (kv.getValue() >= getMinCountForField(field)) {  
      final String fieldValue = kv.getKey();
      final int pivotCount = kv.getValue();

      SimpleOrderedMap<Object> pivot = new SimpleOrderedMap<>();
      pivot.add( "field", field );
      if (null == fieldValue) {
        pivot.add( "value", null );
      } else {
        ftype.readableToIndexed(fieldValue, termval);
        pivot.add( "value", ftype.toObject(sfield, termval.get()) );
      }
      pivot.add( "count", pivotCount );

      final DocSet subset = getSubset(parsed.docs, sfield, fieldValue);
      
      addPivotQueriesAndRanges(pivot, params, subset, facetQueries, facetRanges);

      if( subField != null )  {
        NamedList<Integer> facetCounts;
        if(!vnames.isEmpty()){
          String val = vnames.pop();
          facetCounts = new NamedList<>();
          facetCounts.add(val, getSubsetSize(subset,
                                             searcher.getSchema().getField(subField),
                                             val));
        } else {
          facetCounts = this.getTermCountsForPivots(subField, parsed.withDocs(subset));
        }

        if (facetCounts.size() >= 1) {
          pivot.add( "pivot", doPivots( facetCounts, subField, nextField, fnames, vnames, parsed.withDocs(subset), statsFields, facetQueries, facetRanges) );
        }
      }
      if ((isShard || 0 < pivotCount) && ! statsFields.isEmpty()) {
        Map<String, StatsValues> stv = new LinkedHashMap<>();
        for (StatsField statsField : statsFields) {
          stv.put(statsField.getOutputKey(), statsField.computeLocalStatsValues(subset));
        }
        pivot.add("stats", StatsComponent.convertToResponse(stv));
      }
      values.add( pivot );
    }

  }
  // put the field back on the list
  fnames.push( nextField );
  return values;
}
 
Example 20
Source File: TaskHistoryWriter.java    From helios with Apache License 2.0 4 votes vote down vote up
private TaskStatusEvent getNext() {
  // Some explanation: We first find the eldest event from amongst the queues (ok, they're
  // deques, but we really use it as a put back queue), and only then to we try to get
  // a lock on the relevant queue from whence we got the event.  Assuming that all worked
  // *and* that the event we have wasn't rolled off due to max-size limitations, we then
  // pull the item off the queue and return it.  We're basically doing optimistic concurrency,
  // and skewing things so that adding to this should be cheap.

  while (true) {
    final TaskStatusEvent current = findEldestEvent();

    // Didn't find anything that needed processing?
    if (current == null) {
      return null;
    }

    final JobId id = current.getStatus().getJob().getId();
    final Deque<TaskStatusEvent> deque = items.get(id);
    if (deque == null) {
      // shouldn't happen because we should be the only one pulling items off, but....
      continue;
    }

    synchronized (deque) {
      if (!deque.peek().equals(current)) {
        // item got rolled off, try again
        continue;
      }

      // Pull it off the queue and be paranoid.
      final TaskStatusEvent newCurrent = deque.poll();
      count.decrementAndGet();
      checkState(current.equals(newCurrent), "current should equal newCurrent");
      // Safe because this is the *only* place we hold these two locks at the same time.
      synchronized (items) {
        // Extra paranoia: curDeque should always == deque
        final Deque<TaskStatusEvent> curDeque = items.get(id);
        if (curDeque != null && curDeque.isEmpty()) {
          items.remove(id);
        }
      }
      return current;
    }
  }
}