using bts; using Bts.DataStreamCore; using Spark.Core.Diagnostics; using Spark.Core.Disposables; using System; using System.Collections; namespace BCG { public class JoinStream : ViewStream { protected readonly InternalJoinStream? myIjs; protected readonly ColumnMap? myLeftCm; protected readonly ColumnMap? myRightCm; protected readonly bool myIsProject = true; protected ProjectStream? myPs; readonly bool myUseSuperJoin = DataStreamLoadFileConfig.instance.useNewJoin; IDisposable? internalSubscription; IDisposable? projectSubscription; 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) : 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) { 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, 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 void myInternalJoinChangeHandler (object o) { if (myIsProject) { if (myPs == null) { ExceptionAssert.NotNull(myIjs, myLeftCm, myRightCm); var leftSchema = myIjs.getSchema(true) ?? throw new InvalidOperationException("Left schema is null"); var rightSchema = myIjs.getSchema(true) ?? throw new InvalidOperationException("Right schema is null"); var joinSchema = (JoinTableSchema)myIjs.schema; JoinNameResolver resolver = new JoinNameResolver( joinSchema, leftSchema, myLeftCm.columnExpression, rightSchema, myRightCm.columnExpression); IDictionary map = resolver.getMap (); map["id"] = "id"; Disposable.Dispose(ref projectSubscription); myPs = new ProjectStream (myIjs, map); projectSubscription = myPs.Subscribe(myProjectChangeHandler); return; } else Log.assert(false, "This call back should not have been called"); } else { myFireEvent(cloneDataPacket(o)); } } private void myProjectChangeHandler (object o) { ExceptionAssert.NotNull(myIjs); if (myIjs.numberOfSubscribers > 1) { Disposable.Dispose(ref internalSubscription); } myFireEvent(cloneDataPacket(o)); } protected override void myOnFirstSubscriber () { Log.isnull(internalSubscription); internalSubscription = ExceptionAssert.NotNull(myIjs).Subscribe(myInternalJoinChangeHandler); } protected override void myOnLastSubscriber () { Disposable.Dispose(ref internalSubscription); // 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 Disposable.Dispose(ref projectSubscription); myPs = null; } object cloneDataPacket(object dpObj) { var rv = (dpObj as ICloneable)?.Clone(); if (rv is null) { var dp = (IDataPacket)dpObj; rv = new DataPacket(new BlueTable(dp.newRows), new BlueTable(dp.addedRows), new BlueTable(dp.deletedRows)); } return rv; } /**************************************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) { ExceptionAssert.NotNull(myIjs).test(dp, isLeft); } /***********************************************************************************************/ } }