From 75f2678df6ad6dbe127f97b85c55e253d0404892 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 2 Sep 2025 13:50:49 -0400 Subject: [PATCH] cleanup: Fix build warnings * BlueOrbNLStream.cs * ChangeTableNameStream.cs * FilterStream.cs * FilterStream.cs * GroupByOutputTable.cs * GroupByStream.cs * JoinForeignTable.cs * MultiRowRollingMathStream.cs * MutableAppendStream.cs * PersistWindowState.cs * PubsubEchoTestStream.cs * RowDictionaryTable.cs * SortedTable.cs * TimeSeriesStream.cs Also add Metadata prop to IDataPacket --- .../DataStream/BlueOrb/FilterStream.cs | 13 +- .../DataStream/BlueOrb/GroupByStream.cs | 54 +-- .../BlueOrb/MultiRowRollingMathStream.cs | 6 +- .../DataStream/BlueOrb/TimeSeriesStream.cs | 15 +- .../Primitives/Filter/FilterStream.cs | 145 +++++--- .../Primitives/GroupBy/GroupByOutputTable.cs | 20 +- .../Primitives/GroupBy/RowDictionaryTable.cs | 33 +- .../Primitives/Join/JoinForeignTable.cs | 12 +- .../DataStream/Primitives/Join/JoinRow.cs | 2 +- .../Primitives/Misc/PubsubEchoTestStream.cs | 48 ++- .../Project/ChangeTableNameStream.cs | 15 +- .../DataStream/Primitives/Sort/SortedTable.cs | 217 +++++------ .../Primitives/Union/MutableAppendStream.cs | 14 +- .../BlueData/BlueDataUtility.cs | 4 +- .../BlueData/BlueRowDictionaryEnumerator.cs | 8 +- .../BlueData/BlueRowEnumerator.cs | 6 +- .../DataStreamCore/DataPacket.cs | 12 +- .../DataStreamCore/IDataPacket.cs | 26 ++ .../PersistWindowState.cs | 341 +++++++++--------- .../SharedStreams/NL/BlueOrbNLStream.cs | 13 +- 20 files changed, 532 insertions(+), 472 deletions(-) diff --git a/src/DataStreamSuite/DataStream/BlueOrb/FilterStream.cs b/src/DataStreamSuite/DataStream/BlueOrb/FilterStream.cs index 7ebf00629..378982806 100644 --- a/src/DataStreamSuite/DataStream/BlueOrb/FilterStream.cs +++ b/src/DataStreamSuite/DataStream/BlueOrb/FilterStream.cs @@ -23,7 +23,7 @@ namespace bts.BlueOrb { public bool filter(IBlueRow dr) { bool rv = true; for (int i = 0; i < myCols.Length; i++) { - rv &= myRegExps[i].IsMatch(dr[myCols[i]] is DBNull ? "" : dr[myCols[i]].ToString()); + rv &= myRegExps[i].IsMatch(dr[myCols[i]] is DBNull ? "" : dr[myCols[i]]?.ToString() ?? string.Empty); if (!rv) return rv; } @@ -52,12 +52,11 @@ namespace bts.BlueOrb { public FilterStream(ViewStream vs, IStreamFilter filter) { - { - myParentStream = vs; - myFilter = filter; - myFilterString = filter.ToString(); - if (filter is IDynamicStreamFilter) - (filter as IDynamicStreamFilter).onFilterChange += new FilterChangeDelegate(myHandleFilterChange); + myParentStream = vs; + myFilter = filter; + myFilterString = filter.ToString() ?? string.Empty; + if (filter is IDynamicStreamFilter dynFilter) { + dynFilter.onFilterChange += new FilterChangeDelegate(myHandleFilterChange); } } diff --git a/src/DataStreamSuite/DataStream/BlueOrb/GroupByStream.cs b/src/DataStreamSuite/DataStream/BlueOrb/GroupByStream.cs index 81916fee5..9c5b73eab 100644 --- a/src/DataStreamSuite/DataStream/BlueOrb/GroupByStream.cs +++ b/src/DataStreamSuite/DataStream/BlueOrb/GroupByStream.cs @@ -7,6 +7,7 @@ using System.Text.Json; using bts; //using bts.SilverPine; using BCG; +using mbts.utils; namespace bts.BlueOrb { [DataStreamUserRespresentation("GroupByStream")] @@ -14,42 +15,42 @@ namespace bts.BlueOrb { public interface INonIncGroupByEvaluator { // grouped_rows - IBlueRowCollection evaluate(IDictionary> grouped_rows); - string[] get_group_by_cols(); + IBlueRowCollection? evaluate(IDictionary> grouped_rows); + string[]? get_group_by_cols(); } private class DelNonIncGroupByEvaluator : INonIncGroupByEvaluator { - Func _groupby_cols_func; + Func _groupby_cols_func; Func, List> _evaluate_func; - public DelNonIncGroupByEvaluator(Func groupby_cols_func, - Func, List> evaluate_func) { + public DelNonIncGroupByEvaluator(Func groupby_cols_func, Func, List> evaluate_func) { _groupby_cols_func = groupby_cols_func; _evaluate_func = evaluate_func; } - public IBlueRowCollection evaluate(IDictionary> grouped_rows) { - BlueTable rv = null; + public IBlueRowCollection? evaluate(IDictionary> grouped_rows) { + BlueTable? rv = null; foreach (var kv in grouped_rows) { List g_rows = kv.Value; List out_rows = _evaluate_func.Invoke(g_rows); - if (out_rows != null && out_rows.Count > 0) { - rv = rv ?? new BlueTable(out_rows.ElementAt(0).schema); + if (out_rows is null || out_rows.Count == 0) { + continue; } - if (rv != null) - rv.addRows(out_rows); + rv ??= new BlueTable(out_rows.ElementAt(0).schema); + rv.addRows(out_rows); } return rv; } - public string[] get_group_by_cols() { + public string[]? get_group_by_cols() { return _groupby_cols_func(); } - } - private ViewStream _main_stream; - private INonIncGroupByEvaluator _groupby_func; - private BlueRowHasher _row_hasher; - private string[] _groupby_cols; - private Stopwatch _timer = new(); - private int _n_total_fires = 0, _n_expensive_fires = 0; + readonly ViewStream? _main_stream; + readonly INonIncGroupByEvaluator? _groupby_func; + readonly string[]? _groupby_cols; + readonly Stopwatch _timer = new(); + + BlueRowHasher? _row_hasher; + int _n_total_fires = 0; + int _n_expensive_fires = 0; public GroupByStream(ViewStream main_stream, INonIncGroupByEvaluator groupby_func) { _main_stream = main_stream; @@ -58,9 +59,8 @@ namespace bts.BlueOrb { _row_hasher = new BlueRowHasher(_groupby_cols == null ? 0 : _groupby_cols.Length); } - public GroupByStream(ViewStream main_stream, Func groupby_cols_func, - Func, List> evaluate_func) - : this(main_stream, new DelNonIncGroupByEvaluator(groupby_cols_func, evaluate_func)) { } + public GroupByStream(ViewStream main_stream, Func groupby_cols_func, Func, List> evaluate_func) + : this(main_stream, new DelNonIncGroupByEvaluator(groupby_cols_func, evaluate_func)) { } public GroupByStream(ViewStream parent_str, string groupby_cols, string standard_group_by_text) : this(parent_str, groupby_cols, standard_group_by_text, String.Empty) { } @@ -68,7 +68,7 @@ namespace bts.BlueOrb { // this is the main constructor that does everything public GroupByStream(ViewStream parent_str, string groupby_cols, string standard_group_by_ops, string optional_sort_col) { MultiRowOpHelper std_group_by = MultiRowOpHelper.parse_ops(standard_group_by_ops); - Func groupby_cols_func = () => { + Func groupby_cols_func = () => { if (String.IsNullOrEmpty(groupby_cols)) return null; return groupby_cols.Split(',').Select(x => x.Trim()).ToArray(); @@ -90,12 +90,12 @@ namespace bts.BlueOrb { } - protected override void myOnFirstSubscriber() { _main_stream.onChange += _main_stream_onchange; } + protected override void myOnFirstSubscriber() { Log.notnull(_main_stream).onChange += _main_stream_onchange; } - protected override void myOnLastSubscriber() { _main_stream.onChange -= _main_stream_onchange; } + protected override void myOnLastSubscriber() { Log.notnull(_main_stream).onChange -= _main_stream_onchange; } private void _main_stream_onchange(object nv) { - DataPacket dp = nv as DataPacket; + var dp = ExceptionAssert.Argument.Is(nv); ///grouping together _timer.Restart(); IDictionary> grouped_rows = (_groupby_cols == null || _groupby_cols.Length == 0) ? @@ -109,7 +109,7 @@ namespace bts.BlueOrb { // Log.log(bts.Log.Level.Info, bts.log.Subsystem.gen, String.Format("Time to group {0} rows into {1} groups = {2}", dp.newRows.Count, grouped_rows.Count, _timer.lastIntervalTime)); /// reducing _timer.Restart(); - IBlueRowCollection total_state = _groupby_func.evaluate(grouped_rows); + var total_state = Log.notnull(_groupby_func).evaluate(grouped_rows); if (total_state == null) // mayeb returna n empty table ? return; diff --git a/src/DataStreamSuite/DataStream/BlueOrb/MultiRowRollingMathStream.cs b/src/DataStreamSuite/DataStream/BlueOrb/MultiRowRollingMathStream.cs index 5d12cbe68..28e5abd18 100644 --- a/src/DataStreamSuite/DataStream/BlueOrb/MultiRowRollingMathStream.cs +++ b/src/DataStreamSuite/DataStream/BlueOrb/MultiRowRollingMathStream.cs @@ -1,6 +1,8 @@ namespace bts.BlueOrb { using BCG; + using mbts.utils; + using System.Collections.Generic; using System.Net; @@ -24,9 +26,9 @@ } private void _parent_str_onchange(object dp_obj) { - DataPacket dp = dp_obj as DataPacket; + var dp = ExceptionAssert.Argument.Is(dp_obj); var rows = dp.newRows.to_list(); - BlueTable out_table = null; + BlueTable? out_table = null; for (int i = 0; i < rows.Count; i++) { var out_r = MultiRowOpHelper.group_by_compute(rows, i, _rolling_window_sz, _op); if (out_table == null) diff --git a/src/DataStreamSuite/DataStream/BlueOrb/TimeSeriesStream.cs b/src/DataStreamSuite/DataStream/BlueOrb/TimeSeriesStream.cs index dced3abd5..178fc1260 100644 --- a/src/DataStreamSuite/DataStream/BlueOrb/TimeSeriesStream.cs +++ b/src/DataStreamSuite/DataStream/BlueOrb/TimeSeriesStream.cs @@ -1,16 +1,17 @@ using System; -using System.Data.Entity.Core.Mapping; using BCG; +using mbts.utils; + namespace bts.BlueOrb { // Keeps adding changing rows to the stream, hence preserving the history // and creating a time series, for example this can be used to make bbo updates // into a time series of prices/qty [DataStreamUserRespresentation("TimeSeriesStream")] public class TimeSeriesStream : ViewStream { - private CircularBufferTable _table; - private ViewStream _stream; + readonly ViewStream _stream; + CircularBufferTable? _table; private int _id = 0; private int _size = 0; @@ -31,7 +32,7 @@ namespace bts.BlueOrb { private void _onstream_change(object o) { - DataPacket dp = o as DataPacket; + var dp = ExceptionAssert.Argument.Is(o); DateTime now = DateTime.Now; if (_table == null) { IBlueTableSchema schema = new BlueTableSchema(dp.newRows.schema.columnInfo, dp.newRows.schema.tableName); @@ -48,10 +49,4 @@ namespace bts.BlueOrb { myFireEvent(dp_out); } } - - - } - - - diff --git a/src/DataStreamSuite/DataStream/Primitives/Filter/FilterStream.cs b/src/DataStreamSuite/DataStream/Primitives/Filter/FilterStream.cs index 40a372ff7..916e19b04 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Filter/FilterStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Filter/FilterStream.cs @@ -2,17 +2,22 @@ using bts; using Bts.DataStream; using Bts.DataStream.Primitives.Filter; +using Bts.DataStreamCore; using mbts.config; +using mbts.utils; using System; +using System.Diagnostics.CodeAnalysis; +using System.Linq; using System.Text.RegularExpressions; namespace BCG.StreamWorld.Addition { [DataStreamUserRespresentation(false)] public class FilterStream : ViewStream { private ViewStream myBaseStream; - private BlueTable myBaseTable, myStateTable; + BlueTable? myBaseTable; + BlueTable? myStateTable; private IStreamFilter myFilter; public FilterStream(ViewStream vs) @@ -25,8 +30,11 @@ namespace BCG.StreamWorld.Addition { myBaseStream = vs; myFilter = filter; vs.ErrorChanged += OnError; - if (filter is IDynamicStreamFilter) - (filter as IDynamicStreamFilter).onFilterChange += new FilterChangeDelegate(myHandleFilterChange); + if (filter is IDynamicStreamFilter dsf) { + dsf.onFilterChange += OnFilterChange; + void OnFilterChange() => + filter = myFilter; + } } @@ -36,8 +44,8 @@ namespace BCG.StreamWorld.Addition { protected override void myOnLastSubscriber() { myBaseStream.onChange -= myHandleChanges; - myBaseTable.clear(); - myStateTable.clear(); + myBaseTable?.clear(); + myStateTable?.clear(); } bool filterRow(IBlueRow r) { @@ -52,17 +60,13 @@ namespace BCG.StreamWorld.Addition { private void myHandleChanges(object newValue) { try { - DataPacket dp = newValue as DataPacket; - if (myStateTable == null) { - bts.Log.assert(myBaseTable == null); - myStateTable = new BlueTable(dp.schema); - myBaseTable = new BlueTable(dp.schema); - } - BlueTable dels = new BlueTable(dp.schema), - adds = new BlueTable(dp.schema); + var dp = ExceptionAssert.Argument.Is(newValue); + ensureInitialized(dp.Schema); + var dels = new BlueTable(dp.Schema); + var adds = new BlueTable(dp.Schema); foreach (IBlueRow r in dp.deletedRows) { myBaseTable.deleteRow(r); - if (myStateTable.containsID((int)r["id"])) { + if (myStateTable.containsID(r.Id())) { dels.addRow(r); myStateTable.deleteRow(r); } @@ -84,13 +88,13 @@ namespace BCG.StreamWorld.Addition { public IStreamFilter filter { set { myFilter = value; - if (myBaseTable != null) { + if (isInitialized()) { IBlueRowCollection oldState = new BlueTable(myStateTable); myStateTable.clear(); // If the filter throws an exception, we can rethrow it as a StreamFilterException // and include the 'currentRow' (for which the filter failed) to provide context. IBlueRow? currentRow = null; - foreach (IBlueRow r in myBaseTable) { + foreach (var r in myBaseTable.Rows) { try { currentRow = r; if (filterRow(r)) { @@ -110,8 +114,19 @@ namespace BCG.StreamWorld.Addition { } //public string filterString { set { filter = new BooleanExpressionFilter(value); } } + [MemberNotNullWhen(true, nameof(myBaseTable), nameof(myStateTable))] + bool isInitialized() => + myStateTable is not null && myBaseTable is not null; - private void myHandleFilterChange() { filter = myFilter; } + [MemberNotNull(nameof(myBaseTable), nameof(myStateTable))] + void ensureInitialized(IBlueTableSchema schema) { + if (myBaseTable is not null) { + ArgumentNullException.ThrowIfNull(myStateTable); + return; + } + myBaseTable = new BlueTable(schema); + myStateTable = new BlueTable(schema); + } } } @@ -140,7 +155,7 @@ namespace BCG bool rv = true; for (int i = 0; i < myCols.Length; i++) { - rv &= myRegExps[i].IsMatch(dr[myCols[i]] is DBNull ? "" : dr[myCols[i]].ToString()); + rv &= myRegExps[i].IsMatch(dr[myCols[i]]?.ToString() ?? string.Empty); if (!rv) return rv; } @@ -151,13 +166,13 @@ namespace BCG public class FilterStream : ViewStream { - private BlueTable myLastFiredRows; - private IBlueRowCollection myLastReceivedRows; - private IStreamFilter myFilter; - private ViewStream myParentStream; - private string myFilterString; - private BCG.StreamWorld.Addition.FilterStream myFilterStream; - private bool myUseNewFilter = Configuration.get().UseNewFilter; + readonly ViewStream? myParentStream; + readonly string? myFilterString; + readonly StreamWorld.Addition.FilterStream? myFilterStream; + readonly bool myUseNewFilter = Configuration.get().UseNewFilter; + BlueTable? myLastFiredRows; + IBlueRowCollection? myLastReceivedRows; + IStreamFilter? myFilter; #region constructors public delegate bool OnFilter(IBlueRow r); @@ -188,8 +203,11 @@ namespace BCG myParentStream = vs; myFilter = filter; myFilterString = filter.ToString(); - if (filter is IDynamicStreamFilter) - (filter as IDynamicStreamFilter).onFilterChange += new FilterChangeDelegate(myHandleFilterChange); + if (filter is IDynamicStreamFilter dsf) { + dsf.onFilterChange += OnFilterChange; + void OnFilterChange() => + filter = myFilter; + } } } @@ -213,18 +231,21 @@ namespace BCG #endregion #region overridden methods - protected override void myOnFirstSubscriber() => + protected override void myOnFirstSubscriber() { + requireUseOldFilter(); myParentStream.onChange += myHandleCallbacks; + } protected override void myOnLastSubscriber() { + requireUseOldFilter(); myLastFiredRows = null; myParentStream.onChange -= myHandleCallbacks; } protected override void myFireEvent(object o) { - DataPacket dp = o as DataPacket; + var dp = ExceptionAssert.Argument.Is(o); if (myLastValue == null || dp.addedRows.Count + dp.deletedRows.Count > 0) base.myFireEvent(o); } @@ -233,33 +254,30 @@ namespace BCG private void myHandleCallbacks(object newValue) { DataPacket retVal; - DataPacket arrivedPacket = newValue as DataPacket; + var arrivedPacket = ExceptionAssert.Argument.Is(newValue); myLastReceivedRows = arrivedPacket.newRows; try { if (myLastFiredRows != null) { - retVal = new DataPacket(myLastFiredRows, new BlueTable(myLastFiredRows.schema), - new BlueTable(myLastFiredRows.schema)); - foreach (IBlueRow r in arrivedPacket.deletedRows) - if (myLastFiredRows.containsID((int)r["id"])) - { - myLastFiredRows.deleteRow(r); - (retVal.deletedRows as BlueTable).addRow(r); - } - foreach (IBlueRow r in arrivedPacket.addedRows) - if (filterRow(r)) - { - myLastFiredRows.addRow(r); - (retVal.addedRows as BlueTable).addRow(r); - } + var deletedRows = new BlueTable(myLastFiredRows.schema); + foreach (var r in arrivedPacket.deletedRows.Cast().Where(r => myLastFiredRows.containsID(r.Id()))) { + myLastFiredRows.deleteRow(r); + deletedRows.addRow(r); + } + var addedRows = new BlueTable(myLastFiredRows.schema); + foreach (var r in arrivedPacket.addedRows.Cast().Where(filterRow)) { + myLastFiredRows.addRow(r); + addedRows.addRow(r); + } + retVal = new DataPacket(myLastFiredRows, addedRows, deletedRows); } else { - myLastFiredRows = new BlueTable(arrivedPacket.schema); + myLastFiredRows = new BlueTable(arrivedPacket.Schema); retVal = new DataPacket(myLastFiredRows, myLastFiredRows, new BlueTable(myLastFiredRows.schema)); - foreach (IBlueRow r in arrivedPacket.newRows) - if (filterRow(r)) - myLastFiredRows.addRow(r); + foreach (var r in arrivedPacket.newRows.Cast().Where(filterRow)) { + myLastFiredRows.addRow(r); + } } myFireEvent(retVal); } catch (Exception ex) { @@ -271,7 +289,7 @@ namespace BCG { set { - if (myUseNewFilter) + if (checkUseNewFilter()) { myFilterStream.filter = filter; } @@ -301,7 +319,7 @@ namespace BCG } get { - if (myUseNewFilter) + if (checkUseNewFilter()) return myFilterStream.filter; else return myFilter; @@ -315,12 +333,8 @@ namespace BCG return GetType().ToString() + " " + myFilterString; } - private void myHandleFilterChange() - { - filter = myFilter; - } - bool filterRow(IBlueRow r) { + requireUseOldFilter(); try { return myFilter.filter(r); } catch (Exception ex) { @@ -329,5 +343,26 @@ namespace BCG } return false; } + + [MemberNotNullWhen(true, nameof(myFilterStream))] + [MemberNotNullWhen(false, nameof(myFilter))] + [MemberNotNullWhen(false, nameof(myParentStream))] + bool checkUseNewFilter() { + if (myUseNewFilter) { + Log.notnull(myFilterStream); + return true; + } + Log.notnull(myFilter); + Log.notnull(myParentStream); + return false; + } + + [MemberNotNull(nameof(myFilter))] + [MemberNotNull(nameof(myParentStream))] + void requireUseOldFilter() { + if (checkUseNewFilter()) { + throw new InvalidOperationException("Should be using old FilterStream"); + } + } } } diff --git a/src/DataStreamSuite/DataStream/Primitives/GroupBy/GroupByOutputTable.cs b/src/DataStreamSuite/DataStream/Primitives/GroupBy/GroupByOutputTable.cs index cd747823e..103c5c074 100644 --- a/src/DataStreamSuite/DataStream/Primitives/GroupBy/GroupByOutputTable.cs +++ b/src/DataStreamSuite/DataStream/Primitives/GroupBy/GroupByOutputTable.cs @@ -1,6 +1,8 @@ +using Bts.DataStreamCore; + using System; -using BCG.Utilities; using System.Collections; +using System.Collections.Generic; namespace BCG { @@ -9,7 +11,7 @@ namespace BCG /// public class GroupByOutputTable : IBlueRowCollection { - private IDictionary myDict = new Hashtable (); + readonly Dictionary myDict = new(); private BlueTable myTable; public GroupByOutputTable (IBlueTableSchema schema) {myTable = new BlueTable (schema);} @@ -22,10 +24,11 @@ namespace BCG /// public void addRow (IBlueRow row, string groupRepString) { - if (myDict[groupRepString] != null) - myTable.deleteRow ((IBlueRow)myDict[groupRepString]); + if (myDict.TryGetValue(groupRepString, out var r)) { + myTable.deleteRow(r); + } myTable.addRow (row); - myDict[groupRepString] = myTable[(int)row["id"]]; + myDict[groupRepString] = myTable[row.Id()]; } /// @@ -48,11 +51,8 @@ namespace BCG myDict.Clear (); } - public IBlueRow getRow (string groupRepString) - { - return myDict[groupRepString] == null ? null : - myDict[groupRepString] as IBlueRow; - } + public IBlueRow? getRow(string groupRepString) => + myDict.GetValueOrDefault(groupRepString); #region IBlueRowCollection members public IBlueRow this[int id] {get{return myTable[id];}} diff --git a/src/DataStreamSuite/DataStream/Primitives/GroupBy/RowDictionaryTable.cs b/src/DataStreamSuite/DataStream/Primitives/GroupBy/RowDictionaryTable.cs index 286d25c21..0f9ff8c8a 100644 --- a/src/DataStreamSuite/DataStream/Primitives/GroupBy/RowDictionaryTable.cs +++ b/src/DataStreamSuite/DataStream/Primitives/GroupBy/RowDictionaryTable.cs @@ -1,6 +1,6 @@ using System; -using BCG.Utilities; using System.Collections; +using System.Collections.Generic; namespace BCG { @@ -11,12 +11,10 @@ namespace BCG /// public class RowDictionaryTable : IBlueRowCollection { - protected IDictionary myRowCollections = new Hashtable (); + protected Dictionary myRowCollections = new(); protected BlueTable myLinearCollection; protected string[] myCols; protected IBlueTableSchema mySchema; - - protected RowDictionaryTable () {} public RowDictionaryTable (IBlueRowCollection rows, params string[] cols) : this (rows.schema, cols) @@ -40,7 +38,7 @@ namespace BCG public virtual void addRow (IBlueRow row) { - BlueTable rows = (BlueTable)getGroup (row); + var rows = getGroup(row); if (rows == null) { //i.e we dont have any group for this row => we need to create a new group @@ -53,8 +51,7 @@ namespace BCG public virtual void removeRow (IBlueRow row) { - BlueTable rowGroup = (BlueTable)getGroup (row); -// BlueEventLog.logAssert (rowGroup != null && rowGroup.Count > 0); + var rowGroup = getGroup(row) ?? throw new ArgumentException("Row group not found", nameof(row)); rowGroup.deleteRow (row); myLinearCollection.deleteRow (row); if (rowGroup.Count == 0) @@ -65,7 +62,8 @@ namespace BCG public void removeGroup (IBlueRow representativeRow) { - IBlueRowCollection rowGroup = getGroup (representativeRow); + var rowGroup = getGroup(representativeRow) + ?? throw new ArgumentException("Row group not found"); // BlueEventLog.logAssert (rowGroup != null && rowGroup.Count > 0); foreach (IBlueRow row in rowGroup) myLinearCollection.deleteRow (row); @@ -85,24 +83,13 @@ namespace BCG /// //public IBlueRowCollection getGroup (IBlueRow row) {return getGroup ( - public IBlueRowCollection getGroup (IBlueRow r) {return getGroup (myRowToString (r));} + public BlueTable? getGroup (IBlueRow r) => + getGroup(myRowToString(r)); public ICollection getGroups () {return myRowCollections.Values;} - private bool myAreRowsEqual (IBlueRow r1, IBlueRow r2) - { - bool retVal = true; - foreach (string col in myCols) - retVal &= r1[col].Equals(r2[col]); - return retVal; - } - - public IBlueRowCollection getGroup (string repString) - { - IBlueRowCollection retVal = myRowCollections[repString] != null ? - (IBlueRowCollection) myRowCollections[repString] : null; - return retVal; - } + public BlueTable? getGroup(string repString) => + myRowCollections.GetValueOrDefault(repString); private string myRowToString (IBlueRow r) {return BlueDataUtility.rowToString (r, myCols);} diff --git a/src/DataStreamSuite/DataStream/Primitives/Join/JoinForeignTable.cs b/src/DataStreamSuite/DataStream/Primitives/Join/JoinForeignTable.cs index 9bf32b527..23a5b80d2 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/JoinForeignTable.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/JoinForeignTable.cs @@ -1,19 +1,17 @@ -using System; -using BCG.Utilities; - using bts; +using Bts.DataStreamCore; + namespace BCG { internal class JoinForeignTable : JoinTable { public JoinForeignTable (JoinTableSchema schema) : base (schema) {} - public override void addRow (IBlueRow leftRow, IBlueRow rightRow, DataPacket dp) - { + 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; + 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/JoinRow.cs b/src/DataStreamSuite/DataStream/Primitives/Join/JoinRow.cs index bd06127a4..7ff301f38 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Join/JoinRow.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Join/JoinRow.cs @@ -23,7 +23,7 @@ namespace BCG /// left row of the join /// right row of the join /// schema of the joined row - public JoinRow(IBlueRow leftRow, IBlueRow rightRow, JoinTableSchema schema) + public JoinRow(IBlueRow leftRow, IBlueRow? rightRow, JoinTableSchema schema) : this(leftRow.Id(), leftRow, rightRow, schema) { } /// diff --git a/src/DataStreamSuite/DataStream/Primitives/Misc/PubsubEchoTestStream.cs b/src/DataStreamSuite/DataStream/Primitives/Misc/PubsubEchoTestStream.cs index dc1c3f1a0..e05ddbbd9 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Misc/PubsubEchoTestStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Misc/PubsubEchoTestStream.cs @@ -6,8 +6,6 @@ using System.Collections.Generic; using System; using System.IO; using System.Linq; -using System.Runtime.CompilerServices; -using bts.api; using Bts.PubSub.Routing; namespace bts @@ -17,9 +15,9 @@ namespace bts { public class PayloadMessage { - public readonly string msg_id; + public readonly string? msg_id; public readonly Int64 timestamp = 0; - public readonly byte[] payload = null; + public readonly byte[]? payload = null; public static byte[] create_data(int payload_size, byte payload_byte) { @@ -53,10 +51,10 @@ namespace bts { var str = new MemoryStream(); BinaryWriter bw = new BinaryWriter(str); - bw.Write(msg_id); + bw.Write(msg_id ?? string.Empty); bw.Write(timestamp); - bw.Write(payload.Length); - bw.Write(payload); + bw.Write(payload?.Length ?? 0); + bw.Write(payload ?? Array.Empty()); str.Position = 0; var bytes = str.ToArray(); return bytes; @@ -74,17 +72,16 @@ namespace bts } } //public void - private readonly int _msg_size; private readonly TimeSpan _publish_period; private DateTime _last_publish_time = DateTime.MinValue; - private PublisherCore _pub_core; - private ISubscriberCore _sub_core; + PublisherCore? _pub_core; + ISubscriberCore? _sub_core; private BlueTable _table; - private int _n_pubs = 0; private readonly int _max_pubs; private int _n_updates = 0; private ViewStreamDatabase _vsd; - private PayloadMessage _last_sent_msg, _last_recvd_msg; + PayloadMessage? _last_sent_msg; + PayloadMessage? _last_recvd_msg; private const byte _payload_byte = (byte)'A'; private readonly string _tag_name; private readonly int _payload_sz; @@ -131,13 +128,13 @@ namespace bts protected override void myOnLastSubscriber() { _vsd.removeWorker(this); - _sub_core.unsubscribe(_tag_name); + _sub_core?.unsubscribe(_tag_name); } private void _fire_dp_after_send() { // as soon as the data is published, its reflected in the stream state - Log.assert(_last_sent_msg != null); + Log.notnull(_last_sent_msg); BlueRow r = new BlueRow(_table.schema, _table.Count, _last_sent_msg.msg_id, _last_sent_msg.timestamp, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value); _table.addRow(r); @@ -147,7 +144,8 @@ namespace bts private void _fire_dp_after_recv() { - Log.assert(_last_recvd_msg != null); + Log.notnull(_last_recvd_msg); + Log.notnull(_last_sent_msg); // update the last publish row int id = _table.Count - 1; IBlueRow last_row = _table[id]; @@ -167,28 +165,28 @@ namespace bts private void _publish_next_packet() { + Log.notnull(_pub_core); var bytes = PayloadMessage.create_data(_payload_sz, _payload_byte); _last_sent_msg = new PayloadMessage(_get_next_id(), epoch_now(), bytes); var ser_bytes = _last_sent_msg.serialize(); - _n_pubs++; _pub_core.publish("Blob", _tag_name, ser_bytes); _last_publish_time = DateTime.Now; } public void doWork() { - Func, PayloadMessage> find_packet = (updates) => - { - foreach (var u in updates) - { - var recv_msg = PayloadMessage.deserialize(u.data); - if (recv_msg.msg_id == _last_sent_msg.msg_id) - { + PayloadMessage? find_packet(IEnumerable updates) { + if (_last_sent_msg is null) { + return null; + } + foreach (var u in updates) { + var recv_msg = PayloadMessage.deserialize(u.data ?? Array.Empty()); + if (recv_msg.msg_id == _last_sent_msg.msg_id) { return recv_msg; } } return null; - }; + } switch (_state) { case IOState.SEND: @@ -202,7 +200,7 @@ namespace bts } break; case IOState.RECV: - var updates = _sub_core.get_updates(TimeSpan.Zero); + var updates = Log.notnull(_sub_core).get_updates(TimeSpan.Zero); updates = updates.Where(u => u.tag == _tag_name); var recv_msg = find_packet(updates); if (recv_msg != null) diff --git a/src/DataStreamSuite/DataStream/Primitives/Project/ChangeTableNameStream.cs b/src/DataStreamSuite/DataStream/Primitives/Project/ChangeTableNameStream.cs index ef684ceec..872a341e7 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Project/ChangeTableNameStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Project/ChangeTableNameStream.cs @@ -19,15 +19,15 @@ namespace BCG public IBlueTableSchema schema { get { return mySchema; } } - public object this[int index] { get { return myRow[index]; } } + public object? this[int index] => myRow[index]; - public object this[string colName] { get { return myRow[colName]; } } + public object? this[string colName] => myRow[colName]; #endregion #region IDictionary Members - public void Add(object key, object value) + public void Add(object key, object? value) { throw new Exception("The method or operation is not implemented."); } @@ -54,10 +54,9 @@ namespace BCG public ICollection Values { get { return myRow.Values; } } - public object this[object key] - { - get { return myRow[key]; } - set { throw new Exception("The method or operation is not implemented."); } + public object? this[object key] { + get => myRow[key]; + set => throw new NotImplementedException(); } #endregion @@ -82,7 +81,7 @@ namespace BCG } private string myNewName; - private IBlueTableSchema mySchema; + private IBlueTableSchema? mySchema; public ChangeTableNameStream (ViewStream vs, string name) { diff --git a/src/DataStreamSuite/DataStream/Primitives/Sort/SortedTable.cs b/src/DataStreamSuite/DataStream/Primitives/Sort/SortedTable.cs index 77dfecd42..8be32d9c4 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Sort/SortedTable.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Sort/SortedTable.cs @@ -1,108 +1,109 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; - -using Bts.ColumnSortExpressions; - -namespace BCG; - -/// -/// Represents an IBlueRowCollection with the rows sorted according to the -/// -public class SortedTable : IBlueRowCollection { - readonly List rows = new(0); - readonly Dictionary rowIndex = new(0); - readonly IList sortCols = Array.Empty(); - readonly IComparer? comparer; - - /// - /// Sorts the rows using sortExpression as the sorting criteria - /// - /// Rows to be inserted into the table - /// Sorting criteria - public SortedTable(IBlueRowCollection rows, IList sortCols) { - this.sortCols = sortCols; - init(rows); - } - - public SortedTable(IBlueRowCollection rows, IComparer comparer) { - this.comparer = comparer; - init(rows); - } - - public int Count => rows.Count; - public bool IsSynchronized => false; - public IBlueTableSchema schema { get; private set; } - public object SyncRoot => this; - public IBlueRow this[int id] => rowIndex[id] - ?? throw new BlueDataException( - $"ID '{id}' does not exist in table '{schema?.tableName}'"); - - public void addRows(IBlueRowCollection rows) { - // Requires merger of the addedRows to the already existing ones - if (rows.Count == 0) { - // At times there are calls with 0 additions, in that case - // we of course need no processing - return; - } - SortedTable addedRows = - comparer == null ? new SortedTable(rows, sortCols) : - new SortedTable(rows, comparer); - int addedRowsIndex = 0; - SortRow newRow = addedRows.getRowByIndex(addedRowsIndex); - for (int i = 0; i < this.rows.Count; i++) { - if (newRow.CompareTo(this.rows[i]) <= 0) { - this.rows.Insert(i, newRow); - rowIndex[(int)newRow["id"]] = newRow; - if (++addedRowsIndex == addedRows.Count) { - return; - } - newRow = addedRows.getRowByIndex(addedRowsIndex); - } - } - if (addedRowsIndex < addedRows.Count) { - for (int i = addedRowsIndex; i < addedRows.Count; i++) { - newRow = addedRows.getRowByIndex(i); - this.rows.Add(newRow); - rowIndex[(int)newRow["id"]] = newRow; - } - } - } - - public void deleteRows(IBlueRowCollection deletedRows) { - foreach (IBlueRow deletedRow in deletedRows) { - var row = rowIndex[(int)deletedRow["id"]!] as SortRow - ?? throw new InvalidOperationException("Index did not return IBlueRow"); - rows.Remove(row); - rowIndex.Remove((int)row["id"]!); - } - } - - public SortRow getRowByIndex(int index) => - rows[index]!; - - public bool containsID(int id) => - rowIndex[id] is not null; - - public IEnumerator GetEnumerator() => - rows.GetEnumerator(); - - public void CopyTo(Array array, int index) { - for (int i = 0; i < Count; i++) { - array.SetValue(rows[i], index + i); - } - } - - [MemberNotNull(nameof(schema))] - void init(IBlueRowCollection rows) { - schema = rows.schema; - foreach (IBlueRow row in rows) { - SortRow newRow = comparer is null ? - new(row, sortCols) : new(row, comparer); - this.rows.Add(newRow); - rowIndex[(int)row["id"]!] = newRow; - } - this.rows.Sort(); - } -} +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; + +using Bts.ColumnSortExpressions; +using Bts.DataStreamCore; + +namespace BCG; + +/// +/// Represents an IBlueRowCollection with the rows sorted according to the +/// +public class SortedTable : IBlueRowCollection { + readonly List rows = new(0); + readonly Dictionary rowIndex = new(0); + readonly IList sortCols = Array.Empty(); + readonly IComparer? comparer; + + /// + /// Sorts the rows using sortExpression as the sorting criteria + /// + /// Rows to be inserted into the table + /// Sorting criteria + public SortedTable(IBlueRowCollection rows, IList sortCols) { + this.sortCols = sortCols; + init(rows); + } + + public SortedTable(IBlueRowCollection rows, IComparer comparer) { + this.comparer = comparer; + init(rows); + } + + public int Count => rows.Count; + public bool IsSynchronized => false; + public IBlueTableSchema schema { get; private set; } + public object SyncRoot => this; + public IBlueRow this[int id] => rowIndex[id] + ?? throw new BlueDataException( + $"ID '{id}' does not exist in table '{schema?.tableName}'"); + + public void addRows(IBlueRowCollection rows) { + // Requires merger of the addedRows to the already existing ones + if (rows.Count == 0) { + // At times there are calls with 0 additions, in that case + // we of course need no processing + return; + } + SortedTable addedRows = + comparer == null ? new SortedTable(rows, sortCols) : + new SortedTable(rows, comparer); + int addedRowsIndex = 0; + SortRow newRow = addedRows.getRowByIndex(addedRowsIndex); + for (int i = 0; i < this.rows.Count; i++) { + if (newRow.CompareTo(this.rows[i]) <= 0) { + this.rows.Insert(i, newRow); + rowIndex[newRow.Id()] = newRow; + if (++addedRowsIndex == addedRows.Count) { + return; + } + newRow = addedRows.getRowByIndex(addedRowsIndex); + } + } + if (addedRowsIndex < addedRows.Count) { + for (int i = addedRowsIndex; i < addedRows.Count; i++) { + newRow = addedRows.getRowByIndex(i); + this.rows.Add(newRow); + rowIndex[newRow.Id()] = newRow; + } + } + } + + public void deleteRows(IBlueRowCollection deletedRows) { + foreach (IBlueRow deletedRow in deletedRows) { + var row = rowIndex[(int)deletedRow["id"]!] as SortRow + ?? throw new InvalidOperationException("Index did not return IBlueRow"); + rows.Remove(row); + rowIndex.Remove((int)row["id"]!); + } + } + + public SortRow getRowByIndex(int index) => + rows[index]!; + + public bool containsID(int id) => + rowIndex[id] is not null; + + public IEnumerator GetEnumerator() => + rows.GetEnumerator(); + + public void CopyTo(Array array, int index) { + for (int i = 0; i < Count; i++) { + array.SetValue(rows[i], index + i); + } + } + + [MemberNotNull(nameof(schema))] + void init(IBlueRowCollection rows) { + schema = rows.schema; + foreach (IBlueRow row in rows) { + SortRow newRow = comparer is null ? + new(row, sortCols) : new(row, comparer); + this.rows.Add(newRow); + rowIndex[(int)row["id"]!] = newRow; + } + this.rows.Sort(); + } +} diff --git a/src/DataStreamSuite/DataStream/Primitives/Union/MutableAppendStream.cs b/src/DataStreamSuite/DataStream/Primitives/Union/MutableAppendStream.cs index d4f3eb1f1..baa604a1a 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Union/MutableAppendStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Union/MutableAppendStream.cs @@ -27,7 +27,7 @@ namespace BCG { public IBlueTableSchema schema { get { return myRow.schema; } } - public object this[int index] { + public object? this[int index] { get { if (myRow.schema[index].colName == "id") return myID; @@ -36,13 +36,15 @@ namespace BCG { } } - public object this[string colName] { get { return colName == "id" ? (object)myID : myRow[colName]; } } + public object? this[string colName] => + colName == "id" ? myID : myRow[colName]; #endregion #region IDictionary Members - public void Add(object key, object value) { throw new Exception("Row is read only."); } + public void Add(object key, object? value) => + throw new Exception("Row is read only."); public void Clear() { throw new Exception("Row is read only."); } @@ -66,9 +68,9 @@ namespace BCG { } } - public object this[object key] { - get { return key is string ? this[key as string] : this[(int)key]; } - set { throw new Exception("The method or operation is not implemented."); } + public object? this[object key] { + get => key is string keyStr ? this[keyStr] : this[(int)key]; + set => throw new Exception("The method or operation is not implemented."); } #endregion diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs index beeaf04a5..f476bb1c5 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs @@ -402,13 +402,13 @@ namespace BCG { } } - public static DataPacket build_dp(IBlueRowCollection new_rows, DataPacket last_fired_dp) { + public static DataPacket build_dp(IBlueRowCollection new_rows, IDataPacket last_fired_dp) { IBlueRowCollection added_rows = new_rows; IBlueRowCollection deleted_rows = last_fired_dp == null ? new BlueTable(new_rows.schema) : last_fired_dp.newRows; return new DataPacket(new_rows, added_rows, deleted_rows); } - public static DataPacket build_dp_inc(IBlueRowCollection new_rows, DataPacket last_fired_dp, bool deep_row_comapre = false) { + public static DataPacket build_dp_inc(IBlueRowCollection new_rows, IDataPacket last_fired_dp, bool deep_row_comapre = false) { if (last_fired_dp == null) { IBlueRowCollection added_rows = new_rows; IBlueRowCollection deleted_rows = new BlueTable(new_rows.schema); diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowDictionaryEnumerator.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowDictionaryEnumerator.cs index 03ed1d973..b75cb1b8b 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowDictionaryEnumerator.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowDictionaryEnumerator.cs @@ -13,11 +13,9 @@ namespace BCG public override object Current {get{return Entry;}} #region IDictionaryEnumerator members - public object Key {get{return myRow.schema[myPos].colName;}} - - public object Value {get{return myRow[myPos];}} - - public DictionaryEntry Entry {get{return new DictionaryEntry (Key, Value);}} + public override object Key => myRow.schema[myPos].colName; + public override object Value => myRow[myPos]; + public override DictionaryEntry Entry => new(Key, Value); #endregion } } diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowEnumerator.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowEnumerator.cs index 16394e500..5ab304d77 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowEnumerator.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowEnumerator.cs @@ -19,10 +19,10 @@ namespace BCG myRowSize = row.schema.columnNames.Length; } - public DictionaryEntry Entry => + public virtual DictionaryEntry Entry => new(myRow.schema.columnInfo[myPos].colName, myRow[myPos]); - public object Key => myRow.schema.columnInfo[myPos].colName; - public object Value => myRow[myPos]; + public virtual object Key => myRow.schema.columnInfo[myPos].colName; + public virtual object Value => myRow[myPos]; #region IEnumerator members public void Reset() {myPos = 0;} diff --git a/src/DataStreamSuite/DataStreamCore/DataPacket.cs b/src/DataStreamSuite/DataStreamCore/DataPacket.cs index 9c66a8394..171d4be66 100644 --- a/src/DataStreamSuite/DataStreamCore/DataPacket.cs +++ b/src/DataStreamSuite/DataStreamCore/DataPacket.cs @@ -43,7 +43,7 @@ namespace BCG { } public IBlueRowCollection newRows, addedRows, deletedRows; - public IDictionary meta_data = null; + public Dictionary meta_data = null; public DataPacket() { } public DataPacket(IBlueRowCollection newRows, IBlueRowCollection addedRows, IBlueRowCollection deletedRows) { this.newRows = newRows; @@ -54,6 +54,7 @@ namespace BCG { IBlueRowCollection IDataPacket.newRows => newRows; IBlueRowCollection IDataPacket.addedRows => addedRows; IBlueRowCollection IDataPacket.deletedRows => deletedRows; + IReadOnlyDictionary IDataPacket.Metadata => meta_data; public void get_new_added_deleted(out ICollection new_rows, out ICollection added_rows, out ICollection deleted_rows) { new_rows = newRows; @@ -287,15 +288,22 @@ namespace BCG { } public class DataPacket : IDataPacket where T : IBlueRow { - public DataPacket(IBlueRowCollection newRows, IBlueRowCollection addedRows, IBlueRowCollection deletedRows) { + public DataPacket( + IBlueRowCollection newRows, + IBlueRowCollection addedRows, + IBlueRowCollection deletedRows, + IReadOnlyDictionary metadata = null + ) { this.newRows = newRows; this.addedRows = addedRows; this.deletedRows = deletedRows; + Metadata = metadata ?? DictionaryHelper.empty(); } public IBlueRowCollection newRows { get; } public IBlueRowCollection addedRows { get; } public IBlueRowCollection deletedRows { get; } + public IReadOnlyDictionary Metadata { get; } public static DataPacket Empty(IBlueTableSchema schema) => new( diff --git a/src/DataStreamSuite/DataStreamCore/IDataPacket.cs b/src/DataStreamSuite/DataStreamCore/IDataPacket.cs index a61f31b66..900b334b8 100644 --- a/src/DataStreamSuite/DataStreamCore/IDataPacket.cs +++ b/src/DataStreamSuite/DataStreamCore/IDataPacket.cs @@ -13,6 +13,7 @@ namespace BCG { IBlueRowCollection newRows { get; } IBlueRowCollection addedRows { get; } IBlueRowCollection deletedRows { get; } + IReadOnlyDictionary Metadata { get; } //this is an ICollection of IDictionary void get_new_added_deleted(out ICollection new_rows, out ICollection added_rows, out ICollection deleted_rows); } @@ -41,6 +42,12 @@ namespace BCG { dataPacket.addedRows, dataPacket.deletedRows); + public static IDataPacket ClearMetadata(this IDataPacket dataPacket) => + new MetadataWrapper(dataPacket, DictionaryHelper.empty()); + + public static IDataPacket ResetMetadata(this IDataPacket dataPacket, IReadOnlyDictionary metadata) => + new MetadataWrapper(dataPacket, metadata); + public static string GetIncrementalIdString(this IDataPacket dataPacket) { var incIdSerializer = new DataPacketIncrementalIdsJsonSerializer(); return incIdSerializer.Serialize(dataPacket); @@ -63,5 +70,24 @@ namespace BCG { // } // } } + + class MetadataWrapper : IDataPacket { + readonly IDataPacket source; + + public MetadataWrapper(IDataPacket source, IReadOnlyDictionary metadata) { + this.source = source; + Metadata = metadata; + } + + public IBlueTableSchema Schema => source.Schema; + public IBlueRowCollection newRows => source.newRows; + public IBlueRowCollection addedRows => source.addedRows; + public IBlueRowCollection deletedRows => source.deletedRows; + public IReadOnlyDictionary Metadata { get; } + + public void get_new_added_deleted(out ICollection new_rows, out ICollection added_rows, out ICollection deleted_rows) { + source.get_new_added_deleted(out new_rows, out added_rows, out deleted_rows); + } + } } } diff --git a/src/DataStreamSuite/DataStreamQueryAnalyzer/PersistWindowState.cs b/src/DataStreamSuite/DataStreamQueryAnalyzer/PersistWindowState.cs index 1d8e71324..4676d82ab 100644 --- a/src/DataStreamSuite/DataStreamQueryAnalyzer/PersistWindowState.cs +++ b/src/DataStreamSuite/DataStreamQueryAnalyzer/PersistWindowState.cs @@ -1,166 +1,175 @@ -using System; -using System.Reflection; -using System.Windows.Forms; -using System.Drawing; -using System.Diagnostics; -using Microsoft.Win32; - -namespace BCG -{ - public class PersistWindowState - { - // event info that allows form to persist extra window state data - public delegate void WindowStateHandler (object sender, BlueRegistry bluereg); - public event WindowStateHandler LoadState; - public event WindowStateHandler SaveState; - - private Form myParent; - private BlueRegistry myRegistry; - - private int myNormalLeft; - private int myNormalTop; - private int myNormalWidth; - private int myNormalHeight; - private bool myResizable = true; - private FormWindowState myWindowState; - private bool myAllowSaveMinimized = false; - private bool mySavePosition = true; - private bool mySaveState = true; - - public PersistWindowState (bool savePosition) - { - mySavePosition = savePosition; - } - - public PersistWindowState () - { - } - - public bool saveState - { - [DebuggerStepThrough] - set { mySaveState = value; } - } - - public Form parent - { - [DebuggerStepThrough] - get { return myParent; } - set - { - myParent = value; - myRegistry = new BlueRegistry (myParent.Text); - - // subscribe to parent form's events - myParent.Closing += new System.ComponentModel.CancelEventHandler(myOnClosing); - myParent.Resize += new System.EventHandler(myOnResize); - myParent.Move += new System.EventHandler(myOnMove); - myParent.Load += new System.EventHandler(myOnLoad); - - // get initial width and height in case form is never resized - myNormalWidth = myParent.Width; - myNormalHeight = myParent.Height; - } - } - - public BlueRegistry blueRegistry - { - [DebuggerStepThrough] - get { return myRegistry; } - [DebuggerStepThrough] - set { myRegistry = value; } - } - - public bool resizable - { - [DebuggerStepThrough] - set { myResizable = value; } - } - - public bool allowSaveMinimized - { - [DebuggerStepThrough] - set { myAllowSaveMinimized = value; } - } - - private void myOnResize(object sender, System.EventArgs e) - { - // save width and height - if (myParent.WindowState == FormWindowState.Normal && myResizable) - { - myNormalWidth = myParent.Width; - myNormalHeight = myParent.Height; - } - } - - private void myOnMove(object sender, System.EventArgs e) - { - // save position - if(myParent.WindowState == FormWindowState.Normal) - { - myNormalLeft = myParent.Left; - myNormalTop = myParent.Top; - } - // save state - myWindowState = myParent.WindowState; - } - - private void myOnClosing(object sender, System.ComponentModel.CancelEventArgs e) - { - // save position, size and state - if (mySavePosition) - { - myRegistry.setValue (RegistryRoot.HKCU, "Left", myNormalLeft); - myRegistry.setValue (RegistryRoot.HKCU, "Top", myNormalTop); - } - - if (myResizable) - { - myRegistry.setValue (RegistryRoot.HKCU, "Width", myNormalWidth); - myRegistry.setValue (RegistryRoot.HKCU, "Height", myNormalHeight); - } - - // check if we are allowed to save the state as minimized (not normally) - if (!myAllowSaveMinimized) - { - if(myWindowState == FormWindowState.Minimized) - myWindowState = FormWindowState.Normal; - } - - if (mySaveState) - myRegistry.setValue (RegistryRoot.HKCU, "WindowState", (int) myWindowState); - - // fire SaveState event - if (SaveState != null) - SaveState (this, myRegistry); - } - - private void myOnLoad (object sender, System.EventArgs e) - { - if (mySavePosition) - { - int left = (int) myRegistry.getValue (RegistryRoot.HKCU, "Left", myParent.Left); - int top = (int) myRegistry.getValue (RegistryRoot.HKCU, "Top", myParent.Top); - myParent.Location = new Point (left, top); - } - - if (myResizable) - { - int width = (int) myRegistry.getValue (RegistryRoot.HKCU, "Width", myParent.Width); - int height = (int) myRegistry.getValue (RegistryRoot.HKCU, "Height", myParent.Height); - myParent.Size = new Size (width, height); - } - - if (mySaveState) - { - FormWindowState windowState = (FormWindowState) myRegistry.getValue (RegistryRoot.HKCU, "WindowState", - (int) myParent.WindowState); - myParent.WindowState = windowState; - } - - // fire LoadState event - if(LoadState != null) - LoadState (this, myRegistry); - } - } -} +using System; +using System.Reflection; +using System.Windows.Forms; +using System.Drawing; +using System.Diagnostics; +using Microsoft.Win32; + +namespace BCG +{ + public class PersistWindowState + { + // event info that allows form to persist extra window state data + public delegate void WindowStateHandler (object sender, BlueRegistry bluereg); + public event WindowStateHandler? LoadState; + public event WindowStateHandler? SaveState; + + private Form? myParent; + private BlueRegistry? myRegistry; + + private int myNormalLeft; + private int myNormalTop; + private int myNormalWidth; + private int myNormalHeight; + private bool myResizable = true; + private FormWindowState myWindowState; + private bool myAllowSaveMinimized = false; + private bool mySavePosition = true; + private bool mySaveState = true; + + public PersistWindowState (bool savePosition) + { + mySavePosition = savePosition; + } + + public PersistWindowState () + { + } + + public bool saveState + { + [DebuggerStepThrough] + set { mySaveState = value; } + } + + public Form? parent + { + [DebuggerStepThrough] + get { return myParent; } + set + { + myParent = value; + if (myParent is null) { + return; + } + myRegistry = new BlueRegistry (myParent.Text); + + // subscribe to parent form's events + myParent.Closing += new System.ComponentModel.CancelEventHandler(myOnClosing); + myParent.Resize += new System.EventHandler(myOnResize); + myParent.Move += new System.EventHandler(myOnMove); + myParent.Load += new System.EventHandler(myOnLoad); + + // get initial width and height in case form is never resized + myNormalWidth = myParent.Width; + myNormalHeight = myParent.Height; + } + } + + public BlueRegistry? blueRegistry + { + [DebuggerStepThrough] + get { return myRegistry; } + [DebuggerStepThrough] + set { myRegistry = value; } + } + + public bool resizable + { + [DebuggerStepThrough] + set { myResizable = value; } + } + + public bool allowSaveMinimized + { + [DebuggerStepThrough] + set { myAllowSaveMinimized = value; } + } + + void myOnResize(object? sender, EventArgs e) { + if (myParent is null) { + return; + } + // save width and height + if (myParent.WindowState == FormWindowState.Normal && myResizable) + { + myNormalWidth = myParent.Width; + myNormalHeight = myParent.Height; + } + } + + void myOnMove(object? sender, EventArgs e) { + if (myParent is null) { + return; + } + // save position + if(myParent.WindowState == FormWindowState.Normal) + { + myNormalLeft = myParent.Left; + myNormalTop = myParent.Top; + } + // save state + myWindowState = myParent.WindowState; + } + + void myOnClosing(object? sender, System.ComponentModel.CancelEventArgs e) { + // save position, size and state + if (mySavePosition) + { + myRegistry?.setValue (RegistryRoot.HKCU, "Left", myNormalLeft); + myRegistry?.setValue (RegistryRoot.HKCU, "Top", myNormalTop); + } + + if (myResizable) + { + myRegistry?.setValue (RegistryRoot.HKCU, "Width", myNormalWidth); + myRegistry?.setValue (RegistryRoot.HKCU, "Height", myNormalHeight); + } + + // check if we are allowed to save the state as minimized (not normally) + if (!myAllowSaveMinimized) + { + if(myWindowState == FormWindowState.Minimized) + myWindowState = FormWindowState.Normal; + } + + if (mySaveState) + myRegistry?.setValue (RegistryRoot.HKCU, "WindowState", (int) myWindowState); + + // fire SaveState event + if (myRegistry is not null) { + SaveState?.Invoke(this, myRegistry); + } + } + + void myOnLoad (object? sender, EventArgs e) { + if (myRegistry is null || myParent is null) { + return; + } + if (mySavePosition) + { + int left = (int) myRegistry.getValue (RegistryRoot.HKCU, "Left", myParent.Left); + int top = (int) myRegistry.getValue (RegistryRoot.HKCU, "Top", myParent.Top); + myParent.Location = new Point (left, top); + } + + if (myResizable) + { + int width = (int) myRegistry.getValue (RegistryRoot.HKCU, "Width", myParent.Width); + int height = (int) myRegistry.getValue (RegistryRoot.HKCU, "Height", myParent.Height); + myParent.Size = new Size (width, height); + } + + if (mySaveState) + { + FormWindowState windowState = (FormWindowState) myRegistry.getValue (RegistryRoot.HKCU, "WindowState", + (int) myParent.WindowState); + myParent.WindowState = windowState; + } + + // fire LoadState event + if(LoadState != null) + LoadState (this, myRegistry); + } + } +} diff --git a/src/DataStreamSuite/SharedStreams/NL/BlueOrbNLStream.cs b/src/DataStreamSuite/SharedStreams/NL/BlueOrbNLStream.cs index cf40a0cd4..47f983024 100644 --- a/src/DataStreamSuite/SharedStreams/NL/BlueOrbNLStream.cs +++ b/src/DataStreamSuite/SharedStreams/NL/BlueOrbNLStream.cs @@ -3,6 +3,8 @@ using Bts.DataStreamCore; + using mbts.utils; + using System; using System.Collections.Generic; using System.Linq; @@ -13,7 +15,8 @@ public class BlueOrbNLStream : ViewStream { private readonly string _nl_prompt; readonly IDataStreamFactory streamFactory; - private ViewStream _nl_to_dsl_stream, _dsl_stream; + ViewStream? _nl_to_dsl_stream; + ViewStream? _dsl_stream; private IDictionary _dp_metadata = new Dictionary(); public BlueOrbNLStream(string nl_prompt, IDataStreamFactory streamFactory) { _nl_prompt = nl_prompt; @@ -26,7 +29,7 @@ } private void _handle_dsl_callback(object dp_obj) { - DataPacket dp = dp_obj as DataPacket; + var dp = ExceptionAssert.Argument.Is(dp_obj); var r = dp.newRows.to_list().ElementAt(0); string dsl = r.get("response"); _dp_metadata["dsl"] = dsl; @@ -36,9 +39,9 @@ } private void _handle_data_cb(object dp_obj) { - var dp = dp_obj as DataPacket; - dp.meta_data = new Dictionary(_dp_metadata); - DataPacket dp_out = BlueDataUtility.build_dp_inc(dp.newRows, myLastValue as DataPacket, true); + var dp = ExceptionAssert.Argument.Is(dp_obj).ResetMetadata( + new Dictionary(_dp_metadata)); + var dp_out = BlueDataUtility.build_dp_inc(dp.newRows, myLastValue as IDataPacket, true); myFireEvent(dp_out); } }