Search in sources :

Example 51 with Queue

use of com.oath.cyclops.async.adapters.Queue in project cyclops by aol.

the class FutureStream method mergeLatest.

/**
 * Merges this stream and the supplied Streams into a single Stream where the next value
 * is the next returned across any of the involved Streams. Suitable for merging infinite streams
 *
 * <pre>
 * {@code
 * 	FutureStream<Integer> fast =  ... //  [1,2,3,4,5,6,7..]
 * 	FutureStream<Integer> slow =  ... //  [100,200,300,400,500,600..]
 *
 *  FutureStream<Integer> merged = fast.mergeLatest(slow);  //[1,2,3,4,5,6,7,8,100,9,10,11,12,13,14,15,16,200..]
 * }
 * </pre>
 *
 * @param streams
 * @return
 */
default <R> FutureStream<R> mergeLatest(final FutureStream<?>... streams) {
    final Queue queue = Queue.createMergeQueue();
    addToQueue(queue);
    ReactiveSeq.of(streams).forEach(s -> s.addToQueue(queue));
    return fromStream(queue.stream(this.getSubscription()));
}
Also used : Queue(com.oath.cyclops.async.adapters.Queue)

Example 52 with Queue

use of com.oath.cyclops.async.adapters.Queue in project cyclops by aol.

the class PipesTest method testGetPresent.

@Test
public void testGetPresent() {
    pipes.register("hello", new Queue());
    assertTrue(pipes.get("hello").isPresent());
}
Also used : Queue(com.oath.cyclops.async.adapters.Queue) Test(org.junit.Test)

Example 53 with Queue

use of com.oath.cyclops.async.adapters.Queue in project cyclops by aol.

the class LazyToQueue method toQueue.

/*
     * Populate provided queues with the sharded data from this Stream.
     *
     *	@param shards Map of key to Queue shards
     *	@param sharder Sharding function, element to key converter
     * @see com.oath.cyclops.react.stream.traits.ToQueue#toQueue(java.util.Map, java.util.function.Function)
     */
@Override
default <K> void toQueue(final Map<K, Queue<U>> shards, final Function<? super U, ? extends K> sharder) {
    // in this case all the items have to be pushed to the shards,
    // we can't rely on the client pulling them all to getValue them in to the right shards
    final LazyReact service = getPopulator();
    then(it -> shards.get(sharder.apply(it)).offer(it), service.getExecutor()).runThread(() -> {
        shards.values().forEach(it -> it.close());
        returnPopulator(service);
    });
}
Also used : Consumer(java.util.function.Consumer) Queue(com.oath.cyclops.async.adapters.Queue) ClosedQueueException(com.oath.cyclops.async.adapters.Queue.ClosedQueueException) Executor(java.util.concurrent.Executor) LazyReact(cyclops.futurestream.LazyReact) Map(java.util.Map) FutureStream(cyclops.futurestream.FutureStream) CompletedException(com.oath.cyclops.internal.react.async.future.CompletedException) Function(java.util.function.Function) LazyReact(cyclops.futurestream.LazyReact)

Example 54 with Queue

use of com.oath.cyclops.async.adapters.Queue in project cyclops by aol.

the class EagerFutureStreamFunctions method withLatest.

/**
 * Zip two streams into one using a {@link BiFunction} to produce resulting.
 * values. Uses the latest values from each rather than waiting for both.
 */
static <T1, T2, R> ReactiveSeq<R> withLatest(final SimpleReactStream<T1> left, final SimpleReactStream<T2> right, final BiFunction<T1, T2, R> zipper) {
    final Queue q = left.then(it -> new Val(Val.Pos.left, it)).merge(right.then(it -> new Val(Val.Pos.right, it))).toQueue();
    final Iterator<Val> it = q.stream(left.getSubscription()).iterator();
    class Zip implements Iterator<R> {

        T1 lastLeft = null;

        T2 lastRight = null;

        @Override
        public boolean hasNext() {
            return it.hasNext();
        }

        @Override
        public R next() {
            final Val v = it.next();
            if (v.pos == Val.Pos.left) {
                lastLeft = (T1) v.val;
                return zipper.apply(lastLeft, lastRight);
            } else
                lastRight = (T2) v.val;
            return (R) Optional.empty();
        }
    }
    return ReactiveSeq.fromIterator(new Zip()).filter(next -> !(next instanceof Optional));
}
Also used : Optional(java.util.Optional) Iterator(java.util.Iterator) Queue(com.oath.cyclops.async.adapters.Queue)

Example 55 with Queue

use of com.oath.cyclops.async.adapters.Queue in project cyclops by aol.

the class EagerFutureStreamFunctions method dropUntil.

static <T1, T2> ReactiveSeq<T1> dropUntil(final SimpleReactStream<T1> left, final SimpleReactStream<T2> right) {
    final Queue q = left.then(it -> new Val(Val.Pos.left, it)).merge(right.then(it -> new Val(Val.Pos.right, it))).toQueue();
    final Iterator<Val> it = q.stream(left.getSubscription()).iterator();
    new Object();
    class Zip implements Iterator<T1> {

        Optional<T1> lastLeft = Optional.empty();

        Optional<T2> lastRight = Optional.empty();

        @Override
        public boolean hasNext() {
            return it.hasNext();
        }

        @Override
        public T1 next() {
            final Val v = it.next();
            if (v.pos == Val.Pos.left) {
                if (lastRight.isPresent())
                    lastLeft = Optional.of((T1) v.val);
            } else
                lastRight = Optional.of((T2) v.val);
            if (!lastRight.isPresent())
                return (T1) Optional.empty();
            if (lastLeft.isPresent())
                return lastLeft.get();
            else
                return (T1) Optional.empty();
        }
    }
    return ReactiveSeq.fromIterable(() -> new Zip()).filter(next -> !(next instanceof Optional));
}
Also used : Optional(java.util.Optional) Iterator(java.util.Iterator) Queue(com.oath.cyclops.async.adapters.Queue)

Aggregations

Queue (com.oath.cyclops.async.adapters.Queue)68 Test (org.junit.Test)52 List (java.util.List)34 ArrayList (java.util.ArrayList)28 Collectors (java.util.stream.Collectors)25 Stream (java.util.stream.Stream)23 LazyReact (cyclops.futurestream.LazyReact)22 InternalEvent (com.nextdoor.bender.InternalEvent)18 OperationProcessor (com.nextdoor.bender.operation.OperationProcessor)18 QueueFactories (com.oath.cyclops.async.QueueFactories)17 Tuple2 (cyclops.data.tuple.Tuple2)17 Iterator (java.util.Iterator)17 ReactiveSeq (cyclops.reactive.ReactiveSeq)16 Ignore (org.junit.Ignore)16 FutureStream (cyclops.futurestream.FutureStream)15 Arrays.asList (java.util.Arrays.asList)15 Matchers.greaterThan (org.hamcrest.Matchers.greaterThan)15 Matchers.is (org.hamcrest.Matchers.is)15 ThreadPools (com.oath.cyclops.react.ThreadPools)14 Tuple.tuple (cyclops.data.tuple.Tuple.tuple)14