From 9311c27f475c237fe9eaa9d767ef69c5bcbbe02b Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 26 Sep 2023 21:29:02 -0400 Subject: [PATCH] feat: Resurrect DataStream primitives They were originally commented out because the expression parsing utils weren't ready (now they are). --- .../DataStream/DataStream.csproj | 1 + .../Primitives/AddColumns/MathStream.cs | 174 +-- .../Distinct/DistinctRowEvaluator.cs | 58 +- .../Primitives/Distinct/DistinctRowStream.cs | 44 +- .../Primitives/GroupBy/GroupByEvaluator.cs | 142 +-- .../Primitives/Join/FullJoinStream.cs | 56 +- .../Join/InternalJoinForeignStream.cs | 60 +- .../Primitives/Join/InternalJoinStream.cs | 332 ++--- .../Primitives/Join/JoinDeletionIndex.cs | 110 +- .../Primitives/Join/JoinForeignStream.cs | 94 +- .../Primitives/Join/JoinForeignTable.cs | 38 +- .../Primitives/Join/JoinNameResolver.cs | 380 +++--- .../DataStream/Primitives/Join/JoinOperand.cs | 196 +-- .../DataStream/Primitives/Join/JoinRow.cs | 253 ++-- .../DataStream/Primitives/Join/JoinStream.cs | 291 ++--- .../DataStream/Primitives/Join/JoinTable.cs | 146 +-- .../Primitives/Join/JoinTableSchema.cs | 206 +-- .../Primitives/Join/MultiColumnJoinStream.cs | 104 +- .../Primitives/Join/ParallelJoinTest.cs | 550 ++++---- .../Primitives/Join/SuperJoinStream.cs | 1130 +++++++++-------- .../LeftJoin/LeftInternalJoinForeignStream.cs | 66 +- .../LeftJoin/LeftInternalJoinStream.cs | 334 ++--- .../LeftJoin/LeftJoinForeignStream.cs | 94 +- .../Primitives/LeftJoin/LeftJoinStream.cs | 94 +- .../Primitives/Project/ColumnMap.cs | 121 +- .../Primitives/Union/AppendStream.cs | 222 ++-- .../DataStreamCore/AssemblyInfo.cs | 3 + .../BlueData/BlueRowExtensions.cs | 12 + .../DataStreamCore/BlueData/MutableBlueRow.cs | 73 +- 29 files changed, 2737 insertions(+), 2647 deletions(-) create mode 100644 src/DataStreamSuite/DataStreamCore/AssemblyInfo.cs create mode 100644 src/DataStreamSuite/DataStreamCore/BlueData/BlueRowExtensions.cs diff --git a/src/DataStreamSuite/DataStream/DataStream.csproj b/src/DataStreamSuite/DataStream/DataStream.csproj index dcd90c3e9..507294e20 100644 --- a/src/DataStreamSuite/DataStream/DataStream.csproj +++ b/src/DataStreamSuite/DataStream/DataStream.csproj @@ -6,6 +6,7 @@ + diff --git a/src/DataStreamSuite/DataStream/Primitives/AddColumns/MathStream.cs b/src/DataStreamSuite/DataStream/Primitives/AddColumns/MathStream.cs index 38ace9708..9e7cd7ae4 100644 --- a/src/DataStreamSuite/DataStream/Primitives/AddColumns/MathStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/AddColumns/MathStream.cs @@ -1,100 +1,102 @@ -// using System; -// using System.Collections.Generic; -// using System.Linq; -// using System.Text; +using Bts.ArithmeticExpressions; -// namespace BCG -// { +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; -// public class BooleanStringReplaceStream : ViewStream, IStreamMapper -// { -// private BooleanExpressionFilter _bool; -// private string _column, _value; -// private IBlueTableSchema _schema; -// public BooleanStringReplaceStream(ViewStream vs, string boolean_expression, string column, string value) -// { -// _column = column; -// _value = value; -// if(!string.IsNullOrEmpty(boolean_expression)) -// _bool = new BooleanExpressionFilter(boolean_expression); -// mySetRealSource(new MapStream(vs, this)); -// } -// public IBlueTableSchema getOutputSchema(IBlueTableSchema inputSchema) -// { -// _schema = inputSchema; -// return inputSchema; -// } -// public IBlueRow map(IBlueRow row) -// { -// MutableBlueRow result = new MutableBlueRow(_schema, row); -// if(_bool == null || _bool.filter(row)) -// result.setValue(_column, _value); -// return result; -// } +namespace BCG +{ -// } + public class BooleanStringReplaceStream : ViewStream, IStreamMapper + { + private BooleanExpressionFilter _bool; + private string _column, _value; + private IBlueTableSchema _schema; + public BooleanStringReplaceStream(ViewStream vs, string boolean_expression, string column, string value) + { + _column = column; + _value = value; + if(!string.IsNullOrEmpty(boolean_expression)) + _bool = new BooleanExpressionFilter(boolean_expression); + mySetRealSource(new MapStream(vs, this)); + } + public IBlueTableSchema getOutputSchema(IBlueTableSchema inputSchema) + { + _schema = inputSchema; + return inputSchema; + } + public IBlueRow map(IBlueRow row) + { + MutableBlueRow result = new MutableBlueRow(_schema, row); + if(_bool == null || _bool.filter(row)) + result.setValue(_column, _value); + return result; + } -// public class MathStream : ViewStream, IStreamMapper -// { -// private ArithmeticExpressionEvaluator.ArithmeticExpression myExp; -// private BooleanExpressionFilter _bool; -// List myExtraColumns = new List(); -// BlueTableSchema mySchema; + } + + public class MathStream : ViewStream, IStreamMapper + { + private IArithmeticProgram myExp; + private BooleanExpressionFilter _bool; + List myExtraColumns = new List(); + BlueTableSchema mySchema; -// public MathStream(ViewStream vs, string mathExpression) -// : this(vs, mathExpression, string.Empty) -// { -// } + public MathStream(ViewStream vs, string mathExpression) + : this(vs, mathExpression, string.Empty) + { + } -// public MathStream(ViewStream vs, string mathExpression, string boolean_expression) -// { -// myExp = ArithmeticExpressionEvaluator.compile(mathExpression); -// myExtraColumns = ArithmeticExpressionEvaluator.getOutputIdentifiers(myExp); -// if(!string.IsNullOrEmpty(boolean_expression)) -// _bool = new BooleanExpressionFilter(boolean_expression); -// mySetRealSource(new MapStream(vs, this)); -// } + public MathStream(ViewStream vs, string mathExpression, string boolean_expression) + { + myExp = ArithmeticProgramParser.Parse(mathExpression) ?? + throw new Exception($"Failed to parse ArithmeticExpression '{mathExpression}'"); + myExtraColumns = myExp.OutputIdentifiers.ToList(); + if(!string.IsNullOrEmpty(boolean_expression)) + _bool = new BooleanExpressionFilter(boolean_expression); + mySetRealSource(new MapStream(vs, this)); + } -// #region IStreamMapper Members + #region IStreamMapper Members -// public IBlueTableSchema getOutputSchema(IBlueTableSchema inputSchema) -// { -// if (mySchema == null) -// { -// Dictionary colIdx = new Dictionary(); -// foreach (ColumnInfo ci in inputSchema.columnInfo) -// colIdx[ci.colName] = ci; + public IBlueTableSchema getOutputSchema(IBlueTableSchema inputSchema) + { + if (mySchema == null) + { + Dictionary colIdx = new Dictionary(); + foreach (ColumnInfo ci in inputSchema.columnInfo) + colIdx[ci.colName] = ci; -// foreach (string ncn in myExtraColumns) -// { -// ColumnInfo newC; -// if (colIdx.ContainsKey(ncn)) -// newC = new ColumnInfo(colIdx[ncn].colName, colIdx[ncn].colNo, typeof(double)); -// else -// newC = new ColumnInfo(ncn, colIdx.Count, typeof(double)); -// colIdx[ncn] = newC; -// } -// mySchema = new BlueTableSchema(colIdx.Values.ToArray(), "math(" + inputSchema.tableName + ")"); -// } -// return mySchema; -// } + foreach (string ncn in myExtraColumns) + { + ColumnInfo newC; + if (colIdx.ContainsKey(ncn)) + newC = new ColumnInfo(colIdx[ncn].colName, colIdx[ncn].colNo, typeof(double)); + else + newC = new ColumnInfo(ncn, colIdx.Count, typeof(double)); + colIdx[ncn] = newC; + } + mySchema = new BlueTableSchema(colIdx.Values.ToArray(), "math(" + inputSchema.tableName + ")"); + } + return mySchema; + } -// #endregion + #endregion -// #region IDynamicStreamMapper Members + #region IDynamicStreamMapper Members -// public IBlueRow map(IBlueRow row) -// { -// MutableBlueRow result = new MutableBlueRow(mySchema, row); -// if(_bool == null || _bool.filter(row)) -// { -// foreach(var kvp in ArithmeticExpressionEvaluator.eval(myExp, (System.Collections.IDictionary)result)) -// result.setValue(kvp.Key, kvp.Value); -// } -// return result; -// } + public IBlueRow map(IBlueRow row) + { + MutableBlueRow result = new MutableBlueRow(mySchema, row); + if(_bool == null || _bool.filter(row)) + { + myExp.Evaluate(result.AsDictionary()); + } + return result; + } -// #endregion -// } -// } + #endregion + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Distinct/DistinctRowEvaluator.cs b/src/DataStreamSuite/DataStream/Primitives/Distinct/DistinctRowEvaluator.cs index a1908da0b..5239d51ba 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Distinct/DistinctRowEvaluator.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Distinct/DistinctRowEvaluator.cs @@ -1,36 +1,36 @@ -// using System; -// using System.Collections; +using System; +using System.Collections; -// using BCG.Utilities; +using BCG.Utilities; -// namespace BCG -// { -// public class DistinctRowEvaluator : GroupByEvaluator -// { +namespace BCG +{ + public class DistinctRowEvaluator : GroupByEvaluator + { -// private int myIdGenerator = 0; -// public DistinctRowEvaluator (string[] columnNames) : base (null, columnNames) {} + private int myIdGenerator = 0; + public DistinctRowEvaluator (string[] columnNames) : base (null, columnNames) {} -// public DistinctRowEvaluator(string colMapExpression, string[] columnNames) -// : base (colMapExpression, null, columnNames) {} + public DistinctRowEvaluator(string colMapExpression, string[] columnNames) + : base (colMapExpression, null, columnNames) {} -// public override IBlueRow evaluate (DataPacket packet) -// { -// myOutputTable = myOutputTable == null ? new GroupByOutputTable (packet.schema) : myOutputTable; -// string hashString = GroupByStream.getRepString (packet, myGroupByCols); -// if (packet.newRows.Count == 0) -// //if there are no rows for this group we remove the group from the output table -// myOutputTable.removeRow (hashString); -// else if (myOutputTable.getRow (hashString) == null) -// //a row is added for the first time (or first time after the removal of the group) -// myOutputTable.addRow ( -// BlueDataUtility.getNewReplacedRow ( -// BlueDataUtility.extractRow (packet.newRows, 0), -// myIdGenerator++, "id"), -// hashString); + public override IBlueRow evaluate (DataPacket packet) + { + myOutputTable = myOutputTable == null ? new GroupByOutputTable (packet.schema) : myOutputTable; + string hashString = GroupByStream.getRepString (packet, myGroupByCols); + if (packet.newRows.Count == 0) + //if there are no rows for this group we remove the group from the output table + myOutputTable.removeRow (hashString); + else if (myOutputTable.getRow (hashString) == null) + //a row is added for the first time (or first time after the removal of the group) + myOutputTable.addRow ( + BlueDataUtility.getNewReplacedRow ( + BlueDataUtility.extractRow (packet.newRows, 0), + myIdGenerator++, "id"), + hashString); -// return myProject (myOutputTable.getRow (hashString)); -// } -// } -// } + return myProject (myOutputTable.getRow (hashString)); + } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Distinct/DistinctRowStream.cs b/src/DataStreamSuite/DataStream/Primitives/Distinct/DistinctRowStream.cs index dae2754b7..8184b378a 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Distinct/DistinctRowStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Distinct/DistinctRowStream.cs @@ -1,23 +1,23 @@ -// using System; -// using System.Collections; +using System; +using System.Collections; -// namespace BCG -// { -// /// -// /// Provides a stream that with distinct rows with respect to the columns -// /// the user specifies in the argument -// /// -// [DataStreamUserRespresentation ("distinct")] -// public class DistinctRowStream : ViewStream -// { -// public DistinctRowStream (ViewStream stream, params string[] cols) -// { -// ArrayList groupByCols = new ArrayList (); -// foreach (string col in cols) -// foreach (string c in col.Split (' ')) -// groupByCols.Add (c); -// mySetRealSource (new GroupByStream (stream, -// new DistinctRowEvaluator (groupByCols.ToArray (typeof(string)) as string[]), false)); -// } -// } -// } +namespace BCG +{ + /// + /// Provides a stream that with distinct rows with respect to the columns + /// the user specifies in the argument + /// + [DataStreamUserRespresentation ("distinct")] + public class DistinctRowStream : ViewStream + { + public DistinctRowStream (ViewStream stream, params string[] cols) + { + ArrayList groupByCols = new ArrayList (); + foreach (string col in cols) + foreach (string c in col.Split (' ')) + groupByCols.Add (c); + mySetRealSource (new GroupByStream (stream, + new DistinctRowEvaluator (groupByCols.ToArray (typeof(string)) as string[]), false)); + } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/GroupBy/GroupByEvaluator.cs b/src/DataStreamSuite/DataStream/Primitives/GroupBy/GroupByEvaluator.cs index 73a2929af..0802b91e5 100644 --- a/src/DataStreamSuite/DataStream/Primitives/GroupBy/GroupByEvaluator.cs +++ b/src/DataStreamSuite/DataStream/Primitives/GroupBy/GroupByEvaluator.cs @@ -1,82 +1,82 @@ -// using System; +using System; -// namespace BCG -// { -// /// -// /// Serves as an abtraction for some of the functionalities needed in implementations -// /// of IGroupByEvaluator -// /// -// public abstract class GroupByEvaluator : IGroupByEvaluator -// { -// protected string[] myGroupByCols; -// protected string myOutputColumn; -// protected GroupByOutputTable myOutputTable; -// protected string myColMapExpression; +namespace BCG +{ + /// + /// Serves as an abtraction for some of the functionalities needed in implementations + /// of IGroupByEvaluator + /// + public abstract class GroupByEvaluator : IGroupByEvaluator + { + protected string[] myGroupByCols; + protected string myOutputColumn; + protected GroupByOutputTable myOutputTable; + protected string myColMapExpression; -// #region constructors -// public GroupByEvaluator (string outputCol, string[] groupByCols) -// { -// myOutputColumn = outputCol; -// myGroupByCols = groupByCols; -// } + #region constructors + public GroupByEvaluator (string outputCol, string[] groupByCols) + { + myOutputColumn = outputCol; + myGroupByCols = groupByCols; + } -// public GroupByEvaluator (string colMapExpression, string outputCol, string[] groupByCols) -// : this (outputCol, groupByCols) {myColMapExpression = colMapExpression;} -// #endregion + public GroupByEvaluator (string colMapExpression, string outputCol, string[] groupByCols) + : this (outputCol, groupByCols) {myColMapExpression = colMapExpression;} + #endregion -// #region IGroupByEvaluator methods -// public virtual string[] groupByColumns {get{return myGroupByCols;}} + #region IGroupByEvaluator methods + public virtual string[] groupByColumns {get{return myGroupByCols;}} -// public virtual void clear () -// { -// if ( myOutputTable != null ) -// myOutputTable.clear (); -// } + public virtual void clear () + { + if ( myOutputTable != null ) + myOutputTable.clear (); + } -// public virtual IBlueTableSchema getOutputSchema (IBlueTableSchema schema) -// { -// IBlueTableSchema retVal = -// myColMapExpression == null || myColMapExpression.Length < 1 ? -// schema : -// new ProjectHelper (schema, myColMapExpression, schema.tableName).schema; -// return retVal; -// } + public virtual IBlueTableSchema getOutputSchema (IBlueTableSchema schema) + { + IBlueTableSchema retVal = + myColMapExpression == null || myColMapExpression.Length < 1 ? + schema : + new ProjectHelper (schema, myColMapExpression, schema.tableName).schema; + return retVal; + } -// public abstract IBlueRow evaluate (DataPacket dp) ; -// #endregion + public abstract IBlueRow evaluate (DataPacket dp) ; + #endregion -// #region Helper methods -// /// -// /// Projects the r, replacing the id of the returned rows with the id provided in the arg -// /// -// /// -// /// -// /// -// protected IBlueRow myProject (IBlueRow r, int id) -// { -// return r == null ? null : -// myColMapExpression == null || myColMapExpression.Length < 1 ? -// BlueDataUtility.getNewReplacedRow (r, id, "id") : -// BlueDataUtility.getNewReplacedRow(myProjectRow (r, myColMapExpression), -// id, "id"); -// } + #region Helper methods + /// + /// Projects the r, replacing the id of the returned rows with the id provided in the arg + /// + /// + /// + /// + protected IBlueRow myProject (IBlueRow r, int id) + { + return r == null ? null : + myColMapExpression == null || myColMapExpression.Length < 1 ? + BlueDataUtility.getNewReplacedRow (r, id, "id") : + BlueDataUtility.getNewReplacedRow(myProjectRow (r, myColMapExpression), + id, "id"); + } -// /// -// /// Projects the id, conserving the id -// /// -// /// -// /// -// protected IBlueRow myProject (IBlueRow r) {return myProject (r, r == null ? Int32.MinValue : (int)r["id"]);} -// #endregion + /// + /// Projects the id, conserving the id + /// + /// + /// + protected IBlueRow myProject (IBlueRow r) {return myProject (r, r == null ? Int32.MinValue : (int)r["id"]);} + #endregion -// private IBlueRow myProjectRow (IBlueRow r, string colMapExpression) -// { -// BlueTable auxTable = new BlueTable (r.schema); -// auxTable.addRow (r); -// BlueRowCollectionStaticStream staticStream = new BlueRowCollectionStaticStream (auxTable); -// ProjectStream projStream = new ProjectStream (staticStream, colMapExpression); -// return BlueDataUtility.extractRow (projStream.rows, 0); -// } + private IBlueRow myProjectRow (IBlueRow r, string colMapExpression) + { + BlueTable auxTable = new BlueTable (r.schema); + auxTable.addRow (r); + BlueRowCollectionStaticStream staticStream = new BlueRowCollectionStaticStream (auxTable); + ProjectStream projStream = new ProjectStream (staticStream, colMapExpression); + return BlueDataUtility.extractRow (projStream.rows, 0); + } -// } -// } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/FullJoinStream.cs b/src/DataStreamSuite/DataStream/Primitives/Join/FullJoinStream.cs index 31a17fc3a..258345589 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/FullJoinStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/FullJoinStream.cs @@ -1,31 +1,31 @@ -// using System; -// using System.Collections.Generic; -// using System.Linq; -// using System.Text; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; -// namespace BCG -// { -// public class FullJoinStream : ViewStream -// { -// private ViewStream myMainStream; -// public FullJoinStream(ViewStream v1, string joinCol1, string cmExp1, -// ViewStream v2, string joinCol2, string cmExp2, string empty_col) -// { -// myMainStream = new LeftJoinStream(v1, joinCol1, cmExp1, v2, joinCol2, cmExp2); -// ViewStream filtered = new LeftJoinStream(v2, joinCol2, cmExp2, v1, joinCol1, cmExp1); -// filtered = new FilterStream(filtered, String.Format("{0}=null", empty_col)); -// myMainStream = new AppendStream(myMainStream, filtered); -// } +namespace BCG +{ + public class FullJoinStream : ViewStream + { + private ViewStream myMainStream; + public FullJoinStream(ViewStream v1, string joinCol1, string cmExp1, + ViewStream v2, string joinCol2, string cmExp2, string empty_col) + { + myMainStream = new LeftJoinStream(v1, joinCol1, cmExp1, v2, joinCol2, cmExp2); + ViewStream filtered = new LeftJoinStream(v2, joinCol2, cmExp2, v1, joinCol1, cmExp1); + filtered = new FilterStream(filtered, String.Format("{0}=null", empty_col)); + myMainStream = new AppendStream(myMainStream, filtered); + } -// protected override void myOnFirstSubscriber() -// { -// myMainStream.onChange += myHandleMainStreamChange; -// } + protected override void myOnFirstSubscriber() + { + myMainStream.onChange += myHandleMainStreamChange; + } -// private void myHandleMainStreamChange(object newData) -// { -// IBlueRowCollection result = (newData as DataPacket).newRows; -// myFireEvent(new DataPacket(result, result, myLastValue == null ? new BlueTable(result.schema) : (myLastValue as DataPacket).newRows)); -// } -// } -// } + private void myHandleMainStreamChange(object newData) + { + IBlueRowCollection result = (newData as DataPacket).newRows; + myFireEvent(new DataPacket(result, result, myLastValue == null ? new BlueTable(result.schema) : (myLastValue as DataPacket).newRows)); + } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/InternalJoinForeignStream.cs b/src/DataStreamSuite/DataStream/Primitives/Join/InternalJoinForeignStream.cs index 3a842b994..51bf7fdbb 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/InternalJoinForeignStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/InternalJoinForeignStream.cs @@ -1,35 +1,35 @@ -// using System; +using System; -// namespace BCG -// { -// [DataStreamUserRespresentation (false)] -// public class InternalJoinForeignStream : InternalJoinStream -// { -// public InternalJoinForeignStream (ViewStream vsLeft, -// string jcLeft, -// ViewStream vsRight, -// string jcRight) -// : base (vsLeft, jcLeft, vsRight, jcRight) {} +namespace BCG +{ + [DataStreamUserRespresentation (false)] + public class InternalJoinForeignStream : InternalJoinStream + { + public InternalJoinForeignStream (ViewStream vsLeft, + string jcLeft, + ViewStream vsRight, + string jcRight) + : base (vsLeft, jcLeft, vsRight, jcRight) {} -// public InternalJoinForeignStream (ViewStream vsLeft, -// string jcLeft, -// ViewStream vsRight) : this (vsLeft, jcLeft, vsRight, "id") {} + public InternalJoinForeignStream (ViewStream vsLeft, + string jcLeft, + ViewStream vsRight) : this (vsLeft, jcLeft, vsRight, "id") {} -// protected override void myInitFiredPacket () -// { -// myLastFiredPacket = new DataPacket (); -// myLastFiredPacket.newRows = new JoinForeignTable (mySchema); -// myLastFiredPacket.addedRows = new BlueTable (mySchema); -// myLastFiredPacket.deletedRows = new BlueTable (mySchema); -// } + protected override void myInitFiredPacket () + { + myLastFiredPacket = new DataPacket (); + myLastFiredPacket.newRows = new JoinForeignTable (mySchema); + myLastFiredPacket.addedRows = new BlueTable (mySchema); + myLastFiredPacket.deletedRows = new BlueTable (mySchema); + } -// public override 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 JoinForeignTable (mySchema); -// myLastFiredPacket.addedRows = new BlueTable (mySchema); -// } + public override 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 JoinForeignTable (mySchema); + myLastFiredPacket.addedRows = new BlueTable (mySchema); + } -// } -// } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/InternalJoinStream.cs b/src/DataStreamSuite/DataStream/Primitives/Join/InternalJoinStream.cs index d8edb70b7..0f9f9c16c 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/InternalJoinStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/InternalJoinStream.cs @@ -1,183 +1,183 @@ -// using System; -// using System.Collections; +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; +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 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); -// } + 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 += new DataStream.ChangeDelegate (myLeftOperand.changeHandler); -// myRightStream.onChange += new DataStream.ChangeDelegate (myRightOperand.changeHandler); -// } + protected override void myOnFirstSubscriber() + { + myLeftOperand.master = this; + myRightOperand.master = this; + myLeftStream.onChange += new DataStream.ChangeDelegate (myLeftOperand.changeHandler); + myRightStream.onChange += new DataStream.ChangeDelegate (myRightOperand.changeHandler); + } -// protected override void myOnLastSubscriber () -// { -// myInit (); -// myLeftStream.onChange -= new DataStream.ChangeDelegate (myLeftOperand.changeHandler); -// myRightStream.onChange -= new DataStream.ChangeDelegate (myRightOperand.changeHandler); -// } + protected override void myOnLastSubscriber () + { + myInit (); + myLeftStream.onChange -= new DataStream.ChangeDelegate (myLeftOperand.changeHandler); + myRightStream.onChange -= new DataStream.ChangeDelegate (myRightOperand.changeHandler); + } -// public JoinOperand getOtherOperand (JoinOperand jo) -// { -// JoinOperand retVal = jo == myLeftOperand ? myRightOperand : myLeftOperand; -// return retVal; -// } + 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 (); -// } -// } + 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); -// } + 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 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()); -// } + 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; -// } + 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 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 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); -// } -// } + 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; -// } -// } + 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); -// } -// /***********************************************************************************************/ -// } -// } + /**************************************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); + } + /***********************************************************************************************/ + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/JoinDeletionIndex.cs b/src/DataStreamSuite/DataStream/Primitives/Join/JoinDeletionIndex.cs index b2a1099e8..0db00f9ec 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/JoinDeletionIndex.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/JoinDeletionIndex.cs @@ -1,64 +1,64 @@ -// using System; -// using System.Collections; +using System; +using System.Collections; -// namespace BCG -// { -// /// -// /// Hashtable of DoubleyLinkedList tailored for the Join deletions -// /// -// internal class JoinDeletionIndex -// { -// IDictionary myIndex = new Hashtable (); -// private string myOpIdName ; -// private bool myIsLeft; +namespace BCG +{ + /// + /// Hashtable of DoubleyLinkedList tailored for the Join deletions + /// + internal class JoinDeletionIndex + { + IDictionary myIndex = new Hashtable (); + private string myOpIdName ; + private bool myIsLeft; -// /// -// /// -// /// opIdName represents the name of the column corresponding to id of the operand table -// /// Indicator for left or right operand table -// public JoinDeletionIndex(string opIdName, bool isLeft) -// { -// myOpIdName = opIdName; -// myIsLeft = isLeft; -// } + /// + /// + /// opIdName represents the name of the column corresponding to id of the operand table + /// Indicator for left or right operand table + public JoinDeletionIndex(string opIdName, bool isLeft) + { + myOpIdName = opIdName; + myIsLeft = isLeft; + } -// /// -// /// Adds a joinRow to the hashtable; in addition the joinRow is made to point to the node added -// /// -// /// JoinRow to add -// public void add (JoinRow row) -// { -// object o = row[myOpIdName]; -// if (myIndex[o] == null) -// myIndex[o] = new RowList (); -// RowNode nodeAdded = ((RowList)myIndex[o]).add (row); -// if (myIsLeft) -// row.leftNode = nodeAdded; -// else -// row.rightNode = nodeAdded; + /// + /// Adds a joinRow to the hashtable; in addition the joinRow is made to point to the node added + /// + /// JoinRow to add + public void add (JoinRow row) + { + object o = row[myOpIdName]; + if (myIndex[o] == null) + myIndex[o] = new RowList (); + RowNode nodeAdded = ((RowList)myIndex[o]).add (row); + if (myIsLeft) + row.leftNode = nodeAdded; + else + row.rightNode = nodeAdded; -// } + } -// /// -// /// To remove a node, if node does not exist exception is thrown -// /// -// /// -// public void remove (RowNode node) {((RowList)myIndex[node.row[myOpIdName]]).remove (node);} + /// + /// To remove a node, if node does not exist exception is thrown + /// + /// + public void remove (RowNode node) {((RowList)myIndex[node.row[myOpIdName]]).remove (node);} -// public RowList getMatchingRows (object key) { -// RowList retVal = (RowList)myIndex[key]; -// if (retVal == null) -// return new RowList (); -// return retVal; -// } + public RowList getMatchingRows (object key) { + RowList retVal = (RowList)myIndex[key]; + if (retVal == null) + return new RowList (); + return retVal; + } -// /// -// /// Empties the entry for the key provided -// /// -// /// -// public void removeAll (object obj) {myIndex.Remove(obj);} + /// + /// Empties the entry for the key provided + /// + /// + public void removeAll (object obj) {myIndex.Remove(obj);} -// public void clear () {myIndex.Clear ();} + public void clear () {myIndex.Clear ();} -// } -// } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/JoinForeignStream.cs b/src/DataStreamSuite/DataStream/Primitives/Join/JoinForeignStream.cs index a28fb7090..be6dfa0bb 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/JoinForeignStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/JoinForeignStream.cs @@ -1,59 +1,59 @@ -// using System; -// using System.Data; -// using System.Collections; -// using System.Text.RegularExpressions; -// using BCG.Utilities; -// using BCG.Utilities.General; +using System; +using System.Data; +using System.Collections; +using System.Text.RegularExpressions; +using BCG.Utilities; +using BCG.Utilities.General; -// namespace BCG -// { -// public class ForeignJoinStream : JoinStream -// { +namespace BCG +{ + public class ForeignJoinStream : JoinStream + { -// public ForeignJoinStream (ViewStream v1, string joinCol1, ColumnMap cm1, -// ViewStream v2, string joinCol2, ColumnMap cm2) -// : base (v1, joinCol1, cm1, v2, joinCol2, cm2) -// {} + public ForeignJoinStream (ViewStream v1, string joinCol1, ColumnMap cm1, + ViewStream v2, string joinCol2, ColumnMap cm2) + : base (v1, joinCol1, cm1, v2, joinCol2, cm2) + {} -// public ForeignJoinStream (ViewStream v1, string joinCol1, string cmExp1, -// ViewStream v2, string joinCol2, string cmExp2) -// : base (v1, joinCol1, cmExp1, v2, joinCol2, cmExp2) -// {} + public ForeignJoinStream (ViewStream v1, string joinCol1, string cmExp1, + ViewStream v2, string joinCol2, string cmExp2) + : base (v1, joinCol1, cmExp1, v2, joinCol2, cmExp2) + {} -// public ForeignJoinStream (ViewStream v1, string joinCol1, string cmExp1, -// ViewStream v2, string joinCol2) -// : -// this (v1,joinCol1, new ColumnMap(cmExp1), v2, joinCol2) -// {} + public ForeignJoinStream (ViewStream v1, string joinCol1, string cmExp1, + ViewStream v2, string joinCol2) + : + this (v1,joinCol1, new ColumnMap(cmExp1), v2, joinCol2) + {} -// public ForeignJoinStream (ViewStream v1, string joinCol1, -// ViewStream v2, string joinCol2, string cmExp2) -// : -// this (v1,joinCol1, v2, joinCol2, new ColumnMap(cmExp2)) -// {} + public ForeignJoinStream (ViewStream v1, string joinCol1, + ViewStream v2, string joinCol2, string cmExp2) + : + this (v1,joinCol1, v2, joinCol2, new ColumnMap(cmExp2)) + {} -// public ForeignJoinStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2) -// : base (v1, joinCol1, v2, joinCol2) -// {} + public ForeignJoinStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2) + : base (v1, joinCol1, v2, joinCol2) + {} -// public ForeignJoinStream (ViewStream v1, string joinCol1, ColumnMap cm1, ViewStream v2, string joinCol2) -// : base (v1, joinCol1, cm1, v2, joinCol2) -// {} + public ForeignJoinStream (ViewStream v1, string joinCol1, ColumnMap cm1, ViewStream v2, string joinCol2) + : base (v1, joinCol1, cm1, v2, joinCol2) + {} -// public ForeignJoinStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2, ColumnMap cm2) -// : base (v1, joinCol1, v2, joinCol2, cm2) -// {} + public ForeignJoinStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2, ColumnMap cm2) + : base (v1, joinCol1, v2, joinCol2, cm2) + {} -// protected override InternalJoinStream myConstructInternalJoinStream ( -// ViewStream vs1, -// string jc1, -// ViewStream vs2, -// string jc2) -// { -// return new InternalJoinForeignStream (vs1, jc1, vs2, jc2); -// } -// } -// } + protected override InternalJoinStream myConstructInternalJoinStream ( + ViewStream vs1, + string jc1, + ViewStream vs2, + string jc2) + { + return new InternalJoinForeignStream (vs1, jc1, vs2, jc2); + } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/JoinForeignTable.cs b/src/DataStreamSuite/DataStream/Primitives/Join/JoinForeignTable.cs index ec7fda2d8..9bf32b527 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/JoinForeignTable.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/JoinForeignTable.cs @@ -1,20 +1,22 @@ -// using System; -// using BCG.Utilities; +using System; +using BCG.Utilities; -// namespace BCG -// { -// internal class JoinForeignTable : JoinTable -// { -// public JoinForeignTable (JoinTableSchema schema) : base (schema) {} +using bts; -// public override void addRow (IBlueRow leftRow, IBlueRow rightRow, DataPacket dp) -// { -// JoinRow newRow = new JoinRow (leftRow, rightRow, mySchema); -// BlueEventLog.logAssert (myIdIndex[leftRow["id"]] == null); -// myIdIndex[leftRow["id"]] = newRow; -// ((BlueTable)(dp.addedRows)).addRow (newRow); -// myLeftIndex.add (newRow); -// myRightIndex.add (newRow); -// } -// } -// } +namespace BCG +{ + internal class JoinForeignTable : JoinTable + { + public JoinForeignTable (JoinTableSchema schema) : base (schema) {} + + public override void addRow (IBlueRow leftRow, IBlueRow rightRow, DataPacket dp) + { + JoinRow newRow = new JoinRow (leftRow, rightRow, mySchema); + Log.assert(myIdIndex[leftRow["id"]] == null); + myIdIndex[leftRow["id"]] = newRow; + ((BlueTable)(dp.addedRows)).addRow (newRow); + myLeftIndex.add (newRow); + myRightIndex.add (newRow); + } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/JoinNameResolver.cs b/src/DataStreamSuite/DataStream/Primitives/Join/JoinNameResolver.cs index 62fb042f3..56dd56a6f 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/JoinNameResolver.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/JoinNameResolver.cs @@ -1,211 +1,205 @@ -// using System; -// using System.Collections; -// using BCG.Utilities; -// using BCG.Evaluator.ColumnMapExpression; +using System; +using System.Collections; +using BCG.Utilities; -// namespace BCG -// { -// /// -// /// JoinNameResolver is used to bring together the 2 naming processes, ie the one done ny Join to resolve -// /// clashes and the second, that the user does to rename columns -// /// -// public class JoinNameResolver -// { -// /// -// /// Internal class to represent a column (ie name) and its association, left or right -// /// -// internal class OriginalNameInfo -// { -// public bool isLeft; -// public string colName; -// public OriginalNameInfo (string colName, bool isLeft) {this.colName = colName; this.isLeft = isLeft;} -// public override int GetHashCode () -// { -// string prependString = isLeft ? "left" : "right"; -// return String.Concat (prependString, colName).GetHashCode (); -// } -// public override bool Equals (object obj) {return GetHashCode () == obj.GetHashCode ();} -// } +using bts; + +using Bts.ColumnMapExpressions; + +namespace BCG +{ + /// + /// JoinNameResolver is used to bring together the 2 naming processes, ie the one done ny Join to resolve + /// clashes and the second, that the user does to rename columns + /// + public class JoinNameResolver + { + /// + /// Internal class to represent a column (ie name) and its association, left or right + /// + internal class OriginalNameInfo + { + public bool isLeft; + public string colName; + public OriginalNameInfo (string colName, bool isLeft) {this.colName = colName; this.isLeft = isLeft;} + public override int GetHashCode () + { + string prependString = isLeft ? "left" : "right"; + return String.Concat (prependString, colName).GetHashCode (); + } + public override bool Equals (object obj) {return GetHashCode () == obj.GetHashCode ();} + } -// private ArrayList myJoinNames; -// private JoinNameResolver.OriginalNameInfo[] myOriginalColInfo; -// private JoinTableSchema myJoinSchema; -// private IBlueTableSchema myLeftSchema, myRightSchema; -// private string myLeftColExpression, myRightColExpression; -// private string[] myUserRules, myResolvedNames; -// private IDictionary myFinalMap = new Hashtable (); + private ArrayList myJoinNames; + private JoinNameResolver.OriginalNameInfo[] myOriginalColInfo; + private JoinTableSchema myJoinSchema; + private IBlueTableSchema myLeftSchema, myRightSchema; + private string myLeftColExpression, myRightColExpression; + private string[] myUserRules, myResolvedNames; + private IDictionary myFinalMap = new Hashtable (); -// public JoinNameResolver (JoinTableSchema joinSchema, IBlueTableSchema leftSchema, string leftColMapExpression, -// IBlueTableSchema rightSchema, string rightColMapExpression) -// { -// myJoinSchema = joinSchema; -// myLeftSchema = leftSchema; -// myRightSchema = rightSchema; -// myLeftColExpression = leftColMapExpression; -// myRightColExpression = rightColMapExpression; -// /*initialize the joinNames one-D structure. The id is removed as it does not -// * take part in the renaming process */ -// myJoinNames = new ArrayList (joinSchema.columnNames); -// /*initialize the originalInfo one-D structure. Its an array of OriginalNameInfo, ie name and a bool indicating that -// * it belongs to the left or right table */ -// myOriginalColInfo = new OriginalNameInfo [joinSchema.columnNames.Length - 1]; -// myUserRules = new String[myOriginalColInfo.Length]; -// myFormOriginalNameInfo (true); -// myFormOriginalNameInfo (false); -// myFormUserRules (); -// myFormResolvedNames (); -// myFormFinalMap (); -// } + public JoinNameResolver (JoinTableSchema joinSchema, IBlueTableSchema leftSchema, string leftColMapExpression, + IBlueTableSchema rightSchema, string rightColMapExpression) + { + myJoinSchema = joinSchema; + myLeftSchema = leftSchema; + myRightSchema = rightSchema; + myLeftColExpression = leftColMapExpression; + myRightColExpression = rightColMapExpression; + /*initialize the joinNames one-D structure. The id is removed as it does not + * take part in the renaming process */ + myJoinNames = new ArrayList (joinSchema.columnNames); + /*initialize the originalInfo one-D structure. Its an array of OriginalNameInfo, ie name and a bool indicating that + * it belongs to the left or right table */ + myOriginalColInfo = new OriginalNameInfo [joinSchema.columnNames.Length - 1]; + myUserRules = new String[myOriginalColInfo.Length]; + myFormOriginalNameInfo (true); + myFormOriginalNameInfo (false); + myFormUserRules (); + myFormResolvedNames (); + myFormFinalMap (); + } -// /// -// /// Forms the array representing the name as they appear in the JoinOperand. -// /// This is done so that its alligned with the myJoinNames -// /// -// /// indicating whether its the left operand or the right -// private void myFormOriginalNameInfo (bool isLeft) -// { -// IBlueTableSchema operandSchema = isLeft ? myLeftSchema : myRightSchema; -// foreach (string colName in operandSchema.columnNames) -// { -// string joinName = myJoinSchema.getRenamedName (colName, isLeft); -// //joinName is bound to be unique -// int index = myJoinNames.IndexOf (joinName); -// BlueEventLog.logAssert (index >= 0 && index < myOriginalColInfo.Length); -// myOriginalColInfo[index] = new OriginalNameInfo(colName, isLeft); -// } -// } -// static syntax myParser = -// new syntax(); + /// + /// Forms the array representing the name as they appear in the JoinOperand. + /// This is done so that its alligned with the myJoinNames + /// + /// indicating whether its the left operand or the right + private void myFormOriginalNameInfo (bool isLeft) + { + IBlueTableSchema operandSchema = isLeft ? myLeftSchema : myRightSchema; + foreach (string colName in operandSchema.columnNames) + { + string joinName = myJoinSchema.getRenamedName (colName, isLeft); + //joinName is bound to be unique + int index = myJoinNames.IndexOf (joinName); + Log.assert(index >= 0 && index < myOriginalColInfo.Length); + myOriginalColInfo[index] = new OriginalNameInfo(colName, isLeft); + } + } -// /// -// /// Creates the user mapping from the grammer and then actually call the methods that form the array of -// /// user defined names -// /// -// private void myFormUserRules () -// { -// if (DataStreamLoadFileConfig.instance.useStaticParser) -// { -// lock (myParser) -// { -// myFormUserRules(true, (ColumnMapExpression) -// myParser.Parse(myLeftColExpression)); -// myFormUserRules(false, (ColumnMapExpression) -// myParser.Parse(myRightColExpression)); -// } -// } -// else -// { -// syntax parser = new syntax(); -// myFormUserRules(true, (ColumnMapExpression) -// parser.Parse(myLeftColExpression)); -// myFormUserRules(false, (ColumnMapExpression) -// parser.Parse(myRightColExpression)); -// } + /// + /// Creates the user mapping from the grammer and then actually call the methods that form the array of + /// user defined names + /// + private void myFormUserRules () + { + if (DataStreamLoadFileConfig.instance.useStaticParser) + { + myFormUserRules(true, ColumnMapExpression.Parse(myLeftColExpression)); + myFormUserRules(false, ColumnMapExpression.Parse(myRightColExpression)); + } + else + { + // TODO (maybe): Actually use non-static parser (unclear what advantages it will have) + myFormUserRules(true, ColumnMapExpression.Parse(myLeftColExpression)); + myFormUserRules(false, ColumnMapExpression.Parse(myRightColExpression)); + } -// } + } -// /// -// /// Forms the array of user defined names -// /// -// /// -// /// -// private void myFormUserRules (bool isLeft, ColumnMapExpression cme) -// { -// IDictionary userMap = cme.columns; -// if (cme.isInclusive) -// for(int i = 0; i < myOriginalColInfo.Length ; i++) -// { -// OriginalNameInfo oni = myOriginalColInfo[i]; -// if (oni.isLeft == isLeft) -// { -// if (userMap[oni.colName] != null) -// { -// if ((string)userMap[oni.colName] != oni.colName) -// myUserRules[i] = (string)userMap[oni.colName]; -// else -// myUserRules[i] = oni.colName; -// } -// } -// } -// else -// { -// for (int i = 0; i < myOriginalColInfo.Length; i ++) -// { -// OriginalNameInfo oni = myOriginalColInfo[i]; -// if (oni.isLeft == isLeft) -// { -// if (userMap[oni.colName] == null) -// myUserRules[i] = oni.colName; -// else if ((string)userMap[oni.colName] != oni.colName) -// myUserRules[i] = (string)userMap[oni.colName]; -// } -// } -// } -// } + /// + /// Forms the array of user defined names + /// + /// + /// + private void myFormUserRules (bool isLeft, ColumnMapExpression cme) + { + IDictionary userMap = cme.Columns; + if (cme.IsInclusive) + for(int i = 0; i < myOriginalColInfo.Length ; i++) + { + OriginalNameInfo oni = myOriginalColInfo[i]; + if (oni.isLeft == isLeft) + { + if (userMap[oni.colName] != null) + { + if ((string)userMap[oni.colName] != oni.colName) + myUserRules[i] = (string)userMap[oni.colName]; + else + myUserRules[i] = oni.colName; + } + } + } + else + { + for (int i = 0; i < myOriginalColInfo.Length; i ++) + { + OriginalNameInfo oni = myOriginalColInfo[i]; + if (oni.isLeft == isLeft) + { + if (userMap[oni.colName] == null) + myUserRules[i] = oni.colName; + else if ((string)userMap[oni.colName] != oni.colName) + myUserRules[i] = (string)userMap[oni.colName]; + } + } + } + } -// private void myFormResolvedNames() -// { -// string prependString; -// myResolvedNames = new string[myUserRules.Length]; -// Array.Copy (myUserRules, myResolvedNames, myUserRules.Length); -// string[] temp = myUserRules; -// myUserRules = new String[temp.Length + 1]; -// Array.Copy (temp, 0, myUserRules, 0, temp.Length); -// myUserRules[myUserRules.Length - 1] = "id"; + private void myFormResolvedNames() + { + string prependString; + myResolvedNames = new string[myUserRules.Length]; + Array.Copy (myUserRules, myResolvedNames, myUserRules.Length); + string[] temp = myUserRules; + myUserRules = new String[temp.Length + 1]; + Array.Copy (temp, 0, myUserRules, 0, temp.Length); + myUserRules[myUserRules.Length - 1] = "id"; -// for (int i = 0; i < myResolvedNames.Length; i ++) -// { -// ArrayList occurances = myFindAllInCollection (myUserRules, myResolvedNames[i]); -// while (occurances.Count > 1) -// { -// prependString = i < myLeftSchema.columnNames.Length ? "L." : "R."; -// myResolvedNames[i] = prependString + myResolvedNames[i]; -// occurances = myFindAllInCollection (myUserRules, myResolvedNames[i]); -// occurances.AddRange (myFindAllInCollection (myResolvedNames, myResolvedNames[i])); -// } -// } -// } + for (int i = 0; i < myResolvedNames.Length; i ++) + { + ArrayList occurances = myFindAllInCollection (myUserRules, myResolvedNames[i]); + while (occurances.Count > 1) + { + prependString = i < myLeftSchema.columnNames.Length ? "L." : "R."; + myResolvedNames[i] = prependString + myResolvedNames[i]; + occurances = myFindAllInCollection (myUserRules, myResolvedNames[i]); + occurances.AddRange (myFindAllInCollection (myResolvedNames, myResolvedNames[i])); + } + } + } -// private void myFormFinalMap () -// { -// foreach (string resolvedName in myResolvedNames) -// if (resolvedName != null) -// myFinalMap [resolvedName] = myJoinNames[Array.IndexOf (myResolvedNames, resolvedName)]; -// } + private void myFormFinalMap () + { + foreach (string resolvedName in myResolvedNames) + if (resolvedName != null) + myFinalMap [resolvedName] = myJoinNames[Array.IndexOf (myResolvedNames, resolvedName)]; + } -// /// -// /// Utility method to invert a hashtable -// /// -// /// -// /// + /// + /// Utility method to invert a hashtable + /// + /// + /// -// private IDictionary reciprocateDictionary (IDictionary arg) -// { -// IDictionary retVal = new Hashtable (); -// foreach (DictionaryEntry de in arg) -// retVal[de.Value] = de.Key; -// return retVal; -// } + private IDictionary reciprocateDictionary (IDictionary arg) + { + IDictionary retVal = new Hashtable (); + foreach (DictionaryEntry de in arg) + retVal[de.Value] = de.Key; + return retVal; + } -// /// -// /// Utility method to find all occurances of an obkect in an IList -// /// -// /// -// /// -// /// -// private ArrayList myFindAllInCollection (IList list, object obj) -// { -// ArrayList retVal = new ArrayList (); -// for (int i = 0; i < list.Count; i++) -// if (list[i] != null && list[i].Equals (obj)) -// retVal.Add (i); -// return retVal; -// } + /// + /// Utility method to find all occurances of an obkect in an IList + /// + /// + /// + /// + private ArrayList myFindAllInCollection (IList list, object obj) + { + ArrayList retVal = new ArrayList (); + for (int i = 0; i < list.Count; i++) + if (list[i] != null && list[i].Equals (obj)) + retVal.Add (i); + return retVal; + } -// public IDictionary getMap () {return myFinalMap;} + public IDictionary getMap () {return myFinalMap;} -// } -// } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/JoinOperand.cs b/src/DataStreamSuite/DataStream/Primitives/Join/JoinOperand.cs index b1576801f..235a3f343 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/JoinOperand.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/JoinOperand.cs @@ -1,113 +1,113 @@ -// // using System; -// // using System.Collections; -// // using BCG.Utilities; +using System; +using System.Collections; +using BCG.Utilities; -// // namespace BCG -// // { -// // /// -// // /// Represents each operand of the Join Operations. Does some bookkeeping like mantaining indexes on -// // /// rows for fast retrieval and row matching -// // /// -// // public class JoinOperand -// // { -// // private string myJoinCol; -// // private DataPacket myLastArrivedPacket; -// // private JoinOperand myOtherOperand; -// // private bool myIsLeft; -// // private JoinOperandRowIndex myRowIndex; -// // private InternalJoinStream myMaster; -// // private IBlueTableSchema myOpSchema; +namespace BCG +{ + /// + /// Represents each operand of the Join Operations. Does some bookkeeping like mantaining indexes on + /// rows for fast retrieval and row matching + /// + 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; -// // } + 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); -// // } + 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 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 (); -// // } + 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 addRow (IBlueRow row) {myRowIndex.add (row);} -// // internal void deleteRow (IBlueRow row) {myRowIndex.remove (row);} + internal void deleteRow (IBlueRow row) {myRowIndex.remove (row);} -// // internal IBlueTableSchema operandSchema {get{return myOpSchema;}} + internal IBlueTableSchema operandSchema {get{return myOpSchema;}} -// // internal InternalJoinStream master {set{myMaster = value;}} + internal InternalJoinStream master {set{myMaster = value;}} -// // internal string joinCol {get{return myJoinCol;}} + internal string joinCol {get{return myJoinCol;}} -// // internal int joinColOridinal {get{return myOpSchema[myJoinCol].colNo;}} + internal int joinColOridinal {get{return myOpSchema[myJoinCol].colNo;}} -// // internal RowList getMatchingRows (object key) {return (RowList) myRowIndex.getMatchingRows (key);} + internal RowList getMatchingRows (object key) {return (RowList) myRowIndex.getMatchingRows (key);} -// // internal bool hasReceivedData {get{return myLastArrivedPacket != null;}} + 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; -// // } + internal void init () + { + ///should be brought back to the the state before the first callback + ///(ie the state after construction) + myRowIndex = null; + myLastArrivedPacket = null; + } -// // } -// // } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/JoinRow.cs b/src/DataStreamSuite/DataStream/Primitives/Join/JoinRow.cs index 96a7d3be1..e4f8425b5 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/JoinRow.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/JoinRow.cs @@ -1,154 +1,155 @@ -// using System; -// using BCG.Utilities; -// using System.Collections; +using System; +using BCG.Utilities; +using System.Collections; +using bts; -// namespace BCG -// { -// internal class JoinRow : IBlueRow -// { -// private RowNode myLeftNode, myRightNode; -// protected IBlueRow myLeftRow, myRightRow; -// protected int myId; -// protected JoinTableSchema mySchema; -// private object[] myValues; +namespace BCG +{ + internal class JoinRow : IBlueRow + { + private RowNode myLeftNode, myRightNode; + protected IBlueRow myLeftRow, myRightRow; + protected int myId; + protected JoinTableSchema mySchema; + private object[] myValues; -// #region Constructor(s) + #region Constructor(s) -// /// -// /// Constructs a row, assigning it the same ID as that of the left row. -// /// -// /// left row of the join -// /// right row of the join -// /// schema of the joined row -// public JoinRow (IBlueRow leftRow, IBlueRow rightRow, JoinTableSchema schema) -// : this ((int)leftRow["id"], leftRow, rightRow, schema) {} + /// + /// Constructs a row, assigning it the same ID as that of the left row. + /// + /// left row of the join + /// right row of the join + /// schema of the joined row + public JoinRow (IBlueRow leftRow, IBlueRow rightRow, JoinTableSchema schema) + : this ((int)leftRow["id"], leftRow, rightRow, schema) {} -// /// -// /// Constructs a row assigning it the ID provioded as the first argument -// /// -// /// id of the joined row -// /// left row of the join -// /// right row of the join -// /// schema of the joined row -// public JoinRow (int id, IBlueRow leftRow, IBlueRow rightRow, JoinTableSchema schema) -// { -// myId = id; -// myLeftRow = leftRow; -// myRightRow = rightRow; -// mySchema = schema; -// myValues = new object [schema.columnNames.Length]; -// } + /// + /// Constructs a row assigning it the ID provioded as the first argument + /// + /// id of the joined row + /// left row of the join + /// right row of the join + /// schema of the joined row + public JoinRow (int id, IBlueRow leftRow, IBlueRow rightRow, JoinTableSchema schema) + { + myId = id; + myLeftRow = leftRow; + myRightRow = rightRow; + mySchema = schema; + myValues = new object [schema.columnNames.Length]; + } -// #endregion + #endregion -// #region IBlueRow members -// public IBlueTableSchema schema {get{return mySchema;}} -// #endregion + #region IBlueRow members + public IBlueTableSchema schema {get{return mySchema;}} + #endregion -// public bool isPartialJoin{get{return myRightRow == null;}} + public bool isPartialJoin{get{return myRightRow == null;}} -// public RowNode leftNode -// { -// get{return myLeftNode;} -// set{myLeftNode = value;} -// } + public RowNode leftNode + { + get{return myLeftNode;} + set{myLeftNode = value;} + } -// public RowNode rightNode -// { -// get{return myRightNode;} -// set{myRightNode = value;} -// } + public RowNode rightNode + { + get{return myRightNode;} + set{myRightNode = value;} + } -// #region ICollection members -// public int Count{get{return mySchema.columnNames.Length;}} + #region ICollection members + public int Count{get{return mySchema.columnNames.Length;}} -// public bool IsSynchronized {get{return false;}} + public bool IsSynchronized {get{return false;}} -// public object SyncRoot{get{return this;}} + public object SyncRoot{get{return this;}} -// public void CopyTo (Array array, int index) -// { -// int i = 0; -// foreach (object o in this as IEnumerable) -// array.SetValue (o, i++); -// } + public void CopyTo (Array array, int index) + { + int i = 0; + foreach (object o in this as IEnumerable) + array.SetValue (o, i++); + } -// #endregion + #endregion -// #region IDictionary members -// public bool IsFixedSize {get{return true;}} + #region IDictionary members + public bool IsFixedSize {get{return true;}} -// public bool IsReadOnly {get{return true;}} + public bool IsReadOnly {get{return true;}} -// /// -// /// The key can either be the string corrsponding to the column name or -// /// the order of the column -// /// -// public virtual object this [object key] -// { -// get -// { -// if (key is string) -// return this[(string)key]; -// return this[(int)key]; -// } -// set {throw new NotSupportedException ("The IBlueRow is read only");} -// } + /// + /// The key can either be the string corrsponding to the column name or + /// the order of the column + /// + public virtual object this [object key] + { + get + { + if (key is string) + return this[(string)key]; + return this[(int)key]; + } + set {throw new NotSupportedException ("The IBlueRow is read only");} + } -// public object this [int index] -// { -// get -// { -// if (index == mySchema.columnNames.Length - 1) -// return myId; -// else if (index < myLeftRow.Count) -// { -// if (myValues[index] == null) -// myValues[index] = myLeftRow[index]; -// return myValues[index]; -// } -// else if (index >= myLeftRow.Count && index < mySchema.columnNames.Length - 1) -// { -// if (myValues[index] == null) -// myValues[index] = (myRightRow == null) ? DBNull.Value : myRightRow[index - myLeftRow.Count]; -// return myValues[index]; -// } -// BlueEventLog.logAssert (false, "Incorrect index in JoinRow"); -// return null; -// } -// } + public object this [int index] + { + get + { + if (index == mySchema.columnNames.Length - 1) + return myId; + else if (index < myLeftRow.Count) + { + if (myValues[index] == null) + myValues[index] = myLeftRow[index]; + return myValues[index]; + } + else if (index >= myLeftRow.Count && index < mySchema.columnNames.Length - 1) + { + if (myValues[index] == null) + myValues[index] = (myRightRow == null) ? DBNull.Value : myRightRow[index - myLeftRow.Count]; + return myValues[index]; + } + Log.assert(false, "Incorrect index in JoinRow"); + return null; + } + } -// public object this [string colName] {get{return this[mySchema[colName].colNo];}} + public object this [string colName] {get{return this[mySchema[colName].colNo];}} -// public ICollection Keys{get{return mySchema.columnNames;}} + public ICollection Keys{get{return mySchema.columnNames;}} -// public ICollection Values -// { -// get -// { -// object[] retVal = new object[mySchema.columnNames.Length]; -// CopyTo (retVal, 0); -// return retVal; -// } -// } + public ICollection Values + { + get + { + object[] retVal = new object[mySchema.columnNames.Length]; + CopyTo (retVal, 0); + return retVal; + } + } -// public void Add (object key, object value) {throw new NotSupportedException ("The IBlueRow is readonly");} + public void Add (object key, object value) {throw new NotSupportedException ("The IBlueRow is readonly");} -// public void Clear () {throw new NotSupportedException ("The IBlueRow is readonly");} + public void Clear () {throw new NotSupportedException ("The IBlueRow is readonly");} -// public bool Contains (object key){return mySchema[(string)key] != null;} + public bool Contains (object key){return mySchema[(string)key] != null;} -// /// -// /// Note, the default enumerator is the IDictionary's enumerator, so if user needs to -// /// iterate through the values, typecast this into an IEnumerable -// /// -// /// -// public IDictionaryEnumerator GetEnumerator() {return new BlueRowDictionaryEnumerator(this);} + /// + /// Note, the default enumerator is the IDictionary's enumerator, so if user needs to + /// iterate through the values, typecast this into an IEnumerable + /// + /// + public IDictionaryEnumerator GetEnumerator() {return new BlueRowDictionaryEnumerator(this);} -// IEnumerator IEnumerable.GetEnumerator () {return new BlueRowEnumerator(this);} + IEnumerator IEnumerable.GetEnumerator () {return new BlueRowEnumerator(this);} -// public void Remove (object key) {throw new NotSupportedException ("The IBlueRow is readonly");} -// #endregion -// } + public void Remove (object key) {throw new NotSupportedException ("The IBlueRow is readonly");} + #endregion + } -// } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/JoinStream.cs b/src/DataStreamSuite/DataStream/Primitives/Join/JoinStream.cs index 60daab27f..7d05eeefa 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/JoinStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/JoinStream.cs @@ -1,165 +1,166 @@ -// using System; -// using System.Data; -// using System.Collections; -// using System.Text.RegularExpressions; -// using BCG.Utilities; -// using BCG.Utilities.General; +using System; +using System.Data; +using System.Collections; +using System.Text.RegularExpressions; +using BCG.Utilities; +using BCG.Utilities.General; +using bts; -// namespace BCG -// { -// public class JoinStream : ViewStream -// { -// protected InternalJoinStream myIjs; -// protected ProjectStream myPs; -// protected ColumnMap myLeftCm, myRightCm; -// protected bool myIsProject = true; -// private bool myUseSuperJoin = DataStreamLoadFileConfig.instance.useNewJoin; +namespace BCG +{ + public class JoinStream : ViewStream + { + protected InternalJoinStream myIjs; + protected ProjectStream myPs; + protected ColumnMap myLeftCm, myRightCm; + protected bool myIsProject = true; + private bool myUseSuperJoin = DataStreamLoadFileConfig.instance.useNewJoin; -// protected JoinStream () {} -// public JoinStream (ViewStream v1, string joinCol1, ColumnMap cm1, -// ViewStream v2, string joinCol2, ColumnMap cm2) -// { -// if (myUseSuperJoin) -// { -// ViewStream rs = this is LeftJoinStream ? -// new SuperLeftJoinStream(v1, joinCol1, cm1.columnExpression, v2, joinCol2, cm2.columnExpression) as ViewStream : -// new SuperJoinStream(v1, joinCol1, cm1.columnExpression, v2, joinCol2, cm2.columnExpression) as ViewStream; -// mySetRealSource(rs); -// } -// else -// { -// myLeftCm = cm1; -// myRightCm = cm2; -// //myIjs = new InternalJoinStream (v1, joinCol1, v2, joinCol2); -// myIjs = myConstructInternalJoinStream(v1, joinCol1, v2, joinCol2); -// } -// } + protected JoinStream () {} + public JoinStream (ViewStream v1, string joinCol1, ColumnMap cm1, + ViewStream v2, string joinCol2, ColumnMap cm2) + { + if (myUseSuperJoin) + { + ViewStream rs = this is LeftJoinStream ? + new SuperLeftJoinStream(v1, joinCol1, cm1.columnExpression, v2, joinCol2, cm2.columnExpression) as ViewStream : + new SuperJoinStream(v1, joinCol1, cm1.columnExpression, v2, joinCol2, cm2.columnExpression) as ViewStream; + mySetRealSource(rs); + } + else + { + myLeftCm = cm1; + myRightCm = cm2; + //myIjs = new InternalJoinStream (v1, joinCol1, v2, joinCol2); + myIjs = myConstructInternalJoinStream(v1, joinCol1, v2, joinCol2); + } + } -// public JoinStream (ViewStream v1, string joinCol1, string cmExp1, -// ViewStream v2, string joinCol2, string cmExp2) -// : -// this ( -// v1, -// joinCol1, -// (cmExp1 == null ? new ColumnMap("!"):new ColumnMap(cmExp1)), -// v2, -// joinCol2, -// (cmExp2 == null ? new ColumnMap("!"):new ColumnMap(cmExp2))) -// {} + public JoinStream (ViewStream v1, string joinCol1, string cmExp1, + ViewStream v2, string joinCol2, string cmExp2) + : + this ( + v1, + joinCol1, + (cmExp1 == null ? new ColumnMap("!"):new ColumnMap(cmExp1)), + v2, + joinCol2, + (cmExp2 == null ? new ColumnMap("!"):new ColumnMap(cmExp2))) + {} -// public JoinStream (ViewStream v1, string joinCol1, string cmExp1, -// ViewStream v2, string joinCol2) -// : -// this (v1,joinCol1, new ColumnMap(cmExp1), v2, joinCol2) -// {} + public JoinStream (ViewStream v1, string joinCol1, string cmExp1, + ViewStream v2, string joinCol2) + : + this (v1,joinCol1, new ColumnMap(cmExp1), v2, joinCol2) + {} -// public JoinStream (ViewStream v1, string joinCol1, -// ViewStream v2, string joinCol2, string cmExp2) -// : -// this (v1,joinCol1, v2, joinCol2, new ColumnMap(cmExp2)) -// {} + public JoinStream (ViewStream v1, string joinCol1, + ViewStream v2, string joinCol2, string cmExp2) + : + this (v1,joinCol1, v2, joinCol2, new ColumnMap(cmExp2)) + {} -// public JoinStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2) -// { -// if (myUseSuperJoin) -// { -// ViewStream rs = this is LeftJoinStream ? -// new SuperLeftJoinStream(v1, joinCol1, v2, joinCol2) as ViewStream : -// new SuperJoinStream(v1, joinCol1, v2, joinCol2) as ViewStream; -// mySetRealSource(rs); -// } -// else -// { -// //myIjs = new InternalJoinStream (v1, joinCol1, v2, joinCol2); -// myIjs = myConstructInternalJoinStream(v1, joinCol1, v2, joinCol2); -// myIsProject = false; -// } -// } + public JoinStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2) + { + if (myUseSuperJoin) + { + ViewStream rs = this is LeftJoinStream ? + new SuperLeftJoinStream(v1, joinCol1, v2, joinCol2) as ViewStream : + new SuperJoinStream(v1, joinCol1, v2, joinCol2) as ViewStream; + mySetRealSource(rs); + } + else + { + //myIjs = new InternalJoinStream (v1, joinCol1, v2, joinCol2); + myIjs = myConstructInternalJoinStream(v1, joinCol1, v2, joinCol2); + myIsProject = false; + } + } -// public JoinStream (ViewStream v1, string joinCol1, ColumnMap cm1, ViewStream v2, string joinCol2) -// { -// myLeftCm = cm1; -// myRightCm = new ColumnMap ("!"); -// //myIjs = new InternalJoinStream (v1, joinCol1, v2, joinCol2); -// myIjs = myConstructInternalJoinStream (v1, joinCol1, v2, joinCol2); -// } + public JoinStream (ViewStream v1, string joinCol1, ColumnMap cm1, ViewStream v2, string joinCol2) + { + myLeftCm = cm1; + myRightCm = new ColumnMap ("!"); + //myIjs = new InternalJoinStream (v1, joinCol1, v2, joinCol2); + myIjs = myConstructInternalJoinStream (v1, joinCol1, v2, joinCol2); + } -// public JoinStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2, ColumnMap cm2) -// { -// myLeftCm = new ColumnMap ("!"); -// myRightCm = cm2; -// //myIjs = new InternalJoinStream (v1, joinCol1, v2, joinCol2); -// myIjs = myConstructInternalJoinStream (v1, joinCol1, v2, joinCol2); + public JoinStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2, ColumnMap cm2) + { + myLeftCm = new ColumnMap ("!"); + myRightCm = cm2; + //myIjs = new InternalJoinStream (v1, joinCol1, v2, joinCol2); + myIjs = myConstructInternalJoinStream (v1, joinCol1, v2, joinCol2); -// } + } -// protected virtual InternalJoinStream myConstructInternalJoinStream ( -// ViewStream vs1, -// string joinCol1, -// ViewStream vs2, -// string joinCol2) -// { -// return new InternalJoinStream (vs1, joinCol1, vs2, joinCol2); -// } + protected virtual InternalJoinStream myConstructInternalJoinStream ( + ViewStream vs1, + string joinCol1, + ViewStream vs2, + string joinCol2) + { + return new InternalJoinStream (vs1, joinCol1, vs2, joinCol2); + } -// protected void myInternalJoinChangeHandler (object o) -// { -// if (myIsProject) -// { -// if (myPs == null) -// { -// JoinNameResolver resolver = new JoinNameResolver ((JoinTableSchema)myIjs.schema, myIjs.getSchema (true), myLeftCm.columnExpression, -// myIjs.getSchema (false), myRightCm.columnExpression); -// IDictionary map = resolver.getMap (); -// map["id"] = "id"; -// myPs = new ProjectStream (myIjs, map); -// myPs.onChange += new DataStream.ChangeDelegate (myProjectChangeHandler); -// return; -// } -// else -// BlueEventLog.logAssert (false, "This call back should not have been called"); -// } -// else -// myFireEvent ((o as ICloneable).Clone()); -// } + protected void myInternalJoinChangeHandler (object o) + { + if (myIsProject) + { + if (myPs == null) + { + JoinNameResolver resolver = new JoinNameResolver ((JoinTableSchema)myIjs.schema, myIjs.getSchema (true), myLeftCm.columnExpression, + myIjs.getSchema (false), myRightCm.columnExpression); + IDictionary map = resolver.getMap (); + map["id"] = "id"; + myPs = new ProjectStream (myIjs, map); + myPs.onChange += new DataStream.ChangeDelegate (myProjectChangeHandler); + return; + } + else + Log.assert(false, "This call back should not have been called"); + } + else + myFireEvent ((o as ICloneable).Clone()); + } -// private void myProjectChangeHandler (object o) -// { -// if (myIjs.myOnChangeDelegate.GetInvocationList().Length > 1) -// myIjs.onChange -= new DataStream.ChangeDelegate (myInternalJoinChangeHandler); -// myFireEvent ((o as ICloneable).Clone()); + private void myProjectChangeHandler (object o) + { + if (myIjs.myOnChangeDelegate.GetInvocationList().Length > 1) + myIjs.onChange -= new DataStream.ChangeDelegate (myInternalJoinChangeHandler); + myFireEvent ((o as ICloneable).Clone()); -// } + } -// protected override void myOnFirstSubscriber () -// { -// myIjs.onChange += new DataStream.ChangeDelegate (myInternalJoinChangeHandler); -// } + protected override void myOnFirstSubscriber () + { + myIjs.onChange += new DataStream.ChangeDelegate (myInternalJoinChangeHandler); + } -// protected override void myOnLastSubscriber () -// { -// // how does this relate to the -= in myProjectChangeHandler? -// myIjs.onChange -= new DataStream.ChangeDelegate (myInternalJoinChangeHandler); + protected override void myOnLastSubscriber () + { + // how does this relate to the -= in myProjectChangeHandler? + myIjs.onChange -= new DataStream.ChangeDelegate (myInternalJoinChangeHandler); -// // if there is a project after the ijs we need to -// // unsubscribe to it otherwise references will be kept and -// // a memory leak will occur -// if (myPs != null) -// myPs.onChange -= new DataStream.ChangeDelegate (myProjectChangeHandler); -// myPs = null; -// } + // if there is a project after the ijs we need to + // unsubscribe to it otherwise references will be kept and + // a memory leak will occur + if (myPs != null) + myPs.onChange -= new DataStream.ChangeDelegate (myProjectChangeHandler); + myPs = null; + } -// /**************************************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) {myIjs.test (dp, isLeft);} -// /***********************************************************************************************/ -// } -// } + /**************************************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) {myIjs.test (dp, isLeft);} + /***********************************************************************************************/ + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/JoinTable.cs b/src/DataStreamSuite/DataStream/Primitives/Join/JoinTable.cs index d7c99d48f..a2d88c1ea 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/JoinTable.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/JoinTable.cs @@ -1,84 +1,84 @@ -// // using System; -// // using System.Collections; -// // using System.IO; -// // using BCG.Utilities.General; +using System; +using System.Collections; +using System.IO; +using BCG.Utilities.General; -// // namespace BCG -// // { -// // internal class JoinTable : IBlueRowCollection -// // { -// // protected JoinTableSchema mySchema; -// // protected IDictionary myIdIndex = new Hashtable (); -// // protected JoinDeletionIndex myLeftIndex, myRightIndex; -// // private int myNewRowId; +namespace BCG +{ + internal class JoinTable : IBlueRowCollection + { + protected JoinTableSchema mySchema; + protected IDictionary myIdIndex = new Hashtable (); + 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 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 -// // { -// // if (myIdIndex[id] == null) -// // throw new BlueDataException ("ID " + id.ToString() + " does not exist in table " + mySchema.tableName); -// // return (IBlueRow)myIdIndex[id]; -// // } -// // } + public IBlueRow this [int id] + { + get + { + if (myIdIndex[id] == null) + throw new BlueDataException ("ID " + id.ToString() + " does not exist in table " + mySchema.tableName); + return (IBlueRow)myIdIndex[id]; + } + } -// // #region JoinTable members -// // public virtual void addRow (IBlueRow leftRow, IBlueRow rightRow, DataPacket 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); -// // } + #region JoinTable members + public virtual void addRow (IBlueRow leftRow, IBlueRow rightRow, DataPacket 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, DataPacket dp) -// // { -// // JoinDeletionIndex thisIndex = myRightIndex, otherIndex = myLeftIndex; -// // if (isLeft) -// // { -// // thisIndex = myLeftIndex; -// // otherIndex = myRightIndex; -// // } -// // RowList matchingNodes = thisIndex.getMatchingRows (deletedRow["id"]); -// // foreach (RowNode node in matchingNodes) -// // { -// // JoinRow row = (JoinRow)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 virtual void deleteRow (bool isLeft, IBlueRow deletedRow, DataPacket dp) + { + JoinDeletionIndex thisIndex = myRightIndex, otherIndex = myLeftIndex; + if (isLeft) + { + thisIndex = myLeftIndex; + otherIndex = myRightIndex; + } + RowList matchingNodes = thisIndex.getMatchingRows (deletedRow["id"]); + foreach (RowNode node in matchingNodes) + { + JoinRow row = (JoinRow)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 bool IsSynchronized {get{return false;}} -// // public object SyncRoot{get{return this;}} + public object SyncRoot{get{return this;}} -// // public void CopyTo (Array array, int index) {myIdIndex.Values.CopyTo (array, index);} -// // #endregion + public void CopyTo (Array array, int index) {myIdIndex.Values.CopyTo (array, index);} + #endregion -// // #region IEnumerable members -// // public IEnumerator GetEnumerator () {return myIdIndex.Values.GetEnumerator ();} -// // #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 -// // } + #region IBlueRowCollection members + public IBlueTableSchema schema {get{return mySchema;}} + public bool containsID (int id) {return myIdIndex[id] != null;} + #endregion + } -// // } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/JoinTableSchema.cs b/src/DataStreamSuite/DataStream/Primitives/Join/JoinTableSchema.cs index 3126d28db..74ae14431 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/JoinTableSchema.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/JoinTableSchema.cs @@ -1,107 +1,109 @@ -// using System; -// using System.Collections; -// using BCG.Utilities; +using System; +using System.Collections; +using BCG.Utilities; -// namespace BCG -// { -// /// -// /// Represents the schema of the Join Table. This schema is formed by combining the respective -// /// schemas of the operands. -// /// -// public class JoinTableSchema : BlueTableSchema -// { -// private string myLeftJc = "", myRightJc = "", myLeftId = "", myRightId = ""; -// private IDictionary myLeftNameToAlias = new Hashtable (); -// private IDictionary myRightNameToAlias = new Hashtable (); -// private IDictionary myAliasToLeftName = new Hashtable(), -// myAliasToRightName = new Hashtable(); -// /// -// /// Constructs the schema of the Jointable with the schema of the left and right tables. Incase of -// /// a name being there in both the shemas it prefixes the name with "L." or "R." accordingly. Column "id" -// /// is the last column in the schema -// /// -// /// -// /// -// /// -// /// -// public JoinTableSchema (IBlueTableSchema leftSchema, string leftJc, -// IBlueTableSchema rightSchema, string rightJc) -// { -// myIndexByColOrder = new ColumnInfo[leftSchema.columnNames.Length + rightSchema.columnNames.Length + 1]; -// myTableName = leftSchema.tableName + " J " + rightSchema.tableName; -// ColumnInfo[] cis = leftSchema.columnInfo; -// string assignedColName; -// bool renamed = false; -// foreach (ColumnInfo ci in cis) -// { -// assignedColName = ci.colName; -// while (rightSchema[assignedColName] != null) -// { -// assignedColName = "L." + assignedColName; -// renamed = true; -// while (leftSchema[assignedColName] != null) -// assignedColName = "L." + assignedColName; -// } -// ColumnInfo newCi = new ColumnInfo (assignedColName, ci.colNo, ci.colType); -// BlueEventLog.logAssert (myIndexByColName[newCi.colName] == null); -// BlueEventLog.logAssert (myIndexByColOrder[newCi.colNo] == null); -// if (renamed) -// BlueEventLog.logAssert (leftSchema[assignedColName] == null); -// BlueEventLog.logAssert (rightSchema[assignedColName] == null); -// myIndexByColName [newCi.colName] = newCi; -// myIndexByColOrder [newCi.colNo] = newCi; -// myLeftNameToAlias[ci.colName] = newCi.colName; -// myLeftJc = ci.colName == leftJc ? assignedColName : myLeftJc; -// myLeftId = ci.colName == "id" ? assignedColName : myLeftId; -// renamed = false; -// } -// BlueEventLog.logAssert (myLeftJc != "" && myLeftId != ""); -// int n = leftSchema.columnNames.Length; -// cis = rightSchema.columnInfo; -// renamed = false; -// foreach (ColumnInfo ci in cis) -// { -// assignedColName = ci.colName; -// while (leftSchema[assignedColName] != null) -// { -// assignedColName = "R." + assignedColName; -// renamed = true; -// while(rightSchema[assignedColName] != null) -// assignedColName = "R." + assignedColName; -// } -// ColumnInfo newCi = new ColumnInfo (assignedColName, ci.colNo + n, ci.colType); -// BlueEventLog.logAssert (myIndexByColName[assignedColName] == null); -// BlueEventLog.logAssert (myIndexByColOrder[newCi.colNo] == null); -// BlueEventLog.logAssert (leftSchema[assignedColName] == null); -// if (renamed) -// BlueEventLog.logAssert (rightSchema[assignedColName] == null); -// myIndexByColName [newCi.colName] = newCi; -// myIndexByColOrder [newCi.colNo] = newCi; -// myRightNameToAlias[ci.colName] = newCi.colName; -// myRightJc = ci.colName == rightJc ? assignedColName : myRightJc; -// myRightId = ci.colName == "id" ? assignedColName : myLeftJc; -// renamed = false; -// } -// myIndexByColName["id"] = new ColumnInfo("id", myIndexByColName.Count, typeof(int)); -// myIndexByColOrder[myIndexByColOrder.Length - 1] = (ColumnInfo)myIndexByColName["id"]; -// myAliasToLeftName = ProjectHelper.invertDic(myLeftNameToAlias); -// myAliasToRightName = ProjectHelper.invertDic(myRightNameToAlias); -// } +using bts; -// public string leftJoinCol {get{return myLeftJc;}} +namespace BCG +{ + /// + /// Represents the schema of the Join Table. This schema is formed by combining the respective + /// schemas of the operands. + /// + public class JoinTableSchema : BlueTableSchema + { + private string myLeftJc = "", myRightJc = "", myLeftId = "", myRightId = ""; + private IDictionary myLeftNameToAlias = new Hashtable (); + private IDictionary myRightNameToAlias = new Hashtable (); + private IDictionary myAliasToLeftName = new Hashtable(), + myAliasToRightName = new Hashtable(); + /// + /// Constructs the schema of the Jointable with the schema of the left and right tables. Incase of + /// a name being there in both the shemas it prefixes the name with "L." or "R." accordingly. Column "id" + /// is the last column in the schema + /// + /// + /// + /// + /// + public JoinTableSchema (IBlueTableSchema leftSchema, string leftJc, + IBlueTableSchema rightSchema, string rightJc) + { + myIndexByColOrder = new ColumnInfo[leftSchema.columnNames.Length + rightSchema.columnNames.Length + 1]; + myTableName = leftSchema.tableName + " J " + rightSchema.tableName; + ColumnInfo[] cis = leftSchema.columnInfo; + string assignedColName; + bool renamed = false; + foreach (ColumnInfo ci in cis) + { + assignedColName = ci.colName; + while (rightSchema[assignedColName] != null) + { + assignedColName = "L." + assignedColName; + renamed = true; + while (leftSchema[assignedColName] != null) + assignedColName = "L." + assignedColName; + } + ColumnInfo newCi = new ColumnInfo (assignedColName, ci.colNo, ci.colType); + Log.assert(myIndexByColName[newCi.colName] == null); + Log.assert(myIndexByColOrder[newCi.colNo] == null); + if (renamed) + Log.assert(leftSchema[assignedColName] == null); + Log.assert(rightSchema[assignedColName] == null); + myIndexByColName [newCi.colName] = newCi; + myIndexByColOrder [newCi.colNo] = newCi; + myLeftNameToAlias[ci.colName] = newCi.colName; + myLeftJc = ci.colName == leftJc ? assignedColName : myLeftJc; + myLeftId = ci.colName == "id" ? assignedColName : myLeftId; + renamed = false; + } + Log.assert(myLeftJc != "" && myLeftId != ""); + int n = leftSchema.columnNames.Length; + cis = rightSchema.columnInfo; + renamed = false; + foreach (ColumnInfo ci in cis) + { + assignedColName = ci.colName; + while (leftSchema[assignedColName] != null) + { + assignedColName = "R." + assignedColName; + renamed = true; + while(rightSchema[assignedColName] != null) + assignedColName = "R." + assignedColName; + } + ColumnInfo newCi = new ColumnInfo (assignedColName, ci.colNo + n, ci.colType); + Log.assert(myIndexByColName[assignedColName] == null); + Log.assert(myIndexByColOrder[newCi.colNo] == null); + Log.assert(leftSchema[assignedColName] == null); + if (renamed) + Log.assert(rightSchema[assignedColName] == null); + myIndexByColName [newCi.colName] = newCi; + myIndexByColOrder [newCi.colNo] = newCi; + myRightNameToAlias[ci.colName] = newCi.colName; + myRightJc = ci.colName == rightJc ? assignedColName : myRightJc; + myRightId = ci.colName == "id" ? assignedColName : myLeftJc; + renamed = false; + } + myIndexByColName["id"] = new ColumnInfo("id", myIndexByColName.Count, typeof(int)); + myIndexByColOrder[myIndexByColOrder.Length - 1] = (ColumnInfo)myIndexByColName["id"]; + myAliasToLeftName = ProjectHelper.invertDic(myLeftNameToAlias); + myAliasToRightName = ProjectHelper.invertDic(myRightNameToAlias); + } -// public string rightJoinCol {get{return myRightJc;}} + public string leftJoinCol {get{return myLeftJc;}} -// public string getRenamedName (string origName, bool isLeft) -// { -// if (isLeft) -// return (string)myLeftNameToAlias[origName]; -// return (string)myRightNameToAlias[origName]; -// } + public string rightJoinCol {get{return myRightJc;}} -// public string getOriginalName(string rename, bool isLeft) -// { -// return isLeft ? myAliasToLeftName[rename].ToString() : myAliasToRightName[rename].ToString(); -// } -// } -// } + public string getRenamedName (string origName, bool isLeft) + { + if (isLeft) + return (string)myLeftNameToAlias[origName]; + return (string)myRightNameToAlias[origName]; + } + + public string getOriginalName(string rename, bool isLeft) + { + return isLeft ? myAliasToLeftName[rename].ToString() : myAliasToRightName[rename].ToString(); + } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/MultiColumnJoinStream.cs b/src/DataStreamSuite/DataStream/Primitives/Join/MultiColumnJoinStream.cs index b6747817a..0ade66875 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/MultiColumnJoinStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/MultiColumnJoinStream.cs @@ -1,54 +1,54 @@ -// using System; -// using System.Collections.Generic; -// using System.Linq; -// using System.Text; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; -// namespace BCG -// { -// /// -// /// Same as a join except can join based on -// /// multiple columns (this abstracts what we -// /// have been doing for ages - concatenating, -// /// joining and then projecting the concatanted -// /// columns out) -// /// -// public class MultiColumnJoinStream : ViewStream -// { -// public MultiColumnJoinStream(ViewStream leftStream, string leftJoinColumns, string leftProject, -// ViewStream rightStream, string rightJoinColumns, string rightProject) -// : this(leftStream, leftJoinColumns, leftProject, -// rightStream, rightJoinColumns, rightProject, false) -// { } +namespace BCG +{ + /// + /// Same as a join except can join based on + /// multiple columns (this abstracts what we + /// have been doing for ages - concatenating, + /// joining and then projecting the concatanted + /// columns out) + /// + public class MultiColumnJoinStream : ViewStream + { + public MultiColumnJoinStream(ViewStream leftStream, string leftJoinColumns, string leftProject, + ViewStream rightStream, string rightJoinColumns, string rightProject) + : this(leftStream, leftJoinColumns, leftProject, + rightStream, rightJoinColumns, rightProject, false) + { } -// public MultiColumnJoinStream(ViewStream leftStream, string leftJoinColumns, string leftProject, -// ViewStream rightStream, string rightJoinColumns, string rightProject, bool leftJoin) -// { -// string[] leftJoinColArray = leftJoinColumns.Split(' ', ',', ';'), -// rightJoinColArray = rightJoinColumns.Split(' ', ',', ';'); -// if (leftJoinColArray.Length == 1 && rightJoinColArray.Length == 1) -// /// so that this handles regular joins too -// mySetRealSource(leftJoin ? new SuperLeftJoinStream(leftStream, leftJoinColArray[0], leftProject, -// rightStream, rightJoinColArray[0], rightProject) : -// new SuperJoinStream(leftStream, leftJoinColArray[0], leftProject, -// rightStream, rightJoinColArray[0], rightProject)); -// else -// { -// string leftCols = "ljc:"; -// foreach (string c in leftJoinColArray) -// leftCols += c + " "; -// leftCols = leftCols.Trim(); -// string rightCols = "rjc:"; -// foreach (string c in rightJoinColArray) -// rightCols += c + " "; -// rightCols = rightCols.Trim(); -// ViewStream res = -// leftJoin ? new SuperLeftJoinStream(new ConcatenateColumnsStream(leftStream, leftCols), "ljc", leftProject, -// new ConcatenateColumnsStream(rightStream, rightCols), "rjc", rightProject) : -// new SuperJoinStream(new ConcatenateColumnsStream(leftStream, leftCols), "ljc", leftProject, -// new ConcatenateColumnsStream(rightStream, rightCols), "rjc", rightProject); -// res = new ProjectStream(res, "!ljc rjc"); -// mySetRealSource(res); -// } -// } -// } -// } + public MultiColumnJoinStream(ViewStream leftStream, string leftJoinColumns, string leftProject, + ViewStream rightStream, string rightJoinColumns, string rightProject, bool leftJoin) + { + string[] leftJoinColArray = leftJoinColumns.Split(' ', ',', ';'), + rightJoinColArray = rightJoinColumns.Split(' ', ',', ';'); + if (leftJoinColArray.Length == 1 && rightJoinColArray.Length == 1) + /// so that this handles regular joins too + mySetRealSource(leftJoin ? new SuperLeftJoinStream(leftStream, leftJoinColArray[0], leftProject, + rightStream, rightJoinColArray[0], rightProject) : + new SuperJoinStream(leftStream, leftJoinColArray[0], leftProject, + rightStream, rightJoinColArray[0], rightProject)); + else + { + string leftCols = "ljc:"; + foreach (string c in leftJoinColArray) + leftCols += c + " "; + leftCols = leftCols.Trim(); + string rightCols = "rjc:"; + foreach (string c in rightJoinColArray) + rightCols += c + " "; + rightCols = rightCols.Trim(); + ViewStream res = + leftJoin ? new SuperLeftJoinStream(new ConcatenateColumnsStream(leftStream, leftCols), "ljc", leftProject, + new ConcatenateColumnsStream(rightStream, rightCols), "rjc", rightProject) : + new SuperJoinStream(new ConcatenateColumnsStream(leftStream, leftCols), "ljc", leftProject, + new ConcatenateColumnsStream(rightStream, rightCols), "rjc", rightProject); + res = new ProjectStream(res, "!ljc rjc"); + mySetRealSource(res); + } + } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/ParallelJoinTest.cs b/src/DataStreamSuite/DataStream/Primitives/Join/ParallelJoinTest.cs index 8d356c2b6..c0e12cb71 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/ParallelJoinTest.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/ParallelJoinTest.cs @@ -1,371 +1,371 @@ -// using System; -// using System.Collections; -// using System.Collections.Generic; -// using System.Text; -// using BCG.Utilities.General; -// using BCG.Utilities; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; +using BCG.Utilities.General; +using BCG.Utilities; -// namespace BCG -// { +namespace BCG +{ -// /// -// /// This class is used to effect a mapping between values in a join column and entries in three different tables: -// /// the left and right tables of a join, as well as the output table (the join table). -// /// -// class ParallelJoinTestTablePointer -// { -// int myLeftTableId = -1; -// public int LeftTableId -// { -// get { return myLeftTableId; } -// set { myLeftTableId = value; } -// } + /// + /// This class is used to effect a mapping between values in a join column and entries in three different tables: + /// the left and right tables of a join, as well as the output table (the join table). + /// + class ParallelJoinTestTablePointer + { + int myLeftTableId = -1; + public int LeftTableId + { + get { return myLeftTableId; } + set { myLeftTableId = value; } + } -// int myRightTableId = -1; -// public int RightTableId -// { -// get { return myRightTableId; } -// set { myRightTableId = value; } -// } -// int myJoinTableId = -1; -// public int JoinTableId -// { -// get { return myJoinTableId; } -// set { myJoinTableId = value; } -// } + int myRightTableId = -1; + public int RightTableId + { + get { return myRightTableId; } + set { myRightTableId = value; } + } + int myJoinTableId = -1; + public int JoinTableId + { + get { return myJoinTableId; } + set { myJoinTableId = value; } + } -// public ParallelJoinTestTablePointer(int leftId, int rightId, int joinId) -// { -// myLeftTableId = leftId; -// myRightTableId = rightId; -// myJoinTableId = joinId; -// } -// public ParallelJoinTestTablePointer() : this(-1,-1,-1) -// { + public ParallelJoinTestTablePointer(int leftId, int rightId, int joinId) + { + myLeftTableId = leftId; + myRightTableId = rightId; + myJoinTableId = joinId; + } + public ParallelJoinTestTablePointer() : this(-1,-1,-1) + { -// } -// } + } + } -// #region IBlueRow implementation for join and leftJoin + #region IBlueRow implementation for join and leftJoin -// public class ParallelJoinTestRow : IBlueRow -// { -// private JoinProjectHelper myHelper; -// private IBlueRow myLeftRow, myRightRow; -// int myID; + public class ParallelJoinTestRow : IBlueRow + { + private JoinProjectHelper myHelper; + private IBlueRow myLeftRow, myRightRow; + int myID; -// public ParallelJoinTestRow(int id, IBlueRow leftRow, IBlueRow rightRow, JoinProjectHelper joinProjectHelper) -// { -// myID = id; -// myLeftRow = leftRow; -// myRightRow = rightRow; -// myHelper = joinProjectHelper; -// } + public ParallelJoinTestRow(int id, IBlueRow leftRow, IBlueRow rightRow, JoinProjectHelper joinProjectHelper) + { + myID = id; + myLeftRow = leftRow; + myRightRow = rightRow; + myHelper = joinProjectHelper; + } -// public int leftID { get { return (int)myLeftRow["id"]; } } + public int leftID { get { return (int)myLeftRow["id"]; } } -// public int rightID { get { return (int)myRightRow["id"]; } } + public int rightID { get { return (int)myRightRow["id"]; } } -// public IBlueRow leftRow { get { return myLeftRow; } } + public IBlueRow leftRow { get { return myLeftRow; } } -// public IBlueRow rightRow { get { return myRightRow; } } + public IBlueRow rightRow { get { return myRightRow; } } -// #region IBlueRow Members + #region IBlueRow Members -// public IBlueTableSchema schema { get { return myHelper.schema; } } + public IBlueTableSchema schema { get { return myHelper.schema; } } -// public object this[int index] { get { return this[schema[index].colName]; } } + public object this[int index] { get { return this[schema[index].colName]; } } -// public object this[string colName] -// { -// get -// { -// if (colName == "id") -// return myID; -// else -// { -// bool isLeft; -// string col = myHelper.getOriginalCol(colName, out isLeft); -// return isLeft ? myLeftRow[col] : (myRightRow == null ? (object)DBNull.Value : myRightRow[col]); -// } -// } -// } + public object this[string colName] + { + get + { + if (colName == "id") + return myID; + else + { + bool isLeft; + string col = myHelper.getOriginalCol(colName, out isLeft); + return isLeft ? myLeftRow[col] : (myRightRow == null ? (object)DBNull.Value : myRightRow[col]); + } + } + } -// #endregion + #endregion -// #region IDictionary Members + #region IDictionary Members -// public void Add(object key, object value) { { throw new NotSupportedException("IBlueRow is readonly"); } } + public void Add(object key, object value) { { throw new NotSupportedException("IBlueRow is readonly"); } } -// public void Clear() { throw new NotSupportedException("IBlueRow is readonly"); } + public void Clear() { throw new NotSupportedException("IBlueRow is readonly"); } -// public bool Contains(object key) { throw new Exception("The method or operation is not implemented."); } + public bool Contains(object key) { throw new Exception("The method or operation is not implemented."); } -// public System.Collections.IDictionaryEnumerator GetEnumerator() { return new BlueRowDictionaryEnumerator(this); } + public System.Collections.IDictionaryEnumerator GetEnumerator() { return new BlueRowDictionaryEnumerator(this); } -// public bool IsFixedSize { get { return true; } } + public bool IsFixedSize { get { return true; } } -// public bool IsReadOnly {get { return true; }} + public bool IsReadOnly {get { return true; }} -// public System.Collections.ICollection Keys { get { return schema.columnNames; } } + public System.Collections.ICollection Keys { get { return schema.columnNames; } } -// public void Remove(object key) { throw new NotSupportedException("IBlueRow is readonly"); } + public void Remove(object key) { throw new NotSupportedException("IBlueRow is readonly"); } -// public System.Collections.ICollection Values -// { -// get -// { -// List rv = new List (); -// foreach (string col in Keys) -// rv.Add(this[col]); -// return rv; -// } -// } + public System.Collections.ICollection Values + { + get + { + List rv = new List (); + foreach (string col in Keys) + rv.Add(this[col]); + return rv; + } + } -// public object this[object key] -// { -// get { return key is string ? this[key.ToString()] : this[(int)key]; } -// set { throw new NotSupportedException("IBlueRow is readonly"); } -// } + public object this[object key] + { + get { return key is string ? this[key.ToString()] : this[(int)key]; } + set { throw new NotSupportedException("IBlueRow is readonly"); } + } -// #endregion + #endregion -// #region ICollection Members + #region ICollection Members -// public void CopyTo(Array array, int index) { Values.CopyTo(array, index); } + public void CopyTo(Array array, int index) { Values.CopyTo(array, index); } -// public int Count { get { return schema.columnNames.Length; } } + public int Count { get { return schema.columnNames.Length; } } -// public bool IsSynchronized { get { return false; } } + public bool IsSynchronized { get { return false; } } -// public object SyncRoot { get { return this; } } + public object SyncRoot { get { return this; } } -// #endregion + #endregion -// #region IEnumerable Members + #region IEnumerable Members -// System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return new BlueRowEnumerator(this); } + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return new BlueRowEnumerator(this); } -// #endregion -// } + #endregion + } -// #endregion -// /// -// /// This class represents a join on two streams. It works by storing a table with the result of the join. -// /// -// public class ParallelJoinTestStream : ViewStream -// { -// BlueTable myJoinedTable; -// BlueTable myLeftTable; -// BlueTable myRightTable; -// Hashtable myKeyToTablePointerMap; -// ViewStream myVLeft, myVRight; + #endregion + /// + /// This class represents a join on two streams. It works by storing a table with the result of the join. + /// + public class ParallelJoinTestStream : ViewStream + { + BlueTable myJoinedTable; + BlueTable myLeftTable; + BlueTable myRightTable; + Hashtable myKeyToTablePointerMap; + ViewStream myVLeft, myVRight; -// int myIdToUse; -// string myLeftKey, myRightKey, myLeftProj, myRightProj; -// public ParallelJoinTestStream(ViewStream leftStream, string leftKey, ViewStream rightStream, string rightKey) -// : this(leftStream, leftKey, "!", rightStream, rightKey, "!") { } -// public ParallelJoinTestStream(ViewStream leftStream, string leftKey, string leftProj, ViewStream rightStream, string rightKey, string rightProj) -// { -// myVLeft = leftStream; -// myVRight = rightStream; + int myIdToUse; + string myLeftKey, myRightKey, myLeftProj, myRightProj; + public ParallelJoinTestStream(ViewStream leftStream, string leftKey, ViewStream rightStream, string rightKey) + : this(leftStream, leftKey, "!", rightStream, rightKey, "!") { } + public ParallelJoinTestStream(ViewStream leftStream, string leftKey, string leftProj, ViewStream rightStream, string rightKey, string rightProj) + { + myVLeft = leftStream; + myVRight = rightStream; -// myLeftProj = leftProj; -// myRightProj = rightProj; + myLeftProj = leftProj; + myRightProj = rightProj; -// myLeftKey = leftKey; -// myRightKey = rightKey; + myLeftKey = leftKey; + myRightKey = rightKey; -// myKeyToTablePointerMap = new Hashtable(); -// myIdToUse = 0; -// } + myKeyToTablePointerMap = new Hashtable(); + myIdToUse = 0; + } -// protected override void myOnFirstSubscriber() -// { -// myVLeft.onChange += new ChangeDelegate(myVLeft_onChange); -// myVRight.onChange += new ChangeDelegate(myVRight_onChange); -// } + protected override void myOnFirstSubscriber() + { + myVLeft.onChange += new ChangeDelegate(myVLeft_onChange); + myVRight.onChange += new ChangeDelegate(myVRight_onChange); + } -// void myVRight_onChange(object newValue) -// { -// myHandleChange(newValue as DataPacket, false); + void myVRight_onChange(object newValue) + { + myHandleChange(newValue as DataPacket, false); -// } + } -// void myVLeft_onChange(object newValue) -// { -// myHandleChange(newValue as DataPacket, true); + void myVLeft_onChange(object newValue) + { + myHandleChange(newValue as DataPacket, true); -// } + } -// void myHandleChange(DataPacket dp, bool isLeft) -// { + void myHandleChange(DataPacket dp, bool isLeft) + { -// int i = 0; // for going throuhg loops and such -// BlueTable addedRows = null, droppedRows = null; + int i = 0; // for going throuhg loops and such + BlueTable addedRows = null, droppedRows = null; -// if (myJoinedTable == null) -// { -// if (isLeft && myLeftTable == null) -// { -// myLeftTable = new BlueTable(dp.addedRows); + if (myJoinedTable == null) + { + if (isLeft && myLeftTable == null) + { + myLeftTable = new BlueTable(dp.addedRows); -// //go through the list of added rows and get the id of each row, storing this in the TablePointer map -// foreach (IBlueRow row in myLeftTable) -// { -// if (myKeyToTablePointerMap.ContainsKey(row[myLeftKey])) -// { -// ((ParallelJoinTestTablePointer)myKeyToTablePointerMap[row[myLeftKey]]).LeftTableId = (int)row["id"]; -// } -// else -// { -// myKeyToTablePointerMap[row[myLeftKey]] = new ParallelJoinTestTablePointer((int)row["id"],-1,-1); + //go through the list of added rows and get the id of each row, storing this in the TablePointer map + foreach (IBlueRow row in myLeftTable) + { + if (myKeyToTablePointerMap.ContainsKey(row[myLeftKey])) + { + ((ParallelJoinTestTablePointer)myKeyToTablePointerMap[row[myLeftKey]]).LeftTableId = (int)row["id"]; + } + else + { + myKeyToTablePointerMap[row[myLeftKey]] = new ParallelJoinTestTablePointer((int)row["id"],-1,-1); -// } + } -// } + } -// } -// else if (!isLeft && myRightTable == null) -// { -// myRightTable = new BlueTable(dp.addedRows); -// //go through the list of added rows and get the id of each row, storing this in the TablePointer map -// foreach (IBlueRow row in myRightTable) -// { -// if (myKeyToTablePointerMap.ContainsKey(row[myRightKey])) -// { -// ((ParallelJoinTestTablePointer)myKeyToTablePointerMap[row[myRightKey]]).RightTableId = (int)row["id"]; -// } -// else -// { -// myKeyToTablePointerMap[row[myRightKey]] = new ParallelJoinTestTablePointer(-1,(int)row["id"],-1); + } + else if (!isLeft && myRightTable == null) + { + myRightTable = new BlueTable(dp.addedRows); + //go through the list of added rows and get the id of each row, storing this in the TablePointer map + foreach (IBlueRow row in myRightTable) + { + if (myKeyToTablePointerMap.ContainsKey(row[myRightKey])) + { + ((ParallelJoinTestTablePointer)myKeyToTablePointerMap[row[myRightKey]]).RightTableId = (int)row["id"]; + } + else + { + myKeyToTablePointerMap[row[myRightKey]] = new ParallelJoinTestTablePointer(-1,(int)row["id"],-1); -// } + } -// } -// } + } + } -// if (myLeftTable != null && myRightTable != null) -// { -// //construct the schema for the table based upon the schemas of the two tables that are going to be joined together -// int numColumns = myLeftTable.schema.columnInfo.Length; + if (myLeftTable != null && myRightTable != null) + { + //construct the schema for the table based upon the schemas of the two tables that are going to be joined together + int numColumns = myLeftTable.schema.columnInfo.Length; -// ColumnInfo[] newColumnInfoArray = new ColumnInfo[numColumns * 2 + 1]; -// for (i = 0; i < numColumns; i++) -// { -// newColumnInfoArray[i] = new ColumnInfo("L." + myLeftTable.schema.columnNames[i], i, myLeftTable.schema.columnInfo[i].colType); -// newColumnInfoArray[numColumns + i] = new ColumnInfo("R." + myRightTable.schema.columnNames[i], i + numColumns, myRightTable.schema.columnInfo[i].colType); -// } -// newColumnInfoArray[numColumns + i] = new ColumnInfo("id", numColumns + i, (typeof(System.Int64))); + ColumnInfo[] newColumnInfoArray = new ColumnInfo[numColumns * 2 + 1]; + for (i = 0; i < numColumns; i++) + { + newColumnInfoArray[i] = new ColumnInfo("L." + myLeftTable.schema.columnNames[i], i, myLeftTable.schema.columnInfo[i].colType); + newColumnInfoArray[numColumns + i] = new ColumnInfo("R." + myRightTable.schema.columnNames[i], i + numColumns, myRightTable.schema.columnInfo[i].colType); + } + newColumnInfoArray[numColumns + i] = new ColumnInfo("id", numColumns + i, (typeof(System.Int64))); -// IBlueTableSchema newSchema = new BlueTableSchema("Joined Table", newColumnInfoArray); + IBlueTableSchema newSchema = new BlueTableSchema("Joined Table", newColumnInfoArray); -// myJoinedTable = new BlueTable(newSchema); -// addedRows = new BlueTable(newSchema); -// droppedRows = new BlueTable(newSchema); + myJoinedTable = new BlueTable(newSchema); + addedRows = new BlueTable(newSchema); + droppedRows = new BlueTable(newSchema); -// //now that we've created the table, we should have some leftover stuff from the side tables -// //that we were storing until now. Add that stuff to the table. -// BlueTable rowsToAdd = isLeft ? myRightTable : myLeftTable; + //now that we've created the table, we should have some leftover stuff from the side tables + //that we were storing until now. Add that stuff to the table. + BlueTable rowsToAdd = isLeft ? myRightTable : myLeftTable; -// foreach (BlueRow row in rowsToAdd) -// { -// Object[] newRowObjects = new Object[newSchema.columnInfo.Length]; + foreach (BlueRow row in rowsToAdd) + { + Object[] newRowObjects = new Object[newSchema.columnInfo.Length]; -// if (isLeft) -// { -// for (i = 0; i < numColumns; i++) -// { -// newRowObjects[i] = row[i]; -// newRowObjects[numColumns + i] = null; + if (isLeft) + { + for (i = 0; i < numColumns; i++) + { + newRowObjects[i] = row[i]; + newRowObjects[numColumns + i] = null; -// } -// newRowObjects[numColumns + i] = myIdToUse; -// //store the fact that the row with this key value went to this id + } + newRowObjects[numColumns + i] = myIdToUse; + //store the fact that the row with this key value went to this id -// ((ParallelJoinTestTablePointer)myKeyToTablePointerMap[row[myLeftKey]]).JoinTableId = myIdToUse; -// myIdToUse++; + ((ParallelJoinTestTablePointer)myKeyToTablePointerMap[row[myLeftKey]]).JoinTableId = myIdToUse; + myIdToUse++; -// } -// else -// { -// for (i = 0; i < numColumns; i++) -// { -// newRowObjects[i] = null; -// newRowObjects[numColumns + i] = row[i]; + } + else + { + for (i = 0; i < numColumns; i++) + { + newRowObjects[i] = null; + newRowObjects[numColumns + i] = row[i]; -// } -// newRowObjects[numColumns + i] = myIdToUse; -// //store the fact that the row with this key value went to this id -// ((ParallelJoinTestTablePointer)myKeyToTablePointerMap[row[myRightKey]]).JoinTableId = myIdToUse; -// myIdToUse++; -// } + } + newRowObjects[numColumns + i] = myIdToUse; + //store the fact that the row with this key value went to this id + ((ParallelJoinTestTablePointer)myKeyToTablePointerMap[row[myRightKey]]).JoinTableId = myIdToUse; + myIdToUse++; + } -// BlueRow newRow = new BlueRow(newSchema, newRowObjects); -// addedRows.addRow(newRow); -// myJoinedTable.addRow(newRow); + BlueRow newRow = new BlueRow(newSchema, newRowObjects); + addedRows.addRow(newRow); + myJoinedTable.addRow(newRow); -// } + } -// } -// } -// else -// { -// addedRows = new BlueTable(myJoinedTable.schema); -// droppedRows = new BlueTable(myJoinedTable.schema); -// } + } + } + else + { + addedRows = new BlueTable(myJoinedTable.schema); + droppedRows = new BlueTable(myJoinedTable.schema); + } -// //setup is complete. At this point, we have to delete all the rows that were deleted, then add in the rows that were added in. + //setup is complete. At this point, we have to delete all the rows that were deleted, then add in the rows that were added in. -// foreach (BlueRow row in dp.addedRows) -// { -// //check to see if this row has a match in the table already. -// //If it does, get rid of that row in the table because it's OUT OF DATE -// string joinColumn = isLeft ? myLeftKey : myRightKey; + foreach (BlueRow row in dp.addedRows) + { + //check to see if this row has a match in the table already. + //If it does, get rid of that row in the table because it's OUT OF DATE + string joinColumn = isLeft ? myLeftKey : myRightKey; -// if (myKeyToTablePointerMap.ContainsKey(row[joinColumn])) -// { -// //the value is already in the hashtable. -// ParallelJoinTestTablePointer tp = (ParallelJoinTestTablePointer) myKeyToTablePointerMap[row[joinColumn]]; -// int rowOfExistingGuy = tp.JoinTableId; + if (myKeyToTablePointerMap.ContainsKey(row[joinColumn])) + { + //the value is already in the hashtable. + ParallelJoinTestTablePointer tp = (ParallelJoinTestTablePointer) myKeyToTablePointerMap[row[joinColumn]]; + int rowOfExistingGuy = tp.JoinTableId; -// if (rowOfExistingGuy != -1) -// { + if (rowOfExistingGuy != -1) + { -// //remove this guy from the table -// IBlueRow rowToDelete = myJoinedTable[rowOfExistingGuy]; -// myJoinedTable.deleteRow(rowToDelete); -// droppedRows.addRow(rowToDelete); -// tp.JoinTableId = -1; -// } + //remove this guy from the table + IBlueRow rowToDelete = myJoinedTable[rowOfExistingGuy]; + myJoinedTable.deleteRow(rowToDelete); + droppedRows.addRow(rowToDelete); + tp.JoinTableId = -1; + } -// } -// } + } + } -// //finally, fire off a packet, but only if you have enough information about the table to do so. + //finally, fire off a packet, but only if you have enough information about the table to do so. -// if (myJoinedTable != null) -// { -// DataPacket outPacket = new DataPacket(myJoinedTable,addedRows, new BlueTable(myJoinedTable.schema)); -// myFireEvent(outPacket); -// } + if (myJoinedTable != null) + { + DataPacket outPacket = new DataPacket(myJoinedTable,addedRows, new BlueTable(myJoinedTable.schema)); + myFireEvent(outPacket); + } -// } + } -// } + } -// } \ No newline at end of file +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/SuperJoinStream.cs b/src/DataStreamSuite/DataStream/Primitives/Join/SuperJoinStream.cs index 9bf978c73..bb2bbf1f9 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/SuperJoinStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/SuperJoinStream.cs @@ -1,570 +1,574 @@ -// using System; -// using System.Collections; -// using System.Collections.Generic; -// using System.Text; -// using BCG.Utilities.General; -// using BCG.Utilities; -// using mbts; - -// namespace BCG -// { -// internal class SingleRowJoinDicionary : IDictionary -// { -// private TKey myKey; -// private TValue myValue; - -// #region IDictionary Members - -// public void Add(TKey key, TValue value) -// { -// myKey = key; -// myValue = value; -// } - -// public bool ContainsKey(TKey key) -// { -// return myKey.Equals(key); -// } - -// public ICollection Keys -// { -// get { return new TKey[] { myKey }; } -// } - -// public bool Remove(TKey key) -// { -// bool rv = myKey.Equals(key); -// myKey = default(TKey); -// return rv; -// } - -// public bool TryGetValue(TKey key, out TValue value) -// { -// bool rv = false; -// value = (rv = myKey.Equals(key)) ? myValue : default(TValue); -// return rv; -// } - -// public ICollection Values -// { -// get { throw new Exception("The method or operation is not implemented."); } -// } - -// public TValue this[TKey key] -// { -// get -// { -// throw new Exception("The method or operation is not implemented."); -// } -// set -// { -// throw new Exception("The method or operation is not implemented."); -// } -// } - -// #endregion - -// #region ICollection> Members - -// public void Add(KeyValuePair item) -// { -// throw new Exception("The method or operation is not implemented."); -// } - -// public void Clear() -// { -// throw new Exception("The method or operation is not implemented."); -// } - -// public bool Contains(KeyValuePair item) -// { -// throw new Exception("The method or operation is not implemented."); -// } - -// public void CopyTo(KeyValuePair[] array, int arrayIndex) -// { -// throw new Exception("The method or operation is not implemented."); -// } - -// public int Count -// { -// get { throw new Exception("The method or operation is not implemented."); } -// } - -// public bool IsReadOnly -// { -// get { throw new Exception("The method or operation is not implemented."); } -// } - -// public bool Remove(KeyValuePair item) -// { -// throw new Exception("The method or operation is not implemented."); -// } - -// #endregion - -// #region IEnumerable> Members - -// public IEnumerator> GetEnumerator() -// { -// throw new Exception("The method or operation is not implemented."); -// } - -// #endregion - -// #region IEnumerable Members - -// IEnumerator IEnumerable.GetEnumerator() -// { -// throw new Exception("The method or operation is not implemented."); -// } - -// #endregion -// } - -// #region Helper class to deal with stream projections - -// public class JoinProjectHelper -// { -// private IBlueTableSchema mySchema; -// private Dictionary> myAliasToOriginal = new Dictionary>(); - -// public JoinProjectHelper(IBlueTableSchema leftSchema, -// string leftProj, -// string leftJoinCol, -// IBlueTableSchema rightSchema, -// string rightProj, -// string rightJoinCol) -// { - -// JoinTableSchema joinedSchema = new JoinTableSchema(leftSchema, leftJoinCol, rightSchema, rightJoinCol); -// JoinNameResolver resolver = new JoinNameResolver(joinedSchema, leftSchema, leftProj, rightSchema, rightProj); -// IDictionary map = resolver.getMap(); -// map["id"] = "id"; -// ProjectHelper helper = new ProjectHelper(joinedSchema, ProjectHelper.invertDic(map), -// String.Format("({0} J {1})", leftSchema.tableName, rightSchema.tableName)); - -// mySchema = helper.schema; -// for (int aliasCol = 0; aliasCol < helper.colMap.Length; aliasCol++) -// { -// string aliasColName = mySchema[aliasCol].colName; -// if (aliasColName != "id") -// { -// int joinedColIndex = helper.colMap[aliasCol]; -// bool isLeft = joinedColIndex < leftSchema.columnInfo.Length; -// string originalCol = joinedSchema.getOriginalName(joinedSchema[joinedColIndex].colName, isLeft); -// myAliasToOriginal.Add(aliasColName, new Pair(originalCol, isLeft)); -// } -// } -// } - -// public IBlueTableSchema schema -// { -// get { return mySchema; } -// } - -// public string getOriginalCol(string col, out bool isLeft) -// { -// Pair p = myAliasToOriginal[col]; -// isLeft = p.second; -// return p.first; -// } -// } +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; +using BCG.Utilities.General; +using BCG.Utilities; +using mbts; +using mbts.spark; +using Spark.Core.Collections; + +namespace BCG +{ + internal class SingleRowJoinDicionary : IDictionary + { + private TKey myKey; + private TValue myValue; + + #region IDictionary Members + + public void Add(TKey key, TValue value) + { + myKey = key; + myValue = value; + } + + public bool ContainsKey(TKey key) + { + return myKey.Equals(key); + } + + public ICollection Keys + { + get { return new TKey[] { myKey }; } + } + + public bool Remove(TKey key) + { + bool rv = myKey.Equals(key); + myKey = default(TKey); + return rv; + } + + public bool TryGetValue(TKey key, out TValue value) + { + bool rv = false; + value = (rv = myKey.Equals(key)) ? myValue : default(TValue); + return rv; + } + + public ICollection Values + { + get { throw new Exception("The method or operation is not implemented."); } + } + + public TValue this[TKey key] + { + get + { + throw new Exception("The method or operation is not implemented."); + } + set + { + throw new Exception("The method or operation is not implemented."); + } + } + + #endregion + + #region ICollection> Members + + public void Add(KeyValuePair item) + { + throw new Exception("The method or operation is not implemented."); + } + + public void Clear() + { + throw new Exception("The method or operation is not implemented."); + } + + public bool Contains(KeyValuePair item) + { + throw new Exception("The method or operation is not implemented."); + } + + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + throw new Exception("The method or operation is not implemented."); + } + + public int Count + { + get { throw new Exception("The method or operation is not implemented."); } + } + + public bool IsReadOnly + { + get { throw new Exception("The method or operation is not implemented."); } + } + + public bool Remove(KeyValuePair item) + { + throw new Exception("The method or operation is not implemented."); + } + + #endregion + + #region IEnumerable> Members + + public IEnumerator> GetEnumerator() + { + throw new Exception("The method or operation is not implemented."); + } + + #endregion + + #region IEnumerable Members + + IEnumerator IEnumerable.GetEnumerator() + { + throw new Exception("The method or operation is not implemented."); + } + + #endregion + } + + #region Helper class to deal with stream projections + + public class JoinProjectHelper + { + private IBlueTableSchema mySchema; + private Dictionary> myAliasToOriginal = new Dictionary>(); + + public JoinProjectHelper(IBlueTableSchema leftSchema, + string leftProj, + string leftJoinCol, + IBlueTableSchema rightSchema, + string rightProj, + string rightJoinCol) + { + + JoinTableSchema joinedSchema = new JoinTableSchema(leftSchema, leftJoinCol, rightSchema, rightJoinCol); + JoinNameResolver resolver = new JoinNameResolver(joinedSchema, leftSchema, leftProj, rightSchema, rightProj); + IDictionary map = resolver.getMap(); + map["id"] = "id"; + ProjectHelper helper = new ProjectHelper( + joinedSchema, + GenericDictionaryWrapper.Create(ProjectHelper.invertDic(map)), + String.Format("({0} J {1})", leftSchema.tableName, rightSchema.tableName)); + + mySchema = helper.schema; + for (int aliasCol = 0; aliasCol < helper.colMap.Length; aliasCol++) + { + string aliasColName = mySchema[aliasCol].colName; + if (aliasColName != "id") + { + int joinedColIndex = helper.colMap[aliasCol]; + bool isLeft = joinedColIndex < leftSchema.columnInfo.Length; + string originalCol = joinedSchema.getOriginalName(joinedSchema[joinedColIndex].colName, isLeft); + myAliasToOriginal.Add(aliasColName, new Pair(originalCol, isLeft)); + } + } + } + + public IBlueTableSchema schema + { + get { return mySchema; } + } + + public string getOriginalCol(string col, out bool isLeft) + { + Pair p = myAliasToOriginal[col]; + isLeft = p.second; + return p.first; + } + } -// #endregion + #endregion -// #region IBlueRow implementation for join and leftJoin + #region IBlueRow implementation for join and leftJoin -// public class SuperJoinRow : IBlueRow -// { -// private JoinProjectHelper myHelper; -// private IBlueRow myLeftRow, myRightRow; -// int myID; + public class SuperJoinRow : IBlueRow + { + private JoinProjectHelper myHelper; + private IBlueRow myLeftRow, myRightRow; + int myID; -// public SuperJoinRow(int id, IBlueRow leftRow, IBlueRow rightRow, JoinProjectHelper joinProjectHelper) -// { -// myID = id; -// myLeftRow = leftRow; -// myRightRow = rightRow; -// myHelper = joinProjectHelper; -// } + public SuperJoinRow(int id, IBlueRow leftRow, IBlueRow rightRow, JoinProjectHelper joinProjectHelper) + { + myID = id; + myLeftRow = leftRow; + myRightRow = rightRow; + myHelper = joinProjectHelper; + } -// public int leftID { get { return (int)myLeftRow["id"]; } } + public int leftID { get { return (int)myLeftRow["id"]; } } -// public int rightID { get { return (int)myRightRow["id"]; } } + public int rightID { get { return (int)myRightRow["id"]; } } -// public IBlueRow leftRow { get { return myLeftRow; } } - -// public IBlueRow rightRow { get { return myRightRow; } } - -// #region IBlueRow Members - -// public IBlueTableSchema schema { get { return myHelper.schema; } } - -// public object this[int index] { get { return this[schema[index].colName]; } } - -// public object this[string colName] -// { -// get -// { -// if (colName == "id") -// return myID; -// else -// { -// bool isLeft; -// string col = myHelper.getOriginalCol(colName, out isLeft); -// return isLeft ? myLeftRow[col] : (myRightRow == null ? (object)DBNull.Value : myRightRow[col]); -// } -// } -// } - -// #endregion - -// #region IDictionary Members - -// public void Add(object key, object value) { { throw new NotSupportedException("IBlueRow is readonly"); } } - -// public void Clear() { throw new NotSupportedException("IBlueRow is readonly"); } - -// public bool Contains(object key) -// { -// return myLeftRow.Contains(key) || myRightRow.Contains(key); -// } - -// public System.Collections.IDictionaryEnumerator GetEnumerator() { return new BlueRowDictionaryEnumerator(this); } - -// public bool IsFixedSize { get { return true; } } - -// public bool IsReadOnly { get { return true; } } - -// public System.Collections.ICollection Keys { get { return schema.columnNames; } } - -// public void Remove(object key) { throw new NotSupportedException("IBlueRow is readonly"); } - -// public System.Collections.ICollection Values -// { -// get -// { -// List rv = new List(); -// foreach (string col in Keys) -// rv.Add(this[col]); -// return rv; -// } -// } - -// public object this[object key] -// { -// get { return key is string ? this[key.ToString()] : this[(int)key]; } -// set { throw new NotSupportedException("IBlueRow is readonly"); } -// } - -// #endregion - -// #region ICollection Members - -// public void CopyTo(Array array, int index) { Values.CopyTo(array, index); } - -// public int Count { get { return schema.columnNames.Length; } } - -// public bool IsSynchronized { get { return false; } } - -// public object SyncRoot { get { return this; } } - -// #endregion - -// #region IEnumerable Members - -// System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return new BlueRowEnumerator(this); } - -// #endregion -// } - -// #endregion -// /// -// /// this class is written 3-4 yrs after the original -// /// join, and learning from the past mistakes, is -// /// made super fast and mem efficent -// /// -// public class SuperJoinStream : ViewStream -// { -// protected Dictionary myLeftUnmatchedRowsAdds = new Dictionary(), -// myLeftUnmatchedRowsDels = new Dictionary(); -// private bool myManageUnmappedLeftRows = false; -// protected HighPerformanceTimer myTimer = new HighPerformanceTimer(); -// protected string myLeftProj = String.Empty, myRightProj = String.Empty; -// protected IBlueTableSchema myLeftSchema, myRightSchema; -// protected JoinProjectHelper myJoinProjectHelper; -// protected BlueTable myState; -// protected int myID; -// protected ViewStream myLeftStream, myRightStream; -// protected string myLeftKeyCol, myRightKeyCol; -// protected Dictionary> myLeftKeyToLeftRows = new Dictionary>(), -// myRightKeyToRightRows = new Dictionary>(); - -// protected Dictionary> myLeftIDToResultRows = new Dictionary>(), -// myRightIDToResultRows = new Dictionary>(); - -// public SuperJoinStream(ViewStream leftStream, string leftKey, ViewStream rightStream, string rightKey) -// : this(leftStream, leftKey, "!", rightStream, rightKey, "!") -// { } - -// public SuperJoinStream(ViewStream leftStream, string leftKey, string leftProj, -// ViewStream rightStream, string rightKey, string rightProj) -// : this(leftStream, leftKey, leftProj, rightStream, rightKey, rightProj, false) -// { -// } - -// internal SuperJoinStream(ViewStream leftStream, string leftKey, string leftProj, -// ViewStream rightStream, string rightKey, string rightProj, bool manageUnmappedLeftRows) -// { -// myLeftStream = leftStream; -// myRightStream = rightStream; -// myLeftKeyCol = leftKey; -// myRightKeyCol = rightKey; -// myLeftProj = leftProj; -// myRightProj = rightProj; -// myManageUnmappedLeftRows = manageUnmappedLeftRows; -// } - -// protected override void myOnFirstSubscriber() -// { -// myLeftStream.onChange += new ChangeDelegate(myHandleLeftStreamChanges); -// myRightStream.onChange += new ChangeDelegate(myHandleRightStreamChanges); -// } - -// protected override void myOnLastSubscriber() -// { -// myLeftStream.onChange -= new ChangeDelegate(myHandleLeftStreamChanges); -// myRightStream.onChange -= new ChangeDelegate(myHandleRightStreamChanges); -// myLeftKeyToLeftRows.Clear(); -// myRightKeyToRightRows.Clear(); -// myLeftIDToResultRows.Clear(); -// myRightIDToResultRows.Clear(); -// myState = null; -// myLeftSchema = null; -// myRightSchema = null; -// } - -// private void myHandleLeftStreamChanges(object newValue) -// { -// myHandleStreamChanges(newValue as DataPacket, true); -// } - -// private void myHandleRightStreamChanges(object newValue) -// { -// myHandleStreamChanges(newValue as DataPacket, false); -// } - -// private void myHandleStreamChanges(DataPacket dp, bool isLeft) -// { -// myTimer.start(); -// myInitSchema(dp.schema, isLeft); -// BlueTable dels = null, adds = null; -// if (myState != null) -// { -// dels = new BlueTable(myState.schema); -// adds = new BlueTable(myState.schema); -// } -// myDeleteRows(dp.deletedRows, isLeft, myState, dels); -// myAddRows(dp.addedRows, isLeft, myState, adds); -// myTimer.stop(); -// if (myState != null) -// { -// myFireEvent(new DataPacket(myState, adds, dels)); -// myLeftUnmatchedRowsAdds.Clear(); -// myLeftUnmatchedRowsDels.Clear(); -// } -// } - -// private void myInitSchema(IBlueTableSchema schema, bool isLeft) -// { -// if (myState == null) -// { -// if (isLeft) -// { -// myLeftSchema = schema; -// if (myRightSchema != null) -// { -// myJoinProjectHelper = new JoinProjectHelper(myLeftSchema, myLeftProj, myLeftKeyCol, -// myRightSchema, myRightProj, myRightKeyCol); -// myState = new BlueTable(myJoinProjectHelper.schema); -// } -// } -// else -// { -// myRightSchema = schema; -// if (myLeftSchema != null) -// { -// myJoinProjectHelper = new JoinProjectHelper(myLeftSchema, myLeftProj, myLeftKeyCol, -// myRightSchema, myRightProj, myRightKeyCol); -// myState = new BlueTable(myJoinProjectHelper.schema); -// } -// } -// } -// } - -// private void myAddRows(IBlueRowCollection addedRows, bool isLeft, BlueTable state, BlueTable addedRowsOut) -// { -// object thisKeyCol = isLeft ? myLeftKeyCol : myRightKeyCol, -// otherSideKeyCol = isLeft ? myRightKeyCol : myLeftKeyCol; -// Dictionary> thisKeyToRows = isLeft ? myLeftKeyToLeftRows : myRightKeyToRightRows, -// otherSideKeyToRows = isLeft ? myRightKeyToRightRows : myLeftKeyToLeftRows; -// Dictionary> thisIDToResultRows = isLeft ? myLeftIDToResultRows : myRightIDToResultRows, -// otherIDToResultRows = isLeft ? myRightIDToResultRows : myLeftIDToResultRows; -// foreach (IBlueRow r in addedRows) -// { -// object currentKey = r[thisKeyCol]; -// int currentID = (int)r["id"]; -// ///add it to 1. -// if (!thisKeyToRows.ContainsKey(currentKey)) -// thisKeyToRows.Add(currentKey, new Dictionary()); -// thisKeyToRows[currentKey].Add(currentID, r); -// ///find matching rows, and form result rows -// if (otherSideKeyToRows.ContainsKey(currentKey)) -// { -// ICollection matchingRows = otherSideKeyToRows[currentKey].Values; -// foreach (IBlueRow mr in matchingRows) -// { -// int mrID = (int)mr["id"]; -// SuperJoinRow rr = new SuperJoinRow(myID++, isLeft ? r : mr, isLeft ? mr : r, myJoinProjectHelper); -// state.addRow(rr); -// addedRowsOut.addRow(rr); -// ///add to 3. -// if (!thisIDToResultRows.ContainsKey(currentID)) -// thisIDToResultRows.Add(currentID, new Dictionary()); -// thisIDToResultRows[currentID].Add((int)rr["id"], rr); -// ///add to 4. -// if (!otherIDToResultRows.ContainsKey(mrID)) -// { -// otherIDToResultRows.Add(mrID, new Dictionary()); -// if (myManageUnmappedLeftRows && !isLeft) -// myTakeCareOfUnmatachedLeftRows(mr, false); -// } -// otherIDToResultRows[mrID].Add((int)rr["id"], rr); -// } -// } -// else -// { -// if (myManageUnmappedLeftRows && isLeft) -// myTakeCareOfUnmatachedLeftRows(r, true); -// } -// } -// } - -// private void myDeleteRows(IBlueRowCollection deletedRows, bool isLeft, BlueTable state, BlueTable delRowsOut) -// { -// string thisKeyCol = isLeft ? myLeftKeyCol : myRightKeyCol, -// otherSideKeyCol = isLeft ? myRightKeyCol : myLeftKeyCol; -// Dictionary> thisKeyToRows = isLeft ? myLeftKeyToLeftRows : myRightKeyToRightRows, -// otherSideKeyToRows = isLeft ? myRightKeyToRightRows : myLeftKeyToLeftRows; -// Dictionary> thisIDToResultRows = isLeft ? myLeftIDToResultRows : myRightIDToResultRows, -// otherIDToResultRows = isLeft ? myRightIDToResultRows : myLeftIDToResultRows; -// foreach (IBlueRow r in deletedRows) -// { -// int id = (int)r["id"]; -// object key = r[thisKeyCol]; -// ///delete it from 1. -// thisKeyToRows[key].Remove(id); -// if (thisKeyToRows[key].Count == 0) -// thisKeyToRows.Remove(key); -// ///form return rows and delete from 3. -// if (thisIDToResultRows.ContainsKey(id)) -// { -// ICollection removedResultRows = thisIDToResultRows[id].Values as ICollection; -// thisIDToResultRows.Remove(id); -// foreach (SuperJoinRow rr in removedResultRows) -// { -// ///delete from 4. -// int otherId = isLeft ? rr.rightID : rr.leftID; -// otherIDToResultRows[otherId].Remove((int)rr["id"]); -// if (otherIDToResultRows[otherId].Count == 0) -// otherIDToResultRows.Remove(otherId); -// delRowsOut.addRow(rr); -// state.deleteRow(rr); -// if (myManageUnmappedLeftRows && !isLeft) -// if (!thisKeyToRows.ContainsKey(key)) -// myTakeCareOfUnmatachedLeftRows(rr.leftRow, true); -// } -// } -// else -// { -// if (myManageUnmappedLeftRows && isLeft) -// myTakeCareOfUnmatachedLeftRows(r, false); -// } -// } -// } - -// private void myTakeCareOfUnmatachedLeftRows(IBlueRow leftRow, bool add) -// { -// int lid = (int)leftRow["id"]; -// if (add) -// myLeftUnmatchedRowsAdds.Add(lid, leftRow); -// else -// { -// if (myLeftUnmatchedRowsAdds.ContainsKey(lid)) -// myLeftUnmatchedRowsAdds.Remove(lid); -// else -// myLeftUnmatchedRowsDels.Add((int)leftRow["id"], leftRow); -// } -// } -// } - -// public class SuperLeftJoinStream : SuperJoinStream -// { -// private Dictionary myLeftIDToHalfRows = new Dictionary(); - -// public SuperLeftJoinStream(ViewStream leftStream, string leftJoinCol, -// ViewStream rightStream, string rightJoinCol) -// : this(leftStream, leftJoinCol, "!", rightStream, rightJoinCol, "!") -// { } - -// public SuperLeftJoinStream(ViewStream leftStream, string leftJoinCol, string leftProjExp, -// ViewStream rightStream, string rightJoinCol, string rightProjExp) -// : base(leftStream, leftJoinCol, leftProjExp, rightStream, rightJoinCol, rightProjExp, true) -// { } - -// protected override void myFireEvent(object o) -// { -// myHandleJoinResults(myLeftUnmatchedRowsDels, myLeftUnmatchedRowsAdds, o as DataPacket); -// } - -// private void myHandleJoinResults(Dictionary unmatchedLeftRowsDels, -// Dictionary unmatchedLeftRowAdds, DataPacket resultPacket) -// { -// BlueTable dels = resultPacket.deletedRows as BlueTable, -// adds = resultPacket.addedRows as BlueTable; - -// foreach (IBlueRow lr in unmatchedLeftRowsDels.Values) -// { -// ///an lr will be here if its actually deleted or -// ///a right row is added, so now it has a partner -// ///and is no longer unmatched. -// ///in both cases the half row that this lr -// ///created needs to be removed from this -// ///class's state and added to dels -// ///it means a right is added -// int lid = (int)lr["id"]; -// SuperJoinRow rr = myLeftIDToHalfRows[lid]; -// myLeftIDToHalfRows.Remove(lid); -// myState.deleteRow(rr); -// dels.addRow(rr); -// } - -// foreach (IBlueRow lr in unmatchedLeftRowAdds.Values) -// { -// ///an lr will be here if its added with no -// ///matching right, or a right matching row -// ///is gone so it is unmatched now. -// ///in both cases a half row needs to be -// ///added to this class's state and added -// ///to adds -// int lid = (int)lr["id"]; -// SuperJoinRow rr = new SuperJoinRow(myID++, lr, null, myJoinProjectHelper); -// myLeftIDToHalfRows.Add(lid, rr); -// myState.addRow(rr); -// adds.addRow(rr); -// } -// base.myFireEvent(new DataPacket(myState, adds, dels)); -// } -// } -// } \ No newline at end of file + public IBlueRow leftRow { get { return myLeftRow; } } + + public IBlueRow rightRow { get { return myRightRow; } } + + #region IBlueRow Members + + public IBlueTableSchema schema { get { return myHelper.schema; } } + + public object this[int index] { get { return this[schema[index].colName]; } } + + public object this[string colName] + { + get + { + if (colName == "id") + return myID; + else + { + bool isLeft; + string col = myHelper.getOriginalCol(colName, out isLeft); + return isLeft ? myLeftRow[col] : (myRightRow == null ? (object)DBNull.Value : myRightRow[col]); + } + } + } + + #endregion + + #region IDictionary Members + + public void Add(object key, object value) { { throw new NotSupportedException("IBlueRow is readonly"); } } + + public void Clear() { throw new NotSupportedException("IBlueRow is readonly"); } + + public bool Contains(object key) + { + return myLeftRow.Contains(key) || myRightRow.Contains(key); + } + + public System.Collections.IDictionaryEnumerator GetEnumerator() { return new BlueRowDictionaryEnumerator(this); } + + public bool IsFixedSize { get { return true; } } + + public bool IsReadOnly { get { return true; } } + + public System.Collections.ICollection Keys { get { return schema.columnNames; } } + + public void Remove(object key) { throw new NotSupportedException("IBlueRow is readonly"); } + + public System.Collections.ICollection Values + { + get + { + List rv = new List(); + foreach (string col in Keys) + rv.Add(this[col]); + return rv; + } + } + + public object this[object key] + { + get { return key is string ? this[key.ToString()] : this[(int)key]; } + set { throw new NotSupportedException("IBlueRow is readonly"); } + } + + #endregion + + #region ICollection Members + + public void CopyTo(Array array, int index) { Values.CopyTo(array, index); } + + public int Count { get { return schema.columnNames.Length; } } + + public bool IsSynchronized { get { return false; } } + + public object SyncRoot { get { return this; } } + + #endregion + + #region IEnumerable Members + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return new BlueRowEnumerator(this); } + + #endregion + } + + #endregion + /// + /// this class is written 3-4 yrs after the original + /// join, and learning from the past mistakes, is + /// made super fast and mem efficent + /// + public class SuperJoinStream : ViewStream + { + protected Dictionary myLeftUnmatchedRowsAdds = new Dictionary(), + myLeftUnmatchedRowsDels = new Dictionary(); + private bool myManageUnmappedLeftRows = false; + protected HighPerformanceTimer myTimer = new HighPerformanceTimer(); + protected string myLeftProj = String.Empty, myRightProj = String.Empty; + protected IBlueTableSchema myLeftSchema, myRightSchema; + protected JoinProjectHelper myJoinProjectHelper; + protected BlueTable myState; + protected int myID; + protected ViewStream myLeftStream, myRightStream; + protected string myLeftKeyCol, myRightKeyCol; + protected Dictionary> myLeftKeyToLeftRows = new Dictionary>(), + myRightKeyToRightRows = new Dictionary>(); + + protected Dictionary> myLeftIDToResultRows = new Dictionary>(), + myRightIDToResultRows = new Dictionary>(); + + public SuperJoinStream(ViewStream leftStream, string leftKey, ViewStream rightStream, string rightKey) + : this(leftStream, leftKey, "!", rightStream, rightKey, "!") + { } + + public SuperJoinStream(ViewStream leftStream, string leftKey, string leftProj, + ViewStream rightStream, string rightKey, string rightProj) + : this(leftStream, leftKey, leftProj, rightStream, rightKey, rightProj, false) + { + } + + internal SuperJoinStream(ViewStream leftStream, string leftKey, string leftProj, + ViewStream rightStream, string rightKey, string rightProj, bool manageUnmappedLeftRows) + { + myLeftStream = leftStream; + myRightStream = rightStream; + myLeftKeyCol = leftKey; + myRightKeyCol = rightKey; + myLeftProj = leftProj; + myRightProj = rightProj; + myManageUnmappedLeftRows = manageUnmappedLeftRows; + } + + protected override void myOnFirstSubscriber() + { + myLeftStream.onChange += new ChangeDelegate(myHandleLeftStreamChanges); + myRightStream.onChange += new ChangeDelegate(myHandleRightStreamChanges); + } + + protected override void myOnLastSubscriber() + { + myLeftStream.onChange -= new ChangeDelegate(myHandleLeftStreamChanges); + myRightStream.onChange -= new ChangeDelegate(myHandleRightStreamChanges); + myLeftKeyToLeftRows.Clear(); + myRightKeyToRightRows.Clear(); + myLeftIDToResultRows.Clear(); + myRightIDToResultRows.Clear(); + myState = null; + myLeftSchema = null; + myRightSchema = null; + } + + private void myHandleLeftStreamChanges(object newValue) + { + myHandleStreamChanges(newValue as DataPacket, true); + } + + private void myHandleRightStreamChanges(object newValue) + { + myHandleStreamChanges(newValue as DataPacket, false); + } + + private void myHandleStreamChanges(DataPacket dp, bool isLeft) + { + myTimer.start(); + myInitSchema(dp.schema, isLeft); + BlueTable dels = null, adds = null; + if (myState != null) + { + dels = new BlueTable(myState.schema); + adds = new BlueTable(myState.schema); + } + myDeleteRows(dp.deletedRows, isLeft, myState, dels); + myAddRows(dp.addedRows, isLeft, myState, adds); + myTimer.stop(); + if (myState != null) + { + myFireEvent(new DataPacket(myState, adds, dels)); + myLeftUnmatchedRowsAdds.Clear(); + myLeftUnmatchedRowsDels.Clear(); + } + } + + private void myInitSchema(IBlueTableSchema schema, bool isLeft) + { + if (myState == null) + { + if (isLeft) + { + myLeftSchema = schema; + if (myRightSchema != null) + { + myJoinProjectHelper = new JoinProjectHelper(myLeftSchema, myLeftProj, myLeftKeyCol, + myRightSchema, myRightProj, myRightKeyCol); + myState = new BlueTable(myJoinProjectHelper.schema); + } + } + else + { + myRightSchema = schema; + if (myLeftSchema != null) + { + myJoinProjectHelper = new JoinProjectHelper(myLeftSchema, myLeftProj, myLeftKeyCol, + myRightSchema, myRightProj, myRightKeyCol); + myState = new BlueTable(myJoinProjectHelper.schema); + } + } + } + } + + private void myAddRows(IBlueRowCollection addedRows, bool isLeft, BlueTable state, BlueTable addedRowsOut) + { + object thisKeyCol = isLeft ? myLeftKeyCol : myRightKeyCol, + otherSideKeyCol = isLeft ? myRightKeyCol : myLeftKeyCol; + Dictionary> thisKeyToRows = isLeft ? myLeftKeyToLeftRows : myRightKeyToRightRows, + otherSideKeyToRows = isLeft ? myRightKeyToRightRows : myLeftKeyToLeftRows; + Dictionary> thisIDToResultRows = isLeft ? myLeftIDToResultRows : myRightIDToResultRows, + otherIDToResultRows = isLeft ? myRightIDToResultRows : myLeftIDToResultRows; + foreach (IBlueRow r in addedRows) + { + object currentKey = r[thisKeyCol]; + int currentID = (int)r["id"]; + ///add it to 1. + if (!thisKeyToRows.ContainsKey(currentKey)) + thisKeyToRows.Add(currentKey, new Dictionary()); + thisKeyToRows[currentKey].Add(currentID, r); + ///find matching rows, and form result rows + if (otherSideKeyToRows.ContainsKey(currentKey)) + { + ICollection matchingRows = otherSideKeyToRows[currentKey].Values; + foreach (IBlueRow mr in matchingRows) + { + int mrID = (int)mr["id"]; + SuperJoinRow rr = new SuperJoinRow(myID++, isLeft ? r : mr, isLeft ? mr : r, myJoinProjectHelper); + state.addRow(rr); + addedRowsOut.addRow(rr); + ///add to 3. + if (!thisIDToResultRows.ContainsKey(currentID)) + thisIDToResultRows.Add(currentID, new Dictionary()); + thisIDToResultRows[currentID].Add((int)rr["id"], rr); + ///add to 4. + if (!otherIDToResultRows.ContainsKey(mrID)) + { + otherIDToResultRows.Add(mrID, new Dictionary()); + if (myManageUnmappedLeftRows && !isLeft) + myTakeCareOfUnmatachedLeftRows(mr, false); + } + otherIDToResultRows[mrID].Add((int)rr["id"], rr); + } + } + else + { + if (myManageUnmappedLeftRows && isLeft) + myTakeCareOfUnmatachedLeftRows(r, true); + } + } + } + + private void myDeleteRows(IBlueRowCollection deletedRows, bool isLeft, BlueTable state, BlueTable delRowsOut) + { + string thisKeyCol = isLeft ? myLeftKeyCol : myRightKeyCol, + otherSideKeyCol = isLeft ? myRightKeyCol : myLeftKeyCol; + Dictionary> thisKeyToRows = isLeft ? myLeftKeyToLeftRows : myRightKeyToRightRows, + otherSideKeyToRows = isLeft ? myRightKeyToRightRows : myLeftKeyToLeftRows; + Dictionary> thisIDToResultRows = isLeft ? myLeftIDToResultRows : myRightIDToResultRows, + otherIDToResultRows = isLeft ? myRightIDToResultRows : myLeftIDToResultRows; + foreach (IBlueRow r in deletedRows) + { + int id = (int)r["id"]; + object key = r[thisKeyCol]; + ///delete it from 1. + thisKeyToRows[key].Remove(id); + if (thisKeyToRows[key].Count == 0) + thisKeyToRows.Remove(key); + ///form return rows and delete from 3. + if (thisIDToResultRows.ContainsKey(id)) + { + ICollection removedResultRows = thisIDToResultRows[id].Values as ICollection; + thisIDToResultRows.Remove(id); + foreach (SuperJoinRow rr in removedResultRows) + { + ///delete from 4. + int otherId = isLeft ? rr.rightID : rr.leftID; + otherIDToResultRows[otherId].Remove((int)rr["id"]); + if (otherIDToResultRows[otherId].Count == 0) + otherIDToResultRows.Remove(otherId); + delRowsOut.addRow(rr); + state.deleteRow(rr); + if (myManageUnmappedLeftRows && !isLeft) + if (!thisKeyToRows.ContainsKey(key)) + myTakeCareOfUnmatachedLeftRows(rr.leftRow, true); + } + } + else + { + if (myManageUnmappedLeftRows && isLeft) + myTakeCareOfUnmatachedLeftRows(r, false); + } + } + } + + private void myTakeCareOfUnmatachedLeftRows(IBlueRow leftRow, bool add) + { + int lid = (int)leftRow["id"]; + if (add) + myLeftUnmatchedRowsAdds.Add(lid, leftRow); + else + { + if (myLeftUnmatchedRowsAdds.ContainsKey(lid)) + myLeftUnmatchedRowsAdds.Remove(lid); + else + myLeftUnmatchedRowsDels.Add((int)leftRow["id"], leftRow); + } + } + } + + public class SuperLeftJoinStream : SuperJoinStream + { + private Dictionary myLeftIDToHalfRows = new Dictionary(); + + public SuperLeftJoinStream(ViewStream leftStream, string leftJoinCol, + ViewStream rightStream, string rightJoinCol) + : this(leftStream, leftJoinCol, "!", rightStream, rightJoinCol, "!") + { } + + public SuperLeftJoinStream(ViewStream leftStream, string leftJoinCol, string leftProjExp, + ViewStream rightStream, string rightJoinCol, string rightProjExp) + : base(leftStream, leftJoinCol, leftProjExp, rightStream, rightJoinCol, rightProjExp, true) + { } + + protected override void myFireEvent(object o) + { + myHandleJoinResults(myLeftUnmatchedRowsDels, myLeftUnmatchedRowsAdds, o as DataPacket); + } + + private void myHandleJoinResults(Dictionary unmatchedLeftRowsDels, + Dictionary unmatchedLeftRowAdds, DataPacket resultPacket) + { + BlueTable dels = resultPacket.deletedRows as BlueTable, + adds = resultPacket.addedRows as BlueTable; + + foreach (IBlueRow lr in unmatchedLeftRowsDels.Values) + { + ///an lr will be here if its actually deleted or + ///a right row is added, so now it has a partner + ///and is no longer unmatched. + ///in both cases the half row that this lr + ///created needs to be removed from this + ///class's state and added to dels + ///it means a right is added + int lid = (int)lr["id"]; + SuperJoinRow rr = myLeftIDToHalfRows[lid]; + myLeftIDToHalfRows.Remove(lid); + myState.deleteRow(rr); + dels.addRow(rr); + } + + foreach (IBlueRow lr in unmatchedLeftRowAdds.Values) + { + ///an lr will be here if its added with no + ///matching right, or a right matching row + ///is gone so it is unmatched now. + ///in both cases a half row needs to be + ///added to this class's state and added + ///to adds + int lid = (int)lr["id"]; + SuperJoinRow rr = new SuperJoinRow(myID++, lr, null, myJoinProjectHelper); + myLeftIDToHalfRows.Add(lid, rr); + myState.addRow(rr); + adds.addRow(rr); + } + base.myFireEvent(new DataPacket(myState, adds, dels)); + } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftInternalJoinForeignStream.cs b/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftInternalJoinForeignStream.cs index b680644f3..e9b142de6 100644 --- a/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftInternalJoinForeignStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftInternalJoinForeignStream.cs @@ -1,38 +1,38 @@ -// using System; +using System; -// namespace BCG -// { -// [DataStreamUserRespresentation (false)] -// public class LeftInternalJoinForeignStream : LeftInternalJoinStream -// { -// public LeftInternalJoinForeignStream (ViewStream vsLeft, -// string jcLeft, -// ViewStream vsRight, -// string jcRight) : -// base (vsLeft, jcLeft, vsRight, jcRight) {} +namespace BCG +{ + [DataStreamUserRespresentation (false)] + public class LeftInternalJoinForeignStream : LeftInternalJoinStream + { + public LeftInternalJoinForeignStream (ViewStream vsLeft, + string jcLeft, + ViewStream vsRight, + string jcRight) : + base (vsLeft, jcLeft, vsRight, jcRight) {} -// public LeftInternalJoinForeignStream (ViewStream vsLeft, -// string jcLeft, -// ViewStream vsRight) : -// this (vsLeft, jcLeft, vsRight, "id") {} + public LeftInternalJoinForeignStream (ViewStream vsLeft, + string jcLeft, + ViewStream vsRight) : + this (vsLeft, jcLeft, vsRight, "id") {} -// public override void onTableClear (bool isLeft) -// { -// myLastFiredPacket.deletedRows = myLastFiredPacket.newRows; -// myLastFiredPacket.newRows = new JoinForeignTable (mySchema); -// myLastFiredPacket.addedRows = new BlueTable (mySchema); -// if (!isLeft) -// foreach (IBlueRow row in myLeftDataPacket.newRows) -// ((JoinTable)myLastFiredPacket.newRows).addRow (row, null, myLastFiredPacket); -// } + public override void onTableClear (bool isLeft) + { + myLastFiredPacket.deletedRows = myLastFiredPacket.newRows; + myLastFiredPacket.newRows = new JoinForeignTable (mySchema); + myLastFiredPacket.addedRows = new BlueTable (mySchema); + if (!isLeft) + foreach (IBlueRow row in myLeftDataPacket.newRows) + ((JoinTable)myLastFiredPacket.newRows).addRow (row, null, myLastFiredPacket); + } -// protected override void myInitFiredPacket () -// { -// myLastFiredPacket = new DataPacket (); -// myLastFiredPacket.newRows = new JoinForeignTable (mySchema); -// myLastFiredPacket.addedRows = new BlueTable (mySchema); -// myLastFiredPacket.deletedRows = new BlueTable (mySchema); -// } + protected override void myInitFiredPacket () + { + myLastFiredPacket = new DataPacket (); + myLastFiredPacket.newRows = new JoinForeignTable (mySchema); + myLastFiredPacket.addedRows = new BlueTable (mySchema); + myLastFiredPacket.deletedRows = new BlueTable (mySchema); + } -// } -// } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftInternalJoinStream.cs b/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftInternalJoinStream.cs index 88511470d..0d1921ec2 100644 --- a/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftInternalJoinStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftInternalJoinStream.cs @@ -1,182 +1,182 @@ -// using System; -// using System.Collections; -// using BCG.Utilities; +using System; +using System.Collections; +using BCG.Utilities; -// 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 DataPacket myLeftDataPacket, myRightDataPacket; -// //Container to contain all the left rows that are orphaned during a deletion operation -// private IDictionary myLeftOrphanedRows; -// public LeftInternalJoinStream (ViewStream vsLeft, string jcLeft, ViewStream vsRight, string jcRight) : -// base (vsLeft, jcLeft, vsRight, jcRight) {} +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 DataPacket myLeftDataPacket, myRightDataPacket; + //Container to contain all the left rows that are orphaned during a deletion operation + private IDictionary 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 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 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; -// } + protected override void myInit() + { + base.myInit (); + myLeftOrphanedRows = null; + myLeftDataPacket = myRightDataPacket = null; + } -// private void myLeftChangeHandler (object o) -// { -// myLeftDataPacket = (DataPacket)o; -// myTestAndFire (true); -// } + private void myLeftChangeHandler (object o) + { + myLeftDataPacket = (DataPacket)o; + myTestAndFire (true); + } -// private void myRightChangeHandler (object o) -// { -// myRightDataPacket = (DataPacket)o; -// myTestAndFire (false); -// } + 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; -// DataPacket dp = isLeft ? myLeftDataPacket : 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); -// } -// } + /// + /// 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; + DataPacket dp = isLeft ? myLeftDataPacket : 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) -// { -// foreach (IBlueRow row in rows) -// myAddRow (row, isLeft); -// if (!isLeft) -// foreach (RowNode node in myLeftOrphanedRows.Values) -// ((JoinTable)myLastFiredPacket.newRows).addRow (node.row, null, myLastFiredPacket); -// } + /// + /// Called by JoinOperand + /// + /// + /// + public override void addRows (IBlueRowCollection rows, bool isLeft) + { + foreach (IBlueRow row in rows) + myAddRow (row, isLeft); + if (!isLeft) + foreach (RowNode node in myLeftOrphanedRows.Values) + ((JoinTable)myLastFiredPacket.newRows).addRow (node.row, null, myLastFiredPacket); + } -// /// -// /// called by JoinOperand -// /// -// /// -// /// -// public override void deleteRows (IBlueRowCollection rows, bool isLeft) -// { -// myLeftOrphanedRows = new Hashtable (); -// foreach (IBlueRow row in rows) -// myDeleteRow (row, isLeft); -// } + /// + /// called by JoinOperand + /// + /// + /// + public override void deleteRows (IBlueRowCollection rows, bool isLeft) + { + myLeftOrphanedRows = new Hashtable (); + foreach (IBlueRow row in rows) + myDeleteRow (row, isLeft); + } -// private void myAddRow (IBlueRow row, bool isLeft) -// { -// if (isLeft) -// { -// myLeftOperand.addRow (row); -// RowList matchedRows = myRightOperand.getMatchingRows (row[myLeftJoinColOrdinal]); -// 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 -// { -// RowList matchedRows = myLeftOperand.getMatchingRows (row[myRightJoinColOrdinal]); -// int rightMatchingRowCount = myRightOperand.getMatchingRows (row[myRightJoinColOrdinal]).Count; -// myRightOperand.addRow (row); -// int newRightMatchingRowCount = myRightOperand.getMatchingRows (row[myRightJoinColOrdinal]).Count; -// if (matchedRows.Count != 0){ -// foreach (RowNode matchedNode in matchedRows) -// { -// //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) -// { -// if (myLeftOrphanedRows[matchedNode.row["id"]] == null) -// ((JoinTable)myLastFiredPacket.newRows).deleteRow (true, matchedNode.row, myLastFiredPacket); -// else -// myLeftOrphanedRows.Remove (matchedNode.row["id"]); -// } -// //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 myAddRow (IBlueRow row, bool isLeft) + { + if (isLeft) + { + myLeftOperand.addRow (row); + RowList matchedRows = myRightOperand.getMatchingRows (row[myLeftJoinColOrdinal]); + 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 + { + RowList matchedRows = myLeftOperand.getMatchingRows (row[myRightJoinColOrdinal]); + int rightMatchingRowCount = myRightOperand.getMatchingRows (row[myRightJoinColOrdinal]).Count; + myRightOperand.addRow (row); + int newRightMatchingRowCount = myRightOperand.getMatchingRows (row[myRightJoinColOrdinal]).Count; + if (matchedRows.Count != 0){ + foreach (RowNode matchedNode in matchedRows) + { + //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) + { + if (myLeftOrphanedRows[matchedNode.row["id"]] == null) + ((JoinTable)myLastFiredPacket.newRows).deleteRow (true, matchedNode.row, myLastFiredPacket); + else + myLeftOrphanedRows.Remove (matchedNode.row["id"]); + } + //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) -// { -// if (isLeft) -// { -// myLeftOperand.deleteRow (row); -// ((JoinTable)myLastFiredPacket.newRows).deleteRow (true, row, myLastFiredPacket); -// } -// else -// { -// RowList matchedRows = myLeftOperand.getMatchingRows (row[myRightJoinColOrdinal]); -// int rightMatchingRowCount = myRightOperand.getMatchingRows (row[myRightJoinColOrdinal]).Count; -// myRightOperand.deleteRow (row); -// int newRightMatchingRowCount = myRightOperand.getMatchingRows (row[myRightJoinColOrdinal]).Count; -// if (matchedRows.Count != 0) -// { -// if (rightMatchingRowCount > 0 && newRightMatchingRowCount == 0) -// //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. -// foreach (RowNode matchedNode in matchedRows) -// { -// // BlueEventLog.logAssert (myLeftOrphanedRows[matchedNode.row["id"]] == null); -// myLeftOrphanedRows[matchedNode.row["id"]] = matchedNode; -// } -// ((JoinTable)myLastFiredPacket.newRows).deleteRow (false, row, myLastFiredPacket); -// } -// } -// } + private void myDeleteRow (IBlueRow row, bool isLeft) + { + if (isLeft) + { + myLeftOperand.deleteRow (row); + ((JoinTable)myLastFiredPacket.newRows).deleteRow (true, row, myLastFiredPacket); + } + else + { + RowList matchedRows = myLeftOperand.getMatchingRows (row[myRightJoinColOrdinal]); + int rightMatchingRowCount = myRightOperand.getMatchingRows (row[myRightJoinColOrdinal]).Count; + myRightOperand.deleteRow (row); + int newRightMatchingRowCount = myRightOperand.getMatchingRows (row[myRightJoinColOrdinal]).Count; + if (matchedRows.Count != 0) + { + if (rightMatchingRowCount > 0 && newRightMatchingRowCount == 0) + //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. + foreach (RowNode matchedNode in matchedRows) + { + // BlueEventLog.logAssert (myLeftOrphanedRows[matchedNode.row["id"]] == null); + myLeftOrphanedRows[matchedNode.row["id"]] = matchedNode; + } + ((JoinTable)myLastFiredPacket.newRows).deleteRow (false, row, myLastFiredPacket); + } + } + } -// public override void onTableClear (bool isLeft) -// { -// myLastFiredPacket.deletedRows = myLastFiredPacket.newRows; -// myLastFiredPacket.newRows = new JoinTable (mySchema); -// myLastFiredPacket.addedRows = new BlueTable (mySchema); -// if (!isLeft) -// foreach (IBlueRow row in myLeftDataPacket.newRows) -// ((JoinTable)myLastFiredPacket.newRows).addRow (row, null, myLastFiredPacket); -// } + public override void onTableClear (bool isLeft) + { + myLastFiredPacket.deletedRows = myLastFiredPacket.newRows; + myLastFiredPacket.newRows = new JoinTable (mySchema); + myLastFiredPacket.addedRows = new BlueTable (mySchema); + if (!isLeft) + foreach (IBlueRow row in myLeftDataPacket.newRows) + ((JoinTable)myLastFiredPacket.newRows).addRow (row, null, myLastFiredPacket); + } -// } -// } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftJoinForeignStream.cs b/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftJoinForeignStream.cs index 436281cc0..4b31f2fd2 100644 --- a/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftJoinForeignStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftJoinForeignStream.cs @@ -1,59 +1,59 @@ -// using System; -// using System.Data; -// using System.Collections; -// using System.Text.RegularExpressions; -// using BCG.Utilities; -// using BCG.Utilities.General; +using System; +using System.Data; +using System.Collections; +using System.Text.RegularExpressions; +using BCG.Utilities; +using BCG.Utilities.General; -// namespace BCG -// { -// public class LeftJoinForeignStream : JoinStream -// { +namespace BCG +{ + public class LeftJoinForeignStream : JoinStream + { -// public LeftJoinForeignStream (ViewStream v1, string joinCol1, ColumnMap cm1, -// ViewStream v2, string joinCol2, ColumnMap cm2) -// : base (v1, joinCol1, cm1, v2, joinCol2, cm2) -// {} + public LeftJoinForeignStream (ViewStream v1, string joinCol1, ColumnMap cm1, + ViewStream v2, string joinCol2, ColumnMap cm2) + : base (v1, joinCol1, cm1, v2, joinCol2, cm2) + {} -// public LeftJoinForeignStream (ViewStream v1, string joinCol1, string cmExp1, -// ViewStream v2, string joinCol2, string cmExp2) -// : base (v1, joinCol1, cmExp1, v2, joinCol2, cmExp2) -// {} + public LeftJoinForeignStream (ViewStream v1, string joinCol1, string cmExp1, + ViewStream v2, string joinCol2, string cmExp2) + : base (v1, joinCol1, cmExp1, v2, joinCol2, cmExp2) + {} -// public LeftJoinForeignStream (ViewStream v1, string joinCol1, string cmExp1, -// ViewStream v2, string joinCol2) -// : -// this (v1,joinCol1, new ColumnMap(cmExp1), v2, joinCol2) -// {} + public LeftJoinForeignStream (ViewStream v1, string joinCol1, string cmExp1, + ViewStream v2, string joinCol2) + : + this (v1,joinCol1, new ColumnMap(cmExp1), v2, joinCol2) + {} -// public LeftJoinForeignStream (ViewStream v1, string joinCol1, -// ViewStream v2, string joinCol2, string cmExp2) -// : -// this (v1,joinCol1, v2, joinCol2, new ColumnMap(cmExp2)) -// {} + public LeftJoinForeignStream (ViewStream v1, string joinCol1, + ViewStream v2, string joinCol2, string cmExp2) + : + this (v1,joinCol1, v2, joinCol2, new ColumnMap(cmExp2)) + {} -// public LeftJoinForeignStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2) -// : base (v1, joinCol1, v2, joinCol2) -// {} + public LeftJoinForeignStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2) + : base (v1, joinCol1, v2, joinCol2) + {} -// public LeftJoinForeignStream (ViewStream v1, string joinCol1, ColumnMap cm1, ViewStream v2, string joinCol2) -// : base (v1, joinCol1, cm1, v2, joinCol2) -// {} + public LeftJoinForeignStream (ViewStream v1, string joinCol1, ColumnMap cm1, ViewStream v2, string joinCol2) + : base (v1, joinCol1, cm1, v2, joinCol2) + {} -// public LeftJoinForeignStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2, ColumnMap cm2) -// : base (v1, joinCol1, v2, joinCol2, cm2) -// {} + public LeftJoinForeignStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2, ColumnMap cm2) + : base (v1, joinCol1, v2, joinCol2, cm2) + {} -// protected override InternalJoinStream myConstructInternalJoinStream ( -// ViewStream vs1, -// string jc1, -// ViewStream vs2, -// string jc2) -// { -// return new LeftInternalJoinForeignStream (vs1, jc1, vs2, jc2); -// } -// } -// } + protected override InternalJoinStream myConstructInternalJoinStream ( + ViewStream vs1, + string jc1, + ViewStream vs2, + string jc2) + { + return new LeftInternalJoinForeignStream (vs1, jc1, vs2, jc2); + } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftJoinStream.cs b/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftJoinStream.cs index 1011bc112..7425d5da3 100644 --- a/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftJoinStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/LeftJoin/LeftJoinStream.cs @@ -1,58 +1,58 @@ -// using System; -// using System.Data; -// using System.Collections; -// using System.Text.RegularExpressions; -// using BCG.Utilities; -// using BCG.Utilities.General; +using System; +using System.Data; +using System.Collections; +using System.Text.RegularExpressions; +using BCG.Utilities; +using BCG.Utilities.General; -// namespace BCG -// { -// public class LeftJoinStream : JoinStream -// { -// public LeftJoinStream (ViewStream v1, string joinCol1, ColumnMap cm1, -// ViewStream v2, string joinCol2, ColumnMap cm2) -// : base (v1, joinCol1, cm1, v2, joinCol2, cm2) -// {} +namespace BCG +{ + public class LeftJoinStream : JoinStream + { + public LeftJoinStream (ViewStream v1, string joinCol1, ColumnMap cm1, + ViewStream v2, string joinCol2, ColumnMap cm2) + : base (v1, joinCol1, cm1, v2, joinCol2, cm2) + {} -// public LeftJoinStream (ViewStream v1, string joinCol1, string cmExp1, -// ViewStream v2, string joinCol2, string cmExp2) -// : base (v1, joinCol1, cmExp1, v2, joinCol2, cmExp2) -// {} + public LeftJoinStream (ViewStream v1, string joinCol1, string cmExp1, + ViewStream v2, string joinCol2, string cmExp2) + : base (v1, joinCol1, cmExp1, v2, joinCol2, cmExp2) + {} -// public LeftJoinStream (ViewStream v1, string joinCol1, string cmExp1, -// ViewStream v2, string joinCol2) -// : -// this (v1,joinCol1, new ColumnMap(cmExp1), v2, joinCol2) -// {} + public LeftJoinStream (ViewStream v1, string joinCol1, string cmExp1, + ViewStream v2, string joinCol2) + : + this (v1,joinCol1, new ColumnMap(cmExp1), v2, joinCol2) + {} -// public LeftJoinStream (ViewStream v1, string joinCol1, -// ViewStream v2, string joinCol2, string cmExp2) -// : -// this (v1,joinCol1, v2, joinCol2, new ColumnMap(cmExp2)) -// {} + public LeftJoinStream (ViewStream v1, string joinCol1, + ViewStream v2, string joinCol2, string cmExp2) + : + this (v1,joinCol1, v2, joinCol2, new ColumnMap(cmExp2)) + {} -// public LeftJoinStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2) -// : base (v1, joinCol1, v2, joinCol2) -// {} + public LeftJoinStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2) + : base (v1, joinCol1, v2, joinCol2) + {} -// public LeftJoinStream (ViewStream v1, string joinCol1, ColumnMap cm1, ViewStream v2, string joinCol2) -// : base (v1, joinCol1, cm1, v2, joinCol2) -// {} + public LeftJoinStream (ViewStream v1, string joinCol1, ColumnMap cm1, ViewStream v2, string joinCol2) + : base (v1, joinCol1, cm1, v2, joinCol2) + {} -// public LeftJoinStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2, ColumnMap cm2) -// : base (v1, joinCol1, v2, joinCol2, cm2) -// {} + public LeftJoinStream (ViewStream v1, string joinCol1, ViewStream v2, string joinCol2, ColumnMap cm2) + : base (v1, joinCol1, v2, joinCol2, cm2) + {} -// protected override InternalJoinStream myConstructInternalJoinStream ( -// ViewStream vs1, -// string jc1, -// ViewStream vs2, -// string jc2) -// { -// return new LeftInternalJoinStream (vs1, jc1, vs2, jc2); -// } -// } -// } + protected override InternalJoinStream myConstructInternalJoinStream ( + ViewStream vs1, + string jc1, + ViewStream vs2, + string jc2) + { + return new LeftInternalJoinStream (vs1, jc1, vs2, jc2); + } + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Project/ColumnMap.cs b/src/DataStreamSuite/DataStream/Primitives/Project/ColumnMap.cs index 0123c7e68..7bdfd4a90 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Project/ColumnMap.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Project/ColumnMap.cs @@ -1,68 +1,65 @@ -// using System; -// using System.Collections; -// using System.Data; -// using System.Text.RegularExpressions; -// using BCG.Utilities; -// using BCG.Evaluator.ColumnMapExpression; +using System; +using System.Collections; +using System.Data; +using System.Text.RegularExpressions; +using BCG.Utilities; +using Bts.ColumnMapExpressions; -// namespace BCG -// { -// /// -// /// ColumnMap governs what columns that are to be projected, hidden or aliased -// /// -// public class ColumnMap -// { -// private string myColMapExpression; +namespace BCG +{ + /// + /// ColumnMap governs what columns that are to be projected, hidden or aliased + /// + public class ColumnMap + { + private string myColMapExpression; -// /// -// /// ColumnMap governs what columns that are to be projected, hidden or aliased. If all columns -// /// are to be preserved with their names, ColumnMap("", false) should be passed -// /// -// /// -// /// Includes the mapping of the columns to their respective aliases as well as the columns that -// /// need to be included/excluded depending on the second parameter. e.g. "a:b c" means to map -// /// a onto b, and include or exclude c depending on the value of the second parameter -// /// -// /// -// /// isInclusive decides whether to include the unmapped columns in argument one or to exclude it -// /// from the resulting table -// /// -// public ColumnMap (string colMapExpression) -// { -// myColMapExpression = colMapExpression == null ? "!" : colMapExpression; -// } + /// + /// ColumnMap governs what columns that are to be projected, hidden or aliased. If all columns + /// are to be preserved with their names, ColumnMap("", false) should be passed + /// + /// + /// Includes the mapping of the columns to their respective aliases as well as the columns that + /// need to be included/excluded depending on the second parameter. e.g. "a:b c" means to map + /// a onto b, and include or exclude c depending on the value of the second parameter + /// + /// + /// isInclusive decides whether to include the unmapped columns in argument one or to exclude it + /// from the resulting table + /// + public ColumnMap (string colMapExpression) + { + myColMapExpression = colMapExpression == null ? "!" : colMapExpression; + } -// public IDictionary getColumnMap (IBlueTableSchema schema) -// { -// IDictionary retVal = new Hashtable (); -// syntax parser = -// new syntax(); - -// ColumnMapExpression e = (ColumnMapExpression) -// parser.Parse(this.myColMapExpression); + public IDictionary getColumnMap (IBlueTableSchema schema) + { + IDictionary retVal = new Hashtable (); + var e = ColumnMapExpression.Parse(myColMapExpression) ?? + throw new Exception($"Failed to parse column expression '{myColMapExpression}'"); -// if (e.isInclusive) -// { -// retVal = new Hashtable (e.columns); -// retVal["id"] = "id"; -// } -// else -// foreach (ColumnInfo ci in schema.columnInfo) -// if (e.columns[ci.colName] == null) -// retVal[ci.colName] = ci.colName; -// else if ((string)e.columns[ci.colName] != ci.colName) -// retVal[ci.colName] = e.columns[ci.colName]; -// return invertDictionary (retVal); -// } + if (e.IsInclusive) { + retVal = new Hashtable(e.Columns) { + ["id"] = "id" + }; + } + else + foreach (ColumnInfo ci in schema.columnInfo) + if (e.Columns[ci.colName] == null) + retVal[ci.colName] = ci.colName; + else if (e.Columns[ci.colName] != ci.colName) + retVal[ci.colName] = e.Columns[ci.colName]; + return invertDictionary (retVal); + } -// public static IDictionary invertDictionary (IDictionary dic) -// { -// IDictionary retVal = new Hashtable (); -// foreach (DictionaryEntry de in dic) -// retVal[de.Value] = de.Key; -// return retVal; -// } + public static IDictionary invertDictionary (IDictionary dic) + { + IDictionary retVal = new Hashtable (); + foreach (DictionaryEntry de in dic) + retVal[de.Value] = de.Key; + return retVal; + } -// public string columnExpression {get{return myColMapExpression;}} -// } -// } \ No newline at end of file + public string columnExpression {get{return myColMapExpression;}} + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Union/AppendStream.cs b/src/DataStreamSuite/DataStream/Primitives/Union/AppendStream.cs index 50a2e7339..a2458379f 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Union/AppendStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Union/AppendStream.cs @@ -1,118 +1,118 @@ -// using System; -// using System.Data; -// using System.Collections; -// using BCG.Utilities; -// using BCG.Utilities.General; -// namespace BCG -// { -// /// -// /// AppendStream appends 2 tables with each other, with a generated id having no bearing to the original id. -// /// The column ordering the 2 tables need not be the same -// /// -// public class AppendStream : UnionStream -// { -// private bool myUseNewAppend = DataStreamLoadFileConfig.instance.useNewAppend; -// public AppendStream(ViewStream v1, ViewStream v2) -// { -// if (myUseNewAppend) -// mySetRealSource(new MutableAppendStream(v1, v2)); -// else -// { -// myLeftVs = v1; -// myRightVs = v2; -// } -// } +using System; +using System.Data; +using System.Collections; +using BCG.Utilities; +using BCG.Utilities.General; +namespace BCG +{ + /// + /// AppendStream appends 2 tables with each other, with a generated id having no bearing to the original id. + /// The column ordering the 2 tables need not be the same + /// + public class AppendStream : UnionStream + { + private bool myUseNewAppend = DataStreamLoadFileConfig.instance.useNewAppend; + public AppendStream(ViewStream v1, ViewStream v2) + { + if (myUseNewAppend) + mySetRealSource(new MutableAppendStream(v1, v2)); + else + { + myLeftVs = v1; + myRightVs = v2; + } + } -// public AppendStream (params ViewStream[] vss) -// { + public AppendStream (params ViewStream[] vss) + { -// if (myUseNewAppend) -// mySetRealSource(new MutableAppendStream(vss)); -// else -// { -// if (vss.Length == 1) -// mySetRealSource(vss[0]); -// else if (vss.Length > 1) -// { -// AppendStream rv = null; -// foreach (ViewStream s in vss) -// rv = rv == null ? new AppendStream(new ViewStream[] { s }) : new AppendStream(rv, s); -// mySetRealSource(rv); -// } -// else -// //don't do anything if they didnt' supply any viewstreams -// mySetRealSource(null); -// } + if (myUseNewAppend) + mySetRealSource(new MutableAppendStream(vss)); + else + { + if (vss.Length == 1) + mySetRealSource(vss[0]); + else if (vss.Length > 1) + { + AppendStream rv = null; + foreach (ViewStream s in vss) + rv = rv == null ? new AppendStream(new ViewStream[] { s }) : new AppendStream(rv, s); + mySetRealSource(rv); + } + else + //don't do anything if they didnt' supply any viewstreams + mySetRealSource(null); + } -// } + } -// public override void add (IBlueRowCollection rows, bool isLeft) -// { -// IDictionary index = isLeft ? myLeftIndex : myRightIndex; -// int[] indexToIndexMap = isLeft ? myLeftIndexToIndexMap : myRightIndexToIndexMap; -// foreach (IBlueRow row in rows) -// { -// UnionRow newRow = new UnionRow (myLastFiredPacket.newRows.schema, row, -// myIdCounter ++, myIdIndex, indexToIndexMap); -// ((BlueTable)myLastFiredPacket.newRows).addRow (newRow); -// index[row["id"]] = newRow; -// ((BlueTable)(myLastFiredPacket.addedRows)).addRow (newRow); -// } -// } + public override void add (IBlueRowCollection rows, bool isLeft) + { + IDictionary index = isLeft ? myLeftIndex : myRightIndex; + int[] indexToIndexMap = isLeft ? myLeftIndexToIndexMap : myRightIndexToIndexMap; + foreach (IBlueRow row in rows) + { + UnionRow newRow = new UnionRow (myLastFiredPacket.newRows.schema, row, + myIdCounter ++, myIdIndex, indexToIndexMap); + ((BlueTable)myLastFiredPacket.newRows).addRow (newRow); + index[row["id"]] = newRow; + ((BlueTable)(myLastFiredPacket.addedRows)).addRow (newRow); + } + } -// public override void delete (IBlueRowCollection rows, bool isLeft) -// { -// IDictionary index = isLeft ? myLeftIndex : myRightIndex; -// foreach (IBlueRow axedRow in rows) -// { -// int rowId = (int)axedRow["id"]; -// IBlueRow row = (IBlueRow)index[rowId]; -// index.Remove (rowId); -// ((BlueTable)myLastFiredPacket.newRows).deleteRow (row); -// ((BlueTable)(myLastFiredPacket.deletedRows)).addRow (row); -// } -// } + public override void delete (IBlueRowCollection rows, bool isLeft) + { + IDictionary index = isLeft ? myLeftIndex : myRightIndex; + foreach (IBlueRow axedRow in rows) + { + int rowId = (int)axedRow["id"]; + IBlueRow row = (IBlueRow)index[rowId]; + index.Remove (rowId); + ((BlueTable)myLastFiredPacket.newRows).deleteRow (row); + ((BlueTable)(myLastFiredPacket.deletedRows)).addRow (row); + } + } -// public override void mergeChanges (IBlueRowCollection dels, IBlueRowCollection adds, bool isLeft) -// { -// IDictionary temp = new Hashtable (); -// IDictionary index = isLeft ? myLeftIndex : myRightIndex; -// int[] indexToIndexMap = isLeft ? myLeftIndexToIndexMap : myRightIndexToIndexMap; -// IBlueRow corrRow; -// foreach (IBlueRow dRow in dels) -// { -// corrRow = index[dRow["id"]] as IBlueRow; -// (myLastFiredPacket.newRows as BlueTable).deleteRow (corrRow); -// (myLastFiredPacket.deletedRows as BlueTable).addRow (corrRow); -// index.Remove (dRow["id"]); -// if (adds.containsID ((int)dRow["id"])) -// { -// IBlueRow r = adds[(int)dRow["id"]]; -// corrRow = new UnionRow (myLastFiredPacket.schema, r, (int)corrRow["id"], myIdIndex, indexToIndexMap); -// (myLastFiredPacket.newRows as BlueTable).addRow (corrRow); -// (myLastFiredPacket.addedRows as BlueTable).addRow (corrRow); -// index.Add (r["id"], corrRow); -// temp.Add (r["id"], true); -// } -// } -// foreach (IBlueRow aRow in adds) -// { -// if (!temp.Contains (aRow["id"])) -// { -// corrRow = new UnionRow (myLastFiredPacket.schema, aRow, myIdCounter++, myIdIndex, indexToIndexMap); -// (myLastFiredPacket.newRows as BlueTable).addRow (corrRow); -// (myLastFiredPacket.addedRows as BlueTable).addRow (corrRow); -// index.Add (aRow["id"], corrRow); -// } -// } -// } -// } + public override void mergeChanges (IBlueRowCollection dels, IBlueRowCollection adds, bool isLeft) + { + IDictionary temp = new Hashtable (); + IDictionary index = isLeft ? myLeftIndex : myRightIndex; + int[] indexToIndexMap = isLeft ? myLeftIndexToIndexMap : myRightIndexToIndexMap; + IBlueRow corrRow; + foreach (IBlueRow dRow in dels) + { + corrRow = index[dRow["id"]] as IBlueRow; + (myLastFiredPacket.newRows as BlueTable).deleteRow (corrRow); + (myLastFiredPacket.deletedRows as BlueTable).addRow (corrRow); + index.Remove (dRow["id"]); + if (adds.containsID ((int)dRow["id"])) + { + IBlueRow r = adds[(int)dRow["id"]]; + corrRow = new UnionRow (myLastFiredPacket.schema, r, (int)corrRow["id"], myIdIndex, indexToIndexMap); + (myLastFiredPacket.newRows as BlueTable).addRow (corrRow); + (myLastFiredPacket.addedRows as BlueTable).addRow (corrRow); + index.Add (r["id"], corrRow); + temp.Add (r["id"], true); + } + } + foreach (IBlueRow aRow in adds) + { + if (!temp.Contains (aRow["id"])) + { + corrRow = new UnionRow (myLastFiredPacket.schema, aRow, myIdCounter++, myIdIndex, indexToIndexMap); + (myLastFiredPacket.newRows as BlueTable).addRow (corrRow); + (myLastFiredPacket.addedRows as BlueTable).addRow (corrRow); + index.Add (aRow["id"], corrRow); + } + } + } + } -// public class MergeStream : ViewStream -// { -// public MergeStream (ViewStream v1, ViewStream v2, string[] mergeCols) -// { -// mySetRealSource (new DistinctRowStream (new AppendStream (v1, v2), mergeCols)); -// } -// } -// } \ No newline at end of file + public class MergeStream : ViewStream + { + public MergeStream (ViewStream v1, ViewStream v2, string[] mergeCols) + { + mySetRealSource (new DistinctRowStream (new AppendStream (v1, v2), mergeCols)); + } + } +} diff --git a/src/DataStreamSuite/DataStreamCore/AssemblyInfo.cs b/src/DataStreamSuite/DataStreamCore/AssemblyInfo.cs new file mode 100644 index 000000000..de84fed34 --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("DataStream")] diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowExtensions.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowExtensions.cs new file mode 100644 index 000000000..4f2d62c06 --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowExtensions.cs @@ -0,0 +1,12 @@ +#nullable enable + +namespace BCG; + +using Spark.Core.Collections; + +using System.Collections.Generic; + +public static class BlueRowExtensions { + public static IDictionary AsGenericDictionary(this IBlueRow row) => + GenericDictionaryWrapper.Create(row); +} diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/MutableBlueRow.cs b/src/DataStreamSuite/DataStreamCore/BlueData/MutableBlueRow.cs index a21920bf8..fa09d3aee 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/MutableBlueRow.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/MutableBlueRow.cs @@ -1,6 +1,8 @@ using System; using System.Data; using System.Collections; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace BCG { @@ -36,6 +38,11 @@ namespace BCG myValues[colNo] = newValue; } +#nullable enable + public IDictionary AsDictionary() => + new DictionaryWrapper(this); +#nullable disable + private static object[] myCloneArray (IBlueTableSchema schema,object[] vals) { object[] retVal = new object[schema.columnNames.Length]; @@ -52,5 +59,69 @@ namespace BCG return rv; } - } + class DictionaryWrapper : IDictionary { + readonly MutableBlueRow source; + + public DictionaryWrapper(MutableBlueRow source) => + this.source = source; + + public ICollection Keys => source.schema.columnNames; + + public ICollection Values => source.myValues; + + public int Count => source.Count; + + public bool IsReadOnly => false; + + public object this[string key] { + get => source[key]; + set => source.setValue(key, value); + } + + public void Add(string key, object value) => + source.setValue(key, value); + + public void Add(KeyValuePair item) => + source.setValue(item.Key, item.Value); + + public void Clear() => + source.Clear(); + + public bool Contains(KeyValuePair item) => + source.Contains(item.Key) && Equals(source[item.Key], item.Value); + + public bool ContainsKey(string key) => + source.Contains(key); + + public void CopyTo(KeyValuePair[] array, int arrayIndex) { + for (int i = 0; i < source.Count; i++) { + array[arrayIndex++] = new(source.schema.columnNames[i], source.myValues[i]); + } + } + + public IEnumerator> GetEnumerator() { + throw new NotImplementedException(); + } + + public bool Remove(string key) { + throw new NotImplementedException(); + } + + public bool Remove(KeyValuePair item) { + throw new NotImplementedException(); + } + + public bool TryGetValue(string key, [MaybeNullWhen(false)] out object value) { + if (source.Contains(key)) { + value = source[key]; + return true; + } + value = default; + return false; + } + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + } + } }