Files
spark/src/DataStreamSuite/DataStreamCore/BlueData/BlueRow.cs
T
John Colvin 10bb90c731 fix: Use IDictionary.GetEnumerator explicitly to iterate keyvals
We really need to replace these old non-generic collections with generic ones.
BlueRow's IDictionaryEnumerator implementation iterates over values when passed
to a foreach loop, not key value pairs.
2025-11-19 23:55:35 -05:00

326 lines
11 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Linq;
//using BCG.Utilities;
using System.Text;
namespace BCG
{
/// <summary>
/// BlueRow is an analog of DataRow, an immutable one though. It constructs itself either
/// from dotNet DataRow or IBlueRow. One row can belong to any number of tables, 0 or 1 or many.
/// All what the BlueRow requires is a schema to manage its internal information
/// </summary>
[Serializable]
public class BlueRow : IBlueRow
{
protected Object[] myValues;
protected IBlueTableSchema mySchema;
#region Private helper methods
/// <summary>
/// Initializes the internal data structures
/// </summary>
/// <param name="schema"></param>
private void myInit(IBlueTableSchema schema)
{
mySchema = schema;
myValues = new Object[schema.columnNames.Length];
}
#endregion
#region Constructors
protected BlueRow() { }
public BlueRow(IBlueRow r)
{
myInit(r.schema);
foreach (ColumnInfo ci in mySchema.columnInfo)
myValues[ci.colNo] = r[ci.colNo];
}
/// <summary>
/// The BlueRow initializes itself from the row passed. The row is expected
/// to have the same schema (including the col ordering) as the row being created
/// </summary>
/// <param name="table"></param>
/// <param name="row"></param>
public BlueRow(IBlueTableSchema schema, IBlueRow row)
{
myInit(schema);
foreach (ColumnInfo ci in mySchema.columnInfo)
myValues[ci.colNo] = row[ci.colNo];
}
/// <summary>
/// Constructs itself from the schema and the array of values that correspond
/// to the rows
/// </summary>
/// <param name="schema">
/// Schema of the row
/// </param>
/// <param name="values">
/// Values represent the values in the row, the order should be the same as
/// represented in the schema
/// </param>
public BlueRow(IBlueTableSchema schema, params Object[] values)
{
myValues = values;
mySchema = schema;
}
/// <summary>
/// Initilizes itself from a dotNet DataRow. The row is expected to
/// have the same schema (including the column ordering) as the row being created
/// </summary>
/// <param name="table"></param>
/// <param name="row"></param>
public BlueRow(IBlueTableSchema schema, DataRow row)
{
myInit(schema);
foreach (DataColumn col in row.Table.Columns)
myValues[col.Ordinal] = row[col.Ordinal];
}
public BlueRow(IBlueTableSchema schema, IDictionary row)
{
myInit(schema);
// Use GetEnumerator instead of a foreach loop because the latter calls
// '((IEnumerator)row).GetEnumerator()', which iterates over values
// instead of key value pairs.
var en = row.GetEnumerator();
while (en.MoveNext()) {
var de = en.Entry;
if (mySchema[de.Key as string] is not null) {
myValues[mySchema[de.Key as string].colNo] = de.Value;
}
}
}
/// <summary>
/// Constructs a new BlueRow by extracting the values fields enumerated
/// in schema from the POD (Plain Old Data) object passed. Obviously
/// the object's type must contain at least all the fields defined
/// by the schema and the types of these fields should match their counterparts.
/// See new BlueTableSchema(string tableName, Type).
/// This is a static function rather than a constructor because
/// the type signature of the constructor would not be unique (it would
/// conflict with several of the constructurs above)
/// </summary>
/// <param name="schema">The schema of the row</param>
/// <param name="id">The id of the row</param>
/// <param name="pod">The Plain Old Data object containing the data</param>
static public IBlueRow fromPOD(IBlueTableSchema schema, int id, Object pod)
{ return myBlueRowFromPOD(myGetAccessors(schema, pod), schema, id, pod); }
private static Dictionary<string, FieldInfo> myGetAccessors(IBlueTableSchema schema, object o)
{
var accessors = new Dictionary<string, FieldInfo>();
foreach (string fn in schema.columnNames)
if (fn.ToLower() != "id")
{
FieldInfo fi = o.GetType().GetField(fn);
if (fi != null)
accessors[fn] = fi;
}
return accessors;
}
private static IBlueRow myBlueRowFromPOD(Dictionary<string, FieldInfo> accessors,
IBlueTableSchema schema,
int id,
object pod)
{
MutableBlueRow r = new MutableBlueRow(schema);
foreach (var kvp in accessors)
{
object v = kvp.Value.GetValue(pod);
if (v == null)
v = DBNull.Value;
r.setValue(kvp.Key, v);
}
r.setValue("id", id);
return r;
}
public delegate int IdGenerator(object pod);
static public IBlueRowCollection fromPODCollection(IBlueTableSchema schema, IdGenerator g, IEnumerable pods)
{
Dictionary<string, FieldInfo> accessors = null;
BlueTable result = new BlueTable(schema);
foreach (Object o in pods)
{
if (accessors == null)
accessors = myGetAccessors(schema, o);
result.addRow(myBlueRowFromPOD(accessors, schema, g(o), o));
}
return result;
}
private static void myPodFromBlueRow(Dictionary<string, FieldInfo> accessors,
IBlueRow r, object pod)
{
foreach (var kvp in accessors)
if (!(r[kvp.Key] is DBNull))
kvp.Value.SetValue(pod, r[kvp.Key]);
}
/// <summary>
/// Copies a blue row to a POD (Plain Old Data) object.
/// </summary>
/// <param name="toObject"></param>
public static T copyToPOD<T>(IBlueRow r) where T : new()
{
T pod = new T();
myPodFromBlueRow(myGetAccessors(r.schema, pod), r, pod);
return pod;
}
public static List<T> toPODCollection<T>(IBlueRowCollection tb) where T : new()
{
List<T> result = new List<T>();
Dictionary<string, FieldInfo> accessors = null;
foreach (IBlueRow r in tb)
{
T pod = new T();
if (accessors == null)
accessors = myGetAccessors(tb.schema, pod);
myPodFromBlueRow(accessors, r, pod);
result.Add(pod);
}
return result;
}
// For viewing BlueRow in the debugger more easily
public override string ToString()
{
return this.ToString(true);
}
public string ToString(bool names)
{
StringBuilder sb = new StringBuilder();
bool first = true;
for (int x = 0; x < this.myValues.Length; x++)
{
if (!first) sb.Append(", ");
first = false;
if (names)
{
sb.Append(this.mySchema.columnNames[x]);
sb.Append("=");
}
sb.Append((this.myValues[x] ?? "(null)").ToString());
}
return sb.ToString();
}
/// <summary>
/// Constructs the schema and then populates the values, both from the IDictionary.
/// The table name (the only thing that can not be derived from the dictionary) is
/// also provided
/// </summary>
/// <param name="tableName"></param>
/// <param name="row"></param>
public BlueRow(string tableName, IDictionary row)
{
if (row is IBlueRow)
myInit(((IBlueRow)row).schema);
else
{
ColumnInfo[] cis = new ColumnInfo[row.Count];
int i = 0;
foreach (DictionaryEntry de in row)
cis[i] = new ColumnInfo(de.Key as string, i++, de.Value.GetType());
myInit(new BlueTableSchema(cis, tableName));
}
foreach (DictionaryEntry de in row)
myValues[mySchema[de.Key as string].colNo] = de.Value;
}
/// <summary>
/// Constructs the schema and then populates the values, both from the IDictionary.
/// </summary>
/// <param name="row"></param>
public BlueRow(IDictionary row) : this("", row) { }
#endregion
#region IBlueRow members
public virtual IBlueTableSchema schema { get { return mySchema; } }
public virtual object this[string colName]
{
get
{
ColumnInfo ci = mySchema[colName];
if (ci != null)
return myValues[ci.colNo];
else
throw new Exception(colName + " is not a valid column name for " + schema.tableName);
}
}
public virtual object this[int index] { get { return myValues[index]; } }
#endregion
#region ICollection members
public virtual int Count { get { return myValues.Length; } }
public virtual bool IsSynchronized { get { return false; } }
public virtual object SyncRoot { get { return this; } }
public virtual void CopyTo(Array array, int index) { myValues.CopyTo(array, index); }
#endregion
#region IDictionary members
public virtual bool IsFixedSize { get { return true; } }
virtual public bool IsReadOnly { get { return true; } }
/// <summary>
/// The key can either be the string corrsponding to the column name or
/// the order of the column
/// </summary>
public virtual object this[object key]
{
get
{
if (key is string)
return this[(string)key];
else if (key is int)
return this[(int)key];
throw new BlueDataException("Wrong type while indexing the row");
}
set { throw new NotSupportedException("The IBlueRow is readonly"); }
}
public virtual ICollection Keys { get { return mySchema.columnNames; } }
public virtual ICollection Values { get { return myValues; } }
public virtual void Add(object key, object value) { throw new NotSupportedException("The IBlueRow is readonly"); }
public virtual void Clear() { throw new NotSupportedException("The IBlueRow is readonly"); }
public virtual bool Contains(object key) { return mySchema[(string)key] != null; }
/// <summary>
/// Note, the default enumerator is the IDictionary's enumerator, so if user needs to
/// iterate through the values, typecast this into an IEnumerable
/// </summary>
/// <returns></returns>
public IDictionaryEnumerator GetEnumerator() { return new BlueRowDictionaryEnumerator(this); }
IEnumerator IEnumerable.GetEnumerator() { return new BlueRowEnumerator(this); }
public virtual void Remove(object key) { throw new NotSupportedException("The IBlueRow is readonly"); }
#endregion
}
}