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,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
public class TextReaderStream : ViewStream
|
||||
{
|
||||
#region constructors
|
||||
public TextReaderStream(string file)
|
||||
{
|
||||
myFile = file;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ViewStream
|
||||
protected override void myOnFirstSubscriber()
|
||||
{
|
||||
StreamReader reader = new StreamReader(myFile);
|
||||
|
||||
#region get schema
|
||||
IBlueTableSchema schema = null; string schemaStr;
|
||||
if ((schemaStr = reader.ReadLine()) == null)
|
||||
{
|
||||
reader.Close();
|
||||
return;
|
||||
}
|
||||
|
||||
schema = myGetSchema(schemaStr);
|
||||
#endregion
|
||||
|
||||
#region read rows and fire update
|
||||
BlueTable adds = new BlueTable(schema);
|
||||
BlueTable dels = new BlueTable(schema);
|
||||
|
||||
string rowStr;
|
||||
while ((rowStr = reader.ReadLine()) != null)
|
||||
{
|
||||
adds.addRow(myGetRow(schema, rowStr));
|
||||
}
|
||||
|
||||
myFireEvent(new DataPacket(adds, adds, dels));
|
||||
#endregion
|
||||
|
||||
reader.Close();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region helpers
|
||||
private IBlueTableSchema myGetSchema(string schemaStr)
|
||||
{
|
||||
List<ColumnInfo> columnInfos = new List<ColumnInfo>();
|
||||
|
||||
List<string> columnInfoStrs = new List<string>(schemaStr.Split(new string[] { TextWriterStream.DELIM }, StringSplitOptions.None));
|
||||
for(int i = 0; i < columnInfoStrs.Count; ++i)
|
||||
{
|
||||
string columnInfoStr = columnInfoStrs[i];
|
||||
|
||||
List<string> columnInfoStrParts = new List<string>(columnInfoStr.Split(new char[] { ':' }, StringSplitOptions.None));
|
||||
if (columnInfoStrParts.Count != 2)
|
||||
{
|
||||
throw (new Exception(String.Format("Unable to decode schema string '{0}'", schemaStr)));
|
||||
}
|
||||
|
||||
columnInfos.Add(new ColumnInfo(columnInfoStrParts[0], i, Type.GetType(columnInfoStrParts[1])));
|
||||
}
|
||||
|
||||
return (new BlueTableSchema(columnInfos.ToArray(), ""));
|
||||
}
|
||||
|
||||
private IBlueRow myGetRow(IBlueTableSchema schema, string rowStr)
|
||||
{
|
||||
List<string> valueStrs = new List<string>(rowStr.Split(new string[] { TextWriterStream.DELIM }, StringSplitOptions.None));
|
||||
|
||||
if (schema.columnInfo.Length != valueStrs.Count)
|
||||
{
|
||||
throw (new Exception(String.Format("Unable to decode row string '{0}'", rowStr)));
|
||||
}
|
||||
|
||||
List<object> values = new List<object>();
|
||||
for(int i = 0; i < schema.columnInfo.Length; ++i)
|
||||
{
|
||||
values.Add(Convert.ChangeType(valueStrs[i], schema.columnInfo[i].colType));
|
||||
}
|
||||
|
||||
return (new BlueRow(schema, values.ToArray()));
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region state
|
||||
private string myFile;
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
||||
using BCG.Utilities.General;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
[DataStreamUserRespresentation("textWriter")]
|
||||
public class TextWriterStream : ViewStream, IWorker
|
||||
{
|
||||
#region constructor
|
||||
public TextWriterStream(ViewStream stream, string fileNamePrefix, int writeFreqSecs)
|
||||
: this(ViewStreamDatabase.instance, stream, fileNamePrefix, writeFreqSecs)
|
||||
{
|
||||
}
|
||||
|
||||
public TextWriterStream(ViewStreamDatabase vsd, ViewStream stream, string fileNamePrefix, int writeFreqSecs)
|
||||
{
|
||||
myVsd = vsd;
|
||||
myStream = stream;
|
||||
myFileNamePrefix = fileNamePrefix;
|
||||
myWriteFreqSecs = writeFreqSecs;
|
||||
myNextWrite = DateTime.MinValue;
|
||||
myGotUpdate = false;
|
||||
mySchemaStr = null;
|
||||
|
||||
if (!myFileNamePrefix.EndsWith("_"))
|
||||
{
|
||||
myFileNamePrefix += "_";
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ViewStream
|
||||
protected override void myOnFirstSubscriber()
|
||||
{
|
||||
myStream.onChange +=new ChangeDelegate(myStreamChange);
|
||||
myVsd.addWorker(this);
|
||||
}
|
||||
|
||||
protected override void myOnLastSubscriber()
|
||||
{
|
||||
myVsd.removeWorker(this);
|
||||
myStream.onChange -= new ChangeDelegate(myStreamChange);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region IWorker
|
||||
public void doWork()
|
||||
{
|
||||
if ((myNextWrite <= DateTime.Now && myGotUpdate))
|
||||
{
|
||||
myGotUpdate = false;
|
||||
myNextWrite = DateTime.Now.AddSeconds(myWriteFreqSecs);
|
||||
myWrite(myStream.rows);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
private void myStreamChange(object o)
|
||||
{
|
||||
myGotUpdate = true;
|
||||
}
|
||||
|
||||
#region helpers
|
||||
public void myWrite(IBlueRowCollection rows)
|
||||
{
|
||||
#region create schema str
|
||||
if (mySchemaStr == null)
|
||||
{
|
||||
myCreateSchemaStr(rows.schema);
|
||||
}
|
||||
#endregion
|
||||
|
||||
// create file name
|
||||
string filename = String.Format("{0}{1}.txt", myFileNamePrefix, myDateTimeStr(DateTime.Now));
|
||||
|
||||
// open file
|
||||
StreamWriter writer = new StreamWriter(filename);
|
||||
|
||||
// write schema str
|
||||
if (mySchemaStr != null)
|
||||
{
|
||||
writer.WriteLine(mySchemaStr);
|
||||
}
|
||||
|
||||
foreach (IBlueRow row in rows)
|
||||
{
|
||||
#region write row str
|
||||
string rowStr = "";
|
||||
foreach (string columnName in row.schema.columnNames)
|
||||
{
|
||||
if (rowStr != "")
|
||||
{
|
||||
rowStr += DELIM;
|
||||
}
|
||||
|
||||
rowStr += row[columnName].ToString();
|
||||
}
|
||||
|
||||
writer.WriteLine(rowStr);
|
||||
#endregion
|
||||
}
|
||||
|
||||
// close file
|
||||
writer.Close();
|
||||
}
|
||||
|
||||
public void myCreateSchemaStr(IBlueTableSchema schema)
|
||||
{
|
||||
mySchemaStr = "";
|
||||
|
||||
foreach (ColumnInfo columnInfo in schema.columnInfo)
|
||||
{
|
||||
if (mySchemaStr != "")
|
||||
{
|
||||
mySchemaStr += DELIM;
|
||||
}
|
||||
|
||||
mySchemaStr += String.Format("{0}:{1}", columnInfo.colName, columnInfo.colType.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
private string myDateTimeStr(DateTime dateTime)
|
||||
{
|
||||
return (String.Format("{0}{1}{2}{3}{4}{5}",
|
||||
dateTime.Year.ToString("D4"), dateTime.Month.ToString("D2"), dateTime.Day.ToString("D2"),
|
||||
dateTime.Hour.ToString("D2"), dateTime.Minute.ToString("D2"), dateTime.Second.ToString("D2")));
|
||||
}
|
||||
#endregion
|
||||
|
||||
public static readonly string DELIM = ", ";
|
||||
|
||||
#region state
|
||||
private ViewStreamDatabase myVsd;
|
||||
private ViewStream myStream;
|
||||
private string myFileNamePrefix;
|
||||
private int myWriteFreqSecs;
|
||||
private DateTime myNextWrite;
|
||||
private bool myGotUpdate;
|
||||
private string mySchemaStr;
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user