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,69 @@
using System;
using System.IO;
using System.Collections;
using BCG.Utilities;
namespace BCG
{
/// <summary>
/// Controls (ie creates dynamically) an IClientDeserializer and then passes data to it.
/// </summary>
public class ClientDeserializationController : IClientDeserializer
{
private IClientDeserializer myDeserializer;
public ClientDeserializationController () {}
#region IClientDeserializer Members
/// <summary>
/// Deserializes bytes into operation and data. If it gets a non create before a create, both
/// the out arrays get initialized to empty arrays
/// </summary>
/// <param name="streams"></param>
/// <param name="theOps"></param>
/// <param name="theRows"></param>
public void deserialize (Stream[] streams, out byte[] theOps, out Array theRows)
{
if (myDeserializer != null)
{
myDeserializer.deserialize (streams, out theOps, out theRows);
}
else
{
Stream totalStream = UtilityMethods.getAggregatedStream (streams);
long initialPos = totalStream.Position;
BinaryReader br = new BinaryReader (totalStream);
byte op;
try
{
op = br.ReadByte ();
}
catch (EndOfStreamException)
{
op = Byte.MaxValue;
}
if (op == StreamOpCode.create)
{
br.ReadString (); //pubsubclientdeserializer
string deserName = br.ReadString ();
myDeserializer = Activator.CreateInstance (Type.GetType (deserName))
as IClientDeserializer;
totalStream.Position = initialPos;
myDeserializer.deserialize (new Stream[] {totalStream}, out theOps, out theRows);
}
else
{
// BlueEventLog.logEvent ("Received a non create before a create", BlueEventLevel.Warning);
theOps = new byte[]{};
theRows = new object[]{};
}
}
}
#endregion
}
}
@@ -0,0 +1,124 @@
using System;
using System.Collections;
using System.IO;
using BCG.Utilities;
//using BCG.Utilities.BlueConfiguration;
namespace BCG
{
/// <summary>
/// Pairs up with the UniversalSerializer to deserialize the corresponding data. Everythign anologous
/// to UniversalSerializer.
/// </summary>
public abstract class ClientDeserializer : IClientDeserializer
{
protected IBlueTableSchema mySchema;
protected IBlueTableSchema myFirstSchema;
protected IClientRowDeserializer myRowDeserializer;
public ClientDeserializer()
{
myFirstSchema = null;
mySchema = null;
}
#region IClientDeserializer Members
void BCG.IClientDeserializer.deserialize(Stream[] streams, out byte[] theOps, out Array theRows)
{
BinaryReader br = new BinaryReader(UtilityMethods.getAggregatedStream(streams));
// BlueTrace.WriteDeserializeStart(streams.Length, br.BaseStream.Length);
// emit # of streams and total length
ArrayList ops = new ArrayList(), rows = new ArrayList();
IBlueRowCollection[] result;
while (true)
{
byte op;
try
{
int value = br.BaseStream.ReadByte();
if (value == -1) break;
op = (byte)value;
// BlueTrace.WriteDeserializeOperation(op);
}
catch (EndOfStreamException)
{
break;
}
if (op == StreamOpCode.create)
{
br.ReadString(); //pubsubDeserializer
br.ReadString(); //clientDeserializer
mySchema = new BlueTableSchema();
mySchema.deserialze(br);
//keep track of the first schema you've ever seen, so you know how to reorder columns
if (myFirstSchema == null)
myFirstSchema = mySchema;
result = new IBlueRowCollection[] { myGetRows(br, op, br.ReadInt32()) };
myEncorporateRows(result[0], op);
ops.Add(op);
rows.Add(result);
}
else
{
if (mySchema != null)
{
if (op == StreamOpCode.add || op == StreamOpCode.change || op == StreamOpCode.remove)
{
result = new IBlueRowCollection[] { myGetRows(br, op, 1) };
myEncorporateRows(result[0], op);
ops.Add(op);
rows.Add(result);
}
else if (op == StreamOpCode.multipleChange)
{
result = new IBlueRowCollection[] { myGetRows(br, op, br.ReadInt32()) };
myEncorporateRows(result[0], op);
ops.Add(op);
rows.Add(result);
}
else if (op == StreamOpCode.multipleRemoveAdd)
{
result =
new IBlueRowCollection[] {
myGetRows (br, StreamOpCode.remove, br.ReadInt32 ()),
myGetRows (br, StreamOpCode.add, br.ReadInt32 ())
};
myEncorporateRows(result[0], StreamOpCode.remove);
myEncorporateRows(result[1], StreamOpCode.add);
ops.Add(op);
rows.Add(result);
}
else
{
// BlueEventLog.logEvent("Illegal op-code received", BlueEventLevel.Error);
}
}
else
{
// BlueEventLog.logEvent("Received a non create operation even though the schema" +
// "has never been created", BlueEventLevel.Warning);
}
}
}
theOps = ops.ToArray(typeof(byte)) as byte[];
theRows = rows.ToArray();
// BlueTrace.WriteDeserializeStop(rows.Count);
}
#endregion
protected abstract IBlueRowCollection myGetRows(BinaryReader br, byte op, int nRows);
protected abstract void myEncorporateRows(IBlueRowCollection rows, byte op);
}
}
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace BCG
{
class ClientReorderingRowDeserializer : IClientRowDeserializer
{
private IBlueTableSchema myOriginalSchema;
private IBlueTableSchema myOutputSchema;
private ClientUniversalRowDeserializer myDeserializer;
public ClientReorderingRowDeserializer(IBlueTableSchema originalSchema, IBlueTableSchema newSchema)
{
myOriginalSchema = originalSchema;
myOutputSchema = newSchema;
myDeserializer = new ClientUniversalRowDeserializer(newSchema);
}
#region IClientRowDeserializer Members
public void deserializeRow(IBlueRowCollection currentState, BinaryReader br, byte op, out IBlueRow r)
{
IBlueRow temp = null;
myDeserializer.deserializeRow(currentState, br, op, out temp);
r = BlueDataUtility.changeSchema(temp, myOutputSchema);
}
#endregion
}
}
@@ -0,0 +1,35 @@
using System;
using System.Collections;
using System.IO;
namespace BCG
{
public class ClientUniversalDeserializer : ClientDeserializer
{
public ClientUniversalDeserializer () {}
protected override IBlueRowCollection myGetRows (BinaryReader br, byte op, int nRows)
{
BlueTable retVal = new BlueTable (mySchema);
if (myRowDeserializer == null)
myRowDeserializer = new ClientUniversalRowDeserializer(mySchema);
else if (op == StreamOpCode.create)
myRowDeserializer = new ClientReorderingRowDeserializer(myFirstSchema, mySchema);
for (int i = 0; i < nRows; i++)
{
IBlueRow row;
myRowDeserializer.deserializeRow (null, br, op, out row);
retVal.addRow (row);
}
return retVal;
}
protected override void myEncorporateRows (IBlueRowCollection rows, byte op)
{
//this does not keep state so ignore calls
}
}
}
@@ -0,0 +1,69 @@
using System;
using System.Collections;
using System.IO;
using BCG.Utilities;
//using BCG.Utilities.BlueConfiguration;
namespace BCG
{
/// <summary>
/// Helps the ClientUniversalDeserializer in deserilizing by dealing with the deserialization process
/// of one row. This leaves the ClientUniversalDeserializer to deserializer headers (indicating operations)
/// </summary>
internal class ClientUniversalRowDeserializer : IClientRowDeserializer
{
private IBlueTableSchema mySchema;
private int myNullMapLength = 0;
private PrimitiveDeserializationHelper myDeserializationHelper;
private BitArray myNullMap;
private int[] mySerIndexToSchemaIndexMap;
public ClientUniversalRowDeserializer (IBlueTableSchema schema)
{
myDeserializationHelper = new PrimitiveDeserializationHelper (mySchema = schema);
myNullMapLength = Convert.ToInt32((Math.Ceiling(Convert.ToDouble(mySchema.columnNames.Length)/8)));
myInitReorderSchema ();
}
#region Schema Reordering initialization
private void myInitReorderSchema ()
{
int idColIndex = mySchema["id"].colNo;
int[] temp = new int [mySchema.columnInfo.Length];
foreach (ColumnInfo ci in mySchema.columnInfo)
{
temp[ci.colNo] =
ci.colNo == idColIndex ? 0 :
ci.colNo < idColIndex ? ci.colNo + 1 : ci.colNo;
}
mySerIndexToSchemaIndexMap = new int[temp.Length];
for (int i = 0; i < temp.Length; i ++)
mySerIndexToSchemaIndexMap [temp[i]] = i;
}
#endregion
#region IClientRowDeserializer Members
public void deserializeRow (IBlueRowCollection currentState, BinaryReader br, byte op, out IBlueRow r)
{
br.ReadInt32 (); //length information for the row not needed
myNullMap = new BitArray(br.ReadBytes (myNullMapLength));
object[] values = new object [mySchema.columnNames.Length];
for (int i = 0; i < values.Length; i++)
{
int colNo = mySerIndexToSchemaIndexMap[i];
values[mySerIndexToSchemaIndexMap[i]] = myNullMap[colNo] ? DBNull.Value : myDeserializationHelper.deserializeColumn (colNo, br);
}
r = new BlueRow(mySchema, values);
}
#endregion
}
}
@@ -0,0 +1,22 @@
using System;
using System.Collections;
using System.IO;
namespace BCG
{
/// <summary>
/// Performs deserialization inside the client app. The client gets either the complete data set or the
/// increments from PubSub and this interface makes sense out of them from a tabular/datastream stand point.
/// </summary>
public interface IClientDeserializer
{
/// <summary>
/// deserializes the data received by the client from the pubsub
/// </summary>
/// <param name="serializedData">the data in byte format received from pubsub</param>
/// <param name="theOps">the operations that the data maps onto</param>
/// <param name="theRows">the data corresponding to the operations, is an array of IblueRowCollection[]</param>
void deserialize (Stream[] streams, out byte[] theOps, out Array theRows);
}
}
@@ -0,0 +1,14 @@
using System;
using System.IO;
using System.Collections;
namespace BCG
{
/// <summary>
/// Deserializes one row. the current state is also provided as some deserialization schemes might use it.
/// </summary>
public interface IClientRowDeserializer
{
void deserializeRow (IBlueRowCollection currentState, BinaryReader br, byte op, out IBlueRow r);
}
}
@@ -0,0 +1,79 @@
using System;
using System.IO;
namespace BCG
{
/// <summary>
/// Deserializes primitive types. Contructs a hash based scheme based on the index, to make everythign O(1)
/// rather than searching it up and making it O(n)
/// </summary>
public class PrimitiveDeserializationHelper
{
private delegate object DeserializeDelegate (int colNo, BinaryReader br);
private DeserializeDelegate[] myDispatchTable;
private ColumnInfo[] myCis;
public PrimitiveDeserializationHelper (IBlueTableSchema schema)
: this (schema.columnInfo)
{}
public PrimitiveDeserializationHelper (ColumnInfo[] cis)
{
myDispatchTable = new DeserializeDelegate [(myCis = cis).Length];
foreach (ColumnInfo ci in cis)
myDispatchTable[ci.colNo] = myGetDispatchMethod (ci.colType);
}
public object deserializeColumn (int colNo, BinaryReader br)
{
return myDispatchTable[colNo] (colNo, br);
}
private DeserializeDelegate myGetDispatchMethod (Type t)
{
if (t.Equals (BlueDataValidTypes.INT)) return new DeserializeDelegate (myDeserializeInt32);
else if (t.Equals (BlueDataValidTypes.DOUBLE)) return new DeserializeDelegate (myDeserializeDouble);
else if (t.Equals (BlueDataValidTypes.STRING)) return new DeserializeDelegate (myDeserializeString);
else if (t.Equals (BlueDataValidTypes.BYTEARRAY)) return new DeserializeDelegate (myDeserializeByteArray);
else if (t.Equals (BlueDataValidTypes.CHAR)) return new DeserializeDelegate (myDeserializeChar);
else if (t.Equals (BlueDataValidTypes.LONG)) return new DeserializeDelegate (myDeserializeLong);
else if (t.Equals (BlueDataValidTypes.SBYTE)) return new DeserializeDelegate (myDeserializeSByte);
else if (t.Equals (BlueDataValidTypes.BOOL)) return new DeserializeDelegate (myDeserializeBool);
else if (t.Equals (BlueDataValidTypes.BYTE)) return new DeserializeDelegate (myDeserializeByte);
else if (t.Equals (BlueDataValidTypes.DECIMAL)) return new DeserializeDelegate (myDeserializeDecimal);
else if (t.Equals (BlueDataValidTypes.UINT)) return new DeserializeDelegate (myDeserializeUInt);
else if (t.Equals (BlueDataValidTypes.ULONG)) return new DeserializeDelegate (myDeserializeULong);
else if (t.Equals (BlueDataValidTypes.DATETIME)) return new DeserializeDelegate (myDeserializeDateTime);
else if (t.Equals (BlueDataValidTypes.SINGLE)) return new DeserializeDelegate (myDeserializeSingle);
else if (SerializationUtilityMethods.isSerializedType (t)) return new DeserializeDelegate (myDeserializeISerializedType);
else throw new ApplicationException (t.FullName + " is not a supported type");
}
private object myDeserializeInt32 (int colNo, BinaryReader br) {return br.ReadInt32 ();}
private object myDeserializeISerializedType (int colNo, BinaryReader br)
{
ISerializedType o = Activator.CreateInstance (myCis[colNo].colType)
as ISerializedType;
o.deserialze (br);
return o;
}
private object myDeserializeDouble (int colNo, BinaryReader br) {return br.ReadDouble ();}
private object myDeserializeString (int colNo, BinaryReader br) {return br.ReadString ();}
private object myDeserializeByteArray (int colNo, BinaryReader br) {return br.ReadBytes (br.ReadInt32 ());}
private object myDeserializeChar (int colNo, BinaryReader br) {return br.ReadChar ();}
private object myDeserializeLong (int colNo, BinaryReader br) {return br.ReadInt64 ();}
private object myDeserializeSByte (int colNo, BinaryReader br) {return br.ReadSByte ();}
private object myDeserializeBool (int colNo, BinaryReader br) {return br.ReadBoolean ();}
private object myDeserializeByte (int colNo, BinaryReader br) {return br.ReadByte ();}
private object myDeserializeDecimal (int colNo, BinaryReader br) {return br.ReadDecimal ();}
private object myDeserializeUInt (int colNo, BinaryReader br) {return br.ReadUInt32 ();}
private object myDeserializeDateTime (int colNo, BinaryReader br)
{
return new DateTime(br.ReadInt64());
//return DateTime.FromOADate (br.ReadDouble ());
//return new DateTime(br.ReadInt64());
}
private object myDeserializeULong (int colNo, BinaryReader br) {return br.ReadUInt64 ();}
private object myDeserializeSingle (int colNo, BinaryReader br) {return br.ReadSingle ();}
}
}