Merging changes

This commit is contained in:
Taha committed 2023-09-04 16:25:40 -04:00
1 parent c4216d2272
commit 797816be7e
437 files changed
+54228 -28

No files matched your search

@@ -0,0 +1,12 @@
using System;
namespace BCG
{
/// <summary>
/// Exception in case of problems with BlueData formulation e.x. the schema not having an id column
/// </summary>
public class BlueDataException : ApplicationException
{
public BlueDataException (string message) : base (message) {}
}
}
@@ -0,0 +1,36 @@
using System;
using System.ComponentModel;
namespace BCG
{
/// <summary>
/// Used by GridBindableBlueTable to
/// </summary>
public class BlueDataPropertyDescriptor : PropertyDescriptor
{
private ColumnInfo myColumnInfo;
public BlueDataPropertyDescriptor (ColumnInfo ci)
: base (ci.colName, new Attribute[]{})
{
myColumnInfo = ci;
}
public override bool IsReadOnly {get{return true;}}
public override Type PropertyType {get{return myColumnInfo.colType;}}
public override bool CanResetValue (object component) {return false;}
public override object GetValue (object component) {return (component as IBlueRow)[myColumnInfo.colName];}
public override void ResetValue(object component) {}
public override void SetValue(object component, object value) {}
public override bool ShouldSerializeValue(object component) {return false;}
public override Type ComponentType {get {return typeof(IBlueRow);}}
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,45 @@
using System;
namespace BCG
{
/// <summary>
/// Allowed types
/// </summary>
public class BlueDataValidTypes
{
public static readonly Type BOOL = typeof(bool);
public static readonly Type BYTE = typeof(byte);
public static readonly Type CHAR = typeof(char);
public static readonly Type DECIMAL = typeof(decimal);
public static readonly Type DOUBLE = typeof(double);
public static readonly Type SHORT = typeof(short);
public static readonly Type INT = typeof(int);
public static readonly Type LONG = typeof(long);
public static readonly Type SBYTE = typeof(sbyte);
//public static readonly Type FLOAT = typeof(float);
public static readonly Type STRING = typeof(string);
//public static readonly Type USHORT = typeof(ushort);
public static readonly Type UINT = typeof(uint);
public static readonly Type ULONG = typeof(ulong);
public static readonly Type DATETIME = typeof(DateTime);
public static readonly Type ISERIALIZEDTYPE = typeof(ISerializedType);
public static readonly Type SINGLE = typeof(Single);
public static readonly Type BYTEARRAY = typeof(byte[]);
public static int getValueTypeSize (Type t)
{
if (t == BOOL || t == BYTE || t == SBYTE)
return 1;
else if (t == SHORT || t == CHAR)
return 2;
else if (t == INT || t == UINT || t == SINGLE)
return 4;
else if (t == DOUBLE || t == LONG || t == ULONG || t == DATETIME)
return 8;
else if (t == DECIMAL)
return 16;
else
throw new ApplicationException("Unsupported Data Type");
}
}
}
@@ -0,0 +1,70 @@
using System;
using BCG;
//using BCG.Utilities;
namespace BCG
{
[Serializable]
public class BlueIntHash
{
[Serializable]
private struct Element
{
public int k;
public int v;
public int next;
}
public BlueIntHash(int capacity)
{
myElementPool = new Element[capacity + 1];
myNumberOfBuckets = capacity * 3 + 1;
myBuckets = new int[myNumberOfBuckets];
}
public void add(int k, int v)
{
int index = myGetHash(k);
myElementPool[myFirstFreeElement].k = k;
myElementPool[myFirstFreeElement].v = v;
myElementPool[myFirstFreeElement].next = myBuckets[index];
myBuckets[index] = myFirstFreeElement;
myFirstFreeElement++;
}
[Serializable]
public class ElementNotFound : Exception
{ }
public int find(int k)
{
int b = myFind(k);
if (b == 0)
throw new ElementNotFound();
else
return myElementPool[b].v;
}
public bool isPresent(int k)
{ return myFind(k) != 0; }
private int myFind(int k)
{
int b = myBuckets[myGetHash(k)];
while (b != 0 && myElementPool[b].k != k)
b = myElementPool[b].next;
return b;
}
private int myGetHash(int k)
{ return k % myNumberOfBuckets; }
private int myFirstFreeElement = 1;
private Element[] myElementPool;
private int[] myBuckets;
private int myNumberOfBuckets;
}
}
@@ -0,0 +1,318 @@
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);
foreach (DictionaryEntry de in row)
if (mySchema[de.Key as string] != 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
}
}
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace BCG
{
/// <summary>
/// Generic IComparer for IBlueRow. Accepts a list of column names to define the sort order. All included columns must be IComparable.
/// </summary>
public class BlueRowComparer : IComparer<IBlueRow>, IComparer
{
private IEnumerable<string> myCompareColumns;
private bool myIsAscending;
public BlueRowComparer(bool ascending, params string[] columns)
{
this.myCompareColumns = columns;
this.myIsAscending = ascending;
}
public BlueRowComparer(params string[] columns)
: this(true, columns)
{
}
public int Compare(IBlueRow x, IBlueRow y)
{
// Guard against nulls
if (x == y) return 0;
if (x == null) return -1;
if (y == null) return 1;
int result = 0;
foreach (string compareColumn in this.myCompareColumns)
{
object left = x[compareColumn];
object right = y[compareColumn];
if (left == right) result = 0;
else if (left == null || left == DBNull.Value) result = -1;
else if (right == null || right == DBNull.Value) result = 1;
else result = ((IComparable)left).CompareTo(right);
if (result != 0)
break;
}
return result * (this.myIsAscending ? 1 : -1);
}
#region IComparer Members
public int Compare(object x, object y)
{
return this.Compare(x as IBlueRow, y as IBlueRow);
}
#endregion
}
}
@@ -0,0 +1,23 @@
using System;
using System.Collections;
namespace BCG
{
/// <summary>
/// This enumertaor is used to iterate through an IBlueRow when the IBlueRow is
/// used as an IDictionary
/// </summary>
public class BlueRowDictionaryEnumerator : BlueRowEnumerator, IDictionaryEnumerator
{
public BlueRowDictionaryEnumerator (IBlueRow row) : base (row) {}
public override object Current {get{return Entry;}}
#region IDictionaryEnumerator members
public object Key {get{return myRow.schema[myPos].colName;}}
public object Value {get{return myRow[myPos];}}
public DictionaryEntry Entry {get{return new DictionaryEntry (Key, Value);}}
#endregion
}
}
@@ -0,0 +1,37 @@
using System;
using System.Collections;
namespace BCG
{
/// <summary>
/// Used for enumerating an IEnumerable (ex ICollection). Furthermore, this is the
/// default Enueramtor used when enumerating asn IBlueRow
/// </summary>
public class BlueRowEnumerator : IEnumerator
{
protected int myPos = -1;
protected int myRowSize = -1;
protected IBlueRow myRow;
public BlueRowEnumerator (IBlueRow row)
{
myRow = row;
myRowSize = row.schema.columnNames.Length;
}
#region IEnumerator members
public void Reset() {myPos = 0;}
public bool MoveNext ()
{
if (myPos == myRowSize - 1)
return false;
myPos ++;
return true;
}
public virtual object Current {get{return myRow[myPos];}}
#endregion
}
}
@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BCG
{
public class BlueRowHasher
{
private int _n_keys;
private int _hash_counter = 0;
private bool _thread_safe = false;
private IDictionary<object, object> _root_map = new Dictionary<object, object>();
public BlueRowHasher(int n_keys, bool thread_safe = false)
{
_n_keys = n_keys;
_thread_safe = thread_safe;
}
public string get_string_hash(IBlueRow row, int[] key_cols)
{
return get_hash(row, key_cols).ToString();
}
/// <summary>
/// gets the hash for a row with given keys,
/// if key does not exist is adds it adds it
/// and returns the hash
/// </summary>
public int get_hash(IBlueRow row, int[] key_cols)
{
IDictionary<object, object> d = _root_map;
for (int i = 0; ; i++)
{
object v = row[key_cols[i]];
if (i < _n_keys - 1)
{
object inner_d = null;
if (!d.TryGetValue(v, out inner_d))
{
inner_d = _get_new_dictionary();
d.Add(v, inner_d);
}
d = (IDictionary<object, object>)inner_d;
}
else
{
object rv;
if (!d.TryGetValue (v, out rv))
{
rv = _get_next_hash();
d.Add(v, rv);
}
return (int)rv;
}
}
}
public int get_hash(params object[] key_vals)
{
IDictionary<object, object> d = _root_map;
for (int i = 0; ; i++)
{
object v = key_vals[i];
if (i < _n_keys - 1)
{
object inner_d = null;
if (!d.TryGetValue(v, out inner_d))
{
inner_d = _get_new_dictionary();
d.Add(v, inner_d);
}
d = (IDictionary<object, object>)inner_d;
}
else
{
object rv;
if (!d.TryGetValue(v, out rv))
{
rv = _get_next_hash();
d.Add(v, rv);
}
return (int)rv;
}
}
}
private int _get_next_hash()
{
return _thread_safe ? System.Threading.Interlocked.Increment(ref _hash_counter) : ++_hash_counter;
}
private IDictionary<object, object> _get_new_dictionary()
{
if (_thread_safe)
return new System.Collections.Concurrent.ConcurrentDictionary<object, object>();
else
return new Dictionary<object, object>();
}
}
}
@@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
* This class reprsents one lookup group that is stored together in a lookup table because it contains repeated data patterns.
* if groupNo is zero, then the group is not stord in a lookup table at all.
*
*
*
*/
namespace BCG
{
public class BlueRowLookupGroup
{
BlueTableSchema myEntrySchema;
public IBlueTableSchema entrySchema
{
get { return myEntrySchema; }
//cant' set this value, have to build it from the row's schema
}
//for faster access and such
private int[] myGroupColNos;
public int[] groupColNos
{
get { return myGroupColNos; }
}
private string[] myGroupColNames;
public string[] groupColNames
{
get { return myGroupColNames; }
}
private short myGroupNumber;
public short groupNumber
{
get { return myGroupNumber; }
}
/* how many bits are the ids into the table?
* In c# everything is stored as integers, but when it goes across the wire
* the data can be represented as a byte, a short, or an int
*/
private short myTableSize;
public short tableSize
{
get { return myTableSize; }
}
//if the group number is 0, the group is not stored in tables for serialization
public bool isStoredInTable
{
get { return myGroupNumber != 0; }
}
public BlueRowLookupGroup(short num, short sizeInBits, params string[] cols)
{
myGroupNumber = num;
myTableSize = sizeInBits;
myGroupColNames = cols ;
}
/*Takes a LookupTableSchema for the rows this group caches for, and builds the schema representing
each of these entries in the lookup table. This function should be called by the LookupTableSchema
during its constructor.
This function also caches the column numbers for this group, for quicker access*/
public void buildEntrySchema(LookupTableSchema theSchema)
{
ColumnInfo[] columns = new ColumnInfo[myGroupColNames.Length + 1];
myGroupColNos = new int[myGroupColNames.Length];
//the first column in our table is always the ID
int idColumn = 0;
columns[idColumn] = new ColumnInfo("id", idColumn, typeof(int));
//go through each of the columns
for (int col = 1; col < columns.Length; ++col)
{
ColumnInfo columnInInputTable = theSchema[groupColNames[col - 1]];
if (columnInInputTable.isConstant)
columns[col] = new ColumnInfo(columnInInputTable.colName, col, columnInInputTable.colValue);
else
columns[col] = new ColumnInfo(columnInInputTable.colName, col, columnInInputTable.colType);
myGroupColNos[col - 1] = columnInInputTable.colNo;
}
myEntrySchema = new BlueTableSchema(columns, "Lookup Group " + theSchema.tableName + ":" + myGroupNumber);
}
/*takes a row as input, and extracts the members of the group from the row */
public object[] extractGroupFromRow(IBlueRow input)
{
int numCols = myGroupColNames.Length;
object[] results = new object[numCols];
for (int col = 0; col < numCols; ++col)
results[col] = input[myGroupColNos[col]];
return results;
}
}
}
@@ -0,0 +1,239 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
//using BCG.Utilities;
using System.Linq;
namespace BCG
{
/// <summary>
/// Anologue of the dotNet DataTable.
/// </summary>
[Serializable]
public class BlueTable : IBlueRowCollection
{
/// <summary>
/// Hashtable indexed on "id" to speed up removes
/// </summary>
protected Hashtable myIdIndex = new Hashtable();
/// <summary>
/// Schema the table adheres to
/// </summary>
protected IBlueTableSchema mySchema;
#region Constructors
protected BlueTable() { }
/// <summary>
/// The BlueTable constructs the schema from the table being passed and copies
/// all the data over
/// </summary>
/// <param name="table"></param>
public BlueTable(DataTable table)
{
ColumnInfo[] cis = new ColumnInfo[table.Columns.Count];
int i = 0;
foreach (DataColumn col in table.Columns)
cis[i] = new ColumnInfo(col.ColumnName, i++, col.DataType);
mySchema = new BlueTableSchema(cis, table.TableName);
foreach (DataRow row in table.Rows)
{
IBlueRow newRow = new BlueRow(mySchema, row.ItemArray);
myIdIndex[newRow["id"]] = newRow;
}
}
public BlueTable(IBlueRowCollection table)
{
mySchema = table.schema;
foreach (IBlueRow row in table)
addRow(row);
}
public BlueTable(string tableName,
IEnumerable objects,
params CMap[] columnMappers)
{
// extract schema from mappers
ColumnInfo[] cols = new ColumnInfo[columnMappers.Length];
for (int i = 0; i < columnMappers.Length; i++)
{
cols[i] = new ColumnInfo(
columnMappers[i].getColumnName(), i,
columnMappers[i].getColumnType());
}
mySchema = new BlueTableSchema(cols, tableName);
// load rows
int rowNumber = 0;
foreach (object rowObject in objects)
{
object[] values = new object[columnMappers.Length];
for (int cn = 0; cn < columnMappers.Length; cn++)
values[cn] = columnMappers[cn].getColumnValue(rowNumber, rowObject);
addRow(new BlueRow(mySchema, values));
rowNumber++;
}
}
/// <summary>
/// Gets the schema, no data is iniliazed at construction time
/// </summary>
/// <param name="schema"></param>
public BlueTable(IBlueTableSchema schema) { mySchema = schema; }
#endregion
#region IBlueRowCollection members
/// <summary>
/// Returns the row by the index, interface property
/// </summary>
public virtual IBlueRow this[int id]
{
get
{
object row = myIdIndex[id];
if (row == null)
throw new BlueDataException("ID " + id.ToString() + " does not exist in table " + mySchema.tableName);
return (IBlueRow)row;
}
}
/// <summary>
/// Gets the schema
/// </summary>
public virtual IBlueTableSchema schema { get { return mySchema; } }
public virtual bool containsID(int id) { return myIdIndex[id] != null; }
public virtual bool tryGetValue(int id, out IBlueRow row)
{
row = (IBlueRow)myIdIndex[id];
return (row != null);
}
#endregion
#region BlueTable members
public virtual void clear()
{
myIdIndex.Clear();
}
/// <summary>
/// If a row with the same id exists, it replaces it
/// </summary>
/// <param name="r"></param>
public virtual void forceAddRow(IBlueRow r)
{
myIdIndex[r["id"]] = r;
}
/// <summary>
/// Adds a new row to the table.
/// </summary>
/// <param name="row"></param>
public virtual void addRow(IBlueRow row)
{
object val = row["id"];
if (val == null || val == DBNull.Value)
throw new BlueDataException("The ID of the row being added is null/DNBull");
try
{
myIdIndex.Add(val, row);
}
catch (ArgumentException)
{
throw new BlueDataException("ID already exists in the table " + mySchema.tableName +
" , duplication of field ID not allowed");
}
}
public virtual void addRow(DataRow row)
{
object val = row["id"];
if (val == null || val == DBNull.Value)
throw new BlueDataException("The ID of the row being added is null/DNBull");
if (myIdIndex[val] != null)
throw new BlueDataException("ID already exists in the table, duplication of field ID not allowed");
IBlueRow newRow = new BlueRow(mySchema, row);
myIdIndex[val] = newRow;
}
public virtual void addRows(IBlueRowCollection rows)
{
foreach (IBlueRow nr in rows)
addRow(nr);
}
public virtual void addRows(IEnumerable rows)
{
foreach (IBlueRow nr in rows)
addRow(nr);
}
/// <summary>
/// Removes a row from the table. Complexity = O(1)
/// </summary>
/// <param name="row"></param>
public virtual bool deleteRow(IBlueRow row) { return deleteRow((int)row["id"]); }
public virtual bool deleteRow(DataRow row) { return deleteRow((int)row["id"]); }
/// <summary>
/// Removes a row specified by the id. The index helps in making this operation O(1)
/// </summary>
/// <param name="id"></param>
public virtual bool deleteRow(int id)
{
if (myIdIndex[id] == null)
return false;
myIdIndex.Remove(id);
return true;
}
public virtual bool deleteRows(IBlueRowCollection rows)
{
bool retVal = true;
foreach (IBlueRow r in rows)
retVal &= deleteRow(r);
return retVal;
}
public virtual bool deleteRows(IEnumerable rows)
{
bool retVal = true;
foreach (IBlueRow r in rows)
retVal &= deleteRow(r);
return retVal;
}
public virtual void changeRow(IBlueRow r)
{
myIdIndex[r["id"]] = r;
}
public virtual void changeRows(IBlueRowCollection rows)
{
foreach (IBlueRow r in rows)
changeRow(r);
}
#endregion
#region ICollection members
public virtual IEnumerator GetEnumerator() //{return myRows.GetEnumerator ();}
{ return myIdIndex.Values.GetEnumerator(); }
public virtual bool IsSynchronized { get { return false; } }
public virtual object SyncRoot { get { return this; } }
public virtual void CopyTo(Array array, int index) { myIdIndex.Values.CopyTo(array, index); }
public virtual int Count { get { return myIdIndex.Count; } }
#endregion
}
}
@@ -0,0 +1,286 @@
using System;
using System.Data;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using BCG.Utilities.General;
namespace BCG
{
/// <summary>
/// The BlueTableSchema is an immutable schema holder. Once created it cannot be modified. It can be created
/// either from a DataTable or from an Array of ColumnInfo. The methods through which the user can access it
/// are the indexers (both order and name based), and the property columnInfo and columnNames, that return
/// the respective data, sorted according to the ColumnOrder.
/// </summary>
[Serializable]
public class BlueTableSchema : IBlueTableSchema
{
public class BlueTableSchemaSerializationHelper
{
public static object parse(Type t, string data)
{
if (t == BlueDataValidTypes.BOOL)
return Boolean.Parse(data);
else if (t == BlueDataValidTypes.BYTE)
return Byte.Parse(data);
else if (t == BlueDataValidTypes.CHAR)
return char.Parse(data);
else if (t == BlueDataValidTypes.DATETIME)
return DateTime.Parse(data);
else if (t == BlueDataValidTypes.DOUBLE)
return Double.Parse(data);
else if (t == BlueDataValidTypes.DECIMAL)
return Decimal.Parse(data);
else if (t == BlueDataValidTypes.INT)
return int.Parse(data);
else if (t == BlueDataValidTypes.LONG)
return long.Parse(data);
else if (t == BlueDataValidTypes.SBYTE)
return SByte.Parse(data);
else if (t == BlueDataValidTypes.STRING)
return data;
else if (t == BlueDataValidTypes.UINT)
return uint.Parse(data);
else if (t == BlueDataValidTypes.ULONG)
return ulong.Parse(data);
else if (t == BlueDataValidTypes.SINGLE)
return Single.Parse(data);
else
throw new ApplicationException("Can not be parsed");
}
public static void serialize(BinaryWriter bw, Type t, object o)
{
if (t == BlueDataValidTypes.BOOL)
bw.Write((bool)o);
else if (t == BlueDataValidTypes.BYTE)
bw.Write((byte)o);
else if (t == BlueDataValidTypes.CHAR)
bw.Write((char)o);
else if (t == BlueDataValidTypes.DATETIME)
bw.Write(((DateTime)o).Ticks);
else if (t == BlueDataValidTypes.DOUBLE)
bw.Write((double)o);
else if (t == BlueDataValidTypes.DECIMAL)
bw.Write((decimal)o);
else if (t == BlueDataValidTypes.INT)
bw.Write((int)o);
else if (t == BlueDataValidTypes.LONG)
bw.Write((long)o);
else if (t == BlueDataValidTypes.SBYTE)
bw.Write((sbyte)o);
else if (t == BlueDataValidTypes.STRING)
bw.Write(o as string);
else if (t == BlueDataValidTypes.SINGLE)
bw.Write((Single)o);
else if (t == BlueDataValidTypes.BYTEARRAY)
{
byte[] bs = o as byte[];
bw.Write(bs.Length);
bw.Write(bs);
}
else if (t == BlueDataValidTypes.UINT)
bw.Write((uint)o);
else if (t == BlueDataValidTypes.ULONG)
bw.Write((ulong)o);
else
throw new ApplicationException("Unsupported type");
}
public static object deserialize(BinaryReader br, Type t)
{
if (t == BlueDataValidTypes.BOOL)
return br.ReadBoolean();
else if (t == BlueDataValidTypes.BYTE)
return br.ReadByte();
else if (t == BlueDataValidTypes.CHAR)
return br.ReadChar();
else if (t == BlueDataValidTypes.DATETIME)
return new DateTime(br.ReadInt64());
else if (t == BlueDataValidTypes.DOUBLE)
return br.ReadDouble();
else if (t == BlueDataValidTypes.DECIMAL)
return br.ReadDecimal();
else if (t == BlueDataValidTypes.INT)
return br.ReadInt32();
else if (t == BlueDataValidTypes.LONG)
return br.ReadInt64();
else if (t == BlueDataValidTypes.SBYTE)
return br.ReadSByte();
else if (t == BlueDataValidTypes.STRING)
return br.ReadString();
else if (t == BlueDataValidTypes.BYTEARRAY)
return br.ReadBytes(br.ReadInt32());
else if (t == BlueDataValidTypes.UINT)
return br.ReadUInt32();
else if (t == BlueDataValidTypes.ULONG)
return br.ReadUInt64();
else if (t == BlueDataValidTypes.SINGLE)
return br.ReadSingle();
else
throw new ApplicationException("Unsupported type");
}
}
protected Hashtable myIndexByColName = new Hashtable();
protected ColumnInfo[] myIndexByColOrder;
private string[] myTableColNames;
protected string myTableName;
public BlueTableSchema() { }
public BlueTableSchema(DataTable table)
{
myIndexByColOrder = new ColumnInfo[table.Columns.Count];
myTableName = table.TableName;
foreach (DataColumn col in table.Columns)
{
ColumnInfo ci = new ColumnInfo(col.ColumnName, col.Ordinal, col.DataType);
myIndexByColName[ci.colName] = ci;
myIndexByColOrder[ci.colNo] = ci;
}
}
public BlueTableSchema(ColumnInfo[] cis, string tableName)
{
myConstruct(cis, tableName);
}
private void myConstruct(ColumnInfo[] cis, string tableName)
{
myIndexByColOrder = new ColumnInfo[cis.Length];
myTableName = tableName;
foreach (ColumnInfo ci in cis)
{
myIndexByColOrder[ci.colNo] = ci;
myIndexByColName[ci.colName] = ci;
}
}
public BlueTableSchema(string tableName, params ColumnInfo[] cis) : this(cis, tableName) { }
/// <summary>
/// Creates a BlueTableSchema that exactly mirrors the public fields
/// present in fromType except that an id field will be added regardless
/// of it being present in fromType or not.
/// </summary>
/// <param name="tableName">the table name</param>
/// <param name="fromType">the plain old data type whose fields will be mirrored in the schema</param>
public BlueTableSchema(string tableName, Type fromType)
{
List<ColumnInfo> cis = new List<ColumnInfo>();
int i = 0;
// we sort the field name so that we have a deterministic order given
// the same type.
bool foundId = false;
foreach (FieldInfo fi in fromType.GetFields().OrderBy((f) => { return f.Name; }))
{
if (fi.Name.ToLower().Equals("id"))
foundId = true;
cis.Add(new ColumnInfo(fi.Name, i++, fi.FieldType));
}
if (!foundId)
cis.Add(new ColumnInfo("id", cis.Count, typeof(Int32)));
myConstruct(cis.ToArray(), tableName);
}
public ColumnInfo this[string colName]
{
get
{
object retVal = myIndexByColName[colName];
return retVal != null ? retVal as ColumnInfo : null;
}
}
public ColumnInfo this[int colNo]
{
get { return myIndexByColOrder[colNo]; }
}
/// <summary>
/// The returned Array is sorted according to the Column No
/// </summary>
public string[] columnNames
{
get
{
if (myTableColNames == null)
{
myTableColNames = new string[myIndexByColName.Count];
foreach (DictionaryEntry de in myIndexByColName)
{
ColumnInfo temp = (ColumnInfo)de.Value;
myTableColNames[temp.colNo] = temp.colName;
}
}
return myTableColNames;
}
}
/// <summary>
/// The returned Array is sorted according to the Column No
/// </summary>
public ColumnInfo[] columnInfo
{
get { return myIndexByColOrder; }
}
public string tableName { get { return myTableName; } }
#region ISerializedType methods
public void serialize(BinaryWriter bw)
{
bw.Write(columnNames.Length);
bw.Write(tableName);
foreach (ColumnInfo ci in columnInfo)
{
bw.Write(ci.colName);
bw.Write(ci.colType.ToString());
if (ci.isConstant)
{
bw.Write(true);
BlueTableSchemaSerializationHelper.serialize(bw, ci.colType, ci.colValue);
}
else
{
bw.Write(false);
}
}
}
public void deserialze(BinaryReader br)
{
ColumnInfo[] cis = new ColumnInfo[br.ReadInt32()];
string name = br.ReadString();
for (int i = 0; i < cis.Length; i++)
{
string colName = br.ReadString();
Type t = TypeInstanceLoader.loadType(br.ReadString(), true);
bool isConstant = br.ReadBoolean();
if (isConstant)
{
cis[i] = new ColumnInfo(colName, i, t, BlueTableSchemaSerializationHelper.deserialize(br, t));
}
else
{
cis[i] = new ColumnInfo(colName, i, t);
}
}
myConstruct(cis, name);
}
public int getSerializedSize()
{
throw new NotImplementedException("Feature for provided constant values to" +
"non-primitive types in schema, not provided as of now");
}
#endregion
}
}
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BCG
{
public abstract class CMap
{
public abstract string getColumnName();
public abstract Type getColumnType();
public abstract object getColumnValue(int rowIndex,object rowObject);
}
public class CMap<T> : CMap
{
public CMap(string columnName, MapF mapperFunction)
{
myColumnName = columnName;
myExtractColumn = mapperFunction;
}
private string myColumnName;
public override string getColumnName()
{
return myColumnName;
}
public override Type getColumnType()
{
return typeof(T);
}
public delegate T MapF(int rowIndex, object rowObject);
private MapF myExtractColumn;
public override object getColumnValue(int rowIndex, object rowObject)
{
return myExtractColumn(rowIndex, rowObject);
}
}
}
@@ -0,0 +1,92 @@
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
namespace BCG
{
/// <summary>
/// Circular buffer for rows.
/// </summary>
public class CircularRowCollection : OrderedBlueTable
{
private int myBufferSize;
public CircularRowCollection (IBlueTableSchema schema, int bufferSize)
: base (schema)
{
myBufferSize = bufferSize ;
}
public CircularRowCollection (IBlueRowCollection rows, int bufferSize)
: this (rows.schema, bufferSize)
{
foreach (IBlueRow r in rows)
addRow (r);
}
public int bufferSize
{
get
{
return (myBufferSize);
}
}
/// <summary>
/// Adds a new row to the collection. If the size of the
/// collection becomes greater that
/// buffer size, the first row is returned;
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
public IBlueRow addRowWithReturn (IBlueRow r)
{
base.addRow (r);
if (myOrderedRows.Count > myBufferSize)
{
IBlueRow retVal = myOrderedRows [0] as IBlueRow;
deleteRow (retVal);
return retVal;
}
return null;
}
public override void addRow (IBlueRow row)
{
base.addRow (row);
if (myOrderedRows.Count > myBufferSize)
deleteRow (myOrderedRows [0] as IBlueRow);
}
public override void addRow (DataRow row)
{
base.addRow (row);
if (myOrderedRows.Count > myBufferSize)
deleteRow (myOrderedRows [0] as IBlueRow);
}
public void add_rows(IEnumerable<IBlueRow> rows_to_add, out IBlueRowCollection added_rows, out IBlueRowCollection deleted_rows)
{
BlueTable added_rv = new BlueTable(mySchema), deleted_rv = new BlueTable(mySchema);
if (rows_to_add.Count() < myBufferSize)
{
foreach (IBlueRow r in rows_to_add)
{
IBlueRow dr = addRowWithReturn(r);
added_rv.addRow(r);
if (dr != null)
deleted_rv.addRow(dr);
}
}
else
{
deleted_rv = new BlueTable(this);
clear();
addRows(rows_to_add);
added_rv = new BlueTable(this);
}
added_rows = added_rv;
deleted_rows = deleted_rv;
}
}
}
@@ -0,0 +1,95 @@
using System;
namespace BCG
{
/// <summary>
/// Resprsents information about a column schema, ie its ordinal, the type, the name and constant value or expression. where applicable
/// </summary>
[Serializable]
public class ColumnInfo : IComparable
{
public static bool isAllowedType (Type t)
{
return
t.Equals (typeof (System.Int32)) ||
t.Equals (typeof (System.Int64)) ||
t.Equals (typeof (System.Double)) ||
t.Equals (typeof (System.String)) ||
t.Equals (typeof (System.DateTime)) ? true :
false;
}
//the default column info does not cach values in a lookup table, therefore it is in group 0
public ColumnInfo(string colName, int colNo, Type colType)
: this(colName, colNo, colType, 0)
{
}
public ColumnInfo(string colName, int colNo, Type colType, short colGroup)
{
myColName = colName;
myColNo = colNo;
myColType = colType;
myLookupTableGroup = colGroup;
myColValue = null;
}
public ColumnInfo (string colName, int colNo, object colValue)
: this (colName, colNo, colValue.GetType()) {myColValue = colValue == null ? DBNull.Value : colValue;}
public ColumnInfo (string colName, int colNo, Type colType, object colValue)
: this (colName, colNo, colType)
{
myColValue = colValue == null ? DBNull.Value : colValue;
}
public int CompareTo (object obj)
{
int otherColNo = ((ColumnInfo)(obj)).colNo;
if (myColNo < otherColNo)
return -1;
else if (myColNo > otherColNo)
return 1;
return 0;
}
//public bool isCalculated{get{return myExpression != null;}}
public bool isConstant {get{return myColValue != null;}}
private int myColNo;
private string myColName;
private Type myColType;
//private string myExpression;
private object myColValue;
//no table will ever have more than 65,536 columns
//if it does, we are in serious trouble
private short myLookupTableGroup;
public int colNo{get{return myColNo;}}
public string colName {get{return myColName;}}
public Type colType {get{return myColType;}}
//this value corresponds to how the data in the column is serialized.
//if the group number is 0, the values are not stored in a table
//if the vlaue is non-zero, all columns with the the same lookup table group are stored in the same lookup table together
//are stored together in a lookup table
public short lookupTableGroup
{
get { return myLookupTableGroup; }
set { myLookupTableGroup = value; }
}
//public string expression {get{return myExpression;}}
public object colValue {get{return myColValue;}}
}
}
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BCG
{
public class ColumnKeyedData
{
private int[] _key_col_indices = null;
private string[] _key_col_names;
private BlueRowHasher _row_hasher;
private Dictionary<int, IBlueRow> _row_map = new Dictionary<int, IBlueRow>();
public ColumnKeyedData(params string[] key_col_names)
{
_key_col_names = key_col_names;
_row_hasher = new BlueRowHasher(key_col_names.Length);
}
public void delete_rows(IBlueRowCollection rows)
{
if (rows != null)
{
_init_schema(rows.schema);
foreach (IBlueRow r in rows)
{
int row_hash = _row_hasher.get_hash(r, _key_col_indices);
if (_row_map.ContainsKey(row_hash))
_row_map.Remove(row_hash);
}
}
}
public void add_rows(IBlueRowCollection rows)
{
if (rows != null)
{
_init_schema(rows.schema);
foreach (IBlueRow r in rows)
{
int row_hash = _row_hasher.get_hash(r, _key_col_indices);
_row_map[row_hash] = r;
}
}
}
public void reset_state (IBlueRowCollection rows)
{
_row_map.Clear();
add_rows(rows);
}
public IEnumerable<IBlueRow> get_all_rows() { return _row_map.Values; }
private void _init_schema(IBlueTableSchema schema)
{
if (_key_col_indices == null)
_key_col_indices = BlueDataUtility.get_column_indices(schema, _key_col_names);
}
public IBlueRow get_row(params object[] key_vals)
{
int row_hash = _row_hasher.get_hash(key_vals);
IBlueRow rv = null;
_row_map.TryGetValue(row_hash, out rv);
return rv;
}
}
}
@@ -0,0 +1,54 @@
using System;
using System.IO;
using System.Collections;
namespace BCG
{
public class FileRowCollection : CircularRowCollection
{
public FileRowCollection(string dataFileName,
string schemaFileName)
: this(dataFileName, schemaFileName, Int32.MaxValue)
{ }
/// <summary>
/// </summary>
/// <param name="dataFileName">file with the rows</param>
/// <param name="schemFileName">file with schema (written as columns names in the first line and types in the 2nd</param>
/// <param name="maxSize">if max size is mentioned then a buffer is mantained for only
/// that number of rows, ie the last maxSize number of rows in the file
/// </param>
public FileRowCollection(string dataFileName,
string schemaFileName,
int maxSize)
: this(new StreamReader(dataFileName),
new StreamReader(schemaFileName),
maxSize)
{ }
public FileRowCollection(StreamReader dataReader,
StreamReader schemaReader)
: this(dataReader, schemaReader, Int32.MaxValue)
{ }
public FileRowCollection(StreamReader dataReader,
StreamReader schemaReader,
int maxSize)
: base(BlueDataUtility.createSchemaFromCSVSchemaFile(schemaReader), maxSize)
{
int id = 0;
while (dataReader.Peek() > 0)
{
try
{
addRow(BlueDataUtility.createRowFromCSVFileRow(dataReader.ReadLine(), mySchema, id++));
}
catch (Exception)
{
//BlueEventLog.logEvent (e.Message, BlueEventLevel.Warning);
}
}
}
}
}
@@ -0,0 +1,384 @@
// using System;
// using System.Reflection;
// using System.Reflection.Emit;
// using System.Collections;
// using System.ComponentModel;
// namespace BCG
// {
// public class GridBindableBlueTable : IBindingList, IBlueRowCollection, ITypedList
// {
// private class MyComparer : IComparer
// {
// private string myColName;
// public MyComparer (string colName)
// {
// myColName = colName;
// }
// #region IComparer Members
// public int Compare (object x, object y)
// {
// IBlueRow r1 = x as IBlueRow;
// IBlueRow r2 = y as IBlueRow;
// try
// {
// return
// r1[myColName] == DBNull.Value ?
// r2[myColName] == DBNull.Value ?
// 0
// :
// -1
// :
// r2[myColName] == DBNull.Value ?
// 1
// :
// (r1[myColName] as IComparable).CompareTo (r2[myColName]);
// }
// catch (Exception e)
// {
// Console.WriteLine (e);
// return 0;
// }
// }
// #endregion
// }
// private PropertyDescriptor mySortPropertyDescriptor;
// private IBlueTableSchema mySchema;
// private PropertyDescriptorCollection myPDColl;
// protected ArrayList myList = new ArrayList ();
// protected ListSortDirection mySortDirection;
// protected IComparer myComparer;
// public event System.ComponentModel.ListChangedEventHandler ListChanged;
// public GridBindableBlueTable (IBlueTableSchema schema)
// : this (schema, new ArrayList())
// {}
// public GridBindableBlueTable (IBlueTableSchema schema, ICollection data)
// {
// myComparer = new MyComparer ("id");
// mySchema = schema;
// myInitPropertyDescriptors ();
// foreach (object o in data)
// Add (o);
// }
// private void myInitPropertyDescriptors ()
// {
// ArrayList temp = new ArrayList ();
// foreach (ColumnInfo ci in mySchema.columnInfo)
// temp.Add (new BlueDataPropertyDescriptor (ci));
// myPDColl = new PropertyDescriptorCollection (
// temp.ToArray (typeof(PropertyDescriptor)) as PropertyDescriptor[]);
// }
// #region IList Members
// public bool IsReadOnly {get {return false;}}
// public virtual object this [int index]
// {
// get {return myList[index];}
// set {throw new NotSupportedException ("Illegal operation for a Sorted list");}
// }
// public virtual void RemoveAt(int index)
// {
// myList.RemoveAt (index);
// if (ListChanged != null)
// ListChanged (this, new ListChangedEventArgs (ListChangedType.ItemDeleted, index));
// }
// public virtual void Insert (int index, object value)
// {
// throw new NotSupportedException ("Illegal operation for a Sorted list");
// }
// public virtual void Remove (object value)
// {
// RemoveAt (myList.IndexOf (value));
// }
// public virtual bool Contains (object value)
// {
// return myList.Contains (value);
// }
// public virtual void Clear()
// {
// myList.Clear ();
// if (ListChanged != null)
// ListChanged (this, new ListChangedEventArgs (ListChangedType.Reset, 0));
// }
// public virtual int IndexOf (object value)
// {
// return myList.IndexOf (value);
// }
// /// <summary>
// /// Adds the value to the right index in the sorted list
// /// </summary>
// /// <param name="value"></param>
// /// <returns></returns>
// public virtual int Add (object value)
// {
// int insertionIndex = -1;
// if (Count > 0)
// {
// for (int i = 0; i < myList.Count; i++)
// {
// if ((insertionIndex = myGetInsertionIndex (value, i)) > -1)
// {
// myList.Insert (insertionIndex, value);
// break;
// }
// insertionIndex = -1;
// }
// }
// else
// {
// insertionIndex = 0;
// myList.Add (value);
// }
// if (ListChanged != null)
// ListChanged (this, new ListChangedEventArgs (ListChangedType.ItemAdded, insertionIndex));
// return insertionIndex;
// }
// private int myGetInsertionIndex (object obj, int listIndex)
// {
// if (mySortPropertyDescriptor == null)
// return myList.Count;
// if (mySortDirection.Equals (ListSortDirection.Ascending))
// {
// if (myComparer.Compare (obj, myList[listIndex]) <= 0)
// return listIndex;
// else if (listIndex == myList.Count-1)
// return myList.Count;
// }
// else
// {
// if (myComparer.Compare (obj, myList[listIndex]) >= 0)
// return listIndex;
// else if (listIndex == myList.Count-1)
// return myList.Count;
// }
// return -1;
// }
// public bool IsFixedSize {get {return false;}}
// #endregion
// #region ICollection Members
// public bool IsSynchronized {get {return false;}}
// public int Count {get {return myList.Count;}}
// public void CopyTo(Array array, int index) {myList.CopyTo (array, index);}
// public object SyncRoot {get {return null;}}
// #endregion
// #region IEnumerable Members
// public IEnumerator GetEnumerator() {return myList.GetEnumerator ();}
// #endregion
// #region IBindingList Members
// public void AddIndex(PropertyDescriptor property)
// {
// throw new NotSupportedException ("Adding index is not supported");
// }
// public bool AllowNew
// {
// get {return false;}
// }
// public void ApplySort (PropertyDescriptor property, System.ComponentModel.ListSortDirection direction)
// {
// if (mySortPropertyDescriptor != null
// && mySortPropertyDescriptor.Name == property.Name)
// {
// if (mySortDirection == direction)
// return;
// else
// {
// mySortDirection = direction;
// myList.Reverse ();
// return;
// }
// }
// foreach (PropertyDescriptor pd in myPDColl)
// if (pd.Name == property.Name)
// mySortPropertyDescriptor = pd;
// myComparer = new MyComparer (property.Name);
// mySortDirection = direction;
// myList.Sort (myComparer);
// }
// public PropertyDescriptor SortProperty
// {
// get {return mySortPropertyDescriptor;}
// }
// public int Find (PropertyDescriptor property, object key)
// {
// throw new NotSupportedException ("Find not supported");
// }
// public bool SupportsSorting
// {
// get {return true;}
// }
// public bool IsSorted {get {return mySortPropertyDescriptor != null;}}
// public bool AllowRemove {get {return false;}}
// public bool SupportsSearching {get {return false;}}
// public System.ComponentModel.ListSortDirection SortDirection {get {return mySortDirection;}}
// public bool SupportsChangeNotification {get {return true;}}
// public void RemoveSort()
// {
// mySortPropertyDescriptor = null;
// }
// public object AddNew()
// {
// throw new NotSupportedException ("Find not supported");
// }
// public bool AllowEdit
// {
// get {return false;}
// }
// public void RemoveIndex(PropertyDescriptor property)
// {
// throw new NotSupportedException ("Find not supported");
// }
// #endregion
// #region IBlueRowCollection Members
// IBlueRow BCG.IBlueRowCollection.this[int id]
// {
// get
// {
// foreach (IBlueRow r in (this as IList))
// {
// if (r["id"].Equals(id))
// return r;
// }
// return null;
// }
// }
// public bool containsID (int id)
// {
// return (this as IBlueRowCollection) [id] != null;
// }
// public IBlueTableSchema schema {get {return mySchema;}}
// #endregion
// #region Dynamic Type Creation
// private IBlueRow myCreateDynamicRow (IBlueRow r)
// {
// //return Activator.CreateInstance (myRowType, new object[] {r}) as IBlueRow;
// return r;
// }
// private Type myGenerateType ()
// {
// ILGenerator ilg;
// AssemblyName an = new AssemblyName ();
// an.Name = "DynamicRowAssembly";
// AssemblyBuilder aBuilder =
// AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Run);
// ModuleBuilder mBuilder =
// aBuilder.DefineDynamicModule ("DefaultModule");
// TypeBuilder tBuilder =
// mBuilder.DefineType ("DynamicBlueRow", TypeAttributes.Public, typeof(BlueRow));
// ConstructorBuilder cb =
// tBuilder.DefineConstructor (
// MethodAttributes.Public,
// CallingConventions.Standard,
// new Type[] {typeof(IBlueRow)}
// );
// ilg = cb.GetILGenerator ();
// ilg.Emit (OpCodes.Ldarg_0);
// ilg.Emit (OpCodes.Ldarg_1);
// ilg.Emit (OpCodes.Call,
// typeof(BlueRow).GetConstructor (new Type[] {typeof(IBlueRow)}));
// ilg.Emit (OpCodes.Ret);
// foreach (string col in mySchema.columnNames)
// {
// myAddProperty (col, tBuilder);
// }
// Type dynType = tBuilder.CreateType ();
// return dynType;
// }
// private void myAddProperty (string colName, TypeBuilder tBuilder)
// {
// MethodBuilder methodBuilder =
// tBuilder.DefineMethod ("get_" + colName,
// MethodAttributes.Public,
// CallingConventions.Standard,
// typeof(object),
// new Type[]{}
// );
// ILGenerator ilg =
// methodBuilder.GetILGenerator ();
// ilg.Emit (OpCodes.Ldarg_0);
// ilg.Emit (OpCodes.Ldstr, colName);
// ilg.Emit (OpCodes.Call, typeof(BlueRow).GetMethod ("get_Item", new Type[]{typeof(string)}));
// ilg.Emit (OpCodes.Ret);
// PropertyBuilder propBuilder =
// tBuilder.DefineProperty (colName, System.Reflection.PropertyAttributes.None , typeof(object), new Type[]{});
// propBuilder.SetGetMethod (methodBuilder);
// }
// #endregion
// #region ITypedList Members
// public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
// {
// return myPDColl;
// }
// public string GetListName(PropertyDescriptor[] listAccessors)
// {
// return "GridBindableBlueTable";
// }
// #endregion
// }
// }
@@ -0,0 +1,113 @@
using System;
using System.Collections;
namespace BCG
{
/// <summary>
/// An N-domensional hashtable , providing add and retrieve features
/// for the time being. Does not deal with null keys right now
/// </summary>
///
[Serializable]
public class HashTableND
{
private int myDim, myCount = 0;
private IDictionary myRootDic = new Hashtable ();
public HashTableND (int dim) {myDim = dim;}
public object add (object[] keys, object val)
{
ArrayList t = new ArrayList (keys);
t.Add (val);
return add (t.ToArray());
}
/// <summary>
/// Adds a value to the hashtable. the size of the arg array should be dim+1, with the
/// the first dim entries corresponding to keys and the last to the value to be added
/// </summary>
/// <param name="os"></param>
public object add (object[] os)
{
if (os.Length != myDim+1)
throw new ApplicationException ("size of arg array not right");
else
{
myCount ++;
IDictionary d = myRootDic;
for (int i = 0 ; ; i++)
{
if (i < myDim - 1)
{
if (!d.Contains (os[i]))
d.Add (os[i], new Hashtable ());
d = d[os[i]] as IDictionary;
}
else
{
d[os[i]] = os[i+1];
return os [myDim];
}
}
}
}
/// <summary>
/// Retrieves values from the hastable, with the arg array specifying the keys.
/// </summary>
/// <param name="keys"></param>
/// <returns></returns>
public object getValue (params object[] keys)
{
IDictionary d = myRootDic;
for (int i = 0; ; i++)
if (i < keys.Length-1)
{
d = d[keys[i]] as IDictionary;
if (d == null)
return null;
}
else
return d[keys[i]];
}
public void setValue (object[] keys, object val)
{
ArrayList t = new ArrayList (keys);
t.Add (val);
setValue (t.ToArray());
}
public void setValue (object[] os)
{
if (os.Length != myDim+1)
throw new ApplicationException ("size of arg array not right");
IDictionary d = myRootDic;
for (int i = 0; i < myDim-1; i++)
d = d[os[i]] as IDictionary;
d[os[myDim-1]] = os[myDim];
}
public void remove (params object[] keys)
{
myCount --;
IDictionary d = myRootDic;
for (int i = 0; ; i++)
if (i < keys.Length-1)
d = d[keys[i]] as IDictionary;
else
{
d.Remove (keys[i]);
return;
}
}
public int count {get {return myCount;}}
public ICollection topLevelKeys { get {return myRootDic.Keys;}}
}
}
@@ -0,0 +1,41 @@
using System;
using System.Collections;
namespace BCG
{
/// <summary>
/// Represents a primitive immutable anologue of a dotNet DataRow. The user assumes no info about the row
/// but the number of columns, and the way to access data through the indexer by providing either the
/// ordinal or the name. Also note that the default iteration through the IBlueRow return DictionaryEntry
/// (ie the default iteration is the same as an IDictionary's iteration)
/// </summary>
public interface IBlueRow : IDictionary
{
/// <summary>
/// Gets the schema of the row, which is same as that of the IBlueRowCollections to which it belongs
/// </summary>
IBlueTableSchema schema{get;}
/// <summary>
/// Though the IDictionary supports an untyped indexer, having the typed indexer makes retreivals
/// faster, specially in the case of large tables, where one type checking per cell would amount to
/// a sizeable time, eg for a table with 20000 rows and a schema of 20 columns, we will save about
/// half a million type checkings
/// </summary>
object this[int index] {get;}
object this[string colName] {get;}
}
namespace Extensions
{
public static class IBlueRowExtensions
{
public static bool isDBNull(this object val)
{
return (val == null || val == DBNull.Value);
}
public static bool isNotDBNull(this object val)
{
return (val != null && val != DBNull.Value);
}
}
}
}
@@ -0,0 +1,19 @@
using System;
using System.Collections;
namespace BCG
{
/// <summary>
/// Represents a readonly, very primitive form of dotNet DataTable. All it has has an indexer for
/// getting the rows
/// </summary>
public interface IBlueRowCollection : ICollection
{
/// <summary>
/// Gets the row corresponding to the id
/// </summary>
IBlueRow this[int id]{get;}
IBlueTableSchema schema {get;}
bool containsID (int id);
}
}
@@ -0,0 +1,228 @@
using System;
using System.Diagnostics;
using System.Collections;
//using BCG.Utilities;
//using BCG.Utilities.General;
namespace BCG
{
public class IBlueRowCollectionBuilder
{
static public bool isBlueDataType(Type t)
{
bool a = t.Equals(BlueDataValidTypes.BOOL) || t.Equals(BlueDataValidTypes.BYTE)
|| t.Equals(BlueDataValidTypes.BYTEARRAY) || t.Equals(BlueDataValidTypes.CHAR)
|| t.Equals(BlueDataValidTypes.DATETIME) || t.Equals(BlueDataValidTypes.DECIMAL)
|| t.Equals(BlueDataValidTypes.DOUBLE) || t.Equals(BlueDataValidTypes.INT)
|| t.Equals(BlueDataValidTypes.ISERIALIZEDTYPE) || t.Equals(BlueDataValidTypes.LONG)
|| t.Equals(BlueDataValidTypes.SBYTE) || t.Equals(BlueDataValidTypes.SINGLE)
|| t.Equals(BlueDataValidTypes.STRING) || t.Equals(BlueDataValidTypes.UINT)
|| t.Equals(BlueDataValidTypes.ULONG);
return a;
}
static public double[] subtract(object o1, object o2)
{
Type t = o1.GetType();
if (t == typeof(DateTime))
return new double[] { ((DateTime)o1 - (DateTime)o2).TotalDays };
else if (t.IsPrimitive && t.IsValueType)
return new double[] { Convert.ToDouble(o1) - Convert.ToDouble(o2) };
else if (t.IsArray && t.GetElementType().IsValueType && t.GetElementType().IsPrimitive)
{
double[] a1 = (double[])Array.ConvertAll<object, double>((object[])o1,
delegate (object o) { return Convert.ToDouble(o); });
double[] a2;
if (o2.GetType().IsArray)
a2 = (double[])Array.ConvertAll<object, double>((object[])o2,
delegate (object o) { return Convert.ToDouble(o); });
else
{
a2 = new double[a1.Length];
for (int i = 0; i < a1.Length; i++)
a2[i] = (double)o2;
}
double[] op = new double[a1.Length];
for (int i = 0; i < a1.Length; i++)
op[i] = a1[i] - a2[i];
return op;
}
else
return new double[] { 0.0 };
}
static public double[] add(object o1, object o2)
{
Type t = o1.GetType();
if (t.IsPrimitive && t.IsValueType)
return new double[] { Convert.ToDouble(o1) + Convert.ToDouble(o2) };
else if (t.IsArray && t.GetElementType().IsValueType && t.GetElementType().IsPrimitive)
{
double[] a1 = (double[])Array.ConvertAll<object, double>((object[])o1,
delegate (object o) { return Convert.ToDouble(o); });
double[] a2;
if (o2.GetType().IsArray)
a2 = (double[])Array.ConvertAll<object, double>((object[])o2,
delegate (object o) { return Convert.ToDouble(o); });
else
{
a2 = new double[a1.Length];
for (int i = 0; i < a1.Length; i++)
a2[i] = (double)o2;
}
double[] op = new double[a1.Length];
for (int i = 0; i < a1.Length; i++)
op[i] = a1[i] + a2[i];
return op;
}
else
return new double[] { 0.0 };
}
static public double[] mult(object o1, object o2)
{
Type t = o1.GetType();
if (t.IsPrimitive && t.IsValueType)
return new double[] { Convert.ToDouble(o1) * Convert.ToDouble(o2) };
else if (t.IsArray && t.GetElementType().IsValueType && t.GetElementType().IsPrimitive)
{
double[] a1 = (double[])Array.ConvertAll<object, double>((object[])o1,
delegate (object o) { return Convert.ToDouble(o); });
double[] a2;
if (o2.GetType().IsArray)
a2 = (double[])Array.ConvertAll<object, double>((object[])o2,
delegate (object o) { return Convert.ToDouble(o); });
else
{
a2 = new double[a1.Length];
for (int i = 0; i < a1.Length; i++)
a2[i] = (double)o2;
}
double[] op = new double[a1.Length];
for (int i = 0; i < a1.Length; i++)
op[i] = a1[i] * a2[i];
return op;
}
else
return new double[] { 0.0 };
}
public delegate double[] BinaryDelegate(object o1, object o2);
static public IBlueRowCollection operate(IBlueRowCollection d1, IBlueRowCollection d2,
BinaryDelegate del)
{
int jj = 0;
ColumnInfo[] newinfo = new ColumnInfo[d1.schema.columnInfo.Length];
foreach (ColumnInfo info in d1.schema.columnInfo)
newinfo[jj++] = new ColumnInfo(info.colName, info.colNo, typeof(double));
IBlueTableSchema schem = new BlueTableSchema(newinfo, d1.schema.tableName + "_" +
d2.schema.tableName + "_processed");
BlueTable res = new BlueTable(schem);
for (int i = 0; i < d1.Count; i++)
{
IBlueRow r1 = BlueDataUtility.extractRow(d1, i);
IBlueRow r2 = BlueDataUtility.extractRow(d2, i);
object[] vals = new object[r1.Count];
int idx = 0;
foreach (ColumnInfo info in res.schema.columnInfo)
vals[idx++] = del(r1[info.colName], r2[info.colName]);
res.addRow(new BlueRow(schem, vals));
}
return res;
}
static public IBlueRowCollection merge(string mergedName, IBlueRowCollection[] data)
{
SortedList colinfos = new SortedList();
foreach (IBlueRowCollection col in data)
{
IBlueTableSchema s = col.schema;
foreach (ColumnInfo info in s.columnInfo)
if (info.colName != "id")
colinfos[info.colName] = info;
}
int colidx = 1;
ColumnInfo[] ciary = new ColumnInfo[colinfos.Count + 1];
ciary[0] = new ColumnInfo("id", 0, typeof(int));
foreach (ColumnInfo ci in colinfos.Values)
{
ciary[colidx] = new ColumnInfo(ci.colName, colidx, ci.colType);
colidx++;
}
BlueTableSchema newschem = new BlueTableSchema(mergedName, ciary);
BlueTable tab = new BlueTable(newschem);
int numRows = 0;
foreach (IBlueRowCollection col in data)
if (col.Count > numRows)
numRows = col.Count;
for (int i = 0; i < numRows; i++)
{
Hashtable ht = new Hashtable();
ht["id"] = i;
foreach (string name in newschem.columnNames)
{
if (name != "id")
{
ht[name] = DBNull.Value;
foreach (IBlueRowCollection col in data)
{
IBlueRow r = BlueDataUtility.extractRow(col, i);
if (r != null && r.Contains(name))
ht[name] = r[name];
}
}
}
BlueRow br = new BlueRow(newschem, ht);
tab.addRow(br);
}
return tab;
}
static public IBlueRowCollection build(string tblname, object data)
{
return build(tblname, "value", data);
}
static public IBlueRowCollection build(string tblname, string dataname, object data)
{
BlueTable result = null;
if (data == null || tblname == null || tblname == string.Empty)
return null;
if (data is IBlueRowCollection)
{
IBlueRowCollection rows = data as IBlueRowCollection;
result = new BlueTable(new BlueTableSchema(rows.schema.columnInfo, tblname));
result.addRows(rows);
}
else if (data is Array)
{
Array da = (Array)data;
Type et = da.GetType().GetElementType();
if (!isBlueDataType(et))
et = typeof(string);
ColumnInfo ci = new ColumnInfo(dataname, 1, et);
ColumnInfo cii = new ColumnInfo("id", 0, typeof(int));
BlueTableSchema schem = new BlueTableSchema(tblname, cii, ci);
result = new BlueTable(schem);
int i = 0;
foreach (object dat in da)
{
object obj = (isBlueDataType(dat.GetType()) ? dat : dat.ToString());
result.addRow(new BlueRow(schem, i++, obj));
}
}
else if (isBlueDataType(data.GetType()))
{
ColumnInfo cii = new ColumnInfo("id", 0, typeof(int));
Type dt = data.GetType();
ColumnInfo ci = new ColumnInfo(dataname, 1, dt);
BlueTableSchema schem = new BlueTableSchema(tblname, cii, ci);
result = new BlueTable(schem);
result.addRow(new BlueRow(schem, 0, data));
}
else
{
ColumnInfo cii = new ColumnInfo("id", 0, typeof(int));
ColumnInfo ci = new ColumnInfo("value", 1, typeof(string));
BlueTableSchema schem = new BlueTableSchema(tblname, cii, ci);
result = new BlueTable(schem);
result.addRow(new BlueRow(schem, 0, data.ToString()));
}
return result;
}
}
}
@@ -0,0 +1,252 @@
using System;
using System.Collections;
//using BCG.Utilities;
namespace BCG
{
/// <summary>
/// Provides a multiple indexing mechanism, required to increase the peformance of the streams
/// </summary>
public class BlueTableIndexException : Exception
{
public BlueTableIndexException(string msg) : base(msg) { }
}
public interface IBlueTableIndex
{
void addRow(IBlueRow row);
void deleteRowOnPrimaryKey(object pkValue);
void deleteRowOnPrimaryKey(IBlueRow row);
void deleteRowOnBothKeys(object pkValue, object skValue);
void deleteRowOnBothKeys(IBlueRow row);
ArrayList searchRowOnPrimaryKey(object pkValue);
ArrayList searchRowOnPrimaryKey(IBlueRow row);
ArrayList searchRowOnBothKeys(object pkValue, object skValue);
ArrayList searchRowOnBothKeys(IBlueRow row);
}
/*
public class BlueTableDoubleIndex : IBlueTableIndex
{
private IBlueRowCollection myTable;
private string myPrimaryIndex;
private string mySecondaryIndex;
private Hashtable myHt = new Hashtable ();
/// <summary>
/// Builds an index with 2 columns of the table acting as primary index and secondary index
/// respectively
/// </summary>
/// <param name="table">
/// table to index
/// </param>
/// <param name="primaryIndex">
/// primary key to index upon
/// </param>
/// <param name="secondaryIndex">
/// column serving as the secondary key
/// </param>
public BlueTableDoubleIndex (IBlueRowCollection table, string primaryIndex, string secondaryIndex)
{
myTable = table;
myPrimaryIndex = primaryIndex;
mySecondaryIndex = secondaryIndex;
myInit ();
}
private void myInit()
{
foreach (IBlueRow row in myTable)
addRow (row);
}
/// <summary>
/// To add a row to the index
/// </summary>
/// <param name="row">
/// row to be added
/// </param>
public void addRow (IBlueRow row)
{
if (myHt[row[myPrimaryIndex]] != null)
((Hashtable)(myHt[row[myPrimaryIndex]]))[row[mySecondaryIndex]] = row;
else
{
TimeManager.start ();
Hashtable ht = new Hashtable();
TimeManager.stop ();
ht[row[mySecondaryIndex]] = row;
myHt[row[myPrimaryIndex]] = ht;
}
}
/// <summary>
/// If index or the primary key has already been deleted appropriate exceptions are thrown
/// </summary>
/// <param name="row"></param>
public void deleteRow (IBlueRow row)
{
if (myHt[row[myPrimaryIndex]] == null)
{
throw new BlueTableIndexException ("Theres no index (primary hash table key) for this index value");
}
((Hashtable)(myHt[row[myPrimaryIndex]])).Remove(row[mySecondaryIndex]);
if (((Hashtable)(myHt[row[myPrimaryIndex]])).Keys.Count == 0)
myHt.Remove(row[myPrimaryIndex]);
}
/// <summary>
/// Returns an array of DataRow that match the primary key being passed as an argument
/// </summary>
/// <param name="o">
/// Primary Key of the table
/// </param>
/// <returns>
/// Array of rows matching the criteria
/// </returns>
public IBlueRow[] findValueFromPrimaryKey (object o)
{
object oht = myHt[o];
if (oht == null)
return new IBlueRow[0];
Hashtable ht = (Hashtable)oht;
IBlueRow[] rows = new IBlueRow[ht.Keys.Count];
ht.Values.CopyTo(rows, 0);
return rows;
}
/// <summary>
/// returns as array of the primary keys
/// </summary>
/// <returns>
/// object[] of primary keys
/// </returns>
public Array getPrimaryKeys()
{
object[] retval = new object [myHt.Keys.Count];
myHt.Keys.CopyTo (retval, 0);
return retval;
}
}
*/
/// <summary>
/// Provides a 1-Level indexing structure.
/// </summary>
public class BlueTableSingleIndex : IBlueTableIndex
{
string myPrimaryKey;
string mySecondaryKey;
Hashtable myHt = new Hashtable();
public BlueTableSingleIndex(string primaryKey, string secondaryKey)
{
myPrimaryKey = primaryKey;
mySecondaryKey = secondaryKey;
}
/// <summary>
/// The primary key is the indexing key, and the secondary key is used as the key for
/// searching and hence deletion
/// </summary>
/// <param name="table"></param>
/// <param name="primaryKey"></param>
/// <param name="secondaryKey"></param>
public BlueTableSingleIndex(IBlueRowCollection table, string primaryKey, string secondaryKey)
: this(primaryKey, secondaryKey)
{
foreach (IBlueRow row in table)
addRow(row);
}
/// <summary>
/// Adds row to the indexing new structure
/// </summary>
/// <param name="row"></param>
public void addRow(IBlueRow row)
{
if (myHt[row[myPrimaryKey]] == null)
myHt[row[myPrimaryKey]] = new ArrayList();
((ArrayList)(myHt[row[myPrimaryKey]])).Add(row);
}
/// <summary>
/// Getts rows based on the Primary Key. Matching of the primary key is the only criteria
/// </summary>
/// <param name="row"></param>
/// <returns></returns>
public ArrayList searchRowOnPrimaryKey(IBlueRow row)
{
return searchRowOnPrimaryKey(row[myPrimaryKey]);
}
public ArrayList searchRowOnPrimaryKey(object pkValue)
{
if (myHt[pkValue] == null)
return new ArrayList();
return (ArrayList)myHt[pkValue];
}
/// <summary>
/// Gets rows based on the primary AND the secondary key
/// </summary>
/// <param name="row"></param>
/// <returns></returns>
public ArrayList searchRowOnBothKeys(IBlueRow row)
{
return searchRowOnBothKeys(row[myPrimaryKey], row[mySecondaryKey]);
}
public ArrayList searchRowOnBothKeys(object pkValue, object skValue)
{
if (myHt[pkValue] == null)
return new ArrayList();
ArrayList matchingList = (ArrayList)myHt[pkValue];
ArrayList retVal = new ArrayList();
foreach (IBlueRow r in matchingList)
if (r[mySecondaryKey].Equals(skValue))
retVal.Add(r);
return retVal;
}
/// <summary>
/// Deletes rows based on the primary key only
/// </summary>
/// <param name="row"></param>
public void deleteRowOnPrimaryKey(IBlueRow row)
{
deleteRowOnPrimaryKey(row[myPrimaryKey]);
}
public void deleteRowOnPrimaryKey(object pkValue)
{
if (myHt[pkValue] == null)
throw new BlueTableIndexException("Key to be deleted was not found in the index");
myHt.Remove(pkValue);
}
public void deleteRowOnBothKeys(IBlueRow row)
{
deleteRowOnBothKeys(row[myPrimaryKey], row[mySecondaryKey]);
}
public void deleteRowOnBothKeys(object pkValue, object skValue)
{
if (myHt[pkValue] == null)
throw new BlueTableIndexException("Key to be deleted was not found in the index");
ArrayList matchingList = (ArrayList)myHt[pkValue];
ArrayList aux = new ArrayList();
foreach (IBlueRow r in matchingList)
if (r[mySecondaryKey].Equals(skValue))
aux.Add(r);
foreach (IBlueRow r in aux)
matchingList.Remove(r);
}
}
}
@@ -0,0 +1,33 @@
using System;
using System.Collections;
namespace BCG
{
/// <summary>
/// Represents a schema for the table, which can be indexed either with the column name or
/// the index of the column
/// </summary>
public interface IBlueTableSchema : ISerializedType
{
/// <summary>
/// index w.r.t. the ordinal
/// </summary>
ColumnInfo this[int index]{get;}
/// <summary>
/// index w.r.t. column name
/// </summary>
ColumnInfo this[string colName]{get;}
/// <summary>
/// Utility method to get all the column names
/// </summary>
string[] columnNames {get;}
/// <summary>
/// Utility method to get all the info about all the columns
/// </summary>
ColumnInfo[] columnInfo{get;}
/// <summary>
/// Name of the table, whose schema it holds
/// </summary>
string tableName {get;}
}
}
@@ -0,0 +1,10 @@
using System;
namespace BCG
{
/// <summary>
/// Tables that dont need to be cloned by the ViewStream into an ImmutableBlueTable should
/// implement this interface
/// </summary>
public interface IImmutableRowCollection : IBlueRowCollection {}
}
@@ -0,0 +1,31 @@
using System;
using System.IO;
namespace BCG
{
/// <summary>
/// Each type that needs to be transported through the PubSub mechanism, should
/// implement this interface, so that PubSub knows how to serialize it and
/// how to contruct an object out of a byte array
/// </summary>
public interface ISerializedType
{
/// <summary>
/// serializes itself and writes into a BinaryWriter; note that the underlying stream's pointer
/// moves to the end of the written bytes
/// </summary>
/// <returns></returns>
void serialize (BinaryWriter bw);
/// <summary>
/// construct itself from the bytes read off the BinaryReader; note that the underlying stream's pointer
/// moves to the end of the bytes read
/// </summary>
/// <param name="br"></param>
void deserialze (BinaryReader br);
/// <summary>
/// Should return the size in bytes, when the instance is written to a stream of binary data
/// </summary>
/// <returns></returns>
int getSerializedSize ();
}
}
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
//using BCG.Utilities.General;
/*
* An instance of this class represents one particular type of tag. The class encapsulates
* the grammer of the tag, and contains a method for extracting metadata from tags of this type,
* as well as a method for converting metatdata back into a tag
*
* */
namespace BCG
{
public interface ITagType
{
//parses a given tag to see if it matches this type. If the tag does not match this type, null is returned.
//If the tag DOES match this type, then the metadata about the tag is returned.
IDictionary<MetaDataField, string> parse(string tag);
//takes metadata about the tag and turns it back into the tag
string generateTag(IDictionary<MetaDataField, string> metaData);
}
}
@@ -0,0 +1,103 @@
using System;
using BCG;
//using BCG.Utilities;
namespace BCG
{
[Serializable]
public class ImmutableBlueTable : IImmutableRowCollection
{
public ImmutableBlueTable(IBlueRowCollection c)
{
mySchema = c.schema;
myHash = new BlueIntHash(c.Count);
myRows = new IBlueRow[c.Count];
int id_col = c.schema["id"].colNo;
int n = 0;
foreach (IBlueRow r in c)
{
myRows[n] = r;
var id = (int)r[id_col];
if (myHash.isPresent(id))
throw new Exception("Duplicate row");
myHash.add(id, n);
n++;
}
}
#region IBlueRowCollection Members}
public IBlueRow this[int id]
{
get
{
try
{
return myRows[myHash.find(id)];
}
catch (BlueIntHash.ElementNotFound)
{
throw new BlueDataException("Row with id " + id.ToString() +
" was not found in table " +
mySchema.tableName);
}
}
}
public IBlueTableSchema schema
{
get
{
return mySchema;
}
}
public bool containsID(int id)
{ return myHash.isPresent(id); }
#endregion
#region ICollection Members
public bool IsSynchronized
{
get
{
return false;
}
}
public int Count
{
get
{ return myRows.Length; }
}
public void CopyTo(Array array, int index)
{
myRows.CopyTo(array, index);
}
public object SyncRoot
{
get
{ return null; }
}
#endregion
#region IEnumerable Members
public System.Collections.IEnumerator GetEnumerator()
{ return myRows.GetEnumerator(); }
#endregion
private BlueIntHash myHash;
private IBlueRow[] myRows;
private IBlueTableSchema mySchema;
}
}
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BCG
{
public class InvalidMetaDataException : Exception
{
public InvalidMetaDataException(MetaDataField field) : base("Error: Invalid metadata: field " + field.ToString() + " was expected but missing.") { }
}
}
@@ -0,0 +1,139 @@
using System;
namespace BCG
{
public class KeysBasedTable2 : IBlueRowCollection
{
private int myID = 0;
private HashTableND myHash;
private BlueTable myTable;
private string[] myKeys;
public KeysBasedTable2 (IBlueTableSchema schema, params string[] keys)
{
myTable = new BlueTable (schema);
myKeys = keys;
myHash = new HashTableND (myKeys.Length);
}
#region IBlueRowCollection Members
public IBlueRow this[int id] {get {return myTable[id];}}
public bool containsID (int id) {return myTable.containsID (id);}
public IBlueTableSchema schema {get {return myTable.schema;}}
#endregion
#region ICollection Members
public bool IsSynchronized {get {return myTable.IsSynchronized;}}
public int Count {get {return myTable.Count;}}
public void CopyTo (Array array, int index) {myTable.CopyTo (array, index);}
public object SyncRoot {get {return myTable.SyncRoot;}}
#endregion
#region IEnumerable Members
public System.Collections.IEnumerator GetEnumerator() {return myTable.GetEnumerator ();}
#endregion
public void update (IBlueRow row, out IBlueRow replacedRow)
{
object[] keyVals = myGetKeyVals (row);
replacedRow = myHash.getValue (keyVals) as IBlueRow;
if (replacedRow == null)
{
myHash.add (keyVals, row);
}
else
{
myTable.deleteRow ((int)replacedRow["id"]);
myHash.setValue (keyVals, row);
}
myTable.addRow (row);
}
public void updateWithIDGeneration(IBlueRow row, out IBlueRow addedRow, out IBlueRow deletedRow)
{
object[] keyVals = myGetKeyVals(row);
deletedRow = myHash.getValue(keyVals) as IBlueRow;
if (deletedRow == null)
{
addedRow = BlueDataUtility.getNewReplacedRow(row, myID++, "id");
myHash.add(keyVals, addedRow);
myTable.addRow(addedRow);
}
else
{
addedRow = BlueDataUtility.getNewReplacedRow(row, deletedRow["id"], "id");
myHash.setValue(keyVals, addedRow);
myTable.deleteRow(deletedRow);
myTable.addRow(addedRow);
}
}
public void updateMultipleRows(IBlueRowCollection updatesRows,
out IBlueRowCollection removedRows, out IBlueRowCollection addedRows)
{
BlueTable preUpdateState = new BlueTable(myTable);
removedRows = new BlueTable(schema); addedRows = new BlueTable(schema);
foreach (IBlueRow r in updatesRows)
{
IBlueRow rRow, aRow;
updateWithIDGeneration(r, out aRow, out rRow);
if (rRow != null && preUpdateState.containsID ((int)rRow["id"]))
(removedRows as BlueTable).forceAddRow(rRow);
(addedRows as BlueTable).forceAddRow(aRow);
}
}
public void deleteRow (IBlueRow row, out IBlueRow deletedRow)
{
object[] keys = myGetKeyVals(row);
deletedRow = myHash.getValue(keys) as IBlueRow;
if (deletedRow != null) {
myTable.deleteRow(deletedRow);
myHash.remove(keys);
}
}
public void deleteRow (IBlueRow row)
{
IBlueRow t1;
deleteRow (row, out t1);
}
public void deleteMultipleRows(IBlueRowCollection rows)
{
foreach (IBlueRow r in rows)
deleteRow(r);
}
public void update (IBlueRow row)
{
IBlueRow t1;
update (row, out t1);
}
public IBlueRow getRow (object[] keys)
{
return myHash.getValue (keys) as IBlueRow;
}
public IBlueRow getRow(IBlueRow r)
{
return getRow(myGetKeyVals(r));
}
public IBlueRow this[params object[] keyVals] {get {return myHash.getValue (keyVals) as IBlueRow;}}
private object[] myGetKeyVals (IBlueRow r) {return BlueDataUtility.getSelectedValues (r, myKeys);}
}
}
@@ -0,0 +1,324 @@
using System;
using System.Data;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using BCG.Utilities.General;
namespace BCG
{
/// <summary>
/// The LookupTableSchema is an immutable schema holder. Once created it cannot be modified. It can be created
/// either from a DataTable or from an Array of ColumnInfo. The methods through which the user can access it
/// are the indexers (both order and name based), and the property columnInfo and columnNames, that return
/// the respective data, sorted according to the ColumnOrder.
/// </summary>
[Serializable]
public class LookupTableSchema : IBlueTableSchema
{
public class LookupTableSchemaSerializationHelper
{
public static object parse(Type t, string data)
{
if (t == BlueDataValidTypes.BOOL)
return Boolean.Parse(data);
else if (t == BlueDataValidTypes.BYTE)
return Byte.Parse(data);
else if (t == BlueDataValidTypes.CHAR)
return char.Parse(data);
else if (t == BlueDataValidTypes.DATETIME)
return DateTime.Parse(data);
else if (t == BlueDataValidTypes.DOUBLE)
return Double.Parse(data);
else if (t == BlueDataValidTypes.DECIMAL)
return Decimal.Parse(data);
else if (t == BlueDataValidTypes.INT)
return int.Parse(data);
else if (t == BlueDataValidTypes.LONG)
return long.Parse(data);
else if (t == BlueDataValidTypes.SBYTE)
return SByte.Parse(data);
else if (t == BlueDataValidTypes.STRING)
return data;
else if (t == BlueDataValidTypes.UINT)
return uint.Parse(data);
else if (t == BlueDataValidTypes.ULONG)
return ulong.Parse(data);
else if (t == BlueDataValidTypes.SINGLE)
return Single.Parse(data);
else
throw new ApplicationException("Can not be parsed");
}
public static void serialize(BinaryWriter bw, Type t, object o)
{
if (t == BlueDataValidTypes.BOOL)
bw.Write((bool)o);
else if (t == BlueDataValidTypes.BYTE)
bw.Write((byte)o);
else if (t == BlueDataValidTypes.CHAR)
bw.Write((char)o);
else if (t == BlueDataValidTypes.DATETIME)
bw.Write(((DateTime)o).Ticks);
else if (t == BlueDataValidTypes.DOUBLE)
bw.Write((double)o);
else if (t == BlueDataValidTypes.DECIMAL)
bw.Write((decimal)o);
else if (t == BlueDataValidTypes.INT)
bw.Write((int)o);
else if (t == BlueDataValidTypes.LONG)
bw.Write((long)o);
else if (t == BlueDataValidTypes.SBYTE)
bw.Write((sbyte)o);
else if (t == BlueDataValidTypes.STRING)
bw.Write(o as string);
else if (t == BlueDataValidTypes.SINGLE)
bw.Write((Single)o);
else if (t == BlueDataValidTypes.BYTEARRAY)
{
byte[] bs = o as byte[];
bw.Write(bs.Length);
bw.Write(bs);
}
else if (t == BlueDataValidTypes.UINT)
bw.Write((uint)o);
else if (t == BlueDataValidTypes.ULONG)
bw.Write((ulong)o);
else
throw new ApplicationException("Unsupported type");
}
public static object deserialize(BinaryReader br, Type t)
{
if (t == BlueDataValidTypes.BOOL)
return br.ReadBoolean();
else if (t == BlueDataValidTypes.BYTE)
return br.ReadByte();
else if (t == BlueDataValidTypes.CHAR)
return br.ReadChar();
else if (t == BlueDataValidTypes.DATETIME)
return new DateTime(br.ReadInt64());
else if (t == BlueDataValidTypes.DOUBLE)
return br.ReadDouble();
else if (t == BlueDataValidTypes.DECIMAL)
return br.ReadDecimal();
else if (t == BlueDataValidTypes.INT)
return br.ReadInt32();
else if (t == BlueDataValidTypes.LONG)
return br.ReadInt64();
else if (t == BlueDataValidTypes.SBYTE)
return br.ReadSByte();
else if (t == BlueDataValidTypes.STRING)
return br.ReadString();
else if (t == BlueDataValidTypes.BYTEARRAY)
return br.ReadBytes(br.ReadInt32());
else if (t == BlueDataValidTypes.UINT)
return br.ReadUInt32();
else if (t == BlueDataValidTypes.ULONG)
return br.ReadUInt64();
else if (t == BlueDataValidTypes.SINGLE)
return br.ReadSingle();
else
throw new ApplicationException("Unsupported type");
}
}
protected Hashtable myIndexByColName = new Hashtable();
protected ColumnInfo[] myIndexByColOrder;
private string[] myTableColNames;
private BlueRowLookupGroup[] myLookupGroups;
protected string myTableName;
public LookupTableSchema() { }
public LookupTableSchema(string tableName, ColumnInfo[] cis, BlueRowLookupGroup[] groups)
{
myConstruct(tableName, cis, groups);
}
private void myConstruct(string tableName, ColumnInfo[] cis, BlueRowLookupGroup[] groups)
{
myIndexByColOrder = new ColumnInfo[cis.Length];
myTableName = tableName;
foreach (ColumnInfo ci in cis)
{
myIndexByColOrder[ci.colNo] = ci;
myIndexByColName[ci.colName] = ci;
}
myLookupGroups = groups;
foreach (BlueRowLookupGroup group in groups)
{
group.buildEntrySchema(this);
}
}
public ColumnInfo this[string colName]
{
get
{
object retVal = myIndexByColName[colName];
return retVal != null ? retVal as ColumnInfo : null;
}
}
public BlueRowLookupGroup[] lookupGroups
{
get { return myLookupGroups; }
}
public ColumnInfo this[int colNo]
{
get { return myIndexByColOrder[colNo]; }
}
/// <summary>
/// The returned Array is sorted according to the Column No
/// </summary>
public string[] columnNames
{
get
{
if (myTableColNames == null)
{
myTableColNames = new string[myIndexByColName.Count];
foreach (DictionaryEntry de in myIndexByColName)
{
ColumnInfo temp = (ColumnInfo)de.Value;
myTableColNames[temp.colNo] = temp.colName;
}
}
return myTableColNames;
}
}
/// <summary>
/// The returned Array is sorted according to the Column No
/// </summary>
public ColumnInfo[] columnInfo
{
get { return myIndexByColOrder; }
}
public string tableName { get { return myTableName; } }
#region ISerializedType methods
public void serialize(BinaryWriter bw)
{
bw.Write(columnNames.Length);
bw.Write(tableName);
//first, write the column names
foreach (ColumnInfo ci in columnInfo)
{
bw.Write(ci.colName);
bw.Write(ci.colType.ToString());
if (ci.isConstant)
{
bw.Write(true);
LookupTableSchemaSerializationHelper.serialize(bw, ci.colType, ci.colValue);
}
else
{
bw.Write(false);
bw.Write(ci.lookupTableGroup);
}
}
//this is here for error checking
bw.Write(myLookupGroups.Length);
//now, write the size of each lookup table group
foreach (BlueRowLookupGroup group in myLookupGroups)
{
bw.Write(group.groupNumber);
bw.Write(group.tableSize);
bw.Write(group.groupColNames.Length);
foreach (string colName in group.groupColNames)
{
//write the index of this column in the table
bw.Write(this[colName].colNo);
}
}
}
public void deserialze(BinaryReader br)
{
int numCols = br.ReadInt32();
//these are the final output columns that will be created
ColumnInfo[] columnInfos = new ColumnInfo[numCols];
Dictionary<int, List<string>> colLookupGroupMap = new Dictionary<int, List<string>>();
string tableName = br.ReadString();
for (int colNo = 0; colNo < numCols; ++colNo)
{
string colName = br.ReadString();
Type colType = TypeInstanceLoader.loadType(br.ReadString(), true);
bool colIsConst = br.ReadBoolean();
if (colIsConst)
{
columnInfos[colNo] = new ColumnInfo(colName, colNo, LookupTableSchemaSerializationHelper.deserialize(br, colType));
}
else
{
short thisColGroup = br.ReadInt16();
columnInfos[colNo] = new ColumnInfo(colName, colNo, colType, thisColGroup);
}
}
int numGroups = br.ReadInt32();
BlueRowLookupGroup[] theGroups = new BlueRowLookupGroup[numGroups];
for (int group = 0; group < numGroups; ++group)
{
short groupNo = br.ReadInt16();
short groupSizeInBits = br.ReadInt16();
int numColsInGroup = br.ReadInt32();
int[] groupCols = new int[numColsInGroup];
string[] groupColNames = new string[numColsInGroup];
for (int col = 0; col < numColsInGroup; ++col)
{
groupCols[col] = br.ReadInt32();
groupColNames[col] = columnInfos[groupCols[col]].colName;
}
//create the group
theGroups[groupNo] = new BlueRowLookupGroup(groupNo, groupSizeInBits, groupColNames);
}
myConstruct(tableName, columnInfos, theGroups);
}
public int getSerializedSize()
{
throw new NotImplementedException("Feature for provided constant values to" +
"non-primitive types in schema, not provided as of now");
}
#endregion
}
}
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BCG
{
/*
These fields are first going to be used by BlueRay to help it determine what differnet tags mean, but eventually
* the goal is that they will be used for all services in this system.
*
* not all metadata fields are present in all metadata; it depends upon the class. For example, a bluepages service
* probably won't have a 'symbol' or 'exchange' field populated, but it'll probably have the 'type' of service populated
*/
public enum MetaDataField { Protocol, //is this a bluepages service, a pubsub tag, a blueray tag, a 29 west thing, or what
Type, //if it's a multicast, is it option, fireprice, whatever. If it's bluepages, is it persistence, pubsub, whatever
Symbol,
Exchange,
Platform,
MarketDataType, // BAM (bid, ask, mid), last, etc
RealName }; //does this thing have a better name we could call it
}
@@ -0,0 +1,141 @@
using System;
using System.Reflection;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
namespace BCG.Utilities.General
{
public class CSVParser
{
enum state
{
waitingForToken,
inQuote,
waitingForSeparator,
possibleEndOfQuote,
inToken,
}
class CSVParseException : Exception
{
public CSVParseException (string msg) : base (msg)
{}
}
public static string [] parse (string csv)
{
ArrayList tokens = new ArrayList();
StringBuilder currentToken = null;
state s = state.waitingForToken;
int n = 0;
foreach (char c in csv)
{
n++;
switch (s)
{
case state.waitingForToken:
if (c == ',')
tokens.Add("");
else if (c == '"')
{
s = state.inQuote;
currentToken = new StringBuilder();
}
else if (Char.IsWhiteSpace(c))
continue;
else
{
s = state.inToken;
currentToken = new StringBuilder();
currentToken.Append(c);
}
break;
case state.inQuote:
if (c == '"')
{
s = state.possibleEndOfQuote;
}
else
{
currentToken.Append(c);
}
break;
case state.possibleEndOfQuote:
if (c == '"')
{
s = state.inQuote;
currentToken.Append(c);
}
else if (Char.IsWhiteSpace(c))
{
tokens.Add(currentToken.ToString());
s = state.waitingForSeparator;
}
else if (c == ',')
{
tokens.Add(currentToken.ToString());
s = state.waitingForToken;
}
else
throw new Exception("Expected ',' or end of line at column " + n.ToString());
break;
case state.waitingForSeparator :
if (c == ',')
{
s = state.waitingForToken;
}
else if (Char.IsWhiteSpace(c))
continue;
else
throw new Exception("Expected ',' or end of line at column " + n.ToString());
break;
case state.inToken :
if (c == ',')
{
s = state.waitingForToken;
tokens.Add(currentToken.ToString());
}
else
{
currentToken.Append(c);
}
break;
}
}
if (s == state.inToken || s == state.possibleEndOfQuote)
tokens.Add(currentToken.ToString());
else if (s == state.waitingForToken)
tokens.Add("");
else if ( s != state.waitingForSeparator)
throw new Exception("Missing column or unterminated string at end of line");
string [] result = new string[tokens.Count];
tokens.CopyTo(result,0);
return result;
}
static void test()
{
for (;;)
{
try
{
foreach (string s in parse(Console.ReadLine()))
{
Console.WriteLine(s);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
}
@@ -0,0 +1,397 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace BCG
{
[Serializable]
public class CollectionHelpers
{
public delegate bool EqualDelegate(object a, object b);
public static ArrayList difference (ICollection a, ICollection b,
EqualDelegate ed)
{
ArrayList result = new ArrayList();
foreach (Object aa in a)
{
bool found = false;
foreach (Object bb in b)
{
if (ed(aa, bb))
{
found = true;
break;
}
}
if (!found)
result.Add (aa);
}
return result;
}
public static ArrayList difference (ICollection a, ICollection b)
{
ArrayList result = new ArrayList();
foreach (Object aa in a)
{
bool found = false;
foreach (Object bb in b)
{
if (aa.Equals(bb))
{
found = true;
break;
}
}
if (!found)
result.Add (aa);
}
return result;
}
public static ArrayList sorted_difference (ICollection sa, ICollection sb) {
ArrayList result = new ArrayList ();
IEnumerator ia = sa.GetEnumerator ();
IEnumerator ib = sb.GetEnumerator ();
IEnumerator iremainder = ia;
bool moreA,moreB;
for (moreA = ia.MoveNext (), moreB = ib.MoveNext (); moreA && moreB;) {
int cmp = ((IComparable)ia.Current).CompareTo (ib.Current);
if (cmp < 0) {
result.Add (ia.Current);
moreA = ia.MoveNext ();
iremainder = ia;
}
else if (cmp > 0)
moreB = ib.MoveNext ();
else {
moreA = ia.MoveNext ();
moreB = ib.MoveNext ();
}
}
for (; moreA; moreA = iremainder.MoveNext ())
result.Add (iremainder.Current);
return result;
}
public static bool typedequal<T>(ICollection<T> lhs, ICollection<T> rhs)
{
if(lhs == null || rhs == null)
{
return false;
}
T[] l = new T[lhs.Count];
T[] r = new T[rhs.Count];
lhs.CopyTo(l, 0);
rhs.CopyTo(r, 0);
return equal((IList)l, (IList)r);
}
public static bool equal(ICollection lhs, ICollection rhs)
{
if ( lhs == null || rhs == null )
{
return false;
}
Array l = new object[lhs.Count];
Array r = new object[rhs.Count];
lhs.CopyTo(l, 0);
rhs.CopyTo(r,0);
return equal(l,r);
}
public static bool equal(IList lhs, IList rhs)
{
if ( lhs == null || rhs == null )
{
return false;
}
if(lhs.Count != rhs.Count)
return false;
else
{
for(int i = 0; i < lhs.Count; i++)
{
if(!lhs[i].Equals(rhs[i]))
return false;
}
return true;
}
}
public static Array fromArrayList(ArrayList al, Type t)
{
Array result = Array.CreateInstance(t, al.Count);
al.CopyTo(result,0);
return result;
}
public static Array append (Array a1, Array a2, Type t)
{
Array result = Array.CreateInstance(t, a1.Length + a2.Length);
a1.CopyTo(result,0);
a2.CopyTo(result,a1.Length);
return result;
}
public static Array subtract (Array from, params object [] elemens)
{
return fromArrayList(
difference(from, elemens),from.GetType().
GetElementType());
}
public static bool hasDuplicates (ICollection collection)
{
Hashtable h = new Hashtable();
foreach (object o in collection)
h[o] = true;
return h.Keys.Count != collection.Count;
}
public static ICollection subtract (IDictionary d1, IDictionary d2)
{
ArrayList result = new ArrayList();
foreach (object d1k in d1.Keys)
if (!d2.Contains(d1k))
result.Add(d1k);
return result;
}
/// <summary>
/// Is c1 a subset of c2?
/// </summary>
/// <param name="c1"></param>
/// <param name="c2"></param>
/// <returns></returns>
public static bool isSubset (ICollection c1, ICollection c2)
{return difference(c1,c2).Count == 0;}
public static void orderedUniqueInsert (ArrayList coll,object item) {
int ifnd = coll.BinarySearch (item);
if (ifnd < 0)
coll.Insert (~ifnd,item);
else
coll[ifnd] = item;
}
public static void orderedRemove (ArrayList coll,object item) {
int ifnd = coll.BinarySearch (item);
if (ifnd >= 0)
coll.RemoveAt (ifnd);
}
public static bool t_equals<T>(T t1, T t2) { return (t1 != null && t2 != null && t1.Equals(t2)) || (t1 == null && t2 == null); }
public static bool dictionaries_equal<K, V>(IDictionary<K, V> new_d, IDictionary<K, V> old_d, Func<V, V, bool> comp = null)
{
IDictionary<K, V> deleted_d, added_d, changed_d;
compare_dictionaries(new_d, old_d, out deleted_d, out added_d, out changed_d, comp);
return deleted_d.Count == 0 && added_d.Count == 0 && changed_d.Count == 0;
}
public static void compare_dictionaries<K, V>(IDictionary<K, V> new_d, IDictionary<K, V> old_d,
out IDictionary<K, V> deleted_d, out IDictionary<K, V> added_d, out IDictionary<K, V> changed_d,
Func<V, V, bool> comp = null)
{
new_d = new_d ?? new Dictionary<K, V>();
old_d = old_d ?? new Dictionary<K, V>();
deleted_d = new Dictionary<K, V>();
added_d = new Dictionary<K, V>();
changed_d = new Dictionary<K, V>();
comp = comp ?? t_equals<V>;
foreach (KeyValuePair<K, V> p in new_d)
{
V old_d_val = default(V);
if (!old_d.TryGetValue(p.Key, out old_d_val))
added_d.Add(p);
else if (!comp(p.Value, old_d_val))
changed_d.Add(p);
}
foreach (KeyValuePair<K, V> p in old_d)
if (!new_d.ContainsKey(p.Key))
deleted_d.Add(p);
}
/// Compares 2 dictionaries with the same key types
/// but different data types. Takes a function to
/// compare the 2 data types
public static void compare_dictionaries<K, N, O>(IReadOnlyDictionary<K, N> new_d, IReadOnlyDictionary<K, O> old_d,
out IDictionary<K, O> deleted_d, out IDictionary<K, N> added_d, out IDictionary<K, Tuple<N,O>> changed_d,
Func<N, O, bool> comp)
{
new_d = new_d ?? new Dictionary<K, N>();
old_d = old_d ?? new Dictionary<K, O>();
deleted_d = new Dictionary<K, O>();
added_d = new Dictionary<K, N>();
changed_d = new Dictionary<K, Tuple<N,O>>();
foreach (KeyValuePair<K, N> p in new_d)
{
O old_val = default(O);
if (!old_d.TryGetValue(p.Key, out old_val))
added_d.Add(p);
else if (!comp(p.Value, old_val))
changed_d.Add(p.Key, new Tuple<N, O>(p.Value, old_val));
}
foreach (KeyValuePair<K, O> p in old_d)
if (!new_d.ContainsKey(p.Key))
deleted_d.Add(p);
}
public static void compare_dictionary_keys<K, V>(IDictionary<K, V> new_d, IDictionary<K, V> old_d,
out HashSet<K> deleted_keys, out HashSet<K> added_keys)
{
new_d = new_d ?? new Dictionary<K, V>();
old_d = old_d ?? new Dictionary<K, V>();
deleted_keys = new HashSet<K>();
added_keys = new HashSet<K>();
foreach (KeyValuePair<K, V> p in new_d)
if (!old_d.ContainsKey(p.Key))
added_keys.Add(p.Key);
foreach (KeyValuePair<K, V> p in old_d)
if (!new_d.ContainsKey(p.Key))
deleted_keys.Add(p.Key);
}
public static void compare_hashsets<T>(HashSet<T> new_d, HashSet<T> old_d,
out HashSet<T> deleted_d, out HashSet<T> added_d)
{
added_d = new HashSet<T>();
deleted_d = new HashSet<T>();
foreach (T t in new_d)
if (!old_d.Contains(t))
added_d.Add(t);
foreach (T t in old_d)
if (!new_d.Contains(t))
deleted_d.Add(t);
}
public static IEnumerable<T> append_arrays<T>(params IEnumerable<T>[] ienum_list)
{
List<T> rv = new List<T>();
foreach (IEnumerable<T> ienum in ienum_list)
rv.AddRange(ienum);
return rv;
}
public static bool hashset_equals<T>(HashSet<T> new_d, HashSet<T> old_d)
{
HashSet<T> deleted_d, added_d;
compare_hashsets(new_d, old_d, out deleted_d, out added_d);
return deleted_d.Count == 0 && added_d.Count == 0;
}
public static void find_neighbors<T>(IEnumerable<T> items, Comparison<T> comp_func, T comp, out int low_index, out int high_index)
{
low_index = high_index = -1;
for (int i = 0; i < items.Count(); i++)
{
T p = items.ElementAt (i);
if (comp_func (comp, p) <= 0)
{
high_index = i;
low_index = i - 1;
if (low_index >= 0)
{
T low_index_val = items.ElementAt (low_index);
if (comp_func(comp, low_index_val) >= 0)
break;
}
else
break;
}
else
{
low_index = i;
high_index = i + 1;
if (high_index <= items.Count() - 1)
{
T high_index_val = items.ElementAt (high_index);
if (comp_func(comp, high_index_val) <= 0)
break;
}
else
{
high_index = -1;
break;
}
}
}
}
public static void find_neighbors<T, P>(IEnumerable<T> list, P nearest_to, Func<T, P, double> diff_measure,
out T low_neighbor, out T high_neighbor)
{
low_neighbor = default(T); high_neighbor = default(T);
list = list ?? new List<T>();
double low_min_diff = double.MaxValue, high_min_diff = double.MaxValue;
foreach (T t in list)
{
double this_diff = Math.Abs(diff_measure(t, nearest_to));
if (this_diff <= 0)
{
this_diff = Math.Abs(this_diff);
if (this_diff < low_min_diff)
{
low_min_diff = this_diff;
low_neighbor = t;
}
}
else
{
if (this_diff < high_min_diff)
{
high_min_diff = this_diff;
high_neighbor = t;
}
}
}
}
public static T find_nearest<T,P>(IEnumerable<T> list, P nearest_to, Func<T, double> diff_measure)
{
list = list ?? new List<T>();
double min_diff = double.MaxValue;
T rv = default(T);
foreach (T t in list)
{
double this_diff = Math.Abs(diff_measure(t));
if (this_diff < min_diff)
{
rv = t;
min_diff = this_diff;
}
}
return rv;
}
public static bool are_maps_equal<K, T>(IDictionary<K, T> m1, IDictionary<K, T> m2, Func<T, T, bool> equality_comparer = null)
{
m1 = m1 ?? new Dictionary<K, T>();
m2 = m2 ?? new Dictionary<K, T>();
if (equality_comparer == null)
equality_comparer = (t1, t2) => { return t1.Equals(t2); };
IDictionary<K, T> deleted_d, added_d;
IDictionary<K, Tuple<T, T>> changed_d;
compare_dictionaries<K, T, T>(new Dictionary<K, T>(m1), new Dictionary<K, T>(m2),
out deleted_d, out added_d, out changed_d, equality_comparer);
return deleted_d.Count + added_d.Count + changed_d.Count == 0;
}
}
}
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BCG
{
public class TypeHelper
{
private static HashSet<TypeCode> _numeric_type_codes = new HashSet<TypeCode>
{
TypeCode.Byte, TypeCode.Decimal, TypeCode.Double, TypeCode.Int16, TypeCode.Int32,
TypeCode.Int64, TypeCode.SByte, TypeCode.Single, TypeCode.UInt16, TypeCode.UInt32,
TypeCode.UInt64
};
public static bool is_dbnull(object o) { return o == null || o == DBNull.Value; }
public static bool is_numeric_type(object o)
{
return is_dbnull(o) ? false : _numeric_type_codes.Contains(Type.GetTypeCode(o.GetType()));
}
public static T[] parse_string_to_typed_array<T>(string text, params char[] seperators)
{
if (text == null || text.Length == 0)
return new T[] { };
else
{
List<T> rv = new List<T>();
string[] toks = text.Split(seperators);
foreach (string t in toks)
{
string fin_t = t.Trim();
if (!String.IsNullOrEmpty(fin_t))
{
try
{
T ct = (T)Convert.ChangeType(fin_t, typeof(T));
if (ct != null)
rv.Add(ct);
}
catch (Exception) { }
}
}
return rv.ToArray();
}
}
}
}
@@ -0,0 +1,48 @@
using System;
using System.Reflection;
using System.Diagnostics;
namespace BCG.Utilities.General
{
public class TypeInstanceLoader
{
private TypeInstanceLoader ()
{
}
public static object loadInstance (string typename, params object[] parameters)
{
Type t = null;
try
{
t = loadType (typename);
}
catch (ApplicationException e)
{
Debug.Assert (false, e.Message);
}
return Activator.CreateInstance (t, parameters);
}
public static Type loadType (string name)
{
return loadType(name, false);
}
public static Type loadType (string name, bool throwOnError)
{
Type t = null;
Assembly[] ass = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly a in ass)
{
t = a.GetType(name, false, true);
if (t != null)
return t;
}
if(throwOnError)
throw new ApplicationException ("Can't find type '" + name + "' in all assemblies.");
else
return null;
}
}
}
@@ -0,0 +1,145 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace BCG
{
/// <summary>
/// A generalization of TimeWindowTable. Derive a class from this and implement the IsInTable() method to create a table with a rolling window.
/// Note: When the window is rolled forward it will only call IsInTable() until it finds a row that it can keep. Make sure that the sort is ascending and that IsInTable works on a sorted collection.
/// </summary>
public abstract class MovingWindowTable : IBlueRowCollection
{
protected IComparer<IBlueRow> myRowComparer;
protected IBlueTableSchema mySchema;
protected List<IBlueRow> mySortedRows = new List<IBlueRow>();
protected Lazy<Dictionary<int, IBlueRow>> myIDToRows;
#region constructors
public MovingWindowTable(IBlueTableSchema schema, IComparer<IBlueRow> comparer)
{
this.myRowComparer = comparer;
this.mySchema = schema;
this.myIDToRows = new Lazy<Dictionary<int, IBlueRow>>(InitializeIDToRows);
}
#endregion
public bool addRow(IBlueRow r)
{
if (windowContains(r))
{
int insertIndex = this.mySortedRows.BinarySearch(r, this.myRowComparer);
if (insertIndex < 0) insertIndex = ~insertIndex;
this.mySortedRows.Insert(insertIndex, r);
if (this.myIDToRows.IsValueCreated)
{
this.myIDToRows.Value[(int)r["id"]] = r;
}
return true;
}
return false;
}
public IBlueRowCollection moveWindow()
{
BlueTable removed = new BlueTable(this.mySchema);
if (this.mySortedRows.Count == 0)
return removed;
// we don't necessarily know whether the table is sorted ascending or descending since we let the user pass in a comparer
// so we'll test both ends of the table and start pulling from the end that is outside the window
int index;
if (windowContains(this.mySortedRows[0]))
{
for (index = this.mySortedRows.Count - 1; index >= 0; index--)
{
if (windowContains(this.mySortedRows[index])) break;
removed.addRow(this.mySortedRows[index]);
}
index++;
this.mySortedRows.RemoveRange(index, this.mySortedRows.Count - index);
}
else
{
for (index = 0; index < this.mySortedRows.Count; index++)
{
if (windowContains(this.mySortedRows[index])) break;
removed.addRow(this.mySortedRows[index]);
}
this.mySortedRows.RemoveRange(0, index);
}
return removed;
}
protected abstract bool windowContains(IBlueRow row);
private Dictionary<int, IBlueRow> InitializeIDToRows()
{
Dictionary<int, IBlueRow> idMap = new Dictionary<int, IBlueRow>(this.mySortedRows.Count);
foreach (var row in this.mySortedRows)
{
idMap[(int)row["id"]] = row;
}
return idMap;
}
#region IBlueRowCollection Members
public IBlueRow this[int id]
{
get { return this.myIDToRows.Value[id]; }
}
public IBlueTableSchema schema
{
get { return this.mySchema; }
}
public bool containsID(int id)
{
return this.myIDToRows.Value.ContainsKey(id);
}
#endregion
#region ICollection Members
public void CopyTo(Array array, int index)
{
((System.Collections.ICollection)this.mySortedRows).CopyTo(array, index);
}
public int Count
{
get { return this.mySortedRows.Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
// Throw notimplemented rather than return null which will probably do bad things
get { throw new NotImplementedException(); }
}
#endregion
#region IEnumerable Members
public System.Collections.IEnumerator GetEnumerator()
{
return this.mySortedRows.GetEnumerator();
}
#endregion
}
}
@@ -0,0 +1,56 @@
using System;
using System.Data;
using System.Collections;
namespace BCG
{
[Serializable]
public class MutableBlueRow : BlueRow
{
public MutableBlueRow (IBlueTableSchema schema)
: this (schema, MutableBlueRow.myCreateArray (schema.columnNames.Length, DBNull.Value))
{}
public MutableBlueRow (IBlueTableSchema schema, params object[] values)
: base (schema, MutableBlueRow.myCloneArray (schema,values))
{}
public MutableBlueRow (string tableName, IDictionary row)
: base (tableName, row)
{}
public MutableBlueRow (IBlueTableSchema schema, IDictionary row)
: base (schema, row) { }
public MutableBlueRow (IBlueRow r)
: base (r)
{}
public void setValue (string columnName, object newValue)
{
setValue (mySchema[columnName].colNo, newValue);
}
public void setValue (int colNo, object newValue)
{
myValues[colNo] = newValue;
}
private static object[] myCloneArray (IBlueTableSchema schema,object[] vals)
{
object[] retVal = new object[schema.columnNames.Length];
for (int i =0; i < vals.Length; i++)
retVal[i] = vals[i];
return retVal;
}
private static object[] myCreateArray (int sz, object o)
{
object[] rv = new object[sz];
for (int i = 0; i < sz; i++)
rv[i] = DBNull.Value;
return rv;
}
}
}
@@ -0,0 +1,70 @@
using System;
using System.Collections;
using System.Data;
//using BCG.Utilities;
namespace BCG
{
[Serializable]
public class OrderedBlueTable : BlueTable
{
protected ArrayList myOrderedRows = new ArrayList();
protected OrderedBlueTable() { }
public OrderedBlueTable(IBlueTableSchema schema) : base(schema) { }
public OrderedBlueTable(IBlueRowCollection table) : base(table) { }
public OrderedBlueTable(IBlueRowCollection table, int count) : this(table, 0, count) { }
public OrderedBlueTable(IBlueRowCollection table, int startIndex, int count)
{
//BlueEventLog.logAssert (count >= 0 && startIndex + count <= table.Count);
mySchema = table.schema;
int index = 0;
foreach (IBlueRow r in table)
{
if (index++ >= startIndex)
addRow(r);
if (index == startIndex + count)
break;
}
}
public override void clear()
{
base.clear();
myOrderedRows.Clear();
}
public override void addRow(IBlueRow row)
{
base.addRow(row);
myOrderedRows.Add(row);
}
public override void addRow(DataRow row)
{
base.addRow(row);
myOrderedRows.Add(myIdIndex[row["id"]]);
}
public override bool deleteRow(int id)
{
if (myIdIndex[id] == null)
return false;
myOrderedRows.Remove(myIdIndex[id]);
myIdIndex.Remove(id);
return true;
}
#region ICollection members
public override IEnumerator GetEnumerator() { return myOrderedRows.GetEnumerator(); }
public override void CopyTo(Array array, int index) { myOrderedRows.CopyTo(array, index); }
#endregion
}
}
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BCG
{
public class OrderedInplaceCircularBlueTable
{
}
}
@@ -0,0 +1,27 @@
using System;
namespace BCG.Utilities.General
{
/// <summary>
/// Properties or public fields of a type can be mapped by this attribute, governing
/// their visiblity in the schema that can be constructed out of the object
/// </summary>
public class SchemaVisibility : Attribute
{
private bool myVisible = true;
/// <summary>
/// Tagging visbible as true, results in the property/field to
/// map to itself in the schema. This has the same result as
/// not tagging a field/property
/// </summary>
/// <param name="visible"></param>
public SchemaVisibility (bool visible)
{
myVisible = visible;
}
public bool visible{get{return myVisible;}}
}
}
@@ -0,0 +1,65 @@
using System;
using System.Collections;
namespace BCG
{
/// <summary>
/// Optimized for the scneario when id's are sequential starting from 0 (no indexing is necessary etc)
/// </summary>
public class SequentialIDBlueTable : IBlueRowCollection
{
private IBlueRow[] myRows;
private IBlueTableSchema mySchema;
/// <summary>
/// The refernce to the rows is kept. So if user wants to reuse the variable, its best
/// to pass in a copied value
/// </summary>
/// <param name="schema"></param>
/// <param name="rows"></param>
public SequentialIDBlueTable (IBlueTableSchema schema, IBlueRow[] rows)
{
myRows = rows;
mySchema = schema;
}
public SequentialIDBlueTable(IBlueTableSchema schema, System.Collections.Generic.IReadOnlyList<object[]> rows)
{
mySchema = schema;
myRows = new IBlueRow[rows.Count];
for (int i = 0; i < myRows.Length; i++)
myRows[i] = new BlueRow(mySchema, rows[i]);
}
#region IBlueRowCollection Members
public IBlueRow this[int id] { get { return myRows[id]; } }
public IBlueTableSchema schema {get {return mySchema;}}
public bool containsID (int id)
{
return id > -1 && id < myRows.Length;
}
#endregion
#region ICollection Members
public bool IsSynchronized {get {return false;}}
public int Count {get {return myRows.Length;}}
public void CopyTo (Array array, int index) {myRows.CopyTo (array, index);}
public object SyncRoot {get {return null;}}
#endregion
#region IEnumerable Members
public System.Collections.IEnumerator GetEnumerator() {return myRows.GetEnumerator ();}
#endregion
}
}
@@ -0,0 +1,400 @@
using System;
using System.Collections;
using System.Data;
//using BCG.Utilities;
using System.IO;
namespace BCG
{
/// <summary>
/// SerializedBlueRow 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 SerializedBlueRow requires is a schema to manage its internal information
///
///
/// Data in serialized rows is stored in two places: a byte array contains all nonstring data,
/// and a string array contains the string data. The strings in this array are all just references
/// to strings stored in the internin module (STring.intern)
/// Additdionally, the ids are cached locally in the row, so that they can be accessed faster.
/// </summary>
[Serializable]
public class SerializedBlueRow : IBlueRow
{
protected SerializedBlueTableSchema mySchema;
byte[] myRowData;
string[] myRowStrings;
#region Private helper methods
/// <summary>
/// Initializes the internal data structures
/// </summary>
/// <param name="schema"></param>
private void myInit(IBlueTableSchema schema)
{
if (schema is SerializedBlueTableSchema)
mySchema = schema as SerializedBlueTableSchema;
else
mySchema = new SerializedBlueTableSchema(schema);
myRowData = null;
}
#endregion
#region Constructors
protected SerializedBlueRow() { }
public SerializedBlueRow(IBlueRow r)
{
myInit(r.schema);
Object[] thisRowObjects = new Object[r.schema.columnInfo.Length];
foreach (ColumnInfo col in r.schema.columnInfo)
thisRowObjects[col.colNo] = r[col.colNo];
myBuildFromObjects(thisRowObjects);
}
/// <summary>
/// The SerializedBlueRow 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 SerializedBlueRow(IBlueTableSchema schema, IBlueRow row)
{
myInit(schema);
}
/// <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 SerializedBlueRow(IBlueTableSchema schema, params Object[] values)
{
myInit(schema);
myBuildFromObjects(values);
}
/// <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 SerializedBlueRow(IBlueTableSchema schema, DataRow row)
{
myInit(schema);
Object[] thisRowObjects = new Object[row.Table.Columns.Count];
foreach (DataColumn col in row.Table.Columns)
thisRowObjects[col.Ordinal] = row[col.Ordinal];
myBuildFromObjects(thisRowObjects);
}
public SerializedBlueRow(IBlueTableSchema schema, IDictionary row)
{
myInit(schema);
Object[] thisRowObjects = new Object[row.Count];
foreach (DictionaryEntry de in row)
if (mySchema[de.Key as string] != null)
thisRowObjects[mySchema[de.Key as string].colNo] = de.Value;
myBuildFromObjects(thisRowObjects);
}
/// <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 SerializedBlueRow(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 SerializedBlueTableSchema(cis, tableName));
}
Object[] thisRowObjects = new Object[row.Count];
foreach (DictionaryEntry de in row)
thisRowObjects[mySchema[de.Key as string].colNo] = de.Value;
myBuildFromObjects(thisRowObjects);
}
/// <summary>
/// Constructs the schema and then populates the values, both from the IDictionary.
/// </summary>
/// <param name="row"></param>
public SerializedBlueRow(IDictionary row) : this("", row) { }
#endregion
/// <summary>
/// Creates the serialized blue row from the list of objects. Serializes them using the schema.
/// </summary>
/// <param name="theObjects"></param>
private void myBuildFromObjects(Object[] theObjects)
{
using(BinaryWriter theBinaryWriter = new BinaryWriter(new MemoryStream()))
{
//store the id
int idCol = mySchema["id"].colNo;
theBinaryWriter.Write((int)theObjects[idCol]);
//
//serialize all the data
//
int numCols = theObjects.Length;
//first, write the null map
BitArray NullMap = new BitArray(numCols);
for (int col = 0; col < numCols; ++col)
NullMap.Set(col,(theObjects[col] == null || theObjects[col] == DBNull.Value));
byte[] NullMapBytes = new Byte[mySchema.getNullMapSize()];
NullMap.CopyTo(NullMapBytes,0);
theBinaryWriter.Write(NullMapBytes);
//then, write the non null, non string objects into the byte array
//and copy the string references into the string array
string[] thisColStrings = new string[numCols];
int numStringCols = 0;
for (int col = 0; col < numCols; ++col)
{
//don't serialize null values into the array. They'll just take up space.
//also don't serialize the id
if(NullMap.Get(col) == false && col != idCol)
{
Type thisColType = mySchema[col].colType;
if (thisColType == typeof(string))
thisColStrings[numStringCols++] = String.Intern(((string)theObjects[col]));
else
{
long numBytesBefore = theBinaryWriter.BaseStream.Length;
mySchema.serializeObject(theObjects[col], theBinaryWriter, col);
long numBytesAfter = theBinaryWriter.BaseStream.Length;
}
}
}
//now that we knmow how many string columns are in this row, we should store those references
//we do it thsi way to avoid storing null string refernces.
//it complicates string access a tiny but, it the savings is probably worth it
//becuase a lot of strings are null in tables like trade
myRowStrings = new string[numStringCols];
for (int col = 0; col < numStringCols; ++col)
{
myRowStrings[col] = thisColStrings[col];
}
//now save the bytes of the memory writer
long numBytes = theBinaryWriter.BaseStream.Length;
myRowData = new Byte[numBytes];
theBinaryWriter.Seek(0, SeekOrigin.Begin);
for (int i = 0; i < numBytes; ++i)
myRowData[i] = Convert.ToByte(theBinaryWriter.BaseStream.ReadByte());
/*/This code is for verifying that the row construction actually worked.
//it is test code, and should be commented out when actually used
ICollection thisRowValues = Values;
BlueRow thisBlueRow = new BlueRow(mySchema, theObjects);
string[] diffcolumns = BlueDataUtility.compareRows(this, thisBlueRow);
if(diffcolumns.Length != 0)
throw new BlueDataException(string.Format("Row Serialization Failed on column: {0}", diffcolumns[0]));
//*/
}
}
public string Bytes
{
get { return new System.Text.ASCIIEncoding().GetString(myRowData); }
}
#region IBlueRow members
public virtual IBlueTableSchema schema { get { return mySchema; } }
public virtual object this[string colName]
{
get
{
//see if they're askign for the id. These should be much faster than querying the hashtable
//(this is slow right now, but soon we should swithc to unsafe code and just typecase the byte poitner);
if (colName == "id")
return this[mySchema.IdColumn];
ColumnInfo ci = mySchema[colName];
if (ci != null)
return this[ci.colNo];
else
throw new Exception(colName + " is not a valid column name for " + schema.tableName);
}
}
public unsafe virtual object this[int index]
{
get
{
if (index == mySchema.IdColumn)
{
//the id is stored at the start of the row, to speed up access to it
fixed (byte* id = &myRowData[0])
{
return *((int*)id);
}
}
//the id is the first thing stored in the row. WE want to eat this up first.
if (mySchema[index].colType == typeof(string))
{
BitArray nullMap = mySchema.getNullMap(myRowData);
int indexIntoStringTable = 0;
for (int colNo = 0; colNo < mySchema.columnInfo.Length; ++colNo )
{
if (nullMap.Get(colNo))
{
//this is the string they are asking for
if (colNo == index)
return DBNull.Value; //we want to break out of all the loops and such here
//otherwise, don't increment the index into the string table
}
else
{
if (colNo == index)
return myRowStrings[indexIntoStringTable];
else if (mySchema[colNo].colType == typeof(string))
++indexIntoStringTable;
}
}
}//end if this column is a string
else
return mySchema.getValueNonString(myRowData, index);
//you should never get to this point.
throw new BlueDataException("Error: Malformed Serialized Blue Row, couldn't pull data item");
}
}
#endregion
#region ICollection members
public virtual int Count { get { return mySchema.columnInfo.Length; } }
public virtual bool IsSynchronized { get { return false; } }
public virtual object SyncRoot { get { return this; } }
public virtual void CopyTo(Array array, int index) {
Values.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 mySchema.getAllValues(myRowData, myRowStrings);
}
}
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
#region IComparable Members
public int CompareTo(IBlueRow other)
{
int thisID = (int)this["id"];
int otherID = (int)other["id"];
if (thisID > otherID)
return 1;
else if (thisID == otherID)
return 0;
else
return -1;
}
#endregion
}
}
@@ -0,0 +1,476 @@
using System;
using System.Data;
using System.Collections;
using System.IO;
using BCG.Utilities.General;
//using BCG.Utilities;
namespace BCG
{
/// <summary>
/// The SerializedBlueTableSchema is an immutable schema holder. Once created it cannot be modified. It can be created
/// either from a DataTable or from an Array of ColumnInfo. The methods through which the user can access it
/// are the indexers (both order and name based), and the property columnInfo and columnNames, that return
/// the respective data, sorted according to the ColumnOrder.
///
/// It is designed to work in concert with serialized blue rows. The serialized blue rows hold most information inside
/// memory streams, and uses this schema to unpack the values when they are needed.
/// </summary>
[Serializable]
public class SerializedBlueTableSchema : IBlueTableSchema
{
public class SerializedBlueTableSchemaSerializationHelper
{
public static object parse(Type t, string data)
{
if (t == BlueDataValidTypes.BOOL)
return Boolean.Parse(data);
else if (t == BlueDataValidTypes.BYTE)
return Byte.Parse(data);
else if (t == BlueDataValidTypes.CHAR)
return char.Parse(data);
else if (t == BlueDataValidTypes.DATETIME)
return DateTime.Parse(data);
else if (t == BlueDataValidTypes.DOUBLE)
return Double.Parse(data);
else if (t == BlueDataValidTypes.DECIMAL)
return Decimal.Parse(data);
else if (t == BlueDataValidTypes.INT)
return int.Parse(data);
else if (t == BlueDataValidTypes.LONG)
return long.Parse(data);
else if (t == BlueDataValidTypes.SBYTE)
return SByte.Parse(data);
else if (t == BlueDataValidTypes.STRING)
return data;
else if (t == BlueDataValidTypes.UINT)
return uint.Parse(data);
else if (t == BlueDataValidTypes.ULONG)
return ulong.Parse(data);
else if (t == BlueDataValidTypes.SINGLE)
return Single.Parse(data);
else
throw new ApplicationException("Can not be parsed");
}
public static void serialize(BinaryWriter bw, Type t, object o)
{
if (t == BlueDataValidTypes.BOOL)
bw.Write((bool)o);
else if (t == BlueDataValidTypes.BYTE)
bw.Write((byte)o);
else if (t == BlueDataValidTypes.CHAR)
bw.Write((char)o);
else if (t == BlueDataValidTypes.DATETIME)
bw.Write(((DateTime)o).Ticks);
else if (t == BlueDataValidTypes.DOUBLE)
bw.Write((double)o);
else if (t == BlueDataValidTypes.DECIMAL)
bw.Write((decimal)o);
else if (t == BlueDataValidTypes.INT)
bw.Write((int)o);
else if (t == BlueDataValidTypes.LONG)
bw.Write((long)o);
else if (t == BlueDataValidTypes.SBYTE)
bw.Write((sbyte)o);
else if (t == BlueDataValidTypes.STRING)
bw.Write(o as string);
else if (t == BlueDataValidTypes.SINGLE)
bw.Write((Single)o);
else if (t == BlueDataValidTypes.BYTEARRAY)
{
byte[] bs = o as byte[];
bw.Write(bs.Length);
bw.Write(bs);
}
else if (t == BlueDataValidTypes.UINT)
bw.Write((uint)o);
else if (t == BlueDataValidTypes.ULONG)
bw.Write((ulong)o);
else
throw new ApplicationException("Unsupported type");
}
public static object deserialize(BinaryReader br, Type t)
{
if (t == BlueDataValidTypes.BOOL)
return br.ReadBoolean();
else if (t == BlueDataValidTypes.BYTE)
return br.ReadByte();
else if (t == BlueDataValidTypes.CHAR)
return br.ReadChar();
else if (t == BlueDataValidTypes.DATETIME)
return new DateTime(br.ReadInt64());
else if (t == BlueDataValidTypes.DOUBLE)
return br.ReadDouble();
else if (t == BlueDataValidTypes.DECIMAL)
return br.ReadDecimal();
else if (t == BlueDataValidTypes.INT)
return br.ReadInt32();
else if (t == BlueDataValidTypes.LONG)
return br.ReadInt64();
else if (t == BlueDataValidTypes.SBYTE)
return br.ReadSByte();
else if (t == BlueDataValidTypes.UINT)
return br.ReadUInt32();
else if (t == BlueDataValidTypes.ULONG)
return br.ReadUInt64();
else if (t == BlueDataValidTypes.SINGLE)
return br.ReadSingle();
else
throw new ApplicationException("Unsupported type");
}
}
protected Hashtable myIndexByColName = new Hashtable();
protected ColumnInfo[] myIndexByColOrder;
private string[] myTableColNames;
protected string myTableName;
private int[] myColumnSizesTable;
int myIdCol;
public SerializedBlueTableSchema() { }
//copies the schema from an existing schema, building the dispatch table
public SerializedBlueTableSchema(IBlueTableSchema toCopy)
{
int numCols = toCopy.columnInfo.Length;
ColumnInfo[] theColumns = new ColumnInfo[toCopy.columnInfo.Length];
foreach (ColumnInfo toCopyCI in toCopy.columnInfo)
{
theColumns[toCopyCI.colNo] = toCopyCI;
}
myConstruct(theColumns, toCopy.tableName);
}
public SerializedBlueTableSchema(DataTable table)
{
ColumnInfo[] theseCols = new ColumnInfo[table.Columns.Count];
myTableName = table.TableName;
foreach (DataColumn col in table.Columns)
{
theseCols[col.Ordinal] = new ColumnInfo(col.ColumnName, col.Ordinal, col.DataType);
}
myConstruct(theseCols, myTableName);
}
public SerializedBlueTableSchema(ColumnInfo[] cis, string tableName)
{
myConstruct(cis, tableName);
}
public void serializeObject(object data, BinaryWriter bw, int colNo)
{
mySerializeDispatchTable[colNo](data, bw);
}
private void myConstruct(ColumnInfo[] cis, string tableName)
{
myIndexByColOrder = new ColumnInfo[cis.Length];
myTableName = tableName;
foreach (ColumnInfo ci in cis)
{
myIndexByColOrder[ci.colNo] = ci;
myIndexByColName[ci.colName] = ci;
}
myBuildDispatchTable();
}
public SerializedBlueTableSchema(string tableName, params ColumnInfo[] cis) : this(cis, tableName) { }
public ColumnInfo this[string colName]
{
get
{
object retVal = myIndexByColName[colName];
return retVal != null ? retVal as ColumnInfo : null;
}
}
public ColumnInfo this[int colNo]
{
get { return myIndexByColOrder[colNo]; }
}
/// <summary>
/// The returned Array is sorted according to the Column No
/// </summary>
public string[] columnNames
{
get
{
if (myTableColNames == null)
{
myTableColNames = new string[myIndexByColName.Count];
foreach (DictionaryEntry de in myIndexByColName)
{
ColumnInfo temp = (ColumnInfo)de.Value;
myTableColNames[temp.colNo] = temp.colName;
}
}
return myTableColNames;
}
}
/// <summary>
/// The returned Array is sorted according to the Column No
/// </summary>
public ColumnInfo[] columnInfo
{
get { return myIndexByColOrder; }
}
public string tableName { get { return myTableName; } }
public int IdColumn
{
get { return myIdCol; }
}
#region ISerializedType methods
public void serialize(BinaryWriter bw)
{
bw.Write(columnNames.Length);
bw.Write(tableName);
foreach (ColumnInfo ci in columnInfo)
{
bw.Write(ci.colName);
bw.Write(ci.colType.ToString());
if (ci.isConstant)
{
bw.Write(true);
SerializedBlueTableSchemaSerializationHelper.serialize(bw, ci.colType, ci.colValue);
}
else
{
bw.Write(false);
}
}
}
public void deserialze(BinaryReader br)
{
ColumnInfo[] cis = new ColumnInfo[br.ReadInt32()];
string name = br.ReadString();
for (int i = 0; i < cis.Length; i++)
{
string colName = br.ReadString();
Type t = TypeInstanceLoader.loadType(br.ReadString(), true);
bool isConstant = br.ReadBoolean();
if (isConstant)
{
cis[i] = new ColumnInfo(colName, i, t, SerializedBlueTableSchemaSerializationHelper.deserialize(br, t));
}
else
{
cis[i] = new ColumnInfo(colName, i, t);
}
}
myConstruct(cis, name);
}
public int getSerializedSize()
{
throw new NotImplementedException("Feature for provided constant values to" +
"non-primitive types in schema, not provided as of now");
}
#endregion
//builds a dispatch table used to pull values out of the memory streams in the rows.
private void myBuildDispatchTable()
{
int numCols = myIndexByColOrder.Length;
mySerializeDispatchTable = new SerializeDelegate[numCols];
myPullFromByteBufferDispatchTable = new PullFromByteBufferDelegate[numCols];
myColumnSizesTable = new int[numCols];
ColumnInfo idColumn = (ColumnInfo)myIndexByColName["id"];
myIdCol = idColumn.colNo;
for (int col = 0; col < numCols; ++col)
{
Type thisColType = myIndexByColOrder[col].colType;
//we don't store serializers for the id column, or string columns.
if (col == myIdCol || thisColType == typeof(string))
{
mySerializeDispatchTable[col] = null;
myColumnSizesTable[col] = 0;
myPullFromByteBufferDispatchTable[col] = null;
}
else
{
mySerializeDispatchTable[col] = myGetDispatchMethodSerialize(thisColType);
myPullFromByteBufferDispatchTable[col] = myGetDispatchMethodPullFromBuffer(thisColType);
myColumnSizesTable[col] = BlueDataValidTypes.getValueTypeSize(thisColType);
}
}
}
public int getNullMapSize()
{
int numCols = myIndexByColOrder.Length;
return Convert.ToInt32(Math.Ceiling((Convert.ToDouble(numCols) / 8.0)));
}
//this function reads the null array from inside the byte array.
//it has to copy the bytes over.
public BitArray getNullMap(byte[] theBytes)
{
int nullMapSize = getNullMapSize();
//the null map starts after the id, so that we have instant access to the id.
int byteCursor = BlueDataValidTypes.getValueTypeSize(typeof(Int32));
byte[] nullMapArray = new byte[nullMapSize];
for (int i = 0; i < nullMapSize; ++i)
nullMapArray[i] = theBytes[byteCursor++];
return new BitArray(nullMapArray);
}
//gets the value either from the bytes stream but only if the value is not a string.
//doesn't work if you're getting the id.
public unsafe object getValueNonString(byte[] theBytes, int colNo)
{
if (colNo == myIdCol)
throw new BlueDataException("Error: do not use getValueNonString to try to fetch the id of a row");
BitArray nullMap = getNullMap(theBytes);
if (nullMap.Get(colNo))
return DBNull.Value;
//the rest of the row starts after the id and the null map
int thisOffset = BlueDataValidTypes.getValueTypeSize(typeof(Int32)) + getNullMapSize();
for (int currentColumn = 0; currentColumn < colNo; ++currentColumn)
{
//pull the data from the binary reader, but only if this type is not null or a string
//if the type wer'e looking for is a string, it would have been returned already by the accesor
//in the bluerow.
//if it's the id, it also would have been returned already
if (!nullMap.Get(currentColumn)
&& myIndexByColOrder[currentColumn].colType != typeof(string)
&& currentColumn != myIdCol)
thisOffset += myColumnSizesTable[currentColumn];
}
fixed (byte* thisBytePtr = &theBytes[thisOffset])
{
return myPullFromByteBufferDispatchTable[colNo](thisBytePtr);
}
}
public unsafe object[] getAllValues(byte[] theBytes, string[] stringArray)
{
int numCols = myColumnSizesTable.Length;
object[] theObjs = new Object[numCols];
BitArray nullMap = getNullMap(theBytes);
int thisOffset = BlueDataValidTypes.getValueTypeSize(typeof(Int32)) + getNullMapSize();
int thisStringNo = 0;
for (int currentColumn = 0; currentColumn < numCols; ++currentColumn)
{
//pull the data from the binary reader, but only if this type is not null or a string
//if the type wer'e looking for is a string, it would have been returned already by the accesor
//in the bluerow.
//if it's the id, it also would have been returned already
if (nullMap.Get(currentColumn))
theObjs[currentColumn] = DBNull.Value;
else if (myIndexByColOrder[currentColumn].colType == typeof(string))
theObjs[currentColumn] = stringArray[thisStringNo++];
else if (currentColumn != myIdCol)
{
fixed (byte* thisGuy = &theBytes[thisOffset])
{
theObjs[currentColumn] = myPullFromByteBufferDispatchTable[currentColumn](thisGuy);
}
thisOffset += myColumnSizesTable[currentColumn];
}
}
return theObjs;
}
private delegate void SerializeDelegate(object data, BinaryWriter bw);
private SerializeDelegate[] mySerializeDispatchTable;
private void mySerializeInt32(object data, BinaryWriter bw) { bw.Write((Int32)data); }
private void mySerializeDouble(object data, BinaryWriter bw) { bw.Write((double)data); }
private void mySerializeChar(object data, BinaryWriter bw) { bw.Write((char)data); }
private void mySerializeLong(object data, BinaryWriter bw) { bw.Write((long)data); }
private void mySerializeSByte(object data, BinaryWriter bw) { bw.Write((sbyte)data); }
private void mySerializeBool(object data, BinaryWriter bw) { bw.Write((bool)data); }
private void mySerializeByte(object data, BinaryWriter bw) { bw.Write((byte)data); }
private void mySerializeDecimal(object data, BinaryWriter bw) { bw.Write((decimal)data); }
private void mySerializeUInt(object data, BinaryWriter bw) { bw.Write((uint)data); }
private void mySerializeString(object data, BinaryWriter bw) { bw.Write((string)data); }
private void mySerializeULong(object data, BinaryWriter bw) { bw.Write((ulong)data); }
private void mySerializeDateTime(object data, BinaryWriter bw) { bw.Write(((DateTime)data).ToOADate()); }
private void mySerializeSingle(object data, BinaryWriter bw) { bw.Write((Single)data); }
private SerializeDelegate myGetDispatchMethodSerialize(Type t)
{
if (t.Equals(BlueDataValidTypes.INT)) return new SerializeDelegate(mySerializeInt32);
else if (t.Equals(BlueDataValidTypes.DOUBLE)) return new SerializeDelegate(mySerializeDouble);
else if (t.Equals(BlueDataValidTypes.CHAR)) return new SerializeDelegate(mySerializeChar);
else if (t.Equals(BlueDataValidTypes.LONG)) return new SerializeDelegate(mySerializeLong);
else if (t.Equals(BlueDataValidTypes.SBYTE)) return new SerializeDelegate(mySerializeSByte);
else if (t.Equals(BlueDataValidTypes.BOOL)) return new SerializeDelegate(mySerializeBool);
else if (t.Equals(BlueDataValidTypes.BYTE)) return new SerializeDelegate(mySerializeByte);
else if (t.Equals(BlueDataValidTypes.DECIMAL)) return new SerializeDelegate(mySerializeDecimal);
else if (t.Equals(BlueDataValidTypes.UINT)) return new SerializeDelegate(mySerializeUInt);
else if (t.Equals(BlueDataValidTypes.ULONG)) return new SerializeDelegate(mySerializeULong);
else if (t.Equals(BlueDataValidTypes.DATETIME)) return new SerializeDelegate(mySerializeDateTime);
else if (t.Equals(BlueDataValidTypes.SINGLE)) return new SerializeDelegate(mySerializeSingle);
else if (t.Equals(BlueDataValidTypes.STRING)) return new SerializeDelegate(mySerializeString);
else throw new ApplicationException(t.FullName + " is not a supported type");
}
private unsafe delegate object PullFromByteBufferDelegate(byte* bytePtr);
private PullFromByteBufferDelegate[] myPullFromByteBufferDispatchTable;
private unsafe object myPullInt32(byte* bytePtr) { return *((int*)bytePtr); }
private unsafe object myPullDouble(byte* bytePtr) { return *((double*)bytePtr); }
private unsafe object myPullChar(byte* bytePtr) { return *((char*)bytePtr); }
private unsafe object myPullLong(byte* bytePtr) { return *((long*)bytePtr); }
private unsafe object myPullSByte(byte* bytePtr) { return *((SByte*)bytePtr); }
private unsafe object myPullBool(byte* bytePtr) { return *((bool*)bytePtr); }
private unsafe object myPullByte(byte* bytePtr) { return *(bytePtr); }
private unsafe object myPullDecimal(byte* bytePtr) { return *((decimal*)bytePtr); }
private unsafe object myPullUInt(byte* bytePtr) { return *((UInt32*)bytePtr); }
private unsafe object myPullULong(byte* bytePtr) { return *((UInt64*)bytePtr); }
private unsafe object myPullDateTime(byte* bytePtr)
{
double thisDateTime = *((double*)bytePtr);
return DateTime.FromOADate(thisDateTime);
}
private unsafe object myPullSingle(byte* bytePtr) { return *((Single*)bytePtr); ; }
private unsafe PullFromByteBufferDelegate myGetDispatchMethodPullFromBuffer(Type t)
{
if (t.Equals(BlueDataValidTypes.INT)) return new PullFromByteBufferDelegate(myPullInt32);
else if (t.Equals(BlueDataValidTypes.DOUBLE)) return new PullFromByteBufferDelegate(myPullDouble);
else if (t.Equals(BlueDataValidTypes.CHAR)) return new PullFromByteBufferDelegate(myPullChar);
else if (t.Equals(BlueDataValidTypes.LONG)) return new PullFromByteBufferDelegate(myPullLong);
else if (t.Equals(BlueDataValidTypes.SBYTE)) return new PullFromByteBufferDelegate(myPullSByte);
else if (t.Equals(BlueDataValidTypes.BOOL)) return new PullFromByteBufferDelegate(myPullBool);
else if (t.Equals(BlueDataValidTypes.BYTE)) return new PullFromByteBufferDelegate(myPullByte);
else if (t.Equals(BlueDataValidTypes.DECIMAL)) return new PullFromByteBufferDelegate(myPullDecimal);
else if (t.Equals(BlueDataValidTypes.UINT)) return new PullFromByteBufferDelegate(myPullUInt);
else if (t.Equals(BlueDataValidTypes.ULONG)) return new PullFromByteBufferDelegate(myPullULong);
else if (t.Equals(BlueDataValidTypes.DATETIME)) return new PullFromByteBufferDelegate(myPullDateTime);
else if (t.Equals(BlueDataValidTypes.SINGLE)) return new PullFromByteBufferDelegate(myPullSingle);
else throw new ApplicationException(t.FullName + " is not a supported type to pull from a buffer");
}
}
}
@@ -0,0 +1,121 @@
using System;
using System.ComponentModel;
using System.Collections;
namespace BCG
{
public class SimpleBindableCollection : IBlueRowCollection, ITypedList, IList
{
private ArrayList myRows;
private IBlueTableSchema mySchema;
private PropertyDescriptorCollection myPDColl;
public SimpleBindableCollection (IBlueTableSchema schema, ICollection rows)
{
mySchema = schema;
ArrayList temp = new ArrayList ();
foreach (ColumnInfo ci in mySchema.columnInfo)
temp.Add (new BlueDataPropertyDescriptor (ci));
myPDColl = new PropertyDescriptorCollection (temp.ToArray (typeof(PropertyDescriptor)) as PropertyDescriptor[]);
myRows = new ArrayList (rows);
}
public SimpleBindableCollection (IBlueTableSchema schema)
: this (schema, new ArrayList ())
{}
#region IBlueRowCollection Members
public IBlueRow this[int id]
{
get
{
foreach (IBlueRow r in this)
if (r["id"].Equals (id))
return r;
throw new ApplicationException ("ID + " + id.ToString () + " not found");
}
}
public bool containsID(int id)
{
try
{
return this[id] != null;
}
catch (ApplicationException)
{
return false;
}
}
public IBlueTableSchema schema {get {return mySchema;}}
#endregion
#region ICollection Members
public bool IsSynchronized {get {return false;}}
public int Count {get {return myRows.Count;}}
public void CopyTo(Array array, int index) {myRows.CopyTo (array, index);}
public object SyncRoot { get {return null;}}
#endregion
#region IEnumerable Members
public System.Collections.IEnumerator GetEnumerator()
{
return myRows.GetEnumerator ();
}
#endregion
#region ITypedList Members
public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
{
return myPDColl;
}
public string GetListName(PropertyDescriptor[] listAccessors)
{
return "SimpleBindableCollection";
}
#endregion
#region IList Members
public bool IsReadOnly {get {return true;}}
object System.Collections.IList.this[int index]
{
get {return myRows[index];}
set {throw new ApplicationException ("Collection is read only");}
}
public void RemoveAt(int index) {throw new ApplicationException ("Collection is read only");}
public void Insert(int index, object value) {throw new ApplicationException ("Collection is read only");}
public void Remove(object value) {throw new ApplicationException ("Collection is read only");}
public bool Contains(object value) {return myRows.Contains (value);}
public void Clear() {throw new ApplicationException ("Collection is read only");}
public int IndexOf(object value) {return myRows.IndexOf (value);}
public int Add(object value) {throw new ApplicationException ("Collection is read only");}
public bool IsFixedSize {get {return true;}}
#endregion
public void reset (ICollection rows) {myRows = new ArrayList(rows);}
}
}
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BCG
{
public class SparseVectorBlueTable : IBlueRowCollection
{
private List<IBlueRow> _rows = new List<IBlueRow>(50);
public SparseVectorBlueTable(IBlueTableSchema schema) {
this.schema = schema;
}
public void reserve(int length) {
if (_rows.Capacity < length) {
_rows.Capacity += 1024;
}
while (_rows.Count <= length) {
_rows.Add(null);
}
}
public IBlueRow this[int id] {
get {
return _rows[id];
}
set {
this.reserve(id);
_rows[id] = value;
}
}
public IBlueTableSchema schema { get; private set; }
public bool containsID(int id) {
return (id >= 0 && id < _rows.Count && this[id] != null);
}
public void CopyTo(Array array, int index) { _rows.CopyTo((IBlueRow[])array, index);}
public int Count { get { return _rows.Count; } }
public bool IsSynchronized {get { return false; }}
public object SyncRoot { get { return this; } }
public System.Collections.IEnumerator GetEnumerator() { return _rows.Where(r => r != null).GetEnumerator(); }
}
}
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BCG
{
public class SpreadIndexer
{
public enum IndexerDataType
{
SpreadSpec = 0,
Family,
LongDesc,
ShortDesc,
LegArray
}
private static Dictionary<string, Dictionary<IndexerDataType, object>> _cached_data = new Dictionary<string, Dictionary<IndexerDataType, object>>();
public static object get_value(string spread_def, IndexerDataType data_type)
{
Dictionary<IndexerDataType, object> type_to_data = null;
if (!_cached_data.TryGetValue(spread_def, out type_to_data))
{
type_to_data = new Dictionary<IndexerDataType,object> ();
_cached_data.Add(spread_def, type_to_data);
}
object rv = null;
type_to_data.TryGetValue(data_type, out rv);
return rv;
}
public static void set_value(string spread_def, IndexerDataType data_type, object val)
{
Dictionary<IndexerDataType, object> type_to_data = null;
if (!_cached_data.TryGetValue(spread_def, out type_to_data))
{
type_to_data = new Dictionary<IndexerDataType, object>();
_cached_data.Add(spread_def, type_to_data);
}
type_to_data[data_type] = val;
}
}
}
@@ -0,0 +1,41 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BCG
{
/// <summary>
/// keeps rows that are within n mseconds of now
/// </summary>
public class TimeWindowTable : MovingWindowTable
{
private int myWindowSizeMS;
private string myTimeCol;
public TimeWindowTable(IBlueTableSchema schema, int windowSizeMS, string timeCol)
:base(schema, new BlueRowComparer(timeCol, "id"))
{
this.myTimeCol = timeCol;
this.myWindowSizeMS = windowSizeMS;
}
public bool RemoveRow(IBlueRow row)
{
int index = this.mySortedRows.BinarySearch(row, this.myRowComparer);
if (index >= 0)
{
this.mySortedRows.RemoveAt(index);
return true;
}
return false;
}
protected override bool windowContains(IBlueRow row)
{
DateTime rowTime = (DateTime)row[this.myTimeCol];
return DateTime.Now.Subtract(rowTime).TotalMilliseconds <= this.myWindowSizeMS;
}
}
}
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BCG
{
public class WrapperRow<T> : IBlueRow
{
private readonly IBlueTableSchema _schema;
private readonly T _data;
private readonly int? _id = null;
private Func<int, T, object> _getter;
private readonly int _id_col_index;
public WrapperRow(IBlueTableSchema schema, T data, Func<int, T, object> getter, int? id = 0)
{
_schema = schema;
_data = data;
_getter = getter;
_id = id;
_id_col_index = schema["id"].colNo;
}
public IBlueTableSchema schema { get { return _schema; } }
public object this[int i] { get { return (i == _id_col_index && _id != null) ? _id.Value : _getter(i, _data); } }
public object this[string c] { get { return this[_schema[c].colNo]; } }
public void Add(object key, object value) { throw new NotImplementedException(); }
public void Clear() { throw new NotImplementedException(); }
public bool Contains(object key) { return key != null && key is string && schema.columnNames.Contains(key as string); }
public System.Collections.IDictionaryEnumerator GetEnumerator() { return new BlueRowDictionaryEnumerator(this); }
public bool IsFixedSize { get { return true; } }
public bool IsReadOnly { get { return true; } }
public System.Collections.ICollection Keys { get { return schema.columnNames; } }
public void Remove(object key) { throw new NotImplementedException("IBlueRow is read only"); }
public System.Collections.ICollection Values { get { return _schema.columnInfo.Select(c => this[c.colNo]).ToArray(); } }
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 void CopyTo(Array array, int index) { Values.CopyTo(array, index); }
public int Count { get { return schema.columnNames.Length; } }
public virtual bool IsSynchronized { get { return false; } }
public virtual object SyncRoot { get { return this; } }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return new BlueRowEnumerator(this); }
}
}