Files
spark/src/DataStreamSuite/DataStream/Primitives/Join/JoinOperand.cs
T
John Colvin 9311c27f47 feat: Resurrect DataStream primitives
They were originally commented out because the expression parsing utils
weren't ready (now they are).
2023-09-29 13:10:50 -04:00

114 lines
3.9 KiB
C#

using System;
using System.Collections;
using BCG.Utilities;
namespace BCG
{
/// <summary>
/// Represents each operand of the Join Operations. Does some bookkeeping like mantaining indexes on
/// rows for fast retrieval and row matching
/// </summary>
public class JoinOperand
{
private string myJoinCol;
private DataPacket myLastArrivedPacket;
private JoinOperand myOtherOperand;
private bool myIsLeft;
private JoinOperandRowIndex myRowIndex;
private InternalJoinStream myMaster;
private IBlueTableSchema myOpSchema;
public JoinOperand (string joinCol, bool isLeft)
{
myJoinCol = joinCol;
myIsLeft = isLeft;
}
internal void changeHandler (object o)
{
DataPacket temp = (DataPacket)o;
if (myLastArrivedPacket != null)
mySubsequentChangeHandler ((DataPacket)o);
else
myFirstChangeHandler ((DataPacket)o);
}
private void myFirstChangeHandler (DataPacket arrivedDp)
{
myLastArrivedPacket = arrivedDp;
myOpSchema = myLastArrivedPacket.newRows.schema;
myOtherOperand = myMaster.getOtherOperand (this);
myRowIndex = new JoinOperandRowIndex (myOpSchema[myJoinCol].colNo, myOpSchema["id"].colNo);
if (myOtherOperand.hasReceivedData)
{
//the other operand has already received data, so the join would
//actually be performed now, note, this is the first and only time
//the whole join is being performed. All other joins would be
//incremental
myMaster.startSession ();
myMaster.addRows (myLastArrivedPacket.newRows, myIsLeft);
myMaster.endSession ();
}
else
myRowIndex = new JoinOperandRowIndex (myLastArrivedPacket.newRows,
myOpSchema[myJoinCol].colNo,
myOpSchema["id"].colNo);
}
private void mySubsequentChangeHandler (DataPacket arrivedDp)
{
myLastArrivedPacket = arrivedDp;
if (!myOtherOperand.hasReceivedData)
{
//no reason to call the master to actually perform
//the join, as the other operand is not there. Though
//this case has not been witnessed as yet, but
//theoriticaly its possible
foreach (IBlueRow row in myLastArrivedPacket.deletedRows)
deleteRow (row);
foreach (IBlueRow row in myLastArrivedPacket.addedRows)
addRow (row);
return;
}
myMaster.startSession ();
if (myLastArrivedPacket.newRows.Count == 0)
{
myRowIndex.clear ();
myMaster.onTableClear (myIsLeft);
}
else
{
//the only condition when the join will actually take place
myMaster.deleteRows (myLastArrivedPacket.deletedRows, myIsLeft);
myMaster.addRows (myLastArrivedPacket.addedRows, myIsLeft);
}
myMaster.endSession ();
}
internal void addRow (IBlueRow row) {myRowIndex.add (row);}
internal void deleteRow (IBlueRow row) {myRowIndex.remove (row);}
internal IBlueTableSchema operandSchema {get{return myOpSchema;}}
internal InternalJoinStream master {set{myMaster = value;}}
internal string joinCol {get{return myJoinCol;}}
internal int joinColOridinal {get{return myOpSchema[myJoinCol].colNo;}}
internal RowList getMatchingRows (object key) {return (RowList) myRowIndex.getMatchingRows (key);}
internal bool hasReceivedData {get{return myLastArrivedPacket != null;}}
internal void init ()
{
///should be brought back to the the state before the first callback
///(ie the state after construction)
myRowIndex = null;
myLastArrivedPacket = null;
}
}
}