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