using System; using System.Collections; namespace BCG { /// /// Does all the computations required for performing the Joins. The JoinStream is a wrapper around /// InternalJoinStreaming taking caring of the column namings etc /// [DataStreamUserRespresentation (false)] public class InternalJoinStream : ViewStream { protected JoinOperand myLeftOperand, myRightOperand; protected int myLeftJoinColOrdinal, myRightJoinColOrdinal; protected ViewStream myLeftStream, myRightStream; protected JoinTableSchema mySchema; protected DataPacket myLastFiredPacket; protected bool myOnceFired = false; public IBlueTableSchema schema {get{return mySchema;}} public InternalJoinStream (ViewStream vsLeft, string jcLeft, ViewStream vsRight, string jcRight) { myLeftStream = vsLeft; myRightStream = vsRight; myLeftOperand = new JoinOperand (jcLeft, true); myRightOperand = new JoinOperand (jcRight, false); } protected override void myOnFirstSubscriber() { myLeftOperand.master = this; myRightOperand.master = this; myLeftStream.onChange += myLeftOperand.changeHandler; myRightStream.onChange += myRightOperand.changeHandler; } protected override void myOnLastSubscriber () { myInit (); myLeftStream.onChange -= myLeftOperand.changeHandler; myRightStream.onChange -= myRightOperand.changeHandler; } public JoinOperand getOtherOperand (JoinOperand jo) { JoinOperand retVal = jo == myLeftOperand ? myRightOperand : myLeftOperand; return retVal; } public void startSession () { if (myLastFiredPacket != null) { //reset add and deletes myLastFiredPacket.addedRows = new BlueTable (mySchema); myLastFiredPacket.deletedRows = new BlueTable (mySchema); return; } //first time case, need to initialize everything else if (myLeftOperand.hasReceivedData && myRightOperand.hasReceivedData) { myLeftJoinColOrdinal = myLeftOperand.joinColOridinal; myRightJoinColOrdinal = myRightOperand.joinColOridinal; mySchema = new JoinTableSchema (myLeftOperand.operandSchema, myLeftOperand.joinCol, myRightOperand.operandSchema, myRightOperand.joinCol); myInitFiredPacket (); } } protected virtual void myInitFiredPacket () { myLastFiredPacket = new DataPacket (); myLastFiredPacket.newRows = new JoinTable (mySchema); myLastFiredPacket.addedRows = new BlueTable (mySchema); myLastFiredPacket.deletedRows = new BlueTable (mySchema); } public IBlueTableSchema getSchema (bool isLeft) { IBlueTableSchema retVal; retVal = isLeft ? myLeftOperand.operandSchema : myRightOperand.operandSchema; return retVal; } public void endSession () { if (!myOnceFired) { myOnceFired = true; myFireEvent (myLastFiredPacket.Clone()); } else if (myLastFiredPacket.addedRows.Count + myLastFiredPacket.deletedRows.Count != 0) myFireEvent (myLastFiredPacket.Clone()); } protected virtual void myInit () { myLeftOperand.init (); myRightOperand.init (); myLastFiredPacket = null; } public virtual void onTableClear (bool isLeft) { //incase of natural join, left or right wont matter, the end result zeroes out myLastFiredPacket.deletedRows = myLastFiredPacket.newRows; myLastFiredPacket.newRows = new JoinTable (mySchema); myLastFiredPacket.addedRows = new BlueTable (mySchema); } public virtual void deleteRows (IBlueRowCollection rows, bool isLeft) { JoinOperand thisOp, otherOp; int joinColOrdinal; myAssignAccordingly (out thisOp, out otherOp, out joinColOrdinal, isLeft); foreach (IBlueRow row in rows) { thisOp.deleteRow (row); if (otherOp.getMatchingRows (row[joinColOrdinal]).Count == 0) continue; //row does not participate in join ((JoinTable)(myLastFiredPacket.newRows)).deleteRow (isLeft, row, myLastFiredPacket); } } public virtual void addRows (IBlueRowCollection rows, bool isLeft) { JoinOperand thisOp, otherOp; int joinColOrdinal; myAssignAccordingly (out thisOp, out otherOp, out joinColOrdinal, isLeft); if (!isLeft) { thisOp = myRightOperand; otherOp = myLeftOperand; joinColOrdinal = myRightJoinColOrdinal; } foreach (IBlueRow row in rows) { thisOp.addRow (row); RowList matchedNodes = otherOp.getMatchingRows (row[joinColOrdinal]); if (matchedNodes.Count == 0) continue; //this row did not participate in the join if (isLeft) foreach (RowNode matchedNode in matchedNodes) ((JoinTable)myLastFiredPacket.newRows).addRow (row, matchedNode.row, myLastFiredPacket); else foreach (RowNode matchedNode in matchedNodes) ((JoinTable)myLastFiredPacket.newRows).addRow (matchedNode.row, row, myLastFiredPacket); } } private void myAssignAccordingly (out JoinOperand thisOp, out JoinOperand otherOp, out int joinColOrdinal, bool isLeft) { if (isLeft) { thisOp = myLeftOperand; otherOp = myRightOperand; joinColOrdinal = myLeftJoinColOrdinal; } else { thisOp = myRightOperand; otherOp = myLeftOperand; joinColOrdinal = myRightJoinColOrdinal; } } /**************************************TEST METHOD(S)*********************************************/ /// /// Used for testing purposes only. This enables the test to work without a DataSource (PubServer, /// BaseServer etc), by directly calling the test method and passing data /// /// /// public void test (DataPacket dp, bool isLeft) { JoinOperand jo = isLeft ? myLeftOperand : myRightOperand; jo.changeHandler (dp); } /***********************************************************************************************/ } }