using Bts.DataStreamCore; using Spark.Core.Diagnostics; using System; using System.Collections.Generic; namespace BCG { /// /// Stream to internally handle Left Joins. The only difference between this and the left Join is the waty the /// DataPacket thats fired is initialized. /// [DataStreamUserRespresentation (false)] public class LeftInternalJoinStream : InternalJoinStream { protected IDataPacket? myLeftDataPacket; protected IDataPacket? myRightDataPacket; //Container to contain all the left rows that are orphaned during a deletion operation Dictionary? myLeftOrphanedRows; public LeftInternalJoinStream (ViewStream vsLeft, string jcLeft, ViewStream vsRight, string jcRight) : base (vsLeft, jcLeft, vsRight, jcRight) {} protected override void myOnFirstSubscriber() { myLeftOperand.master = this; myRightOperand.master = this; //the events are not hooked onto the operands delegate, but rather a level of indirection //added in the form on myLeftChangeHandler myLeftStream.onChange += new DataStream.ChangeDelegate (myLeftChangeHandler); myRightStream.onChange += new DataStream.ChangeDelegate (myRightChangeHandler); } protected override void myOnLastSubscriber () { myInit (); myLeftStream.onChange -= new DataStream.ChangeDelegate (myLeftChangeHandler); myRightStream.onChange -= new DataStream.ChangeDelegate (myRightChangeHandler); } protected override void myInit() { base.myInit (); myLeftOrphanedRows = null; myLeftDataPacket = myRightDataPacket = null; } private void myLeftChangeHandler (object o) { myLeftDataPacket = (DataPacket)o; myTestAndFire (true); } private void myRightChangeHandler (object o) { myRightDataPacket = (DataPacket)o; myTestAndFire (false); } /// /// enforces the order of firing the events, this is to enforce that first the right operand /// has data (possibly 0 rows) before we the join is actually calculated => Performance boost /// /// private void myTestAndFire (bool isLeft) { JoinOperand jo = isLeft ? myLeftOperand : myRightOperand; var dp = isLeft ? ExceptionAssert.NotNull(myLeftDataPacket) : ExceptionAssert.NotNull(myRightDataPacket); //to test whether both have received call backs if (myLeftOperand.hasReceivedData) jo.changeHandler (dp); else if (myLeftDataPacket != null && myRightDataPacket != null) { myRightOperand.changeHandler (myRightDataPacket); myLeftOperand.changeHandler (myLeftDataPacket); } } /// /// Called by JoinOperand /// /// /// public override void addRows (IBlueRowCollection rows, bool isLeft) { ExceptionAssert.NotNull(myLastFiredPacket); foreach (IBlueRow row in rows) myAddRow (row, isLeft); if (!isLeft) foreach (RowNode node in ExceptionAssert.NotNull(myLeftOrphanedRows).Values) { ((JoinTable)myLastFiredPacket.newRows).addRow(node.RequireRow(), null, myLastFiredPacket); } } /// /// called by JoinOperand /// /// /// public override void deleteRows (IBlueRowCollection rows, bool isLeft) { myLeftOrphanedRows = new(); foreach (IBlueRow row in rows) myDeleteRow (row, isLeft); } private void myAddRow (IBlueRow row, bool isLeft) { ExceptionAssert.NotNull(myLastFiredPacket); if (isLeft) { myLeftOperand.addRow (row); var joinKey = ExceptionAssert.NotNull(row[myLeftJoinColOrdinal]); RowList matchedRows = myRightOperand.getMatchingRows(joinKey); if (matchedRows.Count == 0) ((JoinTable)myLastFiredPacket.newRows).addRow (row, null, myLastFiredPacket); else foreach (RowNode matchedNode in matchedRows) ((JoinTable)myLastFiredPacket.newRows).addRow (row, matchedNode.row, myLastFiredPacket); } else { var joinKey = ExceptionAssert.NotNull(row[myRightJoinColOrdinal]); RowList matchedRows = myLeftOperand.getMatchingRows(joinKey); int rightMatchingRowCount = myRightOperand.getMatchingRows(joinKey).Count; myRightOperand.addRow (row); int newRightMatchingRowCount = myRightOperand.getMatchingRows(joinKey).Count; if (matchedRows.Count != 0){ foreach (RowNode matchedNode in matchedRows) { ExceptionAssert.NotNull(matchedNode.row); //if the left rows were orphaned in this transaction (ie in this callback), they should //just be removed from the orphanedlist and we dont need to worry about the consistency of //the jointable as a new row will replace it if (rightMatchingRowCount == 0 && newRightMatchingRowCount > 0) { ExceptionAssert.NotNull(myLeftOrphanedRows); int matchedId = matchedNode.row.Id(); if (!myLeftOrphanedRows.Remove(matchedId)) { ((JoinTable)myLastFiredPacket.newRows).deleteRow(true, matchedNode.row, myLastFiredPacket); } } //they were orphaned by a previous transaction and hence the orphans need to be //removed from the Jointable and the complete row added ((JoinTable)myLastFiredPacket.newRows).addRow (matchedNode.row, row, myLastFiredPacket); } } } } private void myDeleteRow (IBlueRow row, bool isLeft) { var last = requireLastFiredPacket(); if (isLeft) { myLeftOperand.deleteRow (row); ((JoinTable)last.newRows).deleteRow (true, row, myLastFiredPacket); } else { var rightJoinKey = row[myRightJoinColOrdinal] ?? throw new InvalidOperationException("RightJoinKey is null"); RowList matchedRows = myLeftOperand.getMatchingRows(rightJoinKey); int rightMatchingRowCount = myRightOperand.getMatchingRows(rightJoinKey).Count; myRightOperand.deleteRow (row); int newRightMatchingRowCount = myRightOperand.getMatchingRows(rightJoinKey).Count; if (matchedRows.Count != 0) { if (rightMatchingRowCount > 0 && newRightMatchingRowCount == 0) { ExceptionAssert.MemberNotNull(myLeftOrphanedRows); //ie all the matchedRows were joined with 'this' right row (method arg), and hence they need to //be handled specially, ie joined with nulls, if there are no right rows with this //joincolumn value being inserted. ExceptionAssert.NotNull(myLeftOrphanedRows); foreach (RowNode matchedNode in matchedRows) { // BlueEventLog.logAssert (myLeftOrphanedRows[matchedNode.row["id"]] == null); myLeftOrphanedRows[matchedNode.row.Id()] = matchedNode; } } ((JoinTable)last.newRows).deleteRow (false, row, myLastFiredPacket); } } } public override void onTableClear (bool isLeft) { if (myLastFiredPacket is null) { return; } requireSchema(); myLastFiredPacket.deletedRows = myLastFiredPacket.newRows; myLastFiredPacket.newRows = new JoinTable (mySchema); myLastFiredPacket.addedRows = new BlueTable (mySchema); if (!isLeft) { ExceptionAssert.MemberNotNull(myLeftDataPacket); foreach (IBlueRow row in myLeftDataPacket.newRows) ((JoinTable)myLastFiredPacket.newRows).addRow (row, null, myLastFiredPacket); } } } }