Rikkola commented on code in PR #6584: URL: https://github.com/apache/incubator-kie-drools/pull/6584#discussion_r2772778981
########## drools-core/src/main/java/org/drools/core/phreak/PhreakBiLinearJoinNode.java: ########## @@ -0,0 +1,471 @@ +/* + * 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.drools.core.phreak; + +import org.drools.core.common.ReteEvaluator; +import org.drools.core.common.TupleSets; +import org.drools.core.reteoo.BetaMemory; +import org.drools.core.common.BiLinearBetaConstraints; +import org.drools.core.reteoo.BiLinearContextEntry; +import org.drools.base.reteoo.NodeTypeEnums; +import org.drools.core.reteoo.BiLinearJoinNode; +import org.drools.core.reteoo.BiLinearRuleTerminalNodeLeftTuple; +import org.drools.core.reteoo.BiLinearTuple; +import org.drools.core.reteoo.LeftTupleSink; +import org.drools.core.reteoo.TupleImpl; +import org.drools.core.reteoo.TupleMemory; +import org.drools.core.util.FastIterator; + +/** + * Phreak processor for BiLinearJoinNode that handles joining two left input networks. + * Unlike traditional joins that have one left and one right input, BiLinearJoinNode + * has two left inputs that get joined together. + */ +public class PhreakBiLinearJoinNode { + + private final ReteEvaluator reteEvaluator; + + public PhreakBiLinearJoinNode(ReteEvaluator reteEvaluator) { + this.reteEvaluator = reteEvaluator; + } + + public void doNode(BiLinearJoinNode biLinearJoinNode, + LeftTupleSink sink, + BetaMemory bm, + TupleSets srcLeftTuples, + TupleSets stagedLeftTuples, + TupleSets trgLeftTuples) { + + // Get tuples from second left input (stored in "right" memory for BiLinear) + TupleSets srcRightTuples = bm.getStagedRightTuples().takeAll(); + + if (srcRightTuples.getDeleteFirst() != null) { + doRightDeletes(bm, srcRightTuples, trgLeftTuples, stagedLeftTuples); + } + if (srcLeftTuples.getDeleteFirst() != null) { + doLeftDeletes(bm, srcLeftTuples, trgLeftTuples, stagedLeftTuples); + } + + // Process updates from both inputs + if (srcRightTuples.getUpdateFirst() != null) { + PhreakNodeOperations.doUpdatesReorderRightMemory(bm, srcRightTuples); + } + if (srcLeftTuples.getUpdateFirst() != null) { + PhreakNodeOperations.doUpdatesReorderLeftMemory(bm, srcLeftTuples); + } + + if (srcRightTuples.getUpdateFirst() != null) { + doRightUpdates(biLinearJoinNode, sink, bm, srcRightTuples, trgLeftTuples, stagedLeftTuples); + } + if (srcLeftTuples.getUpdateFirst() != null) { + doLeftUpdates(biLinearJoinNode, sink, bm, srcLeftTuples, trgLeftTuples, stagedLeftTuples); + } + + // Process inserts from both inputs + if (srcRightTuples.getInsertFirst() != null) { + doRightInserts(biLinearJoinNode, sink, bm, srcRightTuples, trgLeftTuples); + } + if (srcLeftTuples.getInsertFirst() != null) { + doLeftInserts(biLinearJoinNode, sink, bm, srcLeftTuples, trgLeftTuples); + } + + srcRightTuples.resetAll(); + srcLeftTuples.resetAll(); + } + + /** + * Process left tuple inserts for BiLinearJoinNode. + * This method joins tuples from the primary left input with tuples from the second left input. + */ + public void doLeftInserts(BiLinearJoinNode biLinearJoinNode, + LeftTupleSink sink, + BetaMemory<?> bm, + TupleSets srcLeftTuples, + TupleSets trgLeftTuples) { + + TupleMemory ltm = bm.getLeftTupleMemory(); // Memory for first left input + TupleMemory rtm = bm.getRightTupleMemory(); // Memory for second left input (treated as "right" for memory purposes) + Object contextEntry = bm.getContext(); + + // Get BiLinear constraints wrapper (all BiLinearJoinNode constraints are wrapped) + BiLinearBetaConstraints biLinearConstraints = biLinearJoinNode.getBiLinearConstraints(); + + if (biLinearConstraints == null) { + for (TupleImpl leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) { + TupleImpl next = leftTuple.getStagedNext(); + leftTuple.clearStaged(); + leftTuple = next; + } + return; + } + + BiLinearContextEntry biLinearContext = contextEntry instanceof BiLinearContextEntry ? + (BiLinearContextEntry) contextEntry : null; + + if (biLinearContext == null) { + for (TupleImpl leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) { + TupleImpl next = leftTuple.getStagedNext(); + leftTuple.clearStaged(); + leftTuple = next; + } + return; + } Review Comment: I feel like if this happens, there already was a bug on some other location. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
