using Bts.DataStreamCore; using Spark.Core.Diagnostics; using System; using System.Collections; using System.Collections.Generic; namespace BCG { internal class JoinTable : IBlueRowCollection { protected JoinTableSchema mySchema; protected readonly Dictionary myIdIndex = new(); protected JoinDeletionIndex myLeftIndex, myRightIndex; private int myNewRowId; public JoinTable (JoinTableSchema schema) { mySchema = schema; myLeftIndex = new JoinDeletionIndex (mySchema.getRenamedName ("id", true), true); myRightIndex = new JoinDeletionIndex (mySchema.getRenamedName ("id", false), false); } public IBlueRow this [int id] { get { var rowObj = myIdIndex[id] ?? throw new BlueDataException($"ID {id} does not exist in table {mySchema.tableName}"); return (IBlueRow)rowObj; } } #region JoinTable members public virtual void addRow(IBlueRow leftRow, IBlueRow? rightRow, IDataPacket dp) { JoinRow newRow = new JoinRow (myNewRowId, leftRow, rightRow, mySchema); myIdIndex[myNewRowId ++] = newRow; ((BlueTable)(dp.addedRows)).addRow (newRow); myLeftIndex.add (newRow); if (rightRow != null) //in case of left joins when right row is null myRightIndex.add (newRow); } public virtual void deleteRow (bool isLeft, IBlueRow deletedRow, IDataPacket dp) { JoinDeletionIndex thisIndex = myRightIndex, otherIndex = myLeftIndex; if (isLeft) { thisIndex = myLeftIndex; otherIndex = myRightIndex; } RowList matchingNodes = thisIndex.getMatchingRows(deletedRow.Id()); foreach (RowNode node in matchingNodes) { var row = ExceptionAssert.Argument.Is(node.row); RowNode? correspondingNode = isLeft ? row.rightNode : row.leftNode; if (correspondingNode != null) otherIndex.remove (correspondingNode); //in case of partial left joinss myIdIndex.Remove(row.Id()); ((BlueTable)dp.deletedRows).addRow (row); } thisIndex.removeAll(deletedRow.Id()); } #endregion #region ICollection members public int Count{get{return myIdIndex.Count;}} public bool IsSynchronized {get{return false;}} public object SyncRoot{get{return this;}} public void CopyTo (Array array, int index) { foreach (var row in myIdIndex.Values) { array.SetValue(row, index++); } } #endregion #region IEnumerable members public IEnumerator GetEnumerator () {return myIdIndex.Values.GetEnumerator ();} #endregion #region IBlueRowCollection members public IBlueTableSchema schema {get{return mySchema;}} public bool containsID (int id) {return myIdIndex[id] != null;} #endregion } }