inponomarev commented on a change in pull request #9107: URL: https://github.com/apache/kafka/pull/9107#discussion_r548629205
########## File path: streams/src/main/java/org/apache/kafka/streams/kstream/internals/BranchedKStreamImpl.java ########## @@ -0,0 +1,97 @@ +/* + * 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<K, V> implements BranchedKStream<K, V> { + + private static final String BRANCH_NAME = "KSTREAM-BRANCH-"; + + private final KStreamImpl<K, V> source; + private final boolean repartitionRequired; + private final String splitterName; + private final Map<String, KStream<K, V>> result = new HashMap<>(); + + private final List<Predicate<? super K, ? super V>> predicates = new ArrayList<>(); + private final List<String> childNames = new ArrayList<>(); + private final ProcessorGraphNode<K, V> splitterNode; + + BranchedKStreamImpl(final KStreamImpl<K, V> 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<K, V> processorParameters = + new ProcessorParameters<>(new KStreamBranch<>(predicates, childNames), splitterName); + splitterNode = new ProcessorGraphNode<>(splitterName, processorParameters); + source.builder.addGraphNode(source.streamsGraphNode, splitterNode); + } + + @Override + public BranchedKStream<K, V> branch(final Predicate<? super K, ? super V> predicate) { + return branch(predicate, BranchedInternal.empty()); + } + + @Override + public BranchedKStream<K, V> branch(final Predicate<? super K, ? super V> predicate, final Branched<K, V> branched) { + predicates.add(predicate); + createBranch(branched, predicates.size()); + return this; + } + + @Override + public Map<String, KStream<K, V>> defaultBranch() { + return defaultBranch(BranchedInternal.empty()); + } + + @Override + public Map<String, KStream<K, V>> defaultBranch(final Branched<K, V> branched) { + createBranch(branched, 0); Review comment: The default branch should have index 0 (so it will be stable when branches are added or removed), but it should always be checked after all other branches. And when we come to the default branch during message processing, there is actually no need in dereferncing a predicate and calling `test`... that's why I treat the default branch differently. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
