Adding tests for stream serialization
This commit is contained in:
1 parent
52c1e500e6
commit
7104e90fc9
48 files changed
+2827
-2521
No files matched your search
@@ -5,8 +5,7 @@ using System.IO;
|
||||
using BCG.Utilities.General;
|
||||
//using BCG.Utilities;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
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
|
||||
@@ -17,12 +16,9 @@ namespace BCG
|
||||
/// 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)
|
||||
{
|
||||
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)
|
||||
@@ -52,8 +48,7 @@ namespace BCG
|
||||
else
|
||||
throw new ApplicationException("Can not be parsed");
|
||||
}
|
||||
public static void serialize(BinaryWriter bw, Type t, object o)
|
||||
{
|
||||
public static void serialize(BinaryWriter bw, Type t, object o) {
|
||||
if (t == BlueDataValidTypes.BOOL)
|
||||
bw.Write((bool)o);
|
||||
else if (t == BlueDataValidTypes.BYTE)
|
||||
@@ -76,13 +71,11 @@ namespace BCG
|
||||
bw.Write(o as string);
|
||||
else if (t == BlueDataValidTypes.SINGLE)
|
||||
bw.Write((Single)o);
|
||||
else if (t == BlueDataValidTypes.BYTEARRAY)
|
||||
{
|
||||
else if (t == BlueDataValidTypes.BYTEARRAY) {
|
||||
byte[] bs = o as byte[];
|
||||
bw.Write(bs.Length);
|
||||
bw.Write(bs);
|
||||
}
|
||||
else if (t == BlueDataValidTypes.UINT)
|
||||
} else if (t == BlueDataValidTypes.UINT)
|
||||
bw.Write((uint)o);
|
||||
else if (t == BlueDataValidTypes.ULONG)
|
||||
bw.Write((ulong)o);
|
||||
@@ -90,8 +83,7 @@ namespace BCG
|
||||
throw new ApplicationException("Unsupported type");
|
||||
}
|
||||
|
||||
public static object deserialize(BinaryReader br, Type t)
|
||||
{
|
||||
public static object deserialize(BinaryReader br, Type t) {
|
||||
if (t == BlueDataValidTypes.BOOL)
|
||||
return br.ReadBoolean();
|
||||
else if (t == BlueDataValidTypes.BYTE)
|
||||
@@ -132,45 +124,37 @@ namespace BCG
|
||||
public SerializedBlueTableSchema() { }
|
||||
|
||||
//copies the schema from an existing schema, building the dispatch table
|
||||
public SerializedBlueTableSchema(IBlueTableSchema toCopy)
|
||||
{
|
||||
public SerializedBlueTableSchema(IBlueTableSchema toCopy) {
|
||||
int numCols = toCopy.columnInfo.Length;
|
||||
ColumnInfo[] theColumns = new ColumnInfo[toCopy.columnInfo.Length];
|
||||
foreach (ColumnInfo toCopyCI in toCopy.columnInfo)
|
||||
{
|
||||
foreach (ColumnInfo toCopyCI in toCopy.columnInfo) {
|
||||
theColumns[toCopyCI.colNo] = toCopyCI;
|
||||
}
|
||||
myConstruct(theColumns, toCopy.tableName);
|
||||
}
|
||||
|
||||
public SerializedBlueTableSchema(DataTable table)
|
||||
{
|
||||
public SerializedBlueTableSchema(DataTable table) {
|
||||
ColumnInfo[] theseCols = new ColumnInfo[table.Columns.Count];
|
||||
myTableName = table.TableName;
|
||||
foreach (DataColumn col in table.Columns)
|
||||
{
|
||||
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)
|
||||
{
|
||||
public SerializedBlueTableSchema(ColumnInfo[] cis, string tableName) {
|
||||
myConstruct(cis, tableName);
|
||||
}
|
||||
|
||||
public void serializeObject(object data, BinaryWriter bw, int colNo)
|
||||
{
|
||||
public void serializeObject(object data, BinaryWriter bw, int colNo) {
|
||||
mySerializeDispatchTable[colNo](data, bw);
|
||||
|
||||
}
|
||||
|
||||
private void myConstruct(ColumnInfo[] cis, string tableName)
|
||||
{
|
||||
private void myConstruct(ColumnInfo[] cis, string tableName) {
|
||||
myIndexByColOrder = new ColumnInfo[cis.Length];
|
||||
myTableName = tableName;
|
||||
foreach (ColumnInfo ci in cis)
|
||||
{
|
||||
foreach (ColumnInfo ci in cis) {
|
||||
myIndexByColOrder[ci.colNo] = ci;
|
||||
myIndexByColName[ci.colName] = ci;
|
||||
}
|
||||
@@ -179,32 +163,25 @@ namespace BCG
|
||||
|
||||
public SerializedBlueTableSchema(string tableName, params ColumnInfo[] cis) : this(cis, tableName) { }
|
||||
|
||||
public ColumnInfo this[string colName]
|
||||
{
|
||||
get
|
||||
{
|
||||
public ColumnInfo this[string colName] {
|
||||
get {
|
||||
object retVal = myIndexByColName[colName];
|
||||
return retVal != null ? retVal as ColumnInfo : null;
|
||||
}
|
||||
}
|
||||
|
||||
public ColumnInfo this[int colNo]
|
||||
{
|
||||
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)
|
||||
{
|
||||
public string[] columnNames {
|
||||
get {
|
||||
if (myTableColNames == null) {
|
||||
myTableColNames = new string[myIndexByColName.Count];
|
||||
foreach (DictionaryEntry de in myIndexByColName)
|
||||
{
|
||||
foreach (DictionaryEntry de in myIndexByColName) {
|
||||
ColumnInfo temp = (ColumnInfo)de.Value;
|
||||
myTableColNames[temp.colNo] = temp.colName;
|
||||
}
|
||||
@@ -216,64 +193,46 @@ namespace BCG
|
||||
/// <summary>
|
||||
/// The returned Array is sorted according to the Column No
|
||||
/// </summary>
|
||||
public ColumnInfo[] columnInfo
|
||||
{
|
||||
public ColumnInfo[] columnInfo {
|
||||
get { return myIndexByColOrder; }
|
||||
}
|
||||
|
||||
public string tableName { get { return myTableName; } }
|
||||
|
||||
|
||||
public int IdColumn
|
||||
{
|
||||
public int IdColumn {
|
||||
get { return myIdCol; }
|
||||
}
|
||||
|
||||
|
||||
#region ISerializedType methods
|
||||
public void serialize(BinaryWriter bw)
|
||||
{
|
||||
public void serialize(BinaryWriter bw) {
|
||||
bw.Write(columnNames.Length);
|
||||
bw.Write(tableName);
|
||||
foreach (ColumnInfo ci in columnInfo)
|
||||
{
|
||||
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);
|
||||
}
|
||||
bw.Write(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void deserialze(BinaryReader br)
|
||||
{
|
||||
public void deserialze(BinaryReader br) {
|
||||
ColumnInfo[] cis = new ColumnInfo[br.ReadInt32()];
|
||||
string name = br.ReadString();
|
||||
for (int i = 0; i < cis.Length; i++)
|
||||
{
|
||||
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
|
||||
{
|
||||
if (isConstant) {
|
||||
cis[i] = new ColumnInfo(colName, i, t);
|
||||
} else {
|
||||
cis[i] = new ColumnInfo(colName, i, t);
|
||||
}
|
||||
}
|
||||
myConstruct(cis, name);
|
||||
}
|
||||
|
||||
public int getSerializedSize()
|
||||
{
|
||||
public int getSerializedSize() {
|
||||
throw new NotImplementedException("Feature for provided constant values to" +
|
||||
"non-primitive types in schema, not provided as of now");
|
||||
}
|
||||
@@ -282,8 +241,7 @@ namespace BCG
|
||||
|
||||
|
||||
//builds a dispatch table used to pull values out of the memory streams in the rows.
|
||||
private void myBuildDispatchTable()
|
||||
{
|
||||
private void myBuildDispatchTable() {
|
||||
int numCols = myIndexByColOrder.Length;
|
||||
mySerializeDispatchTable = new SerializeDelegate[numCols];
|
||||
myPullFromByteBufferDispatchTable = new PullFromByteBufferDelegate[numCols];
|
||||
@@ -292,18 +250,14 @@ namespace BCG
|
||||
|
||||
myIdCol = idColumn.colNo;
|
||||
|
||||
for (int col = 0; col < numCols; ++col)
|
||||
{
|
||||
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))
|
||||
{
|
||||
if (col == myIdCol || thisColType == typeof(string)) {
|
||||
mySerializeDispatchTable[col] = null;
|
||||
myColumnSizesTable[col] = 0;
|
||||
myPullFromByteBufferDispatchTable[col] = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
mySerializeDispatchTable[col] = myGetDispatchMethodSerialize(thisColType);
|
||||
myPullFromByteBufferDispatchTable[col] = myGetDispatchMethodPullFromBuffer(thisColType);
|
||||
myColumnSizesTable[col] = BlueDataValidTypes.getValueTypeSize(thisColType);
|
||||
@@ -315,16 +269,14 @@ namespace BCG
|
||||
}
|
||||
|
||||
|
||||
public int getNullMapSize()
|
||||
{
|
||||
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)
|
||||
{
|
||||
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));
|
||||
@@ -336,8 +288,7 @@ namespace BCG
|
||||
}
|
||||
//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)
|
||||
{
|
||||
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);
|
||||
@@ -345,8 +296,7 @@ namespace BCG
|
||||
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)
|
||||
{
|
||||
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.
|
||||
@@ -357,16 +307,14 @@ namespace BCG
|
||||
|
||||
thisOffset += myColumnSizesTable[currentColumn];
|
||||
}
|
||||
fixed (byte* thisBytePtr = &theBytes[thisOffset])
|
||||
{
|
||||
fixed (byte* thisBytePtr = &theBytes[thisOffset]) {
|
||||
return myPullFromByteBufferDispatchTable[colNo](thisBytePtr);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public unsafe object[] getAllValues(byte[] theBytes, string[] stringArray)
|
||||
{
|
||||
public unsafe object[] getAllValues(byte[] theBytes, string[] stringArray) {
|
||||
int numCols = myColumnSizesTable.Length;
|
||||
object[] theObjs = new Object[numCols];
|
||||
|
||||
@@ -374,8 +322,7 @@ namespace BCG
|
||||
|
||||
int thisOffset = BlueDataValidTypes.getValueTypeSize(typeof(Int32)) + getNullMapSize();
|
||||
int thisStringNo = 0;
|
||||
for (int currentColumn = 0; currentColumn < numCols; ++currentColumn)
|
||||
{
|
||||
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.
|
||||
@@ -384,10 +331,8 @@ namespace BCG
|
||||
theObjs[currentColumn] = DBNull.Value;
|
||||
else if (myIndexByColOrder[currentColumn].colType == typeof(string))
|
||||
theObjs[currentColumn] = stringArray[thisStringNo++];
|
||||
else if (currentColumn != myIdCol)
|
||||
{
|
||||
fixed (byte* thisGuy = &theBytes[thisOffset])
|
||||
{
|
||||
else if (currentColumn != myIdCol) {
|
||||
fixed (byte* thisGuy = &theBytes[thisOffset]) {
|
||||
theObjs[currentColumn] = myPullFromByteBufferDispatchTable[currentColumn](thisGuy);
|
||||
}
|
||||
thisOffset += myColumnSizesTable[currentColumn];
|
||||
@@ -416,8 +361,7 @@ namespace BCG
|
||||
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)
|
||||
{
|
||||
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);
|
||||
@@ -448,16 +392,14 @@ namespace BCG
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user