Search in sources :

Example 1 with FlatMapTransform

use of com.hazelcast.jet.impl.pipeline.transform.FlatMapTransform in project hazelcast by hazelcast.

the class Planner method findFusableChain.

private static List<Transform> findFusableChain(@Nonnull Transform transform, @Nonnull Map<Transform, List<Transform>> adjacencyMap) {
    ArrayList<Transform> chain = new ArrayList<>();
    for (; ; ) {
        if (!(transform instanceof MapTransform || transform instanceof FlatMapTransform)) {
            break;
        }
        chain.add(transform);
        List<Transform> downstream = adjacencyMap.get(transform);
        if (downstream.size() != 1) {
            break;
        }
        Transform nextTransform = downstream.get(0);
        if (nextTransform.localParallelism() != transform.localParallelism() || nextTransform.shouldRebalanceInput(0)) {
            break;
        }
        transform = nextTransform;
    }
    return chain.size() > 1 ? chain : null;
}
Also used : ArrayList(java.util.ArrayList) MapTransform(com.hazelcast.jet.impl.pipeline.transform.MapTransform) FlatMapTransform(com.hazelcast.jet.impl.pipeline.transform.FlatMapTransform) SinkTransform(com.hazelcast.jet.impl.pipeline.transform.SinkTransform) MapTransform(com.hazelcast.jet.impl.pipeline.transform.MapTransform) TimestampTransform(com.hazelcast.jet.impl.pipeline.transform.TimestampTransform) Transform(com.hazelcast.jet.impl.pipeline.transform.Transform) StreamSourceTransform(com.hazelcast.jet.impl.pipeline.transform.StreamSourceTransform) FlatMapTransform(com.hazelcast.jet.impl.pipeline.transform.FlatMapTransform) FlatMapTransform(com.hazelcast.jet.impl.pipeline.transform.FlatMapTransform)

Example 2 with FlatMapTransform

use of com.hazelcast.jet.impl.pipeline.transform.FlatMapTransform in project hazelcast by hazelcast.

the class Planner method fuseFlatMapTransforms.

@SuppressWarnings("rawtypes")
private static Transform fuseFlatMapTransforms(List<Transform> chain) {
    assert chain.size() > 1 : "chain.size()=" + chain.size();
    assert chain.get(0).upstream().size() == 1;
    int lastFlatMap = 0;
    FunctionEx<Object, Traverser> flatMapFn = null;
    for (int i = 0; i < chain.size(); i++) {
        if (chain.get(i) instanceof FlatMapTransform) {
            FunctionEx<Object, Traverser> function = ((FlatMapTransform) chain.get(i)).flatMapFn();
            FunctionEx<Object, Object> inputMapFn = mergeMapFunctions(chain.subList(lastFlatMap, i));
            if (inputMapFn != null) {
                flatMapFn = flatMapFn == null ? (Object t) -> {
                    Object mappedValue = inputMapFn.apply(t);
                    return mappedValue != null ? function.apply(mappedValue) : Traversers.empty();
                } : flatMapFn.andThen(r -> r.map(inputMapFn).flatMap(function));
            } else {
                flatMapFn = flatMapFn == null ? function::apply : flatMapFn.andThen(r -> r.flatMap(function));
            }
            lastFlatMap = i + 1;
        }
    }
    FunctionEx trailingMapFn = mergeMapFunctions(chain.subList(lastFlatMap, chain.size()));
    String name = chain.stream().map(Transform::name).collect(Collectors.joining(", ", "fused(", ")"));
    Transform fused;
    if (flatMapFn == null) {
        fused = new MapTransform(name, chain.get(0).upstream().get(0), trailingMapFn);
    } else {
        if (trailingMapFn != null) {
            flatMapFn = flatMapFn.andThen(t -> t.map(trailingMapFn));
        }
        fused = new FlatMapTransform(name, chain.get(0).upstream().get(0), flatMapFn);
    }
    // if the first stage of the chain is rebalanced, then we set
    // the rebalance flag of the created fused stage. Only consider
    // the case when first element of the chain is rebalanced
    // because there isn't any other case. If any stage in the
    // middle includes rebalance, then those stages are not fused
    // by findFusableChain().
    fused.setRebalanceInput(0, chain.get(0).shouldRebalanceInput(0));
    return fused;
}
Also used : Traverser(com.hazelcast.jet.Traverser) LoggingUtil(com.hazelcast.jet.impl.util.LoggingUtil) Processor(com.hazelcast.jet.core.Processor) HashMap(java.util.HashMap) ObjIntConsumer(java.util.function.ObjIntConsumer) EventTimePolicy(com.hazelcast.jet.core.EventTimePolicy) ArrayList(java.util.ArrayList) SinkTransform(com.hazelcast.jet.impl.pipeline.transform.SinkTransform) TopologicalSorter.checkTopologicalSort(com.hazelcast.jet.impl.TopologicalSorter.checkTopologicalSort) ILogger(com.hazelcast.logging.ILogger) Map(java.util.Map) MapTransform(com.hazelcast.jet.impl.pipeline.transform.MapTransform) TimestampTransform(com.hazelcast.jet.impl.pipeline.transform.TimestampTransform) Edge.from(com.hazelcast.jet.core.Edge.from) DAG(com.hazelcast.jet.core.DAG) Edge(com.hazelcast.jet.core.Edge) Nonnull(javax.annotation.Nonnull) ProcessorSupplier(com.hazelcast.jet.core.ProcessorSupplier) FunctionEx(com.hazelcast.function.FunctionEx) Logger(com.hazelcast.logging.Logger) Util.toList(com.hazelcast.jet.impl.util.Util.toList) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) Traversers(com.hazelcast.jet.Traversers) Collectors(java.util.stream.Collectors) SupplierEx(com.hazelcast.function.SupplierEx) Transform(com.hazelcast.jet.impl.pipeline.transform.Transform) Consumer(java.util.function.Consumer) Vertex(com.hazelcast.jet.core.Vertex) Util(com.hazelcast.jet.impl.util.Util) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Entry(java.util.Map.Entry) StreamSourceTransform(com.hazelcast.jet.impl.pipeline.transform.StreamSourceTransform) EventTimePolicy.eventTimePolicy(com.hazelcast.jet.core.EventTimePolicy.eventTimePolicy) FlatMapTransform(com.hazelcast.jet.impl.pipeline.transform.FlatMapTransform) Context(com.hazelcast.jet.impl.pipeline.PipelineImpl.Context) Traverser(com.hazelcast.jet.Traverser) FunctionEx(com.hazelcast.function.FunctionEx) MapTransform(com.hazelcast.jet.impl.pipeline.transform.MapTransform) FlatMapTransform(com.hazelcast.jet.impl.pipeline.transform.FlatMapTransform) SinkTransform(com.hazelcast.jet.impl.pipeline.transform.SinkTransform) MapTransform(com.hazelcast.jet.impl.pipeline.transform.MapTransform) TimestampTransform(com.hazelcast.jet.impl.pipeline.transform.TimestampTransform) Transform(com.hazelcast.jet.impl.pipeline.transform.Transform) StreamSourceTransform(com.hazelcast.jet.impl.pipeline.transform.StreamSourceTransform) FlatMapTransform(com.hazelcast.jet.impl.pipeline.transform.FlatMapTransform) FlatMapTransform(com.hazelcast.jet.impl.pipeline.transform.FlatMapTransform)

Aggregations

FlatMapTransform (com.hazelcast.jet.impl.pipeline.transform.FlatMapTransform)2 MapTransform (com.hazelcast.jet.impl.pipeline.transform.MapTransform)2 SinkTransform (com.hazelcast.jet.impl.pipeline.transform.SinkTransform)2 StreamSourceTransform (com.hazelcast.jet.impl.pipeline.transform.StreamSourceTransform)2 TimestampTransform (com.hazelcast.jet.impl.pipeline.transform.TimestampTransform)2 Transform (com.hazelcast.jet.impl.pipeline.transform.Transform)2 ArrayList (java.util.ArrayList)2 FunctionEx (com.hazelcast.function.FunctionEx)1 SupplierEx (com.hazelcast.function.SupplierEx)1 Traverser (com.hazelcast.jet.Traverser)1 Traversers (com.hazelcast.jet.Traversers)1 DAG (com.hazelcast.jet.core.DAG)1 Edge (com.hazelcast.jet.core.Edge)1 Edge.from (com.hazelcast.jet.core.Edge.from)1 EventTimePolicy (com.hazelcast.jet.core.EventTimePolicy)1 EventTimePolicy.eventTimePolicy (com.hazelcast.jet.core.EventTimePolicy.eventTimePolicy)1 Processor (com.hazelcast.jet.core.Processor)1 ProcessorMetaSupplier (com.hazelcast.jet.core.ProcessorMetaSupplier)1 ProcessorSupplier (com.hazelcast.jet.core.ProcessorSupplier)1 Vertex (com.hazelcast.jet.core.Vertex)1