Merging changes
This commit is contained in:
1 parent
c4216d2272
commit
797816be7e
437 files changed
+54228
-28
No files matched your search
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user