All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.kafka.streams.kstream.internals.BranchedKStreamImpl Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.kafka.streams.kstream.internals;

import org.apache.kafka.streams.kstream.Branched;
import org.apache.kafka.streams.kstream.BranchedKStream;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.Predicate;
import org.apache.kafka.streams.kstream.internals.graph.ProcessorGraphNode;
import org.apache.kafka.streams.kstream.internals.graph.ProcessorParameters;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BranchedKStreamImpl implements BranchedKStream {

    private static final String BRANCH_NAME = "KSTREAM-BRANCH-";

    private final KStreamImpl source;
    private final boolean repartitionRequired;
    private final String splitterName;
    private final Map> outputBranches = new HashMap<>();

    private final List> predicates = new ArrayList<>();
    private final List childNames = new ArrayList<>();
    private final ProcessorGraphNode splitterNode;

    BranchedKStreamImpl(final KStreamImpl source, final boolean repartitionRequired, final NamedInternal named) {
        this.source = source;
        this.repartitionRequired = repartitionRequired;
        this.splitterName = named.orElseGenerateWithPrefix(source.builder, BRANCH_NAME);

        // predicates and childNames are passed by reference so when the user adds a branch they get added to
        final ProcessorParameters processorParameters =
                new ProcessorParameters<>(new KStreamBranch<>(predicates, childNames), splitterName);
        splitterNode = new ProcessorGraphNode<>(splitterName, processorParameters);
        source.builder.addGraphNode(source.graphNode, splitterNode);
    }

    @Override
    public BranchedKStream branch(final Predicate predicate) {
        return branch(predicate, BranchedInternal.empty());
    }

    @Override
    public BranchedKStream branch(final Predicate predicate, final Branched branched) {
        predicates.add(predicate);
        createBranch(branched, predicates.size());
        return this;
    }

    @Override
    public Map> defaultBranch() {
        return defaultBranch(BranchedInternal.empty());
    }

    @Override
    public Map> defaultBranch(final Branched branched) {
        createBranch(branched, 0);
        return outputBranches;
    }

    private void createBranch(final Branched branched, final int index) {
        final BranchedInternal branchedInternal = new BranchedInternal<>(branched);
        final String branchChildName = getBranchChildName(index, branchedInternal);
        childNames.add(branchChildName);
        source.builder.newProcessorName(branchChildName);
        final ProcessorParameters parameters = new ProcessorParameters<>(new PassThrough<>(), branchChildName);
        final ProcessorGraphNode branchChildNode = new ProcessorGraphNode<>(branchChildName, parameters);
        source.builder.addGraphNode(splitterNode, branchChildNode);
        final KStreamImpl branch = new KStreamImpl<>(branchChildName, source.keySerde,
                source.valueSerde, source.subTopologySourceNodes,
                repartitionRequired, branchChildNode, source.builder);
        process(branch, branchChildName, branchedInternal);
    }

    private String getBranchChildName(final int index, final BranchedInternal branchedInternal) {
        if (branchedInternal.name() == null) {
            return splitterName + index;
        } else {
            return splitterName + branchedInternal.name();
        }
    }

    private void process(final KStreamImpl branch, final String branchChildName,
                         final BranchedInternal branchedInternal) {
        if (branchedInternal.chainFunction() != null) {
            final KStream transformedStream = branchedInternal.chainFunction().apply(branch);
            if (transformedStream != null) {
                outputBranches.put(branchChildName, transformedStream);
            }
        } else if (branchedInternal.chainConsumer() != null) {
            branchedInternal.chainConsumer().accept(branch);
        } else {
            outputBranches.put(branchChildName, branch);
        }
    }

    @Override
    public Map> noDefaultBranch() {
        return outputBranches;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy