diff --git a/src/DataStreamSuite/DataStream/Publish/PubTableBroker.cs b/src/DataStreamSuite/DataStream/Publish/PubTableBroker.cs index 2e17ccc25..d6b28d187 100644 --- a/src/DataStreamSuite/DataStream/Publish/PubTableBroker.cs +++ b/src/DataStreamSuite/DataStream/Publish/PubTableBroker.cs @@ -1,72 +1,68 @@ using System; using System.Collections; +using System.Collections.Generic; using BCG.Utilities; -namespace BCG -{ +namespace BCG { /// - /// This class will be used by the clients to publish data to the pubsub + /// This class will be used by the clients to publish tabular data to the pubsub /// - public class PubTableBroker - { - protected PublisherCore myPublisherCore; - protected IDictionary myPublisherMap = new Hashtable (); - protected IDictionary mySchemaMap = new Hashtable (); - + public class PubTableBroker { + protected PublisherCore _publisher_core; + protected IDictionary _publisher_map = new Dictionary(); + protected Dictionary _schema_map = new Dictionary(); + protected bool myWaitingForFirstOp = false; - public PubTableBroker () - : this (new PublisherCore()) - {} + public PubTableBroker() + : this(new PublisherCore()) { } - public PubTableBroker (PublisherCore publisher) - {myPublisherCore = publisher;} + public PubTableBroker(PublisherCore publisher) { _publisher_core = publisher; } - public PubTableBroker (IBlueRowCollection[] tablesToPublish) - : this (new PublisherCore(), tablesToPublish) - {} + public PubTableBroker(IBlueRowCollection[] tablesToPublish) + : this(new PublisherCore(), tablesToPublish) { } - public PubTableBroker (PublisherCore publisher, IBlueRowCollection[] tablesToPublish) - { - myPublisherCore = publisher; + public PubTableBroker(PublisherCore publisher, IBlueRowCollection[] tablesToPublish) { + _publisher_core = publisher; - myPublisherCore.beginTransaction (); + _publisher_core.beginTransaction(); foreach (IBlueRowCollection table in tablesToPublish) - createTable (table); - myPublisherCore.commitTransaction (); + createTable(table); + _publisher_core.commitTransaction(); } - public void beginTransaction () - { + public void beginTransaction() { myWaitingForFirstOp = true; } - public void commitTransaction () - { + public void commitTransaction() { + bts.Log.Gen.info("PubTableBroker.commitTransaction"); if (myWaitingForFirstOp) myWaitingForFirstOp = false; else - myPublisherCore.commitTransaction (); + _publisher_core.commitTransaction(); } - public void rollbackTransaction () - { + public void rollbackTransaction() { + bts.Log.Gen.info("PubTableBroker.rollbackTransaction"); if (myWaitingForFirstOp) myWaitingForFirstOp = false; else - myPublisherCore.abortTransaction (); + _publisher_core.abortTransaction(); } - public virtual void createTable (IBlueRowCollection newTable) - { + public virtual void createTable(IBlueRowCollection newTable) { + bts.Log.Gen.info($"PubTableBroker.createTable table: {newTable.schema.tableName}"); myCheckTransaction(); - if (myPublisherMap [newTable.schema.tableName.ToLower()] == null) - { - myPublisherMap [newTable.schema.tableName.ToLower()] = myGetIPubTablePublisher (); - mySchemaMap [newTable.schema.tableName.ToLower()] = newTable.schema; + IPubTablePublisher table_publisher = null; + var name_key = newTable.schema.tableName.ToLower(); + if (!_publisher_map.TryGetValue(name_key, out table_publisher)) { + table_publisher = _create_pubtable_publisher(name_key); + _publisher_map.Add(name_key, table_publisher); + _schema_map.Add(name_key, newTable.schema); } - (myPublisherMap[newTable.schema.tableName.ToLower()] as IPubTablePublisher).createTable (newTable); + table_publisher.createTable(newTable); } /// @@ -74,13 +70,14 @@ namespace BCG /// will be thrown /// /// - public virtual void addRow (IBlueRow r) - { + public virtual void addRow(IBlueRow r) { myCheckTransaction(); - if (myPublisherMap[r.schema.tableName.ToLower()] == null) - throw new ApplicationException ("Row can not be added, as the table that has not been created yet"); + IPubTablePublisher table_publisher = null; + var name_key = r.schema.tableName.ToLower(); + if (!_publisher_map.TryGetValue(name_key, out table_publisher)) + throw new ApplicationException("Row can not be added, as the table that has not been created yet"); else - (myPublisherMap [r.schema.tableName.ToLower()] as IPubTablePublisher).addRow (r); + table_publisher.addRow(r); } /// @@ -88,13 +85,14 @@ namespace BCG /// will be thrown /// /// - public virtual void deleteRow (IBlueRow r) - { + public virtual void deleteRow(IBlueRow r) { myCheckTransaction(); - if (myPublisherMap[r.schema.tableName.ToLower()] == null) - throw new ApplicationException ("Row can not be removed, as the table has not been created yet"); + IPubTablePublisher table_publisher = null; + var name_key = r.schema.tableName.ToLower(); + if (!_publisher_map.TryGetValue(name_key, out table_publisher)) + throw new ApplicationException("Row can not be removed, as the table has not been created yet"); else - (myPublisherMap [r.schema.tableName.ToLower()] as IPubTablePublisher).deleteRow (r); + table_publisher.deleteRow(r); } /// @@ -102,13 +100,14 @@ namespace BCG /// will be thrown /// /// - public virtual void changeRow (IBlueRow r) - { + public virtual void changeRow(IBlueRow r) { myCheckTransaction(); - if (myPublisherMap[r.schema.tableName.ToLower()] == null) - throw new ApplicationException ("Row can not be changed, as the table has not been created yet"); + IPubTablePublisher table_publisher = null; + var name_key = r.schema.tableName.ToLower(); + if (!_publisher_map.TryGetValue(name_key, out table_publisher)) + throw new ApplicationException("Row can not be changed, as the table has not been created yet"); else - (myPublisherMap [r.schema.tableName.ToLower()] as IPubTablePublisher).changeRow (r); + table_publisher.changeRow(r); } /// @@ -116,40 +115,37 @@ namespace BCG /// will be thrown /// /// - public virtual void makeBatchChanges (IBlueRowCollection newRows, - IBlueRowCollection deletedRows, - IBlueRowCollection addedRows) - { + public virtual void makeBatchChanges(IBlueRowCollection newRows, + IBlueRowCollection deletedRows, + IBlueRowCollection addedRows) { myCheckTransaction(); - if (myPublisherMap[newRows.schema.tableName.ToLower()] == null) - throw new ApplicationException ("Batch changes can not be made, as the table has not been created yet"); + IPubTablePublisher table_publisher = null; + var name_key = newRows.schema.tableName.ToLower(); + if (!_publisher_map.TryGetValue(name_key, out table_publisher)) + throw new ApplicationException("Batch changes can not be made, as the table has not been created yet"); else - (myPublisherMap [newRows.schema.tableName.ToLower()] as IPubTablePublisher).batchRemoveAdd (deletedRows, addedRows); + table_publisher.batchRemoveAdd(deletedRows, addedRows); } - public virtual void deleteTableRows (string tableName) - { + public virtual void deleteTableRows(string tableName) { myCheckTransaction(); - (myPublisherMap [tableName.ToLower()] as IPubTablePublisher).createTable ( - new BlueTable (mySchemaMap[tableName.ToLower()] as IBlueTableSchema)); + (_publisher_map[tableName.ToLower()] as IPubTablePublisher).createTable( + new BlueTable(_schema_map[tableName.ToLower()] as IBlueTableSchema)); } - public int getNumberOfWaitingMessages() { return myPublisherCore.getNumberOfWaitingMessages(); } - - public void clearTable(string tableName) - {myPublisherCore.clearState(PubStreamPrefixer.prefix(tableName));} + public int getNumberOfWaitingMessages() { return _publisher_core.getNumberOfWaitingMessages(); } - protected void myCheckTransaction() - { + public void clearTable(string tableName) { _publisher_core.clearState(PubStreamPrefixer.prefix(tableName)); } + + protected void myCheckTransaction() { if (myWaitingForFirstOp) - myPublisherCore.beginTransaction (); + _publisher_core.beginTransaction(); myWaitingForFirstOp = false; } - protected virtual IPubTablePublisher myGetIPubTablePublisher () - { - return new PubTablePublisher (myPublisherCore); + protected virtual IPubTablePublisher _create_pubtable_publisher(string tag) { + return new PubTablePublisher(tag, _publisher_core); } } - } +} diff --git a/src/DataStreamSuite/DataStream/Publish/ViewPublisher.cs b/src/DataStreamSuite/DataStream/Publish/ViewPublisher.cs index 9477b48b7..877101d66 100644 --- a/src/DataStreamSuite/DataStream/Publish/ViewPublisher.cs +++ b/src/DataStreamSuite/DataStream/Publish/ViewPublisher.cs @@ -4,62 +4,51 @@ using System.Data; using BCG.Utilities; -namespace BCG -{ - public class ViewPublisher : IDisposable - { - protected ViewPublisher () {} +namespace BCG { + public class ViewPublisher : IDisposable { + protected ViewPublisher() { } /// If an name IsNullOrEmpty then it uses the name /// in the schema (be careful with that because this /// could lead to double publishes if the schema name /// is not uniquely chosen) - public ViewPublisher(string name, PubTableBroker br, ViewStream vs) - { + public ViewPublisher(string name, PubTableBroker br, ViewStream vs) { myName = name; - myView = new TableStream(name); // TODOds String.IsNullOrEmpty(name) ? vs : new ProjectStream(vs, "!", myName); + myView = vs;//String.IsNullOrEmpty(name) ? vs : new ProjectStream(vs, "!", myName); myBroker = br; myFirstTime = true; myView.onChange += new DataStream.ChangeDelegate(myChangeHandler); } - public void Dispose() - { + public void Dispose() { myView.onChange -= new DataStream.ChangeDelegate(myChangeHandler); if (!String.IsNullOrEmpty(myName)) myBroker.clearTable(myName); } - private void myChangeHandler(object o) - { - DataPacket dp = (DataPacket) o; - if (myFirstTime) - { + private void myChangeHandler(object o) { + DataPacket dp = (DataPacket)o; + if (myFirstTime) { myName = String.IsNullOrEmpty(myName) ? dp.schema.tableName : myName; myLastRowCount = dp.newRows.Count; myBroker.createTable(dp.newRows); myFirstTime = false; - } - else - { - if (((dp.addedRows.Count + dp.deletedRows.Count) != 0)) - { - myLastRowCount = dp.newRows.Count; - myBroker.makeBatchChanges(dp.newRows,dp.deletedRows, dp.addedRows); + } else { + if (((dp.addedRows.Count + dp.deletedRows.Count) != 0)) { + myLastRowCount = dp.newRows.Count; + myBroker.makeBatchChanges(dp.newRows, dp.deletedRows, dp.addedRows); } } } - public int getLastRowPublishedCount() - {return myLastRowCount;} + public int getLastRowPublishedCount() { return myLastRowCount; } - public bool hasPublished() - { return !myFirstTime; } + public bool hasPublished() { return !myFirstTime; } private int myLastRowCount = -1; private string myName; private PubTableBroker myBroker; protected ViewStream myView; private bool myFirstTime; - } + } } diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs index 38008942b..3ae2e9092 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs @@ -13,42 +13,32 @@ using System.Linq.Expressions; //using mbts.spark; using mbts; -namespace BCG -{ - public static class BlueDataUtility - { +namespace BCG { + public static class BlueDataUtility { #region Extensions - public static DateTime get_datetime(this IBlueRow r, string col_name, DateTime err_value = default(DateTime)) - { + public static DateTime get_datetime(this IBlueRow r, string col_name, DateTime err_value = default(DateTime)) { return BlueDataUtility.get_row_datetime_value(r, col_name, err_value); } - public static DateTime get_datetime(this IBlueRow r, int col_no, DateTime err_value = default(DateTime)) - { + public static DateTime get_datetime(this IBlueRow r, int col_no, DateTime err_value = default(DateTime)) { return BlueDataUtility.get_row_datetime_value(r, col_no, err_value); } - public static T get_enum(this IBlueRow r, string col_name, T err_value = default(T)) where T : struct - { + public static T get_enum(this IBlueRow r, string col_name, T err_value = default(T)) where T : struct { return BlueDataUtility.get_row_enum_value(r, col_name, err_value); } - public static T get_enum(this IBlueRow r, int col_no, T err_value = default(T)) where T : struct - { + public static T get_enum(this IBlueRow r, int col_no, T err_value = default(T)) where T : struct { return BlueDataUtility.get_row_enum_value(r, col_no, err_value); } - public static T get(this IBlueRow r, string col_name, T err_value = default(T)) - { + public static T get(this IBlueRow r, string col_name, T err_value = default(T)) { return get_row_value(r, col_name, err_value); } - public static T get(this IBlueRow r, int col_no, T err_value = default(T)) - { + public static T get(this IBlueRow r, int col_no, T err_value = default(T)) { return get_row_value(r, col_no, err_value); } - public static IBlueRowCollection where(this IBlueRowCollection rows, Func where_func) - { + public static IBlueRowCollection where(this IBlueRowCollection rows, Func where_func) { if (rows == null) return null; - else - { + else { BlueTable rv = new BlueTable(rows.schema); foreach (IBlueRow r in rows) if (where_func(r)) @@ -57,28 +47,24 @@ namespace BCG } } - public static IEnumerable to_ienum(this IBlueRowCollection rows) - { + public static IEnumerable to_ienum(this IBlueRowCollection rows) { List rv = new List(); foreach (IBlueRow r in rows) rv.Add(r); return rv; } - public static IBlueTableSchema add_columns(this IBlueTableSchema input_schema, params Tuple[] cols) - { + public static IBlueTableSchema add_columns(this IBlueTableSchema input_schema, params Tuple[] cols) { List cis = new List(input_schema.columnInfo); foreach (Tuple c in cols) cis.Add(new ColumnInfo(c.Item2, cis.Count, c.Item1)); return new BlueTableSchema(input_schema.tableName, cis.ToArray()); } - public static IBlueRowCollection to_collection(this IBlueRow r) - { + public static IBlueRowCollection to_collection(this IBlueRow r) { if (r == null) return null; - else - { + else { BlueTable rv = new BlueTable(r.schema); rv.addRow(r); return rv; @@ -87,17 +73,13 @@ namespace BCG public static IBlueRowCollection adapt_for_sql_storage(this IBlueRowCollection rows, bool handle_datetime_min = false, - bool handle_double_nan = false) - { - if (rows != null) - { + bool handle_double_nan = false) { + if (rows != null) { BlueTable rv = new BlueTable(rows.schema); - foreach (IBlueRow r in rows) - { + foreach (IBlueRow r in rows) { MutableBlueRow mr = new MutableBlueRow(r); rv.addRow(mr); - foreach (ColumnInfo ci in rows.schema.columnInfo) - { + foreach (ColumnInfo ci in rows.schema.columnInfo) { if (handle_datetime_min && ci.colType == typeof(DateTime) && DateTime.MinValue.Equals(r[ci.colName])) mr.setValue(ci.colName, DBNull.Value); if (handle_double_nan && ci.colType == typeof(double) && Double.IsNaN(r.get(ci.colName))) @@ -105,13 +87,35 @@ namespace BCG } } return rv; - } - else + } else return null; } #endregion + public static void print_table(this IBlueRowCollection table, Func get_col_width) { + // Print column headers + Console.WriteLine("______________________________________"); + foreach (var col_name in table.schema.columnNames) { + int col_width = get_col_width(col_name); + Console.Write($"{col_name.PadRight(col_width)} "); + } + Console.WriteLine(); + Console.WriteLine("______________________________________"); + + // Print table data + foreach (IBlueRow row in table) { + foreach (string col_name in table.schema.columnNames) { + int col_width = get_col_width(col_name); + object row_val = row[col_name]; + string row_val_str = row_val.ToString(); + if (row_val != null) { + Console.Write($"{row_val_str.PadRight(col_width)} "); + } + } + Console.WriteLine(); + } + } private static IReadOnlyDictionary _typecode_to_type = new Dictionary { { TypeCode.Boolean, typeof(bool) }, @@ -134,8 +138,7 @@ namespace BCG { TypeCode.UInt64, typeof(UInt64) } }; - public static IBlueTableSchema form_schema(string table_name, params Tuple[] cols) - { + public static IBlueTableSchema form_schema(string table_name, params Tuple[] cols) { List cis = new List(); foreach (Tuple c in cols) cis.Add(new ColumnInfo(c.Item1, cis.Count, _typecode_to_type[c.Item2])); @@ -151,17 +154,14 @@ namespace BCG /// /// /// - public static int getRowHashCode(IBlueRow row, bool isColAlphabetically) - { + public static int getRowHashCode(IBlueRow row, bool isColAlphabetically) { string retVal = ""; - if (isColAlphabetically) - { + if (isColAlphabetically) { string[] colNames = row.schema.columnNames; Array.Sort(colNames); foreach (string col in colNames) retVal += row[col].ToString(); - } - else + } else foreach (object o in row) retVal += o.ToString(); return retVal.GetHashCode(); @@ -173,8 +173,7 @@ namespace BCG /// /// /// - public static int getRowHashCode(IBlueRow row, params string[] colNames) - { + public static int getRowHashCode(IBlueRow row, params string[] colNames) { string retVal = ""; foreach (string colName in colNames) retVal += row[colName].ToString(); @@ -186,8 +185,7 @@ namespace BCG /// /// /// - public static DataTable toDataTable(IBlueRowCollection rows) - { + public static DataTable toDataTable(IBlueRowCollection rows) { DataTable retTable = getTableFromSchema(rows.schema); retTable.BeginLoadData(); foreach (IBlueRow row in rows) @@ -202,8 +200,7 @@ namespace BCG /// /// /// - public static DataRow toDataRow(IBlueRow row, DataTable table) - { + public static DataRow toDataRow(IBlueRow row, DataTable table) { DataRow retRow = table.NewRow(); foreach (string colName in row.schema.columnNames) retRow[colName] = row[colName]; @@ -226,8 +223,7 @@ namespace BCG /// table whos Schema is desired /// /// - public static IBlueTableSchema getSchema(DataTable table) - { + public static IBlueTableSchema getSchema(DataTable table) { ColumnInfo[] cis = new ColumnInfo[table.Columns.Count]; foreach (DataColumn col in table.Columns) cis[col.Ordinal] = new ColumnInfo(col.ColumnName, col.Ordinal, col.DataType); @@ -240,8 +236,7 @@ namespace BCG /// /// /// - public static IBlueTableSchema getSchema(object o) - { + public static IBlueTableSchema getSchema(object o) { return getSchema(o.GetType()); } @@ -251,8 +246,7 @@ namespace BCG /// /// Base schema /// Columns to add - public static IBlueTableSchema addColumnsToSchema(IBlueTableSchema schema, params ColumnInfo[] cis) - { + public static IBlueTableSchema addColumnsToSchema(IBlueTableSchema schema, params ColumnInfo[] cis) { ArrayList rv = new ArrayList(schema.columnInfo); rv.AddRange(cis); return new BlueTableSchema(schema.tableName, rv.ToArray(typeof(ColumnInfo)) as ColumnInfo[]); @@ -264,8 +258,7 @@ namespace BCG /// /// /// - public static IBlueTableSchema appendSchemas(IBlueTableSchema s1, IBlueTableSchema appendedSchema) - { + public static IBlueTableSchema appendSchemas(IBlueTableSchema s1, IBlueTableSchema appendedSchema) { List cis = new List(s1.columnInfo); foreach (ColumnInfo c in appendedSchema.columnInfo) if (c.colName != "id") @@ -274,18 +267,15 @@ namespace BCG } - public static IBlueTableSchema getSchema(Type t) - { + public static IBlueTableSchema getSchema(Type t) { return getSchema(t, false); } - public static IBlueTableSchema getSchema(Type t, bool createIdField) - { + public static IBlueTableSchema getSchema(Type t, bool createIdField) { return getSchema(t, t.Name, createIdField); } - public static IBlueTableSchema getSchema(Type t, string tableName, bool createIdField) - { + public static IBlueTableSchema getSchema(Type t, string tableName, bool createIdField) { ArrayList colInfos = new ArrayList(); int i = 0; foreach (PropertyInfo pi in t.GetProperties()) @@ -305,20 +295,17 @@ namespace BCG tableName); } - public static IBlueTableSchema getSchema(Type t, string tableName) - { + public static IBlueTableSchema getSchema(Type t, string tableName) { return new BlueTableSchema(getSchema(t).columnInfo, tableName); } - public static IBlueRow createRowFromType(IBlueTableSchema schema, object o) - { + public static IBlueRow createRowFromType(IBlueTableSchema schema, object o) { return createRowFromType(schema, o, null); } //creates a row from the given type, and if idVal is not null, using the given id - public static IBlueRow createRowFromType(IBlueTableSchema schema, object o, object idVal) - { + public static IBlueRow createRowFromType(IBlueTableSchema schema, object o, object idVal) { object[] rowArray = new object[schema.columnInfo.Length]; Type t = o.GetType(); foreach (PropertyInfo pi in t.GetProperties()) @@ -337,8 +324,7 @@ namespace BCG } public static MutableBlueRow create_mutable_row(IBlueTableSchema schema, object o, object idVal, - Func type_converter) - { + Func type_converter) { object[] rowArray = new object[schema.columnInfo.Length]; Type t = o.GetType(); foreach (PropertyInfo pi in t.GetProperties()) @@ -357,8 +343,7 @@ namespace BCG } - public static IBlueTableSchema get_schema(Type t, string tableName, bool createIdField, Func type_converter) - { + public static IBlueTableSchema get_schema(Type t, string tableName, bool createIdField, Func type_converter) { List colInfos = new List(); int i = 0; if (createIdField) @@ -372,16 +357,13 @@ namespace BCG } public static MutableBlueRow create_mutable_row_not_slow(IBlueTableSchema schema, FieldInfo[] fields, PropertyInfo[] props, - object o, object idVal, Func data_converter) - { + object o, object idVal, Func data_converter) { object[] rowArray = new object[schema.columnInfo.Length]; - if (props != null) - { + if (props != null) { foreach (PropertyInfo pi in props) rowArray[schema[pi.Name].colNo] = data_converter(pi.PropertyType, pi.GetValue(o, null)); } - if (fields != null) - { + if (fields != null) { foreach (FieldInfo fi in fields) rowArray[schema[fi.Name].colNo] = data_converter(fi.FieldType, fi.GetValue(o)); } @@ -390,19 +372,16 @@ namespace BCG return new MutableBlueRow(schema, rowArray); } - public static T createTypeFromRow(IBlueRow row) - { + public static T createTypeFromRow(IBlueRow row) { return (T)createTypeFromRow(row, typeof(T)); } - public static object createTypeFromRow(IBlueRow row, Type t) - { + public static object createTypeFromRow(IBlueRow row, Type t) { object outValue = Activator.CreateInstance(t); PropertyInfo[] properties = t.GetProperties(); - foreach (PropertyInfo pi in properties) - { + foreach (PropertyInfo pi in properties) { if (myIsActiveProperty(pi)) if (row.Contains(pi.Name)) if (row[pi.Name].Equals(DBNull.Value) && !pi.PropertyType.Equals(typeof(Object))) @@ -411,8 +390,7 @@ namespace BCG pi.SetValue(outValue, row[pi.Name], null); } FieldInfo[] fields = t.GetFields(); - foreach (FieldInfo fi in fields) - { + foreach (FieldInfo fi in fields) { if (myIsActiveField(fi)) if (row.Contains(fi.Name)) if (row[fi.Name].Equals(DBNull.Value) && !fi.FieldType.Equals(typeof(Object))) @@ -427,18 +405,15 @@ namespace BCG //these functions should originally have been made public //but changing that would change a bunch of code - public static bool isActiveProperty(PropertyInfo pi) - { + public static bool isActiveProperty(PropertyInfo pi) { return myIsActiveProperty(pi); } - public static bool isActiveField(FieldInfo fi) - { + public static bool isActiveField(FieldInfo fi) { return myIsActiveField(fi); } - private static bool myIsActiveProperty(PropertyInfo pi) - { + private static bool myIsActiveProperty(PropertyInfo pi) { if (pi.GetGetMethod() == null) return false; foreach (Attribute att in pi.GetCustomAttributes(true)) @@ -447,8 +422,7 @@ namespace BCG return true; } - private static bool myIsActiveField(FieldInfo fi) - { + private static bool myIsActiveField(FieldInfo fi) { if (!fi.IsPublic) return false; foreach (Attribute att in fi.GetCustomAttributes(true)) @@ -457,8 +431,7 @@ namespace BCG return true; } - private static bool myIsActiveMember(MemberInfo mi) - { + private static bool myIsActiveMember(MemberInfo mi) { if (!(mi is FieldInfo) || !(mi is PropertyInfo) || (mi is PropertyInfo && (mi as PropertyInfo).GetGetMethod() == null) || @@ -470,21 +443,17 @@ namespace BCG return true; } - private static bool myDoesRowExistsInTable(IBlueRowCollection brc2, string[] columns, object[] oarray) - { + private static bool myDoesRowExistsInTable(IBlueRowCollection brc2, string[] columns, object[] oarray) { bool rowExists = false; - foreach (IBlueRow br in brc2) - { + foreach (IBlueRow br in brc2) { bool allElementsSame = true; - for (int i = 0; i < columns.Length; i++) - { + for (int i = 0; i < columns.Length; i++) { string col = columns[i]; object o1 = br[col]; object o2 = oarray[i]; allElementsSame = (o1.Equals(o2) == false) ? false : allElementsSame; } - if (allElementsSame == true) - { + if (allElementsSame == true) { rowExists = true; break; } @@ -499,8 +468,7 @@ namespace BCG /// Schema whose table needs to be created /// /// - public static DataTable getTableFromSchema(IBlueTableSchema schema) - { + public static DataTable getTableFromSchema(IBlueTableSchema schema) { DataTable retTable = new DataTable(schema.tableName); foreach (ColumnInfo ci in schema.columnInfo) retTable.Columns.Add(ci.colName, ci.colType); @@ -512,24 +480,20 @@ namespace BCG /// /// source row /// - public static IBlueRow toBlueRow(DataRow row) - { + public static IBlueRow toBlueRow(DataRow row) { return new BlueRow(getSchema(row), row); } - public static IBlueRowCollection toIBlueRowCollection(DataTable dt) - { + public static IBlueRowCollection toIBlueRowCollection(DataTable dt) { BlueTable retVal = new BlueTable(getSchema(dt)); foreach (DataRow dr in dt.Rows) retVal.addRow(dr); return retVal; } - public static IBlueRowCollection toIBlueRowCollection(DataRow[] rows) - { + public static IBlueRowCollection toIBlueRowCollection(DataRow[] rows) { BlueTable retVal = null; - foreach (DataRow dr in rows) - { + foreach (DataRow dr in rows) { IBlueRow r = toBlueRow(dr); if (retVal == null) retVal = new BlueTable(r.schema); @@ -546,8 +510,7 @@ namespace BCG /// /// /// - public static IBlueRow extractRow(IBlueRowCollection rows, int index) - { + public static IBlueRow extractRow(IBlueRowCollection rows, int index) { int i = 0; foreach (IBlueRow row in rows) if (i++ == index) @@ -561,15 +524,13 @@ namespace BCG /// /// /// - public static bool computeRowEquality(IBlueRow r1, IBlueRow r2) - { + public static bool computeRowEquality(IBlueRow r1, IBlueRow r2) { return computeRowEquality(r1, r2, r1.schema.columnNames); } public static bool computeRowEquality(IBlueRow r1, IBlueRow r2, - params string[] colNames) - { + params string[] colNames) { return compareRows(r1, r2, colNames) == 0 ? true : false; } @@ -580,8 +541,7 @@ namespace BCG /// /// /// - public static string rowToString(IBlueRow row, params string[] colNames) - { + public static string rowToString(IBlueRow row, params string[] colNames) { string retVal = ""; foreach (string colName in colNames) retVal += (row[colName] != null ? row[colName].ToString().Trim() : "NULL") + "\n"; @@ -590,12 +550,10 @@ namespace BCG public static string rowToString(IBlueRow row) { return rowToString(row, row.schema.columnNames); } - public static string rowCollectionToString(IBlueRowCollection rowColl) - { + public static string rowCollectionToString(IBlueRowCollection rowColl) { if (rowColl == null) return "Null IBlueRowCollection"; - else - { + else { string retVal = ""; foreach (IBlueRow r in rowColl) retVal += retVal == "" ? rowToString(r) : "\n" + rowToString(r); @@ -614,11 +572,9 @@ namespace BCG /// public static int compareRows(IBlueRow r1, IBlueRow r2, - params string[] colNames) - { + params string[] colNames) { int retVal = 0; - foreach (string colName in colNames) - { + foreach (string colName in colNames) { object val1 = r1[colName]; object val2 = r2[colName]; retVal = _compare_row_cols(val1, val2); @@ -628,16 +584,13 @@ namespace BCG return retVal; } - private static int _compare_row_cols(object v1, object v2) - { + private static int _compare_row_cols(object v1, object v2) { if (_is_null(v1)) return _is_null(v2) ? 0 : -1; - else - { + else { if (_is_null(v2)) return 1; - else - { + else { if (v1 is IComparable && v2 is IComparable) return (v1 as IComparable).CompareTo(v2); else if (v1 is byte[] && v2 is byte[]) @@ -648,8 +601,7 @@ namespace BCG } } - private static bool _compare_bytes(byte[] v1, byte[] v2) - { + private static bool _compare_bytes(byte[] v1, byte[] v2) { if (v1.Length != v2.Length) return false; else @@ -666,8 +618,7 @@ namespace BCG /// a row with the values to be converted /// the desired schema for the output row /// - public static IBlueRow changeSchema(IBlueRow inRow, IBlueTableSchema newSchema) - { + public static IBlueRow changeSchema(IBlueRow inRow, IBlueTableSchema newSchema) { object[] outputValues = new object[newSchema.columnInfo.Length]; for (int i = 0; i < outputValues.Length; ++i) @@ -682,8 +633,7 @@ namespace BCG /// /// /// - public static IBlueRowCollection combineTables(params IBlueRowCollection[] tables) - { + public static IBlueRowCollection combineTables(params IBlueRowCollection[] tables) { //get some base cases out of the way if (tables.Length == 0) return null; @@ -695,14 +645,12 @@ namespace BCG int numCols = tables[0].schema.columnInfo.Length; int idCol = tables[0].schema["id"].colNo; IBlueRowCollection previousTable = null; - foreach (IBlueRowCollection table in tables) - { + foreach (IBlueRowCollection table in tables) { if (previousTable != null) if (!compareSchemas(previousTable.schema, table.schema)) throw new ArgumentException("Error, tables must have the same schema to be combined"); - foreach (IBlueRow r in table) - { + foreach (IBlueRow r in table) { object[] values = new object[numCols]; r.CopyTo(values, 0); values[idCol] = idToUseNext++; @@ -722,8 +670,7 @@ namespace BCG /// /// /// - public static string[] compareRows(IBlueRow r1, IBlueRow r2) - { + public static string[] compareRows(IBlueRow r1, IBlueRow r2) { ArrayList retVal = new ArrayList(); foreach (string col in r1.schema.columnNames) if (!r1[col].Equals(r2[col])) @@ -731,8 +678,7 @@ namespace BCG return retVal.ToArray(typeof(string)) as string[]; } - public static BlueTable cloneRowCollection(IBlueRowCollection rows) - { + public static BlueTable cloneRowCollection(IBlueRowCollection rows) { BlueTable retVal = new BlueTable(rows.schema); foreach (IBlueRow r in rows) retVal.addRow(new BlueRow(r)); @@ -747,8 +693,7 @@ namespace BCG /// /// /// - public static IBlueRow getNewReplacedRow(IBlueRow r, object[] newValues, int[] indexes) - { + public static IBlueRow getNewReplacedRow(IBlueRow r, object[] newValues, int[] indexes) { if (!newValues.Length.Equals(indexes.Length)) throw new Exception("Debug Assert"); @@ -759,13 +704,11 @@ namespace BCG return new BlueRow(r.schema, rowArray); } - public static IBlueRow getNewReplacedRow(IBlueRow r, object newValue, int index) - { + public static IBlueRow getNewReplacedRow(IBlueRow r, object newValue, int index) { return getNewReplacedRow(r, new object[] { newValue }, new int[] { index }); } - public static IBlueRow getNewReplacedRow(IBlueRow r, object newValue, string colName) - { + public static IBlueRow getNewReplacedRow(IBlueRow r, object newValue, string colName) { return getNewReplacedRow(r, newValue, r.schema[colName].colNo); } @@ -775,8 +718,7 @@ namespace BCG /// /// /// - public static IBlueRow computeMaxRow(IBlueRowCollection rows, params string[] cols) - { + public static IBlueRow computeMaxRow(IBlueRowCollection rows, params string[] cols) { IBlueRow retVal = rows.Count != 0 ? BlueDataUtility.extractRow(rows, 0) : null; foreach (IBlueRow r in rows) retVal = compareRows(r, retVal, cols) > 0 ? r : retVal; @@ -789,8 +731,7 @@ namespace BCG /// /// /// - public static IBlueRow computeMinRow(IBlueRowCollection rows, params string[] cols) - { + public static IBlueRow computeMinRow(IBlueRowCollection rows, params string[] cols) { IBlueRow retVal = rows.Count != 0 ? BlueDataUtility.extractRow(rows, 0) : null; foreach (IBlueRow r in rows) retVal = compareRows(r, retVal, cols) < 0 ? r : retVal; @@ -803,24 +744,20 @@ namespace BCG /// /// /// - public static IBlueRow computeSum(IBlueRowCollection rows, params string[] cols) - { + public static IBlueRow computeSum(IBlueRowCollection rows, params string[] cols) { if (rows.Count == 0) return null; IBlueRow retVal = BlueDataUtility.extractRow(rows, 0); object[] sums = new object[cols.Length]; int[] idxs = new int[cols.Length]; - for (int i = 0; i < cols.Length; i++) - { + for (int i = 0; i < cols.Length; i++) { sums[i] = 0.0; idxs[i] = rows.schema[cols[i]].colNo; } - foreach (IBlueRow r in rows) - { - for (int i = 0; i < cols.Length; i++) - { + foreach (IBlueRow r in rows) { + for (int i = 0; i < cols.Length; i++) { double dval = BlueDataUtility.get_row_value(r, cols[i], 0.0); //r[cols[i]] == null || r[cols[i]] is DBNull ? 0.0 : (double)r[cols[i]]; sums[i] = (double)sums[i] + dval; @@ -831,8 +768,7 @@ namespace BCG } - public static IBlueRow computeSum(IBlueRowCollection rows, params string[] cols) - { + public static IBlueRow computeSum(IBlueRowCollection rows, params string[] cols) { if (rows.Count == 0) return null; IBlueRow retVal = BlueDataUtility.extractRow(rows, 0); if (rows.Count == 1) return retVal; @@ -840,16 +776,13 @@ namespace BCG object[] sums = new object[cols.Length]; int[] idxs = new int[cols.Length]; - for (int i = 0; i < cols.Length; i++) - { + for (int i = 0; i < cols.Length; i++) { sums[i] = default(T); idxs[i] = rows.schema[cols[i]].colNo; } - foreach (IBlueRow r in rows) - { - for (int i = 0; i < cols.Length; i++) - { + foreach (IBlueRow r in rows) { + for (int i = 0; i < cols.Length; i++) { T tval = r[cols[i]] == null || r[cols[i]] is DBNull ? default(T) : (T)r[cols[i]]; sums[i] = MagicAdder.Add((T)sums[i], tval); } @@ -864,16 +797,14 @@ namespace BCG /// /// /// - public static bool rowPresent(IBlueRowCollection table, IBlueRow row) - { + public static bool rowPresent(IBlueRowCollection table, IBlueRow row) { return table.containsID((int)row["id"]); } public static void computeDifferences(IBlueRowCollection newRows, IBlueRowCollection oldRows, out IBlueRowCollection dRows, - out IBlueRowCollection aRows) - { + out IBlueRowCollection aRows) { computeDifferences( newRows, oldRows, @@ -883,8 +814,7 @@ namespace BCG } public static void compute_differences_fast(IBlueRowCollection new_rows, IBlueRowCollection old_rows, - out IBlueRowCollection out_d_rows, out IBlueRowCollection out_a_rows) - { + out IBlueRowCollection out_d_rows, out IBlueRowCollection out_a_rows) { int[] col_indices = new int[new_rows.schema.columnNames.Length]; for (int i = 0; i < col_indices.Length; i++) col_indices[i] = i; @@ -892,23 +822,20 @@ namespace BCG } public static void compute_differences_fast(IBlueRowCollection new_rows, IBlueRowCollection old_rows, int[] col_indices, - out IBlueRowCollection out_d_rows, out IBlueRowCollection out_a_rows) - { - Func rows_equal = (r1, r2) => - { - foreach (int col_no in col_indices) - if (!(r1[col_no] ?? DBNull.Value).Equals(r2[col_no] ?? DBNull.Value)) - return false; - return true; - }; + out IBlueRowCollection out_d_rows, out IBlueRowCollection out_a_rows) { + Func rows_equal = (r1, r2) => { + foreach (int col_no in col_indices) + if (!(r1[col_no] ?? DBNull.Value).Equals(r2[col_no] ?? DBNull.Value)) + return false; + return true; + }; BlueTable del_rows = new BlueTable(new_rows.schema), add_rows = new BlueTable(new_rows.schema); // adds + changes foreach (IBlueRow r in new_rows) if (!BlueDataUtility.rowPresent(old_rows, r)) add_rows.addRow(r); - else if (!rows_equal(r, old_rows[(int)r["id"]])) - { + else if (!rows_equal(r, old_rows[(int)r["id"]])) { add_rows.addRow(r); del_rows.addRow(old_rows[(int)r["id"]]); } @@ -925,8 +852,7 @@ namespace BCG IBlueRowCollection c2, string[] columns, out IBlueRowCollection dRows, - out IBlueRowCollection aRows) - { + out IBlueRowCollection aRows) { dRows = new BlueTable(c1.schema); aRows = new BlueTable(c1.schema); @@ -935,8 +861,7 @@ namespace BCG if (!BlueDataUtility.rowPresent(c2, nr)) (aRows as BlueTable).addRow(nr); else if (BlueDataUtility.compareRows(nr, c2[(int) - nr["id"]], columns) != 0) - { + nr["id"]], columns) != 0) { (aRows as BlueTable).addRow(nr); (dRows as BlueTable).addRow(c2[(int)nr["id"]]); @@ -949,14 +874,11 @@ namespace BCG } //brcDiff = brc1 - brc2 - public static void diff(IBlueRowCollection brc1, IBlueRowCollection brc2, string[] columns, out IBlueRowCollection brcDiff) - { + public static void diff(IBlueRowCollection brc1, IBlueRowCollection brc2, string[] columns, out IBlueRowCollection brcDiff) { brcDiff = new BlueTable(brc1.schema); - foreach (IBlueRow br in brc1) - { + foreach (IBlueRow br in brc1) { object[] oarray = new object[columns.Length]; - for (int i = 0; i < columns.Length; i++) - { + for (int i = 0; i < columns.Length; i++) { string col = columns[i]; oarray[i] = br[col]; } @@ -971,8 +893,7 @@ namespace BCG /// /// /// - public static void absorbChanges(BlueTable t, IBlueRowCollection dels, IBlueRowCollection adds) - { + public static void absorbChanges(BlueTable t, IBlueRowCollection dels, IBlueRowCollection adds) { foreach (IBlueRow r in dels) if (!t.deleteRow(r)) throw new Exception(String.Format("{0} does not have row with id {1} to delete", t.schema.tableName, r["id"])); @@ -981,8 +902,7 @@ namespace BCG t.addRow(r); } - public static IBlueRowCollection toBlueRowCollection(IBlueRow r) - { + public static IBlueRowCollection toBlueRowCollection(IBlueRow r) { BlueTable rv = new BlueTable(r.schema); rv.addRow(r); return rv; @@ -994,11 +914,9 @@ namespace BCG /// /// /// - public static IBlueRowCollection toBlueRowCollection(ICollection col) - { + public static IBlueRowCollection toBlueRowCollection(ICollection col) { BlueTable retVal = null; - foreach (IBlueRow r in col) - { + foreach (IBlueRow r in col) { retVal = retVal ?? new BlueTable(r.schema); retVal.addRow(r); } @@ -1017,23 +935,19 @@ namespace BCG IBlueRowCollection adds, out IBlueRowCollection truelyDels, out IBlueRowCollection truelyAdds, - string[] cols) - { + string[] cols) { truelyDels = new BlueTable(dels); truelyAdds = new BlueTable(adds); - foreach (IBlueRow r in dels) - { + foreach (IBlueRow r in dels) { if (adds.containsID((int)r["id"]) && - BlueDataUtility.computeRowEquality(adds[(int)r["id"]], r, cols)) - { + BlueDataUtility.computeRowEquality(adds[(int)r["id"]], r, cols)) { (truelyDels as BlueTable).deleteRow((int)r["id"]); (truelyAdds as BlueTable).deleteRow((int)r["id"]); } } } - public static IBlueRowCollection getSerializableCollection(IBlueRowCollection coll) - { + public static IBlueRowCollection getSerializableCollection(IBlueRowCollection coll) { BlueTable retVal = new OrderedBlueTable(coll.schema); foreach (IBlueRow r in coll) retVal.addRow(new BlueRow(r)); @@ -1046,8 +960,7 @@ namespace BCG /// /// /// - public static ArrayList extractColumn(IBlueRowCollection rows, string colName) - { + public static ArrayList extractColumn(IBlueRowCollection rows, string colName) { ArrayList rv = new ArrayList(); foreach (IBlueRow r in rows) rv.Add(r[colName]); @@ -1066,19 +979,16 @@ namespace BCG public static void checkConsistency(IBlueRowCollection oldState, IBlueRowCollection newRows, IBlueRowCollection delRows, - IBlueRowCollection addedRows) - { + IBlueRowCollection addedRows) { BlueTable calc = new BlueTable(oldState); Debug.Assert(oldState != null && newRows != null && delRows != null && addedRows != null); - foreach (IBlueRow r in delRows) - { + foreach (IBlueRow r in delRows) { Debug.Assert(calc.containsID((int)r["id"]), "deleted row's id = " + r["id"] + " does not exist"); calc.deleteRow(r); } - foreach (IBlueRow r in addedRows) - { + foreach (IBlueRow r in addedRows) { Debug.Assert(!calc.containsID((int)r["id"]), "added rows's id = " + r["id"].ToString() + " already exists"); calc.addRow(r); } @@ -1095,8 +1005,7 @@ namespace BCG /// /// /// - public static object[] getSelectedValues(IBlueRow r, params string[] colNames) - { + public static object[] getSelectedValues(IBlueRow r, params string[] colNames) { object[] retVal = new object[colNames.Length]; for (int i = 0; i < colNames.Length; i++) retVal[i] = r[colNames[i]]; @@ -1112,10 +1021,8 @@ namespace BCG /// /// /// - public static IBlueRow getRowWithKey(IBlueRowCollection rows, string[] keyColumns, object[] colValues) - { - foreach (IBlueRow r in rows) - { + public static IBlueRow getRowWithKey(IBlueRowCollection rows, string[] keyColumns, object[] colValues) { + foreach (IBlueRow r in rows) { object[] vals = getSelectedValues(r, keyColumns); if (CollectionHelpers.equal(vals, colValues)) return r; @@ -1123,8 +1030,7 @@ namespace BCG return null; } - public static IBlueRowCollection deepCopy(IBlueRowCollection rows) - { + public static IBlueRowCollection deepCopy(IBlueRowCollection rows) { BlueTable retVal = new BlueTable(rows.schema); foreach (IBlueRow r in rows) retVal.addRow(new BlueRow(r)); @@ -1138,8 +1044,7 @@ namespace BCG /// /// /// - public static int[] getColumnsInSortedOrder(IBlueTableSchema schema, bool includeId) - { + public static int[] getColumnsInSortedOrder(IBlueTableSchema schema, bool includeId) { ArrayList names = new ArrayList(schema.columnNames), retVal = new ArrayList(); names.Sort(); @@ -1152,40 +1057,33 @@ namespace BCG //this class is used so that we can sort a schema by column name //that way, prepublished tables will allways get published with schemas in the same order //regardless of how the underlying streams end up working - private class ColumnInfoComparisonWrapper : IComparable - { + private class ColumnInfoComparisonWrapper : IComparable { ColumnInfo myColumn; - public string ColumnName - { + public string ColumnName { get { return myColumn.colName; } //can't set it, these are only for reading columns } - public ColumnInfo Column - { + public ColumnInfo Column { get { return myColumn; } } - public ColumnInfoComparisonWrapper(ColumnInfo theColumn) - { + public ColumnInfoComparisonWrapper(ColumnInfo theColumn) { myColumn = theColumn; } - public int CompareTo(ColumnInfoComparisonWrapper otherGuy) - { + public int CompareTo(ColumnInfoComparisonWrapper otherGuy) { return ColumnName.CompareTo(otherGuy.ColumnName); } } - public static IBlueTableSchema getSortedSchema(IBlueTableSchema inSchema) - { + public static IBlueTableSchema getSortedSchema(IBlueTableSchema inSchema) { List sortedColNames = new List(inSchema.columnNames); sortedColNames.Sort(); List sortedColInfos = new List(); int colNo = 0; - foreach (string c in sortedColNames) - { + foreach (string c in sortedColNames) { ColumnInfo ci = inSchema[c]; sortedColInfos.Add(new ColumnInfo(c, colNo++, ci.colType)); @@ -1193,15 +1091,13 @@ namespace BCG return new BlueTableSchema(inSchema.tableName, sortedColInfos.ToArray()); } - public static IBlueTableSchema getSortedSchema(IBlueTableSchema theSchema, out int[] sortedSchemaToOriginalSchemaMapping, out int idColumnInSortedSchema) - { + public static IBlueTableSchema getSortedSchema(IBlueTableSchema theSchema, out int[] sortedSchemaToOriginalSchemaMapping, out int idColumnInSortedSchema) { //for now this function will not do anything //that is because we don't want to break the C++ deserialization code int numCols = theSchema.columnNames.Length; sortedSchemaToOriginalSchemaMapping = new int[numCols]; - for (int col = 0; col < theSchema.columnNames.Length; ++col) - { + for (int col = 0; col < theSchema.columnNames.Length; ++col) { //the mapping is just the identity mapping sortedSchemaToOriginalSchemaMapping[col] = col; @@ -1255,43 +1151,34 @@ namespace BCG /// /// /// true if equal - public static bool compareSchemas(IBlueTableSchema s1, IBlueTableSchema s2) - { + public static bool compareSchemas(IBlueTableSchema s1, IBlueTableSchema s2) { bool result = true; - try - { + try { if (s1.columnInfo.Length != s2.columnInfo.Length) result = false; else - foreach (ColumnInfo ci1 in s1.columnInfo) - { + foreach (ColumnInfo ci1 in s1.columnInfo) { ColumnInfo ci2 = s2[ci1.colNo]; - if (ci1.colName != ci2.colName || ci1.colType != ci2.colType) - { + if (ci1.colName != ci2.colName || ci1.colType != ci2.colType) { result = false; break; } } - } - catch - { + } catch { result = false; // the col# wasn't found in s2 } return result; } - public static IBlueRowCollection removeRows(BlueTable table, string[] cols, object[] vals) - { + public static IBlueRowCollection removeRows(BlueTable table, string[] cols, object[] vals) { IBlueRowCollection dels = findMatchingRows(table, cols, vals); table.deleteRows(dels); return dels; } - public static IBlueRowCollection findMatchingRows(IBlueRowCollection rows, string[] cols, object[] vals) - { + public static IBlueRowCollection findMatchingRows(IBlueRowCollection rows, string[] cols, object[] vals) { BlueTable rv = new BlueTable(rows.schema); - foreach (IBlueRow r in rows) - { + foreach (IBlueRow r in rows) { bool match = true; for (int i = 0; i < cols.Length; i++) match &= vals[i].Equals(r[cols[i]]); @@ -1301,8 +1188,7 @@ namespace BCG return rv; } - public static void checkRowTypes(IBlueRowCollection rows) - { + public static void checkRowTypes(IBlueRowCollection rows) { foreach (IBlueRow r in rows) foreach (string col in r.schema.columnNames) if (r[col] != null && r[col] != DBNull.Value) @@ -1310,14 +1196,12 @@ namespace BCG String.Format("col {0} in table {1} is of type {2} in schema but is a {3} in the row", col, rows.schema.tableName, r.schema[col].colType.ToString(), r[col].GetType().ToString())); } - public static void addRows(ref BlueTable table, ICollection rows) - { + public static void addRows(ref BlueTable table, ICollection rows) { foreach (IBlueRow r in rows) table.addRow(r); } - public static void removeRows(ref BlueTable table, ICollection rows) - { + public static void removeRows(ref BlueTable table, ICollection rows) { foreach (IBlueRow r in rows) table.deleteRow(r); } @@ -1325,8 +1209,7 @@ namespace BCG public static void sortOutPacket(IBlueRowCollection dels, IBlueRowCollection adds, string[] keyCols, out IBlueRowCollection addedRows, out IBlueRowCollection deletedRows, - out IBlueRowCollection changedRows) - { + out IBlueRowCollection changedRows) { addedRows = new BlueTable(dels.schema); deletedRows = new BlueTable(dels.schema); changedRows = new BlueTable(dels.schema); @@ -1339,49 +1222,39 @@ namespace BCG foreach (IBlueRow r in adds) keyedAdds.update(r); - foreach (IBlueRow r in keyedDels) - { + foreach (IBlueRow r in keyedDels) { object[] key = BlueDataUtility.getSelectedValues(r, keyCols); - if (keyedAdds.getRow(key) != null) - { + if (keyedAdds.getRow(key) != null) { ///change (changedRows as BlueTable).addRow(r); - } - else - { + } else { ///delete (deletedRows as BlueTable).addRow(r); } } - foreach (IBlueRow r in keyedAdds) - { + foreach (IBlueRow r in keyedAdds) { object[] key = BlueDataUtility.getSelectedValues(r, keyCols); - if (keyedDels.getRow(key) == null) - { + if (keyedDels.getRow(key) == null) { (addedRows as BlueTable).addRow(r); } } } - public static int HashExpStrike(DateTime exp, double strike) - { + public static int HashExpStrike(DateTime exp, double strike) { return ((DateTime)exp).ToShortDateString().GetHashCode() ^ strike.GetHashCode(); } - public static IEnumerable collection_to_ienum(IBlueRowCollection rows) - { + public static IEnumerable collection_to_ienum(IBlueRowCollection rows) { List rv = new List(); foreach (IBlueRow r in rows) rv.Add(r); return rv; } - public static IBlueRowCollection ienum_to_collection(IEnumerable rows) - { + public static IBlueRowCollection ienum_to_collection(IEnumerable rows) { BlueTable rv = null; - if (rows.Count() > 0) - { + if (rows.Count() > 0) { rv = new BlueTable(rows.ElementAt(0).schema); foreach (IBlueRow r in rows) rv.addRow(r); @@ -1389,15 +1262,13 @@ namespace BCG return rv; } - public static IBlueTableSchema add_column(IBlueTableSchema input_schema, string new_col_name, Type new_col_type) - { + public static IBlueTableSchema add_column(IBlueTableSchema input_schema, string new_col_name, Type new_col_type) { List cis = new List(input_schema.columnInfo); cis.Add(new ColumnInfo(new_col_name, cis.Count, new_col_type)); return new BlueTableSchema(input_schema.tableName, cis.ToArray()); } - public static IBlueTableSchema remove_columns(IBlueTableSchema input_schema, params string[] col_names) - { + public static IBlueTableSchema remove_columns(IBlueTableSchema input_schema, params string[] col_names) { Dictionary removed_col_names = new Dictionary(); foreach (string c in col_names) removed_col_names[c] = true; @@ -1408,16 +1279,14 @@ namespace BCG return new BlueTableSchema(cis.ToArray(), input_schema.tableName); } - public static int[] get_column_indices(IBlueTableSchema schema, string[] col_names) - { + public static int[] get_column_indices(IBlueTableSchema schema, string[] col_names) { List rv = new List(); foreach (string c in col_names) rv.Add(schema[c].colNo); return rv.ToArray(); } - public static string get_appended_string_val(IBlueRow r, int[] col_indices) - { + public static string get_appended_string_val(IBlueRow r, int[] col_indices) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < col_indices.Length; i++) sb.Append(r[col_indices[i]]); @@ -1425,35 +1294,29 @@ namespace BCG } public delegate string RowValueToString(IBlueRow r, int col_index); - public static string get_appended_string_val(IBlueRow r, int[] col_indices, RowValueToString to_string_conv) - { + public static string get_appended_string_val(IBlueRow r, int[] col_indices, RowValueToString to_string_conv) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < col_indices.Length; i++) sb.Append(to_string_conv(r, i)); return sb.ToString(); } - public static IDictionary> group_together(IBlueRowCollection rows, string[] group_by_col_names) - { + public static IDictionary> group_together(IBlueRowCollection rows, string[] group_by_col_names) { BlueRowHasher hasher = new BlueRowHasher(group_by_col_names.Length); return group_together(rows, BlueDataUtility.get_column_indices(rows.schema, group_by_col_names), ref hasher); } - public static IDictionary> group_together(IBlueRowCollection rows, string[] group_by_col_names, ref BlueRowHasher hasher) - { + public static IDictionary> group_together(IBlueRowCollection rows, string[] group_by_col_names, ref BlueRowHasher hasher) { return group_together(rows, BlueDataUtility.get_column_indices(rows.schema, group_by_col_names), ref hasher); } - public static IDictionary> group_together(IBlueRowCollection rows, int[] group_by_col_nos, ref BlueRowHasher hasher) - { + public static IDictionary> group_together(IBlueRowCollection rows, int[] group_by_col_nos, ref BlueRowHasher hasher) { Dictionary> rv = new Dictionary>(); hasher = hasher ?? new BlueRowHasher(group_by_col_nos.Length); - foreach (IBlueRow r in rows) - { + foreach (IBlueRow r in rows) { int row_hash = hasher.get_hash(r, group_by_col_nos); List this_group_rows = null; - if (!rv.TryGetValue(row_hash, out this_group_rows)) - { + if (!rv.TryGetValue(row_hash, out this_group_rows)) { this_group_rows = new List(); rv.Add(row_hash, this_group_rows); } @@ -1462,8 +1325,7 @@ namespace BCG return rv; } - public static void copy_to_row(IBlueRow row, string[] data_col_names, MutableBlueRow output_row) - { + public static void copy_to_row(IBlueRow row, string[] data_col_names, MutableBlueRow output_row) { foreach (string cn in data_col_names) output_row.setValue(cn, row[cn]); } @@ -1472,64 +1334,52 @@ namespace BCG /// Copies the column nos specified in data_cols from row, to output_row /// starting from index start_col_index /// - public static void copy_to_row(IBlueRow row, int[] data_col_nos, object[] output_row, int start_col_index) - { + public static void copy_to_row(IBlueRow row, int[] data_col_nos, object[] output_row, int start_col_index) { foreach (int col_no in data_col_nos) output_row[start_col_index++] = row[col_no]; } - public static object compute_min_max(object v1, object v2, bool is_max) - { - if (!TypeHelper.is_dbnull(v1) && !TypeHelper.is_dbnull(v2)) - { - if (TypeHelper.is_numeric_type(v1) && TypeHelper.is_numeric_type(v2)) - { + public static object compute_min_max(object v1, object v2, bool is_max) { + if (!TypeHelper.is_dbnull(v1) && !TypeHelper.is_dbnull(v2)) { + if (TypeHelper.is_numeric_type(v1) && TypeHelper.is_numeric_type(v2)) { double v1_num = (double)Convert.ChangeType(v1, TypeCode.Double), v2_num = (double)Convert.ChangeType(v2, TypeCode.Double); v1_num = Double.IsNaN(v1_num) ? (is_max ? Double.MinValue : Double.MaxValue) : v1_num; v2_num = Double.IsNaN(v2_num) ? (is_max ? Double.MinValue : Double.MaxValue) : v2_num; double rv = is_max ? Math.Max(v1_num, v2_num) : Math.Min(v1_num, v2_num); return Convert.ChangeType(rv, v1.GetType()); - } - else + } else return v1; - } - else + } else return TypeHelper.is_dbnull(v1) ? v2 : v1; } #region Sorting - public static class ParallelSort - { - public class TreeRowEffComparer : IComparer - { + public static class ParallelSort { + public class TreeRowEffComparer : IComparer { private int _depth_in_tree_col_no; private int[] _aggregation_tree_col_nos; private Pair[] _sort_map; - public TreeRowEffComparer(int depth_in_tree_col_no, int[] aggregation_tree_col_nos, Pair[] sort_map) - { + public TreeRowEffComparer(int depth_in_tree_col_no, int[] aggregation_tree_col_nos, Pair[] sort_map) { _depth_in_tree_col_no = depth_in_tree_col_no; _aggregation_tree_col_nos = aggregation_tree_col_nos; _sort_map = sort_map; } - public int Compare(IBlueRow x, IBlueRow y) - { + public int Compare(IBlueRow x, IBlueRow y) { return BlueDataUtility.sort_in_a_tree(x, y, _depth_in_tree_col_no, _aggregation_tree_col_nos, _sort_map); } } - public class TabularRowComparer : IComparer - { + public class TabularRowComparer : IComparer { private IEnumerable> _sort_map; public TabularRowComparer(IEnumerable> sort_map) { _sort_map = sort_map; } public int Compare(IBlueRow x, IBlueRow y) { return BlueDataUtility.compare_rows(x, y, _sort_map); } } - public class TabularRowHashPairComparer : IComparer> - { + public class TabularRowHashPairComparer : IComparer> { private Pair[] _sort_map; public TabularRowHashPairComparer(Pair[] sort_map) { _sort_map = sort_map; } @@ -1538,13 +1388,11 @@ namespace BCG #region Public Static Methods - public static void QuicksortSequential(IList arr, IComparer comparer) - { + public static void QuicksortSequential(IList arr, IComparer comparer) { QuicksortSequential(arr, comparer, 0, arr.Count - 1); } - public static void QuicksortParallel(IList arr, IComparer comparer) - { + public static void QuicksortParallel(IList arr, IComparer comparer) { QuicksortParallel(arr, comparer, 0, arr.Count - 1); } @@ -1552,27 +1400,20 @@ namespace BCG #region Private Static Methods - private static void QuicksortSequential(IList arr, IComparer comparer, int left, int right) - { - if (right > left) - { + private static void QuicksortSequential(IList arr, IComparer comparer, int left, int right) { + if (right > left) { int pivot = Partition(arr, comparer, left, right); QuicksortSequential(arr, comparer, left, pivot - 1); QuicksortSequential(arr, comparer, pivot + 1, right); } } - private static void QuicksortParallel(IList arr, IComparer comparer, int left, int right) - { + private static void QuicksortParallel(IList arr, IComparer comparer, int left, int right) { const int SEQUENTIAL_THRESHOLD = 64; - if (right > left) - { - if (right - left < SEQUENTIAL_THRESHOLD) - { + if (right > left) { + if (right - left < SEQUENTIAL_THRESHOLD) { QuicksortSequential(arr, comparer, left, right); - } - else - { + } else { int pivot = Partition(arr, comparer, left, right); System.Threading.Tasks.Parallel.Invoke(new Action[] { delegate {QuicksortParallel(arr, comparer, left, pivot - 1);}, delegate {QuicksortParallel(arr, comparer, pivot + 1, right); } @@ -1581,25 +1422,21 @@ namespace BCG } } - private static void Swap(IList arr, int i, int j) - { + private static void Swap(IList arr, int i, int j) { T tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } - private static int Partition(IList arr, IComparer comparer, int low, int high) - { + private static int Partition(IList arr, IComparer comparer, int low, int high) { // Simple partitioning implementation int pivotPos = (high + low) / 2; T pivot = arr[pivotPos]; Swap(arr, low, pivotPos); int left = low; - for (int i = low + 1; i <= high; i++) - { - if (comparer.Compare(arr[i], pivot) < 0) - { + for (int i = low + 1; i <= high; i++) { + if (comparer.Compare(arr[i], pivot) < 0) { left++; Swap(arr, i, left); } @@ -1612,10 +1449,8 @@ namespace BCG #endregion } - public static int compare_rows(IBlueRow r1, IBlueRow r2, Dictionary sort_map) - { - foreach (KeyValuePair p in sort_map) - { + public static int compare_rows(IBlueRow r1, IBlueRow r2, Dictionary sort_map) { + foreach (KeyValuePair p in sort_map) { int comp = compare_send_dontcare_to_last(r1, p.Key, r2, p.Key, p.Value); if (comp != 0) return comp; @@ -1623,10 +1458,8 @@ namespace BCG return 0; } - public static int compare_rows(IBlueRow r1, IBlueRow r2, IEnumerable> sort_map) - { - foreach (Pair p in sort_map) - { + public static int compare_rows(IBlueRow r1, IBlueRow r2, IEnumerable> sort_map) { + foreach (Pair p in sort_map) { int comp = compare_send_dontcare_to_last(r1, p.first, r2, p.first, p.second); if (comp != 0) return comp; @@ -1636,15 +1469,13 @@ namespace BCG public static int compare_send_dontcare_to_last(IBlueRow r1, int r1_col_no, IBlueRow r2, int r2_col_no, - bool low_to_high) - { + bool low_to_high) { object o1 = BlueDataUtility.get_row_value(r1, r1_col_no, DBNull.Value), o2 = BlueDataUtility.get_row_value(r2, r2_col_no, DBNull.Value); return compare_send_dontcare_to_last(o1, o2, low_to_high); } - public static List> get_indexed_sort_map(List> sort_map, IBlueTableSchema schema) - { + public static List> get_indexed_sort_map(List> sort_map, IBlueTableSchema schema) { List> rv = new List>(); foreach (Pair p in sort_map) rv.Add(new Pair(schema[p.first].colNo, p.second)); @@ -1655,15 +1486,13 @@ namespace BCG /// empty strings etc are sent to the last public static int compare_send_dontcare_to_last(IBlueRow r1, string r1_col_name, IBlueRow r2, string r2_col_name, - bool low_to_high) - { + bool low_to_high) { object o1 = BlueDataUtility.get_row_value(r1, r1_col_name, DBNull.Value), o2 = BlueDataUtility.get_row_value(r2, r2_col_name, DBNull.Value); return compare_send_dontcare_to_last(o1, o2, low_to_high); } - public static int compare_send_dontcare_to_last(object o1, object o2, bool low_to_high) - { + public static int compare_send_dontcare_to_last(object o1, object o2, bool low_to_high) { if (is_dont_care_val(o1)) return is_dont_care_val(o2) ? 0 : 1; else @@ -1671,8 +1500,7 @@ namespace BCG : ((IComparable)o2).CompareTo(o1)); } - public static bool is_dont_care_val(object o) - { + public static bool is_dont_care_val(object o) { return o == null || o == DBNull.Value || o is string && String.IsNullOrEmpty((string)o) || o is double && (Double.IsInfinity((double)o) || Double.IsNaN((double)o)) || @@ -1684,26 +1512,22 @@ namespace BCG #region Tree Aggregation - public static int[] convert_aggregation_tree(string[] aggregation_tree_col_names, IBlueTableSchema schema) - { + public static int[] convert_aggregation_tree(string[] aggregation_tree_col_names, IBlueTableSchema schema) { List rv = new List(); foreach (string col_name in aggregation_tree_col_names) rv.Add(schema[col_name].colNo); return rv.ToArray(); } - public static bool is_leaf_node(IBlueRow r, int depth_in_tree_col_no, int[] aggregation_tree) - { + public static bool is_leaf_node(IBlueRow r, int depth_in_tree_col_no, int[] aggregation_tree) { return BlueDataUtility.get_row_value(r, depth_in_tree_col_no, -1) == (aggregation_tree.Count() - 1); } - public static bool is_leaf_node(IBlueRow r, string depth_in_tree_col_name, string[] aggregation_tree) - { + public static bool is_leaf_node(IBlueRow r, string depth_in_tree_col_name, string[] aggregation_tree) { return BlueDataUtility.get_row_value(r, depth_in_tree_col_name, -1) == (aggregation_tree.Count() - 1); } - public static string generate_aggregation_based_id(IBlueRow r, int depth_in_tree_col_no, int[] aggregation_tree) - { + public static string generate_aggregation_based_id(IBlueRow r, int depth_in_tree_col_no, int[] aggregation_tree) { int aggregation_priority = BlueDataUtility.get_row_value(r, depth_in_tree_col_no, -1); string rv = String.Empty; for (int p = 0; p <= aggregation_priority; p++) @@ -1713,8 +1537,7 @@ namespace BCG /// Generates a string based id for a row in a tree /// by appending the values of the nodes above it - public static string generate_aggregation_based_id(IBlueRow r, string depth_in_tree_col_name, string[] aggregation_tree) - { + public static string generate_aggregation_based_id(IBlueRow r, string depth_in_tree_col_name, string[] aggregation_tree) { int aggregation_priority = BlueDataUtility.get_row_value(r, depth_in_tree_col_name, -1); string rv = String.Empty; for (int p = 0; p <= aggregation_priority; p++) @@ -1726,15 +1549,12 @@ namespace BCG /// Given an aggregation hierarchy tree, figures if the first /// arg is a parent of the second arg. /// - public static bool is_parent(IBlueRow parent_row, IBlueRow child_row, string depth_in_tree_col_name, string[] aggregation_tree) - { + public static bool is_parent(IBlueRow parent_row, IBlueRow child_row, string depth_in_tree_col_name, string[] aggregation_tree) { System.Diagnostics.Debug.Assert(parent_row.Contains(depth_in_tree_col_name) && child_row.Contains(depth_in_tree_col_name)); int parent_depth = BlueDataUtility.get_row_value(parent_row, depth_in_tree_col_name, Int32.MaxValue), child_depth = BlueDataUtility.get_row_value(child_row, depth_in_tree_col_name, Int32.MaxValue); - if (parent_depth < child_depth) - { - for (int p = 0; p <= parent_depth; p++) - { + if (parent_depth < child_depth) { + for (int p = 0; p <= parent_depth; p++) { string col_name = aggregation_tree.ElementAt(p); object parent_val = BlueDataUtility.get_row_value(parent_row, col_name, DBNull.Value), child_val = BlueDataUtility.get_row_value(child_row, col_name, DBNull.Value); @@ -1742,19 +1562,15 @@ namespace BCG return false; } return true; - } - else + } else return false; } - public static bool is_parent(IBlueRow parent_row, IBlueRow child_row, int depth_in_tree_col_no, int[] aggregation_tree) - { + public static bool is_parent(IBlueRow parent_row, IBlueRow child_row, int depth_in_tree_col_no, int[] aggregation_tree) { int parent_depth = BlueDataUtility.get_row_value(parent_row, depth_in_tree_col_no, Int32.MaxValue), child_depth = BlueDataUtility.get_row_value(child_row, depth_in_tree_col_no, Int32.MaxValue); - if (parent_depth < child_depth) - { - for (int p = 0; p <= parent_depth; p++) - { + if (parent_depth < child_depth) { + for (int p = 0; p <= parent_depth; p++) { int col_no = aggregation_tree[p]; object parent_val = BlueDataUtility.get_row_value(parent_row, col_no, DBNull.Value), child_val = BlueDataUtility.get_row_value(child_row, col_no, DBNull.Value); @@ -1762,26 +1578,21 @@ namespace BCG return false; } return true; - } - else + } else return false; } - public static int sort_in_a_tree(IBlueRow r1, IBlueRow r2, string depth_in_tree_col_name, string[] aggregation_tree, Dictionary sort_map) - { + public static int sort_in_a_tree(IBlueRow r1, IBlueRow r2, string depth_in_tree_col_name, string[] aggregation_tree, Dictionary sort_map) { if (BlueDataUtility.is_parent(r1, r2, depth_in_tree_col_name, aggregation_tree)) return -1; else if (BlueDataUtility.is_parent(r2, r1, depth_in_tree_col_name, aggregation_tree)) return 1; else if (get_row_value(r1, "id", Int32.MaxValue).Equals(get_row_value(r2, "id", Int32.MaxValue))) return 0; - else - { - for (int p = 0; p < aggregation_tree.Count(); p++) - { + else { + for (int p = 0; p < aggregation_tree.Count(); p++) { /// sort_map is used to leaf nodes - if (p == aggregation_tree.Count() - 1) - { + if (p == aggregation_tree.Count() - 1) { /// sorting for leaf nodes is special, since they can be /// sorted by data columns System.Diagnostics.Debug.Assert(BlueDataUtility.is_leaf_node(r1, depth_in_tree_col_name, aggregation_tree) && BlueDataUtility.is_leaf_node(r2, depth_in_tree_col_name, aggregation_tree)); @@ -1801,21 +1612,17 @@ namespace BCG } } - public static int sort_in_a_tree(IBlueRow r1, IBlueRow r2, int depth_in_tree_col_no, int[] aggregation_tree, Pair[] sort_map) - { + public static int sort_in_a_tree(IBlueRow r1, IBlueRow r2, int depth_in_tree_col_no, int[] aggregation_tree, Pair[] sort_map) { if (BlueDataUtility.is_parent(r1, r2, depth_in_tree_col_no, aggregation_tree)) return -1; else if (BlueDataUtility.is_parent(r2, r1, depth_in_tree_col_no, aggregation_tree)) return 1; else if (get_row_value(r1, "id", Int32.MaxValue).Equals(get_row_value(r2, "id", Int32.MaxValue))) return 0; - else - { - for (int p = 0; p < aggregation_tree.Count(); p++) - { + else { + for (int p = 0; p < aggregation_tree.Count(); p++) { /// sort_map is used to leaf nodes - if (p == aggregation_tree.Count() - 1) - { + if (p == aggregation_tree.Count() - 1) { /// sorting for leaf nodes is special, since they can be /// sorted by data columns //System.Diagnostics.Debug.Assert(BlueDataUtility.is_leaf_node(r1, depth_in_tree_col_name, aggregation_tree) && BlueDataUtility.is_leaf_node(r2, depth_in_tree_col_name, aggregation_tree)); @@ -1839,169 +1646,123 @@ namespace BCG #region Column accessors - public static T get_array_value(Array r, int col_no, T error_val) - { - try - { + public static T get_array_value(Array r, int col_no, T error_val) { + try { object rv = (r != null && col_no > -1 && col_no < r.Length) ? r.GetValue(col_no) : DBNull.Value; if (rv == null || rv == DBNull.Value) return error_val; else return (T)Convert.ChangeType(rv, typeof(T)); - } - catch - { + } catch { return error_val; } } - public static DateTime get_array_datetime_value(Array r, int col_no, DateTime error_val) - { - try - { + public static DateTime get_array_datetime_value(Array r, int col_no, DateTime error_val) { + try { object rv = (r != null && col_no > -1 && col_no < r.Length) ? r.GetValue(col_no) : error_val; return _get_datetime_value(rv, error_val); - } - catch - { + } catch { return error_val; } } - public static T get_row_value(IBlueRow r, int col_no, T error_val) - { - try - { + public static T get_row_value(IBlueRow r, int col_no, T error_val) { + try { object rv = (r != null && col_no > -1 && col_no < r.schema.columnInfo.Length) ? r[col_no] : DBNull.Value; if (rv == null || rv == DBNull.Value) return error_val; else return (T)Convert.ChangeType(rv, typeof(T)); - } - catch - { + } catch { return error_val; } } - public static T get_row_value(IBlueRow r, string col_name, T error_val) - { - try - { + public static T get_row_value(IBlueRow r, string col_name, T error_val) { + try { object rv = (r != null && r.Contains(col_name)) ? r[col_name] : DBNull.Value; if (rv == null || rv == DBNull.Value) return error_val; else return (T)Convert.ChangeType(rv, typeof(T)); - } - catch - { + } catch { return error_val; } } - public static T get_row_enum_value(IBlueRow r, string col_name, T error_val) where T : struct - { - try - { + public static T get_row_enum_value(IBlueRow r, string col_name, T error_val) where T : struct { + try { object rv = r.Contains(col_name) ? r[col_name] : DBNull.Value; return _get_row_enum_value(rv, error_val); - } - catch - { + } catch { return error_val; } } - public static T get_row_enum_value(IBlueRow r, int col_no, T error_val) where T : struct - { - try - { + public static T get_row_enum_value(IBlueRow r, int col_no, T error_val) where T : struct { + try { object rv = (r != null && col_no > -1 && col_no < r.schema.columnInfo.Length) ? r[col_no] : DBNull.Value; return _get_row_enum_value(rv, error_val); - } - catch - { + } catch { return error_val; } } - private static T _get_row_enum_value(object o, T error_val) where T : struct - { - try - { + private static T _get_row_enum_value(object o, T error_val) where T : struct { + try { if (o == null || o == DBNull.Value) return error_val; - else if (o is string) - { + else if (o is string) { T result = error_val; if (Enum.TryParse((string)o, true, out result)) return result; return error_val; - } - else if (o is Int32 || o is Int16 || o is UInt16 || o is UInt32) + } else if (o is Int32 || o is Int16 || o is UInt16 || o is UInt32) return (T)o; else if (o is T) return (T)o; else return error_val; - } - catch - { + } catch { return error_val; } } - public static DateTime get_row_datetime_value(IBlueRow r, string col_name, DateTime error_val) - { - try - { + public static DateTime get_row_datetime_value(IBlueRow r, string col_name, DateTime error_val) { + try { object rv = (r != null && r.Contains(col_name)) ? r[col_name] : error_val; return _get_datetime_value(rv, error_val); - } - catch - { + } catch { return error_val; } } - public static DateTime get_row_datetime_value(IBlueRow r, int col_no, DateTime error_val) - { - try - { + public static DateTime get_row_datetime_value(IBlueRow r, int col_no, DateTime error_val) { + try { object rv = (r != null && col_no > -1 && col_no < r.schema.columnInfo.Length) ? r[col_no] : error_val; return _get_datetime_value(rv, error_val); - } - catch - { + } catch { return error_val; } } - private static DateTime _get_datetime_value(object v, DateTime error_val) - { - try - { + private static DateTime _get_datetime_value(object v, DateTime error_val) { + try { if (v == null || v == DBNull.Value) return error_val; - else - { + else { if (v is DateTime) return (DateTime)v; - else if (v is string) - { + else if (v is string) { DateTime rv_dt = error_val; DateTime.TryParse((string)v, out rv_dt); return rv_dt; - } - else - { + } else { double oa_date = (double)Convert.ChangeType(v, typeof(Double)); return DateTime.FromOADate(oa_date); } } - } - catch - { + } catch { return error_val; } } @@ -2012,8 +1773,7 @@ namespace BCG /// /// Reads a schema file name, and return the constructed schema /// - public static IBlueTableSchema createSchemaFromCSVSchemaFile(string schemaFileName) - { + public static IBlueTableSchema createSchemaFromCSVSchemaFile(string schemaFileName) { StreamReader sr = new StreamReader(schemaFileName); IBlueTableSchema retVal = createSchemaFromCSVSchemaFile(sr); sr.Close(); @@ -2024,8 +1784,7 @@ namespace BCG /// Builds a schema from a schema file. Note that the stream is not closed, so /// the calling code should do so /// - public static IBlueTableSchema createSchemaFromCSVSchemaFile(TextReader sr) - { + public static IBlueTableSchema createSchemaFromCSVSchemaFile(TextReader sr) { string tableName = sr.ReadLine(); ArrayList t = new ArrayList(); string l = ""; @@ -2033,14 +1792,12 @@ namespace BCG t.Add(l.Trim()); string[] cols = t.ToArray(typeof(string)) as String[]; ColumnInfo[] cis = new ColumnInfo[cols.Length]; - for (int i = 0; i < cols.Length; i++) - { + for (int i = 0; i < cols.Length; i++) { string[] colTags = cols[i].Split('/'); cis[i] = new ColumnInfo(colTags[0], i, TypeInstanceLoader.loadType(colTags[1])); } IBlueTableSchema retVal = new BlueTableSchema(cis, tableName); - if (retVal["id"] == null) - { + if (retVal["id"] == null) { ArrayList temp = new ArrayList(retVal.columnInfo); temp.Add(new ColumnInfo("id", temp.Count, typeof(System.Int32))); retVal = new BlueTableSchema(retVal.tableName, @@ -2053,17 +1810,13 @@ namespace BCG /// Creates an IBlueRow from a CSV data file row; if the string already contains an id, /// the id argument is ignored /// - public static IBlueRow createRowFromCSVFileRow(string fileRow, IBlueTableSchema schema, int id) - { + public static IBlueRow createRowFromCSVFileRow(string fileRow, IBlueTableSchema schema, int id) { string[] toks = CSVParser.parse(fileRow); ArrayList rowList = new ArrayList(); - if (toks.Length == schema.columnNames.Length) - { + if (toks.Length == schema.columnNames.Length) { for (int i = 0; i < toks.Length; i++) rowList.Add(myConvertTo(schema[i].colType, toks[i])); - } - else - { + } else { int idIndex = schema["id"].colNo; for (int i = 0; i < toks.Length; i++) rowList.Add(myConvertTo(schema[i < idIndex ? i : i + 1].colType, toks[i])); @@ -2075,8 +1828,7 @@ namespace BCG /// /// Returns the CSV data string corresponding to the row /// - public static string toCSVDataString(IBlueRow r) - { + public static string toCSVDataString(IBlueRow r) { string retVal = ""; for (int i = 0; i < r.schema.columnNames.Length; i++) retVal += @@ -2086,8 +1838,7 @@ namespace BCG return retVal.Remove(retVal.Length - 1, 1); } - public static string[] toCSVSchemaFileStrings(IBlueTableSchema schema) - { + public static string[] toCSVSchemaFileStrings(IBlueTableSchema schema) { string[] retVal = new string[2]; retVal[0] = schema.tableName; foreach (ColumnInfo ci in schema.columnInfo) @@ -2099,13 +1850,11 @@ namespace BCG /// /// Converts a CSV data value to its original type. /// - private static object myConvertTo(Type t, string val) - { + private static object myConvertTo(Type t, string val) { val = val.Trim(); if (t.Equals(typeof(System.String))) return val; - else if (val != "") - { + else if (val != "") { if (t.Equals(typeof(System.Char))) return Convert.ToChar(val); if (t.Equals(typeof(System.Int16))) @@ -2124,21 +1873,17 @@ namespace BCG return DBNull.Value; } - private static object myToDateTime(string val) - { - try - { + private static object myToDateTime(string val) { + try { return Convert.ToDateTime(val); - } - catch (FormatException) //datetime is not stored in one of the string formats - { + } catch (FormatException) //datetime is not stored in one of the string formats + { return val.IndexOf('.') > -1 ? DateTime.FromOADate(Convert.ToDouble(val)) : new DateTime(Convert.ToInt64(val)); } } - public static IBlueRowCollection createRowsFromCSVFileWithoutTypes(string fileAddress) - { + public static IBlueRowCollection createRowsFromCSVFileWithoutTypes(string fileAddress) { StreamReader sr = new StreamReader(fileAddress); string cols = sr.ReadLine(); string[] columns = cols.Split(','); @@ -2161,18 +1906,15 @@ namespace BCG /// /// Uses a cached compiled expression to implement a truly generic add function. Will throw if given types that cannot be added. /// - internal class MagicAdder - { + internal class MagicAdder { static Func add; - static MagicAdder() - { + static MagicAdder() { ParameterExpression left = Expression.Parameter(typeof(T), "a"); ParameterExpression right = Expression.Parameter(typeof(T), "b"); add = (Func)Expression.Lambda(Expression.Add(left, right), left, right).Compile(); } - public static T Add(T a, T b) - { + public static T Add(T a, T b) { return add(a, b); } } @@ -2180,8 +1922,7 @@ namespace BCG #region Misc - public static void swap(ref T t1, ref T t2) - { + public static void swap(ref T t1, ref T t2) { T temp = t1; t1 = t2; t2 = temp; diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueTable.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTable.cs index a66c86654..5d9f84537 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueTable.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueTable.cs @@ -134,18 +134,17 @@ namespace BCG /// public virtual void addRow(IBlueRow row) { - object val = row["id"]; + object id = row["id"]; - if (val == null || val == DBNull.Value) + if (id == null || id == DBNull.Value) throw new BlueDataException("The ID of the row being added is null/DNBull"); try { - myIdIndex.Add(val, row); + myIdIndex.Add(id, row); } catch (ArgumentException) { - throw new BlueDataException("ID already exists in the table " + mySchema.tableName + - " , duplication of field ID not allowed"); + throw new BlueDataException($"ID {id} already exists in the table: {mySchema.tableName} , duplication of field ID not allowed"); } } diff --git a/src/DataStreamSuite/DataStreamCore/Messaging/Publish/PubTablePublisher.cs b/src/DataStreamSuite/DataStreamCore/Messaging/Publish/PubTablePublisher.cs index 632f5c541..51acf09e8 100644 --- a/src/DataStreamSuite/DataStreamCore/Messaging/Publish/PubTablePublisher.cs +++ b/src/DataStreamSuite/DataStreamCore/Messaging/Publish/PubTablePublisher.cs @@ -2,171 +2,151 @@ using System; using System.Collections; using System.IO; -namespace BCG -{ - /// - /// The only instance of IPubTableBroker. It handles the publishing of one tag, and holds a specialized serializer - /// that it initializes according to the table, to optimize serialiazation, hence bw and performance - /// This class is held (as an interface) by the PubTableBroker, which guarantees the sanity of calls, - /// ie the first call will always be create. - /// - public class PubTablePublisher : IPubTablePublisher - { +namespace BCG { + /// + /// The only instance of IPubTableBroker. It handles the publishing of one tag, and holds a specialized serializer + /// that it initializes according to the table, to optimize serialiazation, hence bw and performance + /// This class is held (as an interface) by the PubTableBroker, which guarantees the sanity of calls, + /// ie the first call will always be create. + /// + public class PubTablePublisher : IPubTablePublisher { + private readonly string _tag; protected IBlueDataSerializer mySerializer; protected PublisherCore myPubCore; - - public PubTablePublisher (PublisherCore publisherCore) - { + + public PubTablePublisher(string tag, PublisherCore publisherCore) { + _tag = tag; + bts.Log.Gen.info($"PubTablePublisher created for tag: {tag}"); myPubCore = publisherCore; - mySerializer = new BlueDataSerializationController (); + mySerializer = new BlueDataSerializationController(); } #region IPubTablePublisher Members - public virtual void addRow (IBlueRow row) - { + public virtual void addRow(IBlueRow row) { byte[] data; - mySerializer.serialize (StreamOpCode.add, - myGetRowCollectionArray (row), + mySerializer.serialize(StreamOpCode.add, + myGetRowCollectionArray(row), out data); - myPublish (data, row.schema.tableName); + myPublish(data, row.schema.tableName); } - public virtual void deleteRow (IBlueRow row) - { + public virtual void deleteRow(IBlueRow row) { byte[] data; - mySerializer.serialize (StreamOpCode.remove, - myGetRowCollectionArray (row), + mySerializer.serialize(StreamOpCode.remove, + myGetRowCollectionArray(row), out data); - myPublish (data, row.schema.tableName); + myPublish(data, row.schema.tableName); } - public virtual void changeRow(IBlueRow row) - { + public virtual void changeRow(IBlueRow row) { byte[] data; - mySerializer.serialize (StreamOpCode.change, - myGetRowCollectionArray (row), + mySerializer.serialize(StreamOpCode.change, + myGetRowCollectionArray(row), out data); - myPublish (data, row.schema.tableName); + myPublish(data, row.schema.tableName); } - public virtual void createTable (IBlueRowCollection rows) - { + public virtual void createTable(IBlueRowCollection rows) { byte[] data; - mySerializer.serialize (StreamOpCode.create, - new IBlueRowCollection [] {rows}, + mySerializer.serialize(StreamOpCode.create, + new IBlueRowCollection[] { rows }, out data); - myPublish (data, rows.schema.tableName); + bts.Log.Gen.info($"PubTablePublisher.createTable tag: {_tag} rows: {rows.Count} data: {data.Length}"); + myPublish(data, rows.schema.tableName); } - public virtual void batchRemoveAdd (IBlueRowCollection delRows, IBlueRowCollection addRows) - { + public virtual void batchRemoveAdd(IBlueRowCollection delRows, IBlueRowCollection addRows) { byte[] data; - if (myIsRealChange (delRows, addRows)) - batchChange (addRows); - else - { - mySerializer.serialize (StreamOpCode.multipleRemoveAdd, - new IBlueRowCollection []{delRows, addRows}, + if (myIsRealChange(delRows, addRows)) + batchChange(addRows); + else { + mySerializer.serialize(StreamOpCode.multipleRemoveAdd, + new IBlueRowCollection[] { delRows, addRows }, out data); - myPublish (data, delRows.schema.tableName); + myPublish(data, delRows.schema.tableName); } } - public virtual void batchChange (IBlueRowCollection rows) - { + public virtual void batchChange(IBlueRowCollection rows) { byte[] data; - - mySerializer.serialize (StreamOpCode.multipleChange, - new IBlueRowCollection [] {rows}, + + mySerializer.serialize(StreamOpCode.multipleChange, + new IBlueRowCollection[] { rows }, out data); - myPublish (data, rows.schema.tableName); + myPublish(data, rows.schema.tableName); } #endregion #region private methods - private void myPublish (byte[] data, string tableName) - { + private void myPublish(byte[] data, string tableName) { + bts.Log.Gen.info($"PubTablePublisher.myPublish table: {tableName} data: {data.Length}"); if (data.Length > 0) - myPubCore.publish ("DataStreamTag", - PubStreamPrefixer.prefix (tableName), - new MemoryStream (data, 0, data.Length, false, true)); + myPubCore.publish("DataStreamTag", + PubStreamPrefixer.prefix(tableName), + new MemoryStream(data, 0, data.Length, false, true)); } - private IBlueRowCollection[] myGetRowCollectionArray (IBlueRow r) - { - return new IBlueRowCollection []{ + private IBlueRowCollection[] myGetRowCollectionArray(IBlueRow r) { + return new IBlueRowCollection[]{ BlueDataUtility.toBlueRowCollection (new IBlueRow[] {r}) }; } - private bool myIsRealChange (IBlueRowCollection delRows, IBlueRowCollection addRows) - { - if (delRows.Count == addRows.Count) - { + private bool myIsRealChange(IBlueRowCollection delRows, IBlueRowCollection addRows) { + if (delRows.Count == addRows.Count) { foreach (IBlueRow dRow in delRows) - if (!addRows.containsID ((int)dRow["id"])) + if (!addRows.containsID((int)dRow["id"])) return false; return true; - } - else + } else return false; } #endregion } - public class IndependentTablePublisher : PubTablePublisher - { + public class IndependentTablePublisher : PubTablePublisher { bool myIsCreated = false; - public IndependentTablePublisher (PublisherCore pubCore) : base (pubCore) - {} + public IndependentTablePublisher(string tag, PublisherCore pubCore) : base(tag, pubCore) { } - public override void addRow (IBlueRow row) - { - myCheckCreate (row.schema); - base.addRow (row); + public override void addRow(IBlueRow row) { + myCheckCreate(row.schema); + base.addRow(row); } - public override void batchChange (IBlueRowCollection rows) - { - myCheckCreate (rows.schema); - base.batchChange (rows); + public override void batchChange(IBlueRowCollection rows) { + myCheckCreate(rows.schema); + base.batchChange(rows); } - public override void batchRemoveAdd (IBlueRowCollection delRows, IBlueRowCollection addRows) - { - myCheckCreate (delRows.schema); - base.batchRemoveAdd (delRows, addRows); + public override void batchRemoveAdd(IBlueRowCollection delRows, IBlueRowCollection addRows) { + myCheckCreate(delRows.schema); + base.batchRemoveAdd(delRows, addRows); } - public override void changeRow (IBlueRow row) - { - myCheckCreate (row.schema); - base.changeRow (row); + public override void changeRow(IBlueRow row) { + myCheckCreate(row.schema); + base.changeRow(row); } - public override void deleteRow (IBlueRow row) - { - myCheckCreate (row.schema); - base.deleteRow (row); + public override void deleteRow(IBlueRow row) { + myCheckCreate(row.schema); + base.deleteRow(row); } - public override void createTable(IBlueRowCollection rows) - { + public override void createTable(IBlueRowCollection rows) { myIsCreated = true; - base.createTable (rows); + base.createTable(rows); } - private void myCheckCreate (IBlueTableSchema schema) - { - if (!myIsCreated) - { + private void myCheckCreate(IBlueTableSchema schema) { + if (!myIsCreated) { //so that the initiazliation work is done on the serializer byte[] placeHolder; - mySerializer.serialize (StreamOpCode.create, new IBlueRowCollection [] {new BlueTable (schema)}, out placeHolder); + mySerializer.serialize(StreamOpCode.create, new IBlueRowCollection[] { new BlueTable(schema) }, out placeHolder); myIsCreated = true; } } diff --git a/src/DataStreamSuite/DataStreamCore/Messaging/Publish/Serialization/BlueDataSerializationController.cs b/src/DataStreamSuite/DataStreamCore/Messaging/Publish/Serialization/BlueDataSerializationController.cs index f5f674e2c..76e9e1d69 100644 --- a/src/DataStreamSuite/DataStreamCore/Messaging/Publish/Serialization/BlueDataSerializationController.cs +++ b/src/DataStreamSuite/DataStreamCore/Messaging/Publish/Serialization/BlueDataSerializationController.cs @@ -1,18 +1,16 @@ using System; -namespace BCG -{ - /// - /// Deals with the publication of one tag. Gets the specific implementaion of IBlueDataSerializer according to - /// the table name, from the config, and wraps around it. The external code that will deal with user calls for - /// row operations (PubTablePublisher) will only deal with this class, hence will be agnostic of the specalized - /// serialization. - /// - public class BlueDataSerializationController : IBlueDataSerializer - { +namespace BCG { + /// + /// Deals with the publication of one tag. Gets the specific implementaion of IBlueDataSerializer according to + /// the table name, from the config, and wraps around it. The external code that will deal with user calls for + /// row operations (PubTablePublisher) will only deal with this class, hence will be agnostic of the specalized + /// serialization. + /// + public class BlueDataSerializationController : IBlueDataSerializer { private IBlueDataSerializer mySerializer; - public BlueDataSerializationController () {} + public BlueDataSerializationController() { } #region IBlueDataSerializer Members @@ -23,43 +21,37 @@ namespace BCG /// /// /// - public void serialize (byte op, IBlueRowCollection[] rows, out byte[] serializedData) - { - if (op == StreamOpCode.create) - { + public void serialize(byte op, IBlueRowCollection[] rows, out byte[] serializedData) { + if (op == StreamOpCode.create) { if (mySerializer == null) mySerializer = myGetSerializerForTable(rows[0].schema); - - mySerializer.serialize (op, rows, out serializedData); - } - else - { - if (mySerializer != null) - { - mySerializer.serialize (op, rows, out serializedData); - } - else - { - serializedData = new byte[] {}; + + mySerializer.serialize(op, rows, out serializedData); + } else { + if (mySerializer != null) { + mySerializer.serialize(op, rows, out serializedData); + } else { + serializedData = new byte[] { }; } } } #endregion - private IBlueDataSerializer myGetSerializerForTable(IBlueTableSchema theSchema) - { + private IBlueDataSerializer myGetSerializerForTable(IBlueTableSchema theSchema) { + // TODOds + return new UniversalSerializer(theSchema); string serializerName = DataStreamGatewaySettings.instance.getSerializer(theSchema.tableName); object[] serializerArgs = DataStreamGatewaySettings.instance.getSerializerArgs(theSchema.tableName); - if(serializerArgs != null) + if (serializerArgs != null) return Activator.CreateInstance(Type.GetType(serializerName), theSchema, serializerArgs) as IBlueDataSerializer; else return Activator.CreateInstance(Type.GetType(serializerName), theSchema) as IBlueDataSerializer; - + } - + } } diff --git a/src/DataStreamSuite/DataStreamCore/Messaging/Publish/Serialization/UniversalSerializer.cs b/src/DataStreamSuite/DataStreamCore/Messaging/Publish/Serialization/UniversalSerializer.cs index cc499367e..23d803ce9 100644 --- a/src/DataStreamSuite/DataStreamCore/Messaging/Publish/Serialization/UniversalSerializer.cs +++ b/src/DataStreamSuite/DataStreamCore/Messaging/Publish/Serialization/UniversalSerializer.cs @@ -8,7 +8,7 @@ namespace BCG public class UniversalSerializer : BlueDataSerializer { public UniversalSerializer (IBlueTableSchema schema) - : base ("BCG.PubsubUniversalDeserializer, DataStreamGateway", "BCG.ClientUniversalDeserializer") + : base ("BCG.PubsubUniversalDeserializer, DataStreamCore", "BCG.ClientUniversalDeserializer") { myRowSerializer = new UniversalRowSerializer (schema); } diff --git a/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/Deserialization/PubsubDeserializer.cs b/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/Deserialization/PubsubDeserializer.cs index e2e0e0b2b..aafa8d573 100644 --- a/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/Deserialization/PubsubDeserializer.cs +++ b/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/Deserialization/PubsubDeserializer.cs @@ -7,10 +7,11 @@ using System.Text; namespace BCG { /// - /// Base class for all deserializers. + /// Base class for all pubsubs deserializers. /// public abstract class PubsubDeserializer : IPubsubDeserializer { + // this is where the stream state is stored in pubsub? protected IDictionary myState = new Hashtable (); protected IBlueTableSchema mySchema; protected string myClientDeserializerName; //for the getState method @@ -35,6 +36,7 @@ namespace BCG } catch (EndOfStreamException) { + // TODO, logically break loop break; } if (op == StreamOpCode.create) diff --git a/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/Deserialization/PubsubUniversalDeserializer.cs b/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/Deserialization/PubsubUniversalDeserializer.cs index c20947c14..2dc537d4e 100644 --- a/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/Deserialization/PubsubUniversalDeserializer.cs +++ b/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/Deserialization/PubsubUniversalDeserializer.cs @@ -2,24 +2,22 @@ using System; using System.Collections; using System.IO; -namespace BCG -{ - public class PubsubUniversalDeserializer : PubsubDeserializer - { - public PubsubUniversalDeserializer() {} +namespace BCG { + public class PubsubUniversalDeserializer : PubsubDeserializer { + public PubsubUniversalDeserializer() { + bts.Log.Gen.info("PubsubUniversalDeserializer.cons"); + } - protected override IDictionary myGetRows (BinaryReader br, byte op, int noOfRows) - { - myDeserializer = myDeserializer == null ? new PubsubUniversalRowDeserializer (mySchema) : myDeserializer; - Hashtable retVal = new Hashtable (); - for (int i = 0; i < noOfRows; i++) - { + protected override IDictionary myGetRows(BinaryReader br, byte op, int noOfRows) { + myDeserializer = myDeserializer == null ? new PubsubUniversalRowDeserializer(mySchema) : myDeserializer; + Hashtable retVal = new Hashtable(); + for (int i = 0; i < noOfRows; i++) { int id; object row; - myDeserializer.deserializeRow (myState, br, op, out id, out row); - retVal.Add (id, row); + myDeserializer.deserializeRow(myState, br, op, out id, out row); + retVal.Add(id, row); } return retVal; } - } + } } diff --git a/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/PubsubServerTagHandler.cs b/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/PubsubServerTagHandler.cs index e046be78e..bd95c94c4 100644 --- a/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/PubsubServerTagHandler.cs +++ b/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/PubsubServerTagHandler.cs @@ -1,54 +1,46 @@ using System; using System.IO; -using System.Collections; +using System.Collections.Generic; using BCG.Utilities; -namespace BCG -{ +namespace BCG { /// /// Keeps a collection of TagStateBanks to hold the state of each tag /// - public class PubsubServerTagHandler : IPubSubServerTagHandler - { - private IDictionary myTagStates = new Hashtable (); + public class PubsubServerTagHandler : IPubSubServerTagHandler { + private IDictionary _tag_state_map = new Dictionary(); private bool myPublishStatistics = //DataStreamGatewaySettings.instance.enablePubsubStats; false; - - public PubsubServerTagHandler() {} - + #region IPubSubServerTagHandler Members - public byte[] getContent (string tag) - { - TagStateBank stateBank = myTagStates[tag] as TagStateBank; - if (stateBank != null) - { - return stateBank.getConent (); - } - else - { - // BlueEventLog.logEvent (String.Format ("Information requested for tag {0} which does not exist now", tag), BlueEventLevel.Warning); - return new byte[] {}; + public byte[] getContent(string tag) { + TagStateBank tag_state_bank; + if (_tag_state_map.TryGetValue(tag, out tag_state_bank)) { + return tag_state_bank.getConent(); + } else { + bts.Log.Gen.warn("Information requested for tag: {tag}, it does not exist now"); + return new byte[] { }; } } - public void update (string tag, Stream[] messageBodies) - { - if (myPublishStatistics) - { + public void update(string tag, Stream[] messageBodies) { + bts.Log.Gen.info($"PubsubServerTagHandler.update tag: {tag} streams: {messageBodies.Length}"); + if (myPublishStatistics) { StatisticsLogger.instance.log(tag, messageBodies); } - if (!myTagStates.Contains (tag)) - { - // BlueEventLog.logEvent (String.Format ("Tag {0} received for the first time and its state created", tag), BlueEventLevel.Information); - myTagStates.Add (tag, new TagStateBank ()); + TagStateBank tag_state_bank; + if (!_tag_state_map.TryGetValue(tag, out tag_state_bank)) { + bts.Log.Gen.info("tag: {tag} received for the first time creating TagStateBank for it now"); + tag_state_bank = new TagStateBank(); + _tag_state_map.Add(tag, tag_state_bank); } - (myTagStates[tag] as TagStateBank).update (messageBodies); + tag_state_bank.update(messageBodies); } - public void doWork() {} + public void doWork() { } - public string[] supportedTagTypes {get {return new string [] {"DataStreamTag"};}} + public string[] supportedTagTypes { get { return new string[] { "DataStreamTag" }; } } public void register(InternalPublishDelegate callback) { } diff --git a/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/TagStateBank.cs b/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/TagStateBank.cs index 44ea024e9..62136ec34 100644 --- a/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/TagStateBank.cs +++ b/src/DataStreamSuite/DataStreamCore/Messaging/PubsubReceive/TagStateBank.cs @@ -1,26 +1,26 @@ using System; -using System.Collections; +using System.Collections.Generic; using System.IO; -namespace BCG -{ - public class TagStateBank - { - //private IDictionary myState = new Hashtable (); - private PubsubDeserializationController myDeserializationController = new PubsubDeserializationController (); - - public TagStateBank () {} +namespace BCG { + public class TagStateBank { + private PubsubDeserializationController myDeserializationController = new PubsubDeserializationController(); - public byte[] getConent () - { - return myDeserializationController.getState (); + public TagStateBank() { } + + public TagStateBank(string tag) { + bts.Log.Gen.info("TagStateBank created for tag: {tag}"); } - public void update (Stream[] streams) - { + public byte[] getConent() { + return myDeserializationController.getState(); + } + + public void update(Stream[] streams) { + bts.Log.Gen.info($"TagStateBank.update streams: {streams.Length}"); byte[] ops; Array rowCollection; - myDeserializationController.deserialize (streams, out ops, out rowCollection); + myDeserializationController.deserialize(streams, out ops, out rowCollection); } } } diff --git a/src/DataStreamSuite/DataStreamCore/Messaging/SpecializedSerialization/Compression/CompressionSerializer.cs b/src/DataStreamSuite/DataStreamCore/Messaging/SpecializedSerialization/Compression/CompressionSerializer.cs index 4b1c51b74..090263b2d 100644 --- a/src/DataStreamSuite/DataStreamCore/Messaging/SpecializedSerialization/Compression/CompressionSerializer.cs +++ b/src/DataStreamSuite/DataStreamCore/Messaging/SpecializedSerialization/Compression/CompressionSerializer.cs @@ -9,7 +9,7 @@ namespace BCG public class CompressionSerializer : IBlueDataSerializer { private IBlueRowSerializer myRowSerializer; - private string myPubsubDeserializerName = "BCG.CompressionPubsubDeserializer, DataStreamGateway", myClientDeserializerName = "BCG.CompressionClientDeserializer"; + private string myPubsubDeserializerName = "BCG.CompressionPubsubDeserializer, DataStreamCore", myClientDeserializerName = "BCG.CompressionClientDeserializer"; public CompressionSerializer (IBlueTableSchema schema) { diff --git a/src/DataStreamSuite/DataStreamCore/Messaging/stream_serialization.txt b/src/DataStreamSuite/DataStreamCore/Messaging/stream_serialization.txt new file mode 100644 index 000000000..b8a071a72 --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/Messaging/stream_serialization.txt @@ -0,0 +1,13 @@ +PubTableBroker allows publishing of streams through createTable, addRow, deleteRow, changeRow, makeBatchChanges -> +PubTablePublisher:IPubTablePublisher per tag (addRow, deleteRow, changeRow, createTable, batchRemoveAdd) +PubTablePublisher has BlueDataSerializationController:IBlueDataSerializer to serialize the rows to byte[] +BlueDataSerializationController uses a IBlueDataSerializer, which is specialized for the tag (using UniversalSerializer for now) + +IBlueDataSerializer has just one function +void serialize (byte op, IBlueRowCollection[] rows, out byte[] serializedData); +the second arg is IBlueRowCollection[] because in case of op = StreamOpCode.multipleRemoveAdd, it has multiple IBlueRowCollection for rem and add +(BlueDataSerializer.serialize's comment has the serialization layout) + +For each serializer we have 2 matching deserializers, IPubsubDeserializer (for PubSubservr) and IClientDeserializer (for clients subscribing to streams) +For UniversalSerializer, we have PubsubUniversalDeserializer and ClientUniversalDeserializer + diff --git a/src/DataStreamSuite/DataStreamPublisher/DataStreamPublisher.csproj b/src/DataStreamSuite/DataStreamPublisher/DataStreamPublisher.csproj index a46e05152..de755eb51 100644 --- a/src/DataStreamSuite/DataStreamPublisher/DataStreamPublisher.csproj +++ b/src/DataStreamSuite/DataStreamPublisher/DataStreamPublisher.csproj @@ -3,6 +3,8 @@ + + diff --git a/src/DataStreamSuite/DataStreamPublisher/Program.cs b/src/DataStreamSuite/DataStreamPublisher/Program.cs index 3119f167a..b5c21e183 100644 --- a/src/DataStreamSuite/DataStreamPublisher/Program.cs +++ b/src/DataStreamSuite/DataStreamPublisher/Program.cs @@ -12,7 +12,24 @@ Console.WriteLine("Starting DSPublisher..."); //TestPubliser.publish(); //TestPubliser.subscribe(); -TestPublisher ts = new TestPublisher(false); -ts.keep_publishing(TimeSpan.FromSeconds(10)); +// TestPublisher ts = new TestPublisher(false); +// ts.keep_publishing(TimeSpan.FromSeconds(10)); +_subscribe_to_stream(); + +void _subscribe_to_stream() { + BCG.ViewStream vs = new TableStream("time"); + vs.onChange += on_change; +} + +while (true) { + System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); + ViewStreamDatabase.instance.doWork(); +} + + +void on_change(object obj) { + DataPacket dp = obj as DataPacket; + dp.newRows.print_table((col) => { return 10; }); +} \ No newline at end of file diff --git a/src/DataStreamSuite/DataStreamQueryAnalyzer/DataStreamQueryAnalyzer.csproj b/src/DataStreamSuite/DataStreamQueryAnalyzer/DataStreamQueryAnalyzer.csproj index 5a92d30c8..1313974c6 100644 --- a/src/DataStreamSuite/DataStreamQueryAnalyzer/DataStreamQueryAnalyzer.csproj +++ b/src/DataStreamSuite/DataStreamQueryAnalyzer/DataStreamQueryAnalyzer.csproj @@ -12,6 +12,7 @@ + diff --git a/src/DataStreamSuite/DataStreamQueryAnalyzer/Program.cs b/src/DataStreamSuite/DataStreamQueryAnalyzer/Program.cs index 2e97ca116..98da544f3 100644 --- a/src/DataStreamSuite/DataStreamQueryAnalyzer/Program.cs +++ b/src/DataStreamSuite/DataStreamQueryAnalyzer/Program.cs @@ -1,14 +1,12 @@ -using System; -using System.Windows.Forms; using BCG; -using System.IO; -using System.Runtime.Serialization; -using System.Runtime.Serialization.Formatters.Binary; -using bts; -using mbts; -using mbts.config; + using bts.utils; +using mbts.config; + +using System; +using System.Windows.Forms; + static class Program { [STAThread] static void Main(string[] args) { @@ -17,8 +15,6 @@ static class Program { bts.Log.Gen.info($"Starting DSQA"); Console.WriteLine("Starting DataStreamQueryAnalyzer..."); Console.WriteLine("Starting Test Publisher"); - // TestPubliser.publish(); - // TestPubliser.subscribe(); Console.WriteLine("Launching Main Window"); MainForm mainForm = new MainForm(); if (args.Length > 0) { @@ -27,65 +23,3 @@ static class Program { Application.Run(mainForm); } } - -class TestPubItem : IFormatter, IPubItem -{ - public DateTime send_time = DateTime.Now; - public object Deserialize(Stream str) - { - BinaryReader br = new BinaryReader(str); - var d = br.ReadDouble(); - TestPubItem rv = new TestPubItem(); - rv.send_time = DateTime.FromOADate(d); - return rv; - } - public void Serialize(Stream str, Object obj) - { - TestPubItem tpi = obj as TestPubItem; - BinaryWriter bw = new BinaryWriter(str); - bw.Write(tpi.send_time.ToOADate()); - } - public System.Runtime.Serialization.SerializationBinder? Binder { get; set; } - - public System.Runtime.Serialization.StreamingContext Context { get; set; } - - public System.Runtime.Serialization.ISurrogateSelector? SurrogateSelector { get; set; } - - public string getTag() - { - return "Test_Tag"; - } -} - -class TestPubliser -{ - public static void publish() - { - int n_pubs = 1000; - Publisher publisher = new Publisher(); - for (int i = 0; i < n_pubs; i++) - { - TestPubItem pi = new TestPubItem(); - pi.send_time = DateTime.Now; - bts.Log.Gen.info($"DSQA publishing tag: {pi.getTag()}"); - publisher.publish(pi); - System.Threading.Thread.Sleep(100); - MasterWorker.getInstance(false).doWork(); - } - } - public static void subscribe() - { - ManagedSubscriber ms = new ManagedSubscriber(); - ms.subscribe("Test_Tag", new Handler(on_data)); - while (true) - { - ms.doWork(); - } - } - public static void on_data(IPubItem pi) - { - Console.WriteLine("Received data"); - } -} - - diff --git a/src/DataStreamSuite/PubSub/Client/TagType2HandlerMap.cs b/src/DataStreamSuite/PubSub/Client/TagType2HandlerMap.cs index d898016f5..b858e5dc8 100644 --- a/src/DataStreamSuite/PubSub/Client/TagType2HandlerMap.cs +++ b/src/DataStreamSuite/PubSub/Client/TagType2HandlerMap.cs @@ -3,37 +3,33 @@ using System.Collections; using System.Reflection; using BCG.Utilities; using System.Collections.Generic; +using System.Reactive.Subjects; -namespace BCG -{ - public class TagType2HandlerMap - { +namespace BCG { + public class TagType2HandlerMap { private static TagType2HandlerMap myServerHandlerMap = null, myPublisherStateMap = null; - private Dictionary myTagTypeToPubHandlerMap = new Dictionary(); + private Dictionary myTagTypeToPubHandlerMap = + new Dictionary(); - public TagType2HandlerMap() - { - if (PubSub3Config.instance.PS3_TagTypeToAssemblyMap != null) - { - foreach (TagTypeToAssembly.Entry entry in PubSub3Config.instance.PS3_TagTypeToAssemblyMap.entries) - { - try - { + public TagType2HandlerMap() { + if (PubSub3Config.instance.PS3_TagTypeToAssemblyMap != null) { + foreach (TagTypeToAssembly.Entry entry in PubSub3Config.instance.PS3_TagTypeToAssemblyMap.entries) { + try { + var ass_loc = assembly_location; + bts.Log.Gen.info($"TagType2HandlerMap entry: {entry} assembly_location: {ass_loc}"); Assembly assem = Assembly.LoadFrom(entry.assembly); - Type t = assem.GetType(entry.handlerClass, true, false); + Type t = assem.GetType(entry.handlerClass, true, true); object obj = Activator.CreateInstance(t); IPubSubServerTagHandler tagHandler = obj as IPubSubServerTagHandler; + bts.Log.Gen.info($"TagType2HandlerMap successfully created obj: {obj} of type: {t} from assem: {assem}"); string[] tagTypes = tagHandler.supportedTagTypes; - foreach (string tagType in tagTypes) - { + foreach (string tagType in tagTypes) { + bts.Log.Gen.info($"TagType2HandlerMap adding tagType: {tagType} tagHandler: {tagHandler}"); myTagTypeToPubHandlerMap[tagType] = tagHandler; } - } - catch (Exception ex) - { - // BlueEventLog.logEvent(string.Format("error happened while loading type from the assembly ", entry.handlerClass, entry.assembly), BlueEventLevel.Warning); - // BlueEventLog.logEvent(ex); + } catch (Exception ex) { + bts.Log.Gen.error($"TagType2HandlerMap threw exception ex: {ex}"); if (ex is OutOfMemoryException) throw ex; } @@ -41,30 +37,41 @@ namespace BCG } } - private IPubSubServerTagHandler myGetHandlerFromTagType(string tagType) - { + public static string assembly_location { + get { + // string codeBase = Assembly.GetExecutingAssembly().CodeBase; + // UriBuilder uri = new UriBuilder(codeBase); + // string path = Uri.UnescapeDataString(uri.Path); + // return Path.GetDirectoryName(path); + // Get the executing assembly + Assembly executingAssembly = Assembly.GetExecutingAssembly(); + + // Get the location (path) of the executing assembly + string assembly_location = executingAssembly.Location; + + return assembly_location; + } + } + + private IPubSubServerTagHandler myGetHandlerFromTagType(string tagType) { IPubSubServerTagHandler rv; myTagTypeToPubHandlerMap.TryGetValue(tagType, out rv); + bts.Log.Gen.info($"TagType2HandlerMap.myGetHandlerFromTagType tag_type: {tagType} rv: {rv}"); return rv; } - public static IPubSubServerTagHandler getHandler(string type, bool isServer) - { - lock (typeof(IPubSubServerTagHandler)) - { - if (isServer) - { + public static IPubSubServerTagHandler getHandler(string type, bool isServer) { + lock (typeof(IPubSubServerTagHandler)) { + if (isServer) { if (myServerHandlerMap == null) myServerHandlerMap = new TagType2HandlerMap(); return myServerHandlerMap.myGetHandlerFromTagType(type); - } - else - { + } else { if (myPublisherStateMap == null) myPublisherStateMap = new TagType2HandlerMap(); return myPublisherStateMap.myGetHandlerFromTagType(type); } } } - } + } } diff --git a/src/DataStreamSuite/PubSub/Client/TagTypeToAssembly.cs b/src/DataStreamSuite/PubSub/Client/TagTypeToAssembly.cs index 3dbbff2fc..56242ea27 100644 --- a/src/DataStreamSuite/PubSub/Client/TagTypeToAssembly.cs +++ b/src/DataStreamSuite/PubSub/Client/TagTypeToAssembly.cs @@ -1,17 +1,18 @@ using System; -namespace BCG -{ +namespace BCG { [Serializable] - public class TagTypeToAssembly - { + public class TagTypeToAssembly { //[Serializable] - public class Entry - { - public string handlerClass; - public string assembly; + public class Entry { + public string handlerClass = "BCG.PubSubServerTagHandler"; + // TODOds + public string assembly = "C:\\Users\\m_tah\\spark\\build\\Debug\\bin\\net7.0\\DataStreamCore.dll"; + public override string ToString() { + return $"handlerClass: {handlerClass} assembly: {assembly}"; + } } - public Entry[] entries; + public Entry[] entries = new Entry[] { new Entry() }; } } diff --git a/src/DataStreamSuite/PubSub/Config/PubSub3Config.cs b/src/DataStreamSuite/PubSub/Config/PubSub3Config.cs index 7d203e482..2333274ec 100644 --- a/src/DataStreamSuite/PubSub/Config/PubSub3Config.cs +++ b/src/DataStreamSuite/PubSub/Config/PubSub3Config.cs @@ -5,17 +5,13 @@ using System.Text.RegularExpressions; //using BCG.Utilities.BlueConfiguration; //using BCG.Utilities; -namespace BCG -{ +namespace BCG { public class PubSub3Config //: ConfigurationRequest { private static PubSub3Config myInstance; - public static PubSub3Config instance - { - get - { - if (myInstance == null) - { + public static PubSub3Config instance { + get { + if (myInstance == null) { myInstance = new PubSub3Config(); //myInstance = (PubSub3Config) ConfigurationRequester.request(typeof(PubSub3Config)); } @@ -43,7 +39,7 @@ namespace BCG private string[] myPS3_CrushableTags = new string[] { }; //moved from PS2 - private TagTypeToAssembly myTagTypeToAssemblyMap; + private TagTypeToAssembly myTagTypeToAssemblyMap = new TagTypeToAssembly(); private string myPS3_ServerQueue; //substation stuff @@ -83,122 +79,101 @@ namespace BCG // bluecomm transition //public ConfigKeys.BlueComm_PSTransition BlueComm_PSTransition { get; set; } - public PubSub3Config() - { + public PubSub3Config() { //BlueComm_PSTransition = new BCG.ConfigKeys.BlueComm_PSTransition(true); } - public bool isValid() - { + public bool isValid() { return true; } - public int PS3_retriesOnNoBufferSpace - { + public int PS3_retriesOnNoBufferSpace { get { return myPS3_retriesOnNoBufferSpace; } set { myPS3_retriesOnNoBufferSpace = value; } } - public int PS3_millisecondsBetweenNoBufferSpaceRetries - { + public int PS3_millisecondsBetweenNoBufferSpaceRetries { get { return myPS3_millisecondsBetweenNoBufferSpaceRetries; } set { myPS3_millisecondsBetweenNoBufferSpaceRetries = value; } } - public int PS3_millisecondsBetweenTimeSliceUpdates - { + public int PS3_millisecondsBetweenTimeSliceUpdates { get { return myPS3_millisecondsBetweenTimeSliceUpdates; } set { myPS3_millisecondsBetweenTimeSliceUpdates = value; } } - public bool BlueComm_mustDie - { + public bool BlueComm_mustDie { get { return myBlueComm_mustDie; } set { myBlueComm_mustDie = value; } } - public int PS3_HeartBeatInterval - { + public int PS3_HeartBeatInterval { get { return myPS3_HeartBeatInterval; } set { myPS3_HeartBeatInterval = value; } } - public int PS3_DoItAllSecondsBetweenLogs - { + public int PS3_DoItAllSecondsBetweenLogs { get { return myPS3_DoItAllSecondsBetweenLogs; } set { myPS3_DoItAllSecondsBetweenLogs = value; } } - public int PS3_MissingHeartBeatsForGC - { + public int PS3_MissingHeartBeatsForGC { get { return myPS3_MissingHeartBeatsBeforeGC; } set { myPS3_MissingHeartBeatsBeforeGC = value; } } - public bool PS3_UseManagedSockets - { + public bool PS3_UseManagedSockets { get { return myPS3_UseManagedSockets; } set { myPS3_UseManagedSockets = value; } } - public int PS3_StateUpdatePeriod - { + public int PS3_StateUpdatePeriod { get { return myPS3_StateUpdatePeriod; } set { myPS3_StateUpdatePeriod = value; } } - public int PS3_StatisticsUpdatePeriod - { + public int PS3_StatisticsUpdatePeriod { get { return myPS3_StatisticsUpdatePeriod; } set { myPS3_StatisticsUpdatePeriod = value; } } - public int PS3_DisposedConnectionCheckPeriod - { + public int PS3_DisposedConnectionCheckPeriod { get { return myPS3_DisposedConnectionCheckPeriod; } set { myPS3_DisposedConnectionCheckPeriod = value; } } - public int PS3_MessageProcessingWarningThreshold - { + public int PS3_MessageProcessingWarningThreshold { get { return myPS3_MessageProcessingWarningThreshold; } set { myPS3_MessageProcessingWarningThreshold = value; } } - public int PS3_millisecondsBetweenSubInfoUpdates - { + public int PS3_millisecondsBetweenSubInfoUpdates { get { return myPS3_millisecondsBetweenSubInfoUpdates; } set { myPS3_millisecondsBetweenSubInfoUpdates = value; } } - public double PS3_LatencyReportThresholdMS - { + public double PS3_LatencyReportThresholdMS { get { return myPS3_LatencyReportThresholdMS; } set { myPS3_LatencyReportThresholdMS = value; } } - public int PS3_ServerPort - { + public int PS3_ServerPort { get { return myPS3_ServerPort; } set { myPS3_ServerPort = value; } } - public PubSubRoute[] getSubRouting() - { return getRouting(); } + public PubSubRoute[] getSubRouting() { return getRouting(); } - public PubSubRoute[] getPubRouting() - { return getRouting(); } + public PubSubRoute[] getPubRouting() { return getRouting(); } - public PubSubRoute[] getRouting() - { + public PubSubRoute[] getRouting() { // TODO return new PubSubRoute[] { }; // return getRouting(SitesCore.getMySiteName()); } - public PubSubRoute[] getRouting(string forSite) - { + public PubSubRoute[] getRouting(string forSite) { // TODO return new PubSubRoute[] { }; // if (sites_layout != null) { @@ -214,14 +189,12 @@ namespace BCG return null; } - public List getAllPubSubs() - { + public List getAllPubSubs() { return new List(); //return getAllPubSubs(SitesCore.getMySiteName()); } - public List getAllPubSubs(string forSite) - { + public List getAllPubSubs(string forSite) { List result = new List(); foreach (PubSubRoute r in getRouting(forSite)) result.AddRange(r.getAllPubSubs()); @@ -233,17 +206,14 @@ namespace BCG // public ConfigKeys.sites_siteName sites_siteName { get; set; } - public TagTypeToAssembly PS3_TagTypeToAssemblyMap - { + public TagTypeToAssembly PS3_TagTypeToAssemblyMap { get { return myTagTypeToAssemblyMap; } set { myTagTypeToAssemblyMap = value; } } //[Description("PUBSUB: The Message queue to look for pub sub messages")] - public string PS3_ServerQueue - { - get - { + public string PS3_ServerQueue { + get { if (myPS3_ServerQueue == null || myPS3_ServerQueue == "") return "PubSubQueue"; else @@ -252,14 +222,12 @@ namespace BCG set { myPS3_ServerQueue = value; } } - public bool PS3_InternallyPublishSubInfo - { + public bool PS3_InternallyPublishSubInfo { get { return myPS3_InternallyPublishSubInfo; } set { myPS3_InternallyPublishSubInfo = value; } } - public string[] PS3_CrushableTags - { + public string[] PS3_CrushableTags { get { return myPS3_CrushableTags; } set { myPS3_CrushableTags = value; } } @@ -299,95 +267,80 @@ namespace BCG // } - public bool PS3_UseSubstations - { + public bool PS3_UseSubstations { get { return myPS3_UseSubstations; } set { myPS3_UseSubstations = value; } } - public PubSubPortRange PS3_PortRange - { + public PubSubPortRange PS3_PortRange { get { return myPS3_PortRange; } set { myPS3_PortRange = value; } } - public bool PS3_BindToRandomPort - { + public bool PS3_BindToRandomPort { get { return myPS3_BindToRandomPort; } set { myPS3_BindToRandomPort = value; } } - public bool PS3_IsSubstation - { + public bool PS3_IsSubstation { get { return myPS3_IsSubstation; } set { myPS3_IsSubstation = value; } } - public bool PS3_UseDynamicClients - { + public bool PS3_UseDynamicClients { get { return myPS3_UseDynamicClients; } set { myPS3_UseDynamicClients = value; } } - public bool PS3_UseManagedClientsInNativeCode - { + public bool PS3_UseManagedClientsInNativeCode { get { return myPS3_UseManagedClientsInNativeCode; } set { myPS3_UseManagedClientsInNativeCode = value; } } - public string PS3_CompressedTagsRegex - { + public string PS3_CompressedTagsRegex { get { return myPS3_CompressedTagsRegex; } set { myPS3_CompressedTagsRegex = value; } } - public string PS3_DebugTags - { + public string PS3_DebugTags { get { return myPS3_DebugTags; } set { myPS3_DebugTags = value; } } - public string PS3_TagsToReportCompressionRatio - { + public string PS3_TagsToReportCompressionRatio { get { return myPS3_TagsToReportCompressionRatio; } set { myPS3_TagsToReportCompressionRatio = value; } } - public bool PS3_DisableMSMQ - { + public bool PS3_DisableMSMQ { get { return myPS3_DisableMSMQ; } set { myPS3_DisableMSMQ = true; } } - public string PS3_IPTranslation - { + public string PS3_IPTranslation { get { return myPS3_IPTranslation; } set { myPS3_IPTranslation = value; } } - public long PS3_GCActivationThreshold - { + public long PS3_GCActivationThreshold { get { return myGCActivationThreshold; } set { myGCActivationThreshold = value; } } - public int PS3_WaitPeriodAfterGC - { + public int PS3_WaitPeriodAfterGC { get { return myWaitPeriodAfterGC; } set { myWaitPeriodAfterGC = value; } } - public bool PS3_UsePrepublishedServerState - { + public bool PS3_UsePrepublishedServerState { get { return myPS3_UsePrepublishedServerState; } set { myPS3_UsePrepublishedServerState = value; } } - public int PS3_MaxPacketSize - { + public int PS3_MaxPacketSize { get { return myPS3_MaxPacketSize; } set { myPS3_MaxPacketSize = value; } } @@ -404,21 +357,17 @@ namespace BCG // set { myRecovery_PubSubCopies = value; } // } - public bool isTagDebugTag(string tag) - { - if (myDebugTagsRegex == null) - { + public bool isTagDebugTag(string tag) { + if (myDebugTagsRegex == null) { myDebugTagsRegex = new Regex(myPS3_DebugTags, RegexOptions.IgnoreCase); } return myDebugTagsRegex.IsMatch(tag); } - public bool isTagCompressed(string tag) - { + public bool isTagCompressed(string tag) { - if (myCompressedTagsRegex == null) - { + if (myCompressedTagsRegex == null) { myCompressedTagsRegex = new Regex(myPS3_CompressedTagsRegex, RegexOptions.IgnoreCase); } @@ -426,10 +375,8 @@ namespace BCG return (myCompressedTagsRegex.IsMatch(tag)); } - public bool shouldReportTagsCompressionRatio(string tag) - { - if (myTagsToReportCompressionRatioRegex == null) - { + public bool shouldReportTagsCompressionRatio(string tag) { + if (myTagsToReportCompressionRatioRegex == null) { myTagsToReportCompressionRatioRegex = new Regex(myPS3_TagsToReportCompressionRatio, RegexOptions.IgnoreCase); } return (myTagsToReportCompressionRatioRegex.IsMatch(tag)); diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection2.cs b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection2.cs index cc71ad7f4..3048fa1a5 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection2.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection2.cs @@ -119,7 +119,7 @@ namespace BCG try { byte[] msg = await TCPSocketUtils.read_bytes_async(_nw_stream); - bts.Log.Gen.info($"AsyncTCPConnection2._keep_reading side: {_side} self: {get_self_address()} peer: {get_peer_address} msg len: {msg.Length}"); + bts.Log.Gen.info($"AsyncTCPConnection2._keep_reading side: {_side} self: {get_self_address()} peer: {get_peer_address()} msg len: {msg.Length}"); await _handle_msg(msg); } catch (Exception ex) @@ -204,7 +204,7 @@ namespace BCG public void send(byte[] data) { Task.Run(async () => await send_async(data)); - bts.Log.Gen.info($"AsyncTCPConnection2.send self: {get_self_address()} peer: {get_peer_address} msg len: {data.Length} sent successfully"); + bts.Log.Gen.info($"AsyncTCPConnection2.send self: {get_self_address()} peer: {get_peer_address()} msg len: {data.Length} sent successfully"); //_data_send_queue.enqueue(new QueuedMessage(data)); } diff --git a/src/DataStreamSuite/PubSub/PubSubMessage.cs b/src/DataStreamSuite/PubSub/PubSubMessage.cs index 3970f1cf4..db0aaa5a5 100644 --- a/src/DataStreamSuite/PubSub/PubSubMessage.cs +++ b/src/DataStreamSuite/PubSub/PubSubMessage.cs @@ -35,7 +35,7 @@ namespace BCG { } public override string ToString() { - return meta_data.ToString(); + return $"PubSubMessage meta:data: {meta_data.ToString()} body.Posiiton: {body.Position} body.Length: {body.Length}"; } } diff --git a/src/DataStreamSuite/PubSub/PublisherCore.cs b/src/DataStreamSuite/PubSub/PublisherCore.cs index 793a020d3..9eeb4f4e4 100644 --- a/src/DataStreamSuite/PubSub/PublisherCore.cs +++ b/src/DataStreamSuite/PubSub/PublisherCore.cs @@ -7,171 +7,67 @@ using System.Text.RegularExpressions; using BCG.Utilities; -namespace BCG -{ - public class PublisherCore : IPublisherCore - { - //private static BlueCommManaged.IPubSub myBCPS; - - // TODO: what is this? - // public static void setPubSub(BlueCommManaged.IPubSub ps) - // { - // lock(typeof(PublisherCore)) - // { - // BlueEventLog2.instance().assert(myBCPS == null, - // "PublisherCore.setPubSub() should be called only once and before any use of PublisherCore"); - // myBCPS = ps; - // } - // } +namespace BCG { + public class PublisherCore : IPublisherCore { public static string pubSubBlueCommPrefix = "ps/"; public static string pubSubBlueCommTransactionPrefix = "tgps/"; public static string pubSubBlueCommTransactionComponentPrefix = "tcps/"; - // public static BlueCommManaged.IPubSub getPubSub() - // { - // lock (typeof(PublisherCore)) { - // return myBCPS; - // } - // } - enum Destination - { + enum Destination { None, BlueComm, PubSub, Both } - //private IPublisherCore myBlueCommPublisher = null; - private IPublisherCore myPubSubPublisher = null; - private Dictionary myPubDestinations = - new Dictionary(); + private IPublisherCore _publisher_core = null; - public PublisherCore() - { - myPubSubPublisher = new StaticPublisherCore(); - //myBlueCommPublisher = new BlueCommPublisherCore(getPubSub()); + public PublisherCore() { + _publisher_core = new StaticPublisherCore(); } - public PublisherCore(IPubSubRouter theRouter) - { - myPubSubPublisher = new StaticPublisherCore(theRouter); - //myBlueCommPublisher = new BlueCommPublisherCore(getPubSub()); + public PublisherCore(IPubSubRouter theRouter) { + _publisher_core = new StaticPublisherCore(theRouter); } - public PublisherCore(string URL) - { - myPubSubPublisher = new StaticPublisherCore(URL); - //myBlueCommPublisher = new BlueCommPublisherCore(getPubSub()); + public PublisherCore(string URL) { + _publisher_core = new StaticPublisherCore(URL); } #region IPublisherCore Methods - public void Dispose() - { - myPubSubPublisher.Dispose(); - //myBlueCommPublisher.Dispose(); + public void Dispose() { + _publisher_core.Dispose(); } - public int getNumberOfWaitingMessages() { return myPubSubPublisher.getNumberOfWaitingMessages(); } + public int getNumberOfWaitingMessages() { return _publisher_core.getNumberOfWaitingMessages(); } - private Destination myGetPublishDestionation (string tag) - { - return Destination.PubSub; - // lock (this) - // { - // if (PubSub3Config.instance.BlueComm_mustDie) - // return Destination.PubSub; - // else - // { - // Destination d; - // return Destination.PubSub; - // if (myPubDestinations.TryGetValue(tag, out d)) - // return d; - // else - // { - // foreach (string regexp in PubSub3Config.instance.BlueComm_PSTransition.tagsToPublishInPubSub) - // if (Regex.IsMatch(tag, regexp)) - // { - // d = Destination.PubSub; - // foreach (string regexp2 in PubSub3Config.instance.BlueComm_PSTransition.exceptionsToTagsToPublishInPubSub) - // if (Regex.IsMatch(tag, regexp2)) - // { - // d = Destination.None; - // break; - // } - // break; - // } - - // if (!tag.StartsWith("mon")) - // foreach (string regexp in PubSub3Config.instance.BlueComm_PSTransition.tagsToPublishInBlueComm) - // if (Regex.IsMatch(tag, regexp)) - // { - // if (d == Destination.PubSub) - // d = Destination.Both; - // else - // d = Destination.BlueComm; - // foreach (string regexp2 in PubSub3Config.instance.BlueComm_PSTransition.exceptionsToTagsToPublishInBlueComm) - // if (Regex.IsMatch(tag, regexp2)) - // { - // if (d == Destination.Both) - // d = Destination.PubSub; - // else if (d == Destination.BlueComm) - // d = Destination.None; - // break; - // } - // break; - // } - - // myPubDestinations[tag] = d; - // return d; - // } - // } - // } - } - - public void publish(string tag, Stream content) - { + public void publish(string tag, Stream content) { publish(tag, "", content); } - public void publish(string tagType, string tag, Stream content) - { - Destination d = myGetPublishDestionation(tag); - if (d == Destination.PubSub || d == Destination.Both) - myPubSubPublisher.publish(tagType, tag, content); - if (d == Destination.BlueComm || d == Destination.Both){} - //myBlueCommPublisher.publish(tagType, tag, content); + public void publish(string tagType, string tag, Stream content) { + bts.Log.Gen.info($"PublisherCore.publish tag: {tag} stream.Length: {content.Length} stream.Position: {content.Position}"); + _publisher_core.publish(tagType, tag, content); } - public void beginTransaction() - { - myPubSubPublisher.beginTransaction(); - //myBlueCommPublisher.beginTransaction(); + public void beginTransaction() { + _publisher_core.beginTransaction(); } - public void commitTransaction() - { - myPubSubPublisher.commitTransaction(); - //myBlueCommPublisher.commitTransaction(); + public void commitTransaction() { + _publisher_core.commitTransaction(); } - public void abortTransaction() - { - myPubSubPublisher.abortTransaction(); - //myBlueCommPublisher.abortTransaction(); + public void abortTransaction() { + _publisher_core.abortTransaction(); } - public void clearState(string tagToClear) - { - Destination d = myGetPublishDestionation(tagToClear); - if (d == Destination.PubSub || d == Destination.Both) - myPubSubPublisher.clearState(tagToClear); - if (d == Destination.BlueComm || d == Destination.Both){} - //myBlueCommPublisher.clearState(tagToClear); + public void clearState(string tagToClear) { + _publisher_core.clearState(tagToClear); } - public bool inTransaction - { get { return myPubSubPublisher.inTransaction; } } + public bool inTransaction { get { return _publisher_core.inTransaction; } } #endregion diff --git a/src/DataStreamSuite/PubSub/PubsubDeserializationController.cs b/src/DataStreamSuite/PubSub/PubsubDeserializationController.cs index 89e405fbf..93aecd09b 100644 --- a/src/DataStreamSuite/PubSub/PubsubDeserializationController.cs +++ b/src/DataStreamSuite/PubSub/PubsubDeserializationController.cs @@ -3,27 +3,25 @@ using System.IO; using System.Collections; using BCG.Utilities; -namespace BCG -{ +namespace BCG { /// /// Controls (ie creates dynamically) an IPubsubDeserializer based on the type name which is /// part of the published data. Since its also a IPubsubDeserializer, all subsequent calls /// are passed though to the interface /// - public class PubsubDeserializationController : IPubsubDeserializer - { + public class PubsubDeserializationController : IPubsubDeserializer { private IPubsubDeserializer myDeserializer; public PubsubDeserializationController() { } - public void update(Stream stream) - { + public void update(Stream stream) { + bts.Log.Gen.info("PubsubDeserializationController.update stream: {stream.Position} / {stream.Length}"); byte[] ops; Array rows; deserialize(new Stream[] { stream }, out ops, out rows); } - public void update(byte[] bytes) - { + public void update(byte[] bytes) { + bts.Log.Gen.info("PubsubDeserializationController.update bytes: {bytes.Length}"); byte[] ops; Array rows; deserialize(new Stream[] { new MemoryStream(bytes, 0, bytes.Length, false, true) }, out ops, out rows); @@ -31,39 +29,36 @@ namespace BCG #region IPubsubDeserializer Members - public void deserialize(Stream[] streams, out byte[] theOps, out Array theRows) - { - if (myDeserializer != null) - { + public void deserialize(Stream[] streams, out byte[] theOps, out Array theRows) { + + if (myDeserializer != null) { myDeserializer.deserialize(streams, out theOps, out theRows); - } - else - { + bts.Log.Gen.info($"PubsubDeserializationController.deserialize myDeserializer: {myDeserializer} ops: {theOps.Length} theRows: {theRows.Length}"); + } else { Stream totalStream = UtilityMethods.getAggregatedStream(streams); long initialPos = totalStream.Position; BinaryReader br = new BinaryReader(totalStream); byte opCode = br.ReadByte(); - if (opCode == StreamOpCode.create) - { + bts.Log.Gen.info($"PubsubDeserializationController.deserialize opCode: {opCode}"); + if (opCode == StreamOpCode.create) { string deserName = br.ReadString(); - if (deserName == "BCG.PubsubUniversalDeserializer") // so we can play old replay files which would not include the assembly name - deserName = "BCG.PubsubUniversalDeserializer, DataStreamGateway"; - myDeserializer = Activator.CreateInstance(Type.GetType(deserName)) - as IPubsubDeserializer; + // TODOds + if (deserName == "BCG.PubsubUniversalDeserializer, DataStreamGateway") // so we can play old replay files which would not include the assembly name + deserName = "BCG.PubsubUniversalDeserializer, DataStreamCore"; + myDeserializer = Activator.CreateInstance(Type.GetType(deserName)) as IPubsubDeserializer; totalStream.Position = initialPos; myDeserializer.deserialize(new Stream[] { totalStream }, out theOps, out theRows); - } - else - { - // BlueEventLog.logEvent("Received a non create before a create. Bad op code was " + opCode.ToString(), BlueEventLevel.Warning); + bts.Log.Gen.info($"PubsubDeserializationController.deserialize myDeserializer: {myDeserializer} ops: {theOps.Length} theRows: {theRows.Length}"); + + } else { + bts.Log.Gen.error($"Received a non create before a create. Bad op code was : {opCode} "); theOps = new byte[] { }; theRows = new object[] { }; } } } - public byte[] getState() - { + public byte[] getState() { return myDeserializer == null ? new byte[] { } : myDeserializer.getState(); } diff --git a/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs b/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs index 7bf3dd9e2..35460d027 100644 --- a/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs +++ b/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs @@ -71,6 +71,7 @@ namespace BCG { } + // all publishes to the pubsub end here private void myHandlePub(string sender, Stream msg_str, MetaData meta_data) { bts.Log.Gen.info($"PubSubMessageReactor.myHandlePub sender: {sender} metadata: {meta_data} str.pos: {msg_str.Position} str.len: {msg_str.Length}"); mySingleTagTimeSliceTimer.start(); @@ -83,7 +84,6 @@ namespace BCG { var subs_for_tag = myState.getSubscriberConnectionsForTag(pub.tag); bts.Log.Gen.info($"PubSubMessageReactor.myHandlePub {subs_for_tag.Count} subs for tag: {pub.tag}"); - ///for all the subscribers, sends the messages over straightaway foreach (string address in myState.getSubscriberConnectionsForTag(pub.tag)) { ///the position is adjusted to zero, so as to include @@ -93,8 +93,6 @@ namespace BCG { myMessageHub.send(address, msg_str); } - - //reset the position of the message so we can decompress it appropriately msg_str.Position = 0; msg_str.Position = meta_data.size(); @@ -104,12 +102,11 @@ namespace BCG { long compressedSize = msg_str.Length - mdLength; if (compressedSize == 0) { string errorMsg = "Error: tag " + pub.tag + " had compressed size of 0 bytes. This shouldnt' happen. Not decompressing tag or sending out update."; - //BlueEventLog.logEvent(errorMsg, BlueEventLevel.Error); + bts.Log.Gen.error(errorMsg); return; } msg_str = StreamDecompressor.decompressStream(msg_str); long decompressedSize = msg_str.Length - mdLength; - if (decompressedSize != pub.decompressedSize || compressedSize != pub.size) { string compressionWarning = string.Format("Warning: Stream for tag {0} decompressed from {1} to {2} bytes, with {3} byes of metadata. Metadata expected {4} to {5}. Tag state not updated.", pub.tag, @@ -137,15 +134,11 @@ namespace BCG { //BlueEventLog.logEvent(compressionMessage, PS3LogLevels.compressionRatio); } } - - ///updating state msg_str.Position = 0; msg_str.Position += meta_data.size(); myState.updatePubData(pub, msg_str); - mySingleTagTimeSliceTimer.stop(); - double thisTagProcessingTime = mySingleTagTimeSliceTimer.lastIntervalTime; if (onSingleTagPub != null) onSingleTagPub(pub.tag, thisTagProcessingTime); diff --git a/src/DataStreamSuite/PubSub/Server/PubSubState.cs b/src/DataStreamSuite/PubSub/Server/PubSubState.cs index a30f16aac..382d093cd 100644 --- a/src/DataStreamSuite/PubSub/Server/PubSubState.cs +++ b/src/DataStreamSuite/PubSub/Server/PubSubState.cs @@ -115,8 +115,8 @@ namespace BCG { /// /// public void updatePubData(PubMetaData pub, Stream msg) { - int sz = (int)(msg.Length - msg.Position); + bts.Log.Gen.info($"PubSubData.updatePubData pub: msg : {msg.Position} / {msg.Length} sz: {sz}"); ///this is the only place where SingleTagData should be created if (!myTagToData.ContainsKey(pub.tag)) { ///first time data is published for this tag diff --git a/src/DataStreamSuite/PubSub/Server/SingleTagData.cs b/src/DataStreamSuite/PubSub/Server/SingleTagData.cs index 5edf54946..0a0bad199 100644 --- a/src/DataStreamSuite/PubSub/Server/SingleTagData.cs +++ b/src/DataStreamSuite/PubSub/Server/SingleTagData.cs @@ -26,6 +26,7 @@ namespace BCG { else if (myTagType == SystemStatsPublisher.tagType) myHandler = SystemStatsPublisher.getInstance(); else + // this is where it gets the stream handler myHandler = TagType2HandlerMap.getHandler(myTagType, true); if (myHandler != null) myHandler.register(new InternalPublishDelegate(myHandleHandlerCallbacks)); @@ -36,7 +37,8 @@ namespace BCG { public string tagType { get { return myTagType; } } internal void pubUpdate(Stream stream) { - bts.Log.Gen.info($"SingleTagData.pubUpdate tag: {myTag} handler: {myHandler} stream len: {stream.Length - stream.Position}"); + string handler_str = myHandler != null ? myHandler.GetType().ToString() : "NULL"; + bts.Log.Gen.info($"SingleTagData.pubUpdate tag: {myTag} handler: {handler_str} stream: {stream.Position} / {stream.Length}"); if (myHandler != null) { try { myHandler.update(myTag, new Stream[] { stream }); diff --git a/src/DataStreamSuite/PubSub/StaticPublisherCore.cs b/src/DataStreamSuite/PubSub/StaticPublisherCore.cs index 5c8713f6e..06f8e6d83 100644 --- a/src/DataStreamSuite/PubSub/StaticPublisherCore.cs +++ b/src/DataStreamSuite/PubSub/StaticPublisherCore.cs @@ -116,6 +116,9 @@ namespace BCG { tag = theTag; data = theData; } + public override string ToString() { + return $"PublisherCoreMessage tag: {tag} data.Position: {data.Position} data.Length: {data.Length}"; + } } // this class uses an internal queue + thread to serialize and process calls @@ -177,7 +180,9 @@ namespace BCG { public void publish(string tag, Stream content) { publish("", tag, content); } public void publish(string tagType, string tag, Stream content) { - myMainQueue.enqueue(new PublisherCoreMessage(tagType, tag, content)); + PublisherCoreMessage msg = new PublisherCoreMessage(tagType, tag, content); + bts.Log.Gen.info($"StaticPublisherCore.publish msg: {msg}"); + myMainQueue.enqueue(msg); } public void beginTransaction() { @@ -210,6 +215,7 @@ namespace BCG { if (data != null) { int qc = myMainQueue.getCount(); PublisherCoreMessage msg = data as PublisherCoreMessage; + bts.Log.Gen.info($"StaticPublisherCore.myMessagePump msg: {msg}"); if (msg.msgType == PublisherCoreMessageType.transactionBegin) { myBeginTransaction(); } else if (msg.msgType == PublisherCoreMessageType.transactionCommit) { @@ -228,7 +234,7 @@ namespace BCG { } } } catch (Exception ex) { - // BlueEventLog.logEvent(ex); + bts.Log.Gen.error($"StaticPublisherCore.myMessagePump exception ex: {ex}"); if (ex is OutOfMemoryException) throw ex; } @@ -330,7 +336,7 @@ namespace BCG { // this is where the data is actually sent out ! var ps_msg = new PubSubMessage(new MetaData(tMd), transactionData); Console.WriteLine($"serverConn.send metadata: {ps_msg.meta_data}"); - bts.Log.Gen.info($"serverConn.send metadata: {ps_msg.meta_data}"); + bts.Log.Gen.info($"StaticPublisherCore.send ps_msg: {ps_msg}"); transactionSender.send(ps_msg); } } @@ -339,6 +345,7 @@ namespace BCG { } protected virtual void myPublish(string tagType, string tag, Stream content) { + bts.Log.Gen.info($"StaticPublisherCore.myPublish tag: {tag} content: {content.Position} / {content.Length} myInTransaction: {myInTransaction}"); if (myInTransaction) { ///if its in transaction message is cached, and is sent ///only when theres a commit or abort @@ -346,14 +353,13 @@ namespace BCG { myWaitingInTransactionMessages.Add(tag, new TagWaitingInTransaction(tagType, tag)); myWaitingInTransactionMessages[tag].add_pub(content); } else { - //if (CrushedTagManager.instance.isTagCrushable(tag)) { myState.publish(tag, tagType, content); if (!myCrushableTagsWaitingOnAck.ContainsKey(tag)) { - // BlueEventLog.logEvent("Sending crushable tag " + tag, BlueEventLevel.Diagnosis); + bts.Log.Gen.info("Sending crushable tag: {tag}"); mySendData(tag, tagType, content); } else { - // BlueEventLog.logEvent("Could not send tag " + tag + " since its waiting on an ack", BlueEventLevel.Diagnosis); + bts.Log.Gen.info("Could not send tag: {tag} since its waiting on an ack"); myCrushableTagsWaitingOnAck[tag].second++; } } @@ -369,6 +375,7 @@ namespace BCG { // add header, possibly compress the data, and sendto appropriate pubsub private void mySendData(string tag, string tagType, Stream content) { + bts.Log.Gen.info($"StaticPublisherCore.mySendData tag: {tag} tagType: {tagType}"); long initPos = content.Position; // locate right pubsub for this tag IPubSubServerConnection serverConn = myGetOrBuildSender(tag); @@ -396,14 +403,14 @@ namespace BCG { PubSubMessage ps_msg = new PubSubMessage(new MetaData(pub_md), contentToPublish); //Console.WriteLine($"serverConn.send_with_confirmation metadata: {ps_msg.meta_data}"); bts.Log.Gen.info($"serverConn.send_with_confirmation metadata: {ps_msg.meta_data}"); + bts.Log.Gen.info($"StaticPublisherCore.send ps_msg: {ps_msg}"); serverConn.send_with_confirmation(ps_msg, ref refId); myCrushableTagsWaitingOnAck.Add(tag, new Pair(refId, 0)); } else { //Console.WriteLine("StaticPublisherCore.mySend serverConn.send tag: " + tag + " server: " + serverConn.serverURL); // this is where the data is actually sent out var ps_msg = new PubSubMessage(pub_md, contentToPublish); - bts.Log.Gen.info($"serverConn.send metadata: {ps_msg}"); - Console.WriteLine($"serverConn.send metadata: {ps_msg}"); + bts.Log.Gen.info($"StaticPublisherCore.send ps_msg: {ps_msg}"); serverConn.send(ps_msg); } } diff --git a/src/DataStreamSuite/PubSub/Testing/TestPublisher.cs b/src/DataStreamSuite/PubSub/Testing/TestPublisher.cs index f7ee5edf4..76dd3cf9f 100644 --- a/src/DataStreamSuite/PubSub/Testing/TestPublisher.cs +++ b/src/DataStreamSuite/PubSub/Testing/TestPublisher.cs @@ -41,7 +41,9 @@ namespace BCG { MemoryStream ms = new MemoryStream(); pi.to_stream(ms); ms.Position = 0; + pcore.beginTransaction(); pcore.publish(TestPubItem.test_tag, ms); + pcore.commitTransaction(); if (!_hit_key_to_send) System.Threading.Thread.Sleep(wait_per); } diff --git a/src/DataStreamSuite/PubSub/Testing/TestSubscriber.cs b/src/DataStreamSuite/PubSub/Testing/TestSubscriber.cs index 2aa4d6781..5a7764e1e 100644 --- a/src/DataStreamSuite/PubSub/Testing/TestSubscriber.cs +++ b/src/DataStreamSuite/PubSub/Testing/TestSubscriber.cs @@ -6,30 +6,23 @@ using System; using System.IO; using System.Runtime.Serialization; -namespace BCG -{ +namespace BCG { - public class TestSubscriber - { + public class TestSubscriber { SubscriberCore _sub_core = new SubscriberCore(); - public TestSubscriber() - { + public TestSubscriber() { bts.Log.Gen.info("Started TestSubscriber"); } - public void keep_polling(TimeSpan wait_per, int n_polls = System.Int32.MaxValue) - { + public void keep_polling(TimeSpan wait_per, int n_polls = System.Int32.MaxValue) { _sub_core.subscribe(TestPubItem.test_tag); - for (int i = 0; i < n_polls; i++) - { + for (int i = 0; i < n_polls; i++) { var list = _sub_core.getUpdates(); - if (list.Count > 0) - { + if (list.Count > 0) { Console.WriteLine($"Received {list.Count} items"); } - foreach (var p in list) - { - Console.WriteLine($"Received item: {p}"); + foreach (var p in list) { + Console.WriteLine($"Received item: {p} iter: {i}"); } System.Threading.Thread.Sleep(wait_per); }