146 lines
4.9 KiB
C#
146 lines
4.9 KiB
C#
using Spark.Core.Diagnostics;
|
|
|
|
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
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;
|
|
IDataPacket? myLastArrivedPacket;
|
|
JoinOperand? myOtherOperand;
|
|
private bool myIsLeft;
|
|
JoinOperandRowIndex? myRowIndex;
|
|
InternalJoinStream? myMaster;
|
|
|
|
public JoinOperand (string joinCol, bool isLeft)
|
|
{
|
|
myJoinCol = joinCol;
|
|
myIsLeft = isLeft;
|
|
}
|
|
|
|
[MemberNotNullWhen(true, nameof(myMaster), nameof(myLastArrivedPacket), nameof(operandSchema), nameof(myOtherOperand), nameof(myRowIndex))]
|
|
bool isInitialized {
|
|
get {
|
|
if (myLastArrivedPacket is null) {
|
|
return false;
|
|
}
|
|
ExceptionAssert.NotNull(myMaster, myLastArrivedPacket, operandSchema, myOtherOperand, myRowIndex);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
internal void changeHandler (object o)
|
|
{
|
|
var dp = (IDataPacket)o;
|
|
if (isInitialized) {
|
|
mySubsequentChangeHandler (dp);
|
|
} else {
|
|
myFirstChangeHandler(dp);
|
|
}
|
|
}
|
|
|
|
[MemberNotNull(nameof(myLastArrivedPacket), nameof(operandSchema), nameof(myOtherOperand), nameof(myRowIndex))]
|
|
bool myFirstChangeHandler(IDataPacket arrivedDp) {
|
|
ExceptionAssert.NotNull(myMaster);
|
|
myLastArrivedPacket = arrivedDp;
|
|
operandSchema = myLastArrivedPacket.newRows.schema;
|
|
myOtherOperand = myMaster.getOtherOperand (this);
|
|
myRowIndex = new JoinOperandRowIndex (operandSchema[myJoinCol].colNo, operandSchema["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,
|
|
operandSchema[myJoinCol].colNo,
|
|
operandSchema["id"].colNo);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void mySubsequentChangeHandler (IDataPacket arrivedDp) {
|
|
RequireInitialized();
|
|
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) =>
|
|
ExceptionAssert.NotNull(myRowIndex).add(row);
|
|
|
|
internal void deleteRow (IBlueRow row) {
|
|
RequireInitialized();
|
|
myRowIndex.remove (row);
|
|
}
|
|
|
|
internal IBlueTableSchema? operandSchema { get; private set; }
|
|
|
|
internal InternalJoinStream master {set{myMaster = value;}}
|
|
|
|
internal string joinCol {get{return myJoinCol;}}
|
|
|
|
internal int joinColOridinal {
|
|
get {
|
|
RequireInitialized();
|
|
return operandSchema[myJoinCol].colNo;
|
|
}
|
|
}
|
|
|
|
internal RowList getMatchingRows (object key) {
|
|
RequireInitialized();
|
|
return myRowIndex.getMatchingRows(key);
|
|
}
|
|
|
|
internal bool hasReceivedData {get{return myLastArrivedPacket != null;}}
|
|
|
|
internal void Reset() {
|
|
///should be brought back to the the state before the first callback
|
|
///(ie the state after construction)
|
|
myRowIndex = null;
|
|
myLastArrivedPacket = null;
|
|
}
|
|
|
|
[MemberNotNull(nameof(myMaster), nameof(myLastArrivedPacket), nameof(operandSchema), nameof(myOtherOperand), nameof(myRowIndex))]
|
|
internal void RequireInitialized() {
|
|
if (!isInitialized) {
|
|
throw new InvalidOperationException("Internal state not properly initialized");
|
|
}
|
|
}
|
|
}
|
|
}
|