Search in sources :

Example 66 with ArrayDeque

use of java.util.ArrayDeque in project netty by netty.

the class CombinedChannelDuplexHandlerTest method testInboundEvents.

@Test
public void testInboundEvents() {
    final Queue<Event> queue = new ArrayDeque<Event>();
    ChannelInboundHandler inboundHandler = new ChannelInboundHandlerAdapter() {

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.HANDLER_ADDED);
        }

        @Override
        public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.HANDLER_REMOVED);
        }

        @Override
        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.REGISTERED);
        }

        @Override
        public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.UNREGISTERED);
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.ACTIVE);
        }

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.INACTIVE);
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            queue.add(Event.CHANNEL_READ);
        }

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.CHANNEL_READ_COMPLETE);
        }

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            queue.add(Event.USER_EVENT_TRIGGERED);
        }

        @Override
        public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.CHANNEL_WRITABILITY_CHANGED);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            queue.add(Event.EXCEPTION_CAUGHT);
        }
    };
    CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler> handler = new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>(inboundHandler, new ChannelOutboundHandlerAdapter());
    EmbeddedChannel channel = new EmbeddedChannel(handler);
    channel.pipeline().fireChannelWritabilityChanged();
    channel.pipeline().fireUserEventTriggered(MSG);
    channel.pipeline().fireChannelRead(MSG);
    channel.pipeline().fireChannelReadComplete();
    assertEquals(Event.HANDLER_ADDED, queue.poll());
    assertEquals(Event.REGISTERED, queue.poll());
    assertEquals(Event.ACTIVE, queue.poll());
    assertEquals(Event.CHANNEL_WRITABILITY_CHANGED, queue.poll());
    assertEquals(Event.USER_EVENT_TRIGGERED, queue.poll());
    assertEquals(Event.CHANNEL_READ, queue.poll());
    assertEquals(Event.CHANNEL_READ_COMPLETE, queue.poll());
    handler.removeInboundHandler();
    assertEquals(Event.HANDLER_REMOVED, queue.poll());
    // These should not be handled by the inboundHandler anymore as it was removed before
    channel.pipeline().fireChannelWritabilityChanged();
    channel.pipeline().fireUserEventTriggered(MSG);
    channel.pipeline().fireChannelRead(MSG);
    channel.pipeline().fireChannelReadComplete();
    // Should have not received any more events as it was removed before via removeInboundHandler()
    assertTrue(queue.isEmpty());
    assertTrue(channel.finish());
    assertTrue(queue.isEmpty());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ArrayDeque(java.util.ArrayDeque) Test(org.junit.Test)

Example 67 with ArrayDeque

use of java.util.ArrayDeque in project orientdb by orientechnologies.

the class OSQLFunctionShortestPath method walkLeft.

protected List<ORID> walkLeft(final OSQLFunctionShortestPath.OShortestPathContext ctx) {
    ArrayDeque<OrientVertex> nextLevelQueue = new ArrayDeque<OrientVertex>();
    while (!ctx.queueLeft.isEmpty()) {
        ctx.current = ctx.queueLeft.poll();
        Iterable<Vertex> neighbors;
        if (ctx.edgeType == null) {
            neighbors = ctx.current.getVertices(ctx.directionLeft);
        } else {
            neighbors = ctx.current.getVertices(ctx.directionLeft, ctx.edgeTypeParam);
        }
        for (Vertex neighbor : neighbors) {
            final OrientVertex v = (OrientVertex) neighbor;
            final ORID neighborIdentity = v.getIdentity();
            if (ctx.rightVisited.contains(neighborIdentity)) {
                ctx.previouses.put(neighborIdentity, ctx.current.getIdentity());
                return computePath(ctx.previouses, ctx.nexts, neighborIdentity);
            }
            if (!ctx.leftVisited.contains(neighborIdentity)) {
                ctx.previouses.put(neighborIdentity, ctx.current.getIdentity());
                nextLevelQueue.offer(v);
                ctx.leftVisited.add(neighborIdentity);
            }
        }
    }
    ctx.queueLeft = nextLevelQueue;
    return null;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) ORID(com.orientechnologies.orient.core.id.ORID) ArrayDeque(java.util.ArrayDeque)

Example 68 with ArrayDeque

use of java.util.ArrayDeque in project orientdb by orientechnologies.

the class OSQLFunctionShortestPath method walkRight.

protected List<ORID> walkRight(final OSQLFunctionShortestPath.OShortestPathContext ctx) {
    final ArrayDeque<OrientVertex> nextLevelQueue = new ArrayDeque<OrientVertex>();
    while (!ctx.queueRight.isEmpty()) {
        ctx.currentRight = ctx.queueRight.poll();
        Iterable<Vertex> neighbors;
        if (ctx.edgeType == null) {
            neighbors = ctx.currentRight.getVertices(ctx.directionRight);
        } else {
            neighbors = ctx.currentRight.getVertices(ctx.directionRight, ctx.edgeTypeParam);
        }
        for (Vertex neighbor : neighbors) {
            final OrientVertex v = (OrientVertex) neighbor;
            final ORID neighborIdentity = v.getIdentity();
            if (ctx.leftVisited.contains(neighborIdentity)) {
                ctx.nexts.put(neighborIdentity, ctx.currentRight.getIdentity());
                return computePath(ctx.previouses, ctx.nexts, neighborIdentity);
            }
            if (!ctx.rightVisited.contains(neighborIdentity)) {
                ctx.nexts.put(neighborIdentity, ctx.currentRight.getIdentity());
                nextLevelQueue.offer(v);
                ctx.rightVisited.add(neighborIdentity);
            }
        }
    }
    ctx.queueRight = nextLevelQueue;
    return null;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) ORID(com.orientechnologies.orient.core.id.ORID) ArrayDeque(java.util.ArrayDeque)

Example 69 with ArrayDeque

use of java.util.ArrayDeque in project ProPPR by TeamCohen.

the class Multithreading method executeJob.

/**
	 * 
	 * @param nThreads
	 * @param streamer
	 * @param transformer
	 * @param cleanup
	 * @param throttle
	 */
public void executeJob(int nThreads, Iterable<In> streamer, Transformer<In, Out> transformer, Cleanup<Out> cleanup, int throttle) {
    log.info("Executing Multithreading job:" + " streamer: " + streamer.getClass().getCanonicalName() + " transformer: " + transformer.getClass().getCanonicalName() + " throttle: " + throttle);
    ExecutorService transformerPool = Executors.newFixedThreadPool(nThreads, new NamedThreadFactory("transformer"));
    ExecutorService cleanupPool = Executors.newFixedThreadPool(1, new NamedThreadFactory("cleanup"));
    ArrayDeque<Future<?>> transformerQueue = new ArrayDeque<Future<?>>();
    int id = 0;
    if (log.isDebugEnabled())
        log.debug("Adding start " + (id + 1));
    for (In item : streamer) {
        id++;
        if (throttle > 0) {
            tidyQueue(transformerQueue);
            if (transformerQueue.size() > throttle) {
                int wait = 100;
                if (log.isDebugEnabled())
                    log.debug("Throttling @" + id + "...");
                while (transformerQueue.size() > throttle) {
                    try {
                        Thread.sleep(wait);
                    } catch (InterruptedException e) {
                        log.error("Interrupted while throttling tasks");
                    }
                    tidyQueue(transformerQueue);
                    wait *= 1.5;
                }
                if (log.isDebugEnabled())
                    log.debug("Throttling complete " + wait);
            }
        }
        Future<Out> transformerFuture = transformerPool.submit(transformer.transformer(item, id));
        if (log.isDebugEnabled())
            log.debug("Adding done " + (id));
        if (maintainOrder) {
            cleanupPool.submit(cleanup.cleanup(transformerFuture, null, id));
        } else {
            if (log.isDebugEnabled())
                log.debug("Permitting rescheduling of #" + id);
            cleanupPool.submit(cleanup.cleanup(transformerFuture, cleanupPool, id));
        }
        if (throttle > 0)
            transformerQueue.add(transformerFuture);
        if (log.isDebugEnabled())
            log.debug("Adding start " + (id + 1));
    }
    // first we wait for all transformers to finish
    transformerPool.shutdown();
    try {
        transformerPool.awaitTermination(7, TimeUnit.DAYS);
    } catch (InterruptedException e) {
        log.error("Interrupted?", e);
    }
    // at this point all transformers are complete, so no task in the cleanup pool will need to be rescheduled
    cleanupPool.shutdown();
    try {
        if (log.isDebugEnabled())
            log.debug("Finishing cleanup...");
        cleanupPool.awaitTermination(7, TimeUnit.DAYS);
        if (log.isDebugEnabled())
            log.debug("Cleanup finished.");
    } catch (InterruptedException e) {
        log.error("Interrupted?", e);
    }
    log.info("Total items: " + id);
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ArrayDeque(java.util.ArrayDeque)

Example 70 with ArrayDeque

use of java.util.ArrayDeque in project voltdb by VoltDB.

the class SelectSubPlanAssembler method queueJoinOrders.

/**
     * Compute every permutation of the list of involved tables and put them in a deque.
     * TODO(XIN): takes at least 3.3% cpu of planner. Optimize it when possible.
     */
public static ArrayDeque<JoinNode> queueJoinOrders(JoinNode joinNode, boolean findAll) {
    assert (joinNode != null);
    // Clone the original
    JoinNode clonedTree = (JoinNode) joinNode.clone();
    // Split join tree into a set of subtrees. The join type for all nodes in a subtree is the same
    List<JoinNode> subTrees = clonedTree.extractSubTrees();
    assert (!subTrees.isEmpty());
    // Generate possible join orders for each sub-tree separately
    ArrayList<List<JoinNode>> joinOrderList = generateJoinOrders(subTrees);
    // Reassemble the all possible combinations of the sub-tree and queue them
    ArrayDeque<JoinNode> joinOrders = new ArrayDeque<>();
    queueSubJoinOrders(joinOrderList, 0, new ArrayList<JoinNode>(), joinOrders, findAll);
    return joinOrders;
}
Also used : JoinNode(org.voltdb.planner.parseinfo.JoinNode) ArrayList(java.util.ArrayList) List(java.util.List) ArrayDeque(java.util.ArrayDeque)

Aggregations

ArrayDeque (java.util.ArrayDeque)217 ArrayList (java.util.ArrayList)36 Test (org.junit.Test)36 IOException (java.io.IOException)27 HashMap (java.util.HashMap)23 List (java.util.List)20 HashSet (java.util.HashSet)19 Map (java.util.Map)17 Deque (java.util.Deque)11 Iterator (java.util.Iterator)10 NoSuchElementException (java.util.NoSuchElementException)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 File (java.io.File)7 Path (java.nio.file.Path)7 Random (java.util.Random)7 ByteBuffer (java.nio.ByteBuffer)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 HttpFields (org.eclipse.jetty.http.HttpFields)5 Name (com.github.anba.es6draft.ast.scope.Name)4 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)4