130 lines
4.8 KiB
C#
130 lines
4.8 KiB
C#
using Bts.DataStreamCore;
|
|
|
|
using Spark.Core.Diagnostics;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace BCG {
|
|
/// <summary>
|
|
/// SuperMap gives the user the ability
|
|
/// to map one row to one or more rows
|
|
/// (as opposed to MapStream where there
|
|
/// is a one to one mapping)
|
|
/// It takes in an ISuperMapper which does
|
|
/// the mapping. Additinally the ISuperMapper
|
|
/// can chage itself and raise an event causing
|
|
/// a remapping to happen
|
|
/// </summary>
|
|
public class SuperMapStream : ViewStream {
|
|
#region super mapper that wraps around stream mapper
|
|
private class MyMappingWrapper : ISuperStreamMapper {
|
|
private IStreamMapper myMapper;
|
|
|
|
public MyMappingWrapper(IStreamMapper mapper) { myMapper = mapper; }
|
|
|
|
#region ISuperStreamMapper Members
|
|
|
|
public IBlueTableSchema getOutputSchema(IBlueTableSchema schema) { return myMapper.getOutputSchema(schema); }
|
|
|
|
public IBlueRow[] map(IBlueRow inputRow) { return new IBlueRow[] { myMapper.map(inputRow) }; }
|
|
|
|
public event MapperChangeDelegate onMapperChange {
|
|
add { }
|
|
remove { }
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
#endregion
|
|
|
|
private class MyFuncWrapper : ISuperStreamMapper {
|
|
private Func<IBlueTableSchema, IBlueTableSchema> _get_schema;
|
|
private Func<IBlueRow, IEnumerable<IBlueRow>> _map_rows;
|
|
public MyFuncWrapper(Func<IBlueTableSchema, IBlueTableSchema> get_schema,
|
|
Func<IBlueRow, IEnumerable<IBlueRow>> map_rows) {
|
|
_get_schema = get_schema;
|
|
_map_rows = map_rows;
|
|
}
|
|
|
|
public IBlueTableSchema getOutputSchema(IBlueTableSchema schema) { return _get_schema(schema); }
|
|
|
|
public IBlueRow[] map(IBlueRow r) {
|
|
IEnumerable<IBlueRow> rows = _map_rows(r);
|
|
return rows == null ? new IBlueRow[] { } : new List<IBlueRow>(rows).ToArray();
|
|
}
|
|
|
|
public event MapperChangeDelegate? onMapperChange;
|
|
}
|
|
|
|
//maps the id of the underlying stream to the
|
|
//rows of the resulting stream
|
|
private Dictionary<int, List<IBlueRow>> myMap = new Dictionary<int, List<IBlueRow>>();
|
|
private int myID = 0;
|
|
private BlueTable? myRows;
|
|
private BlueTable? myBaseRows;
|
|
private ViewStream myStream;
|
|
private ISuperStreamMapper myMapper;
|
|
public bool generate_ids = true;
|
|
|
|
public SuperMapStream(ViewStream vs, Func<IBlueTableSchema, IBlueTableSchema> get_schema,
|
|
Func<IBlueRow, IEnumerable<IBlueRow>> map_rows)
|
|
: this(vs, new MyFuncWrapper(get_schema, map_rows)) { }
|
|
|
|
public SuperMapStream(ViewStream vs, IBlueTableSchema out_schema, Func<IBlueRow, IEnumerable<IBlueRow>> map_rows)
|
|
: this(vs, s => out_schema, map_rows) { }
|
|
|
|
public SuperMapStream(ViewStream vs, IStreamMapper mapper)
|
|
: this(vs, new MyMappingWrapper(mapper)) { }
|
|
|
|
public SuperMapStream(ViewStream vs, ISuperStreamMapper mapper) {
|
|
myStream = vs;
|
|
myMapper = mapper;
|
|
myMapper.onMapperChange += new MapperChangeDelegate(myHandleMapChange);
|
|
}
|
|
|
|
protected override void myOnFirstSubscriber() { myStream.onChange += new ChangeDelegate(myHandleData); }
|
|
|
|
protected override void myOnLastSubscriber() {
|
|
myStream.onChange -= new ChangeDelegate(myHandleData);
|
|
myRows = null;
|
|
myBaseRows = null;
|
|
myMap.Clear();
|
|
}
|
|
|
|
private void myHandleData(object newValue) {
|
|
var dp = ExceptionAssert.Cast(newValue).To<IDataPacket>();
|
|
myRows ??= new BlueTable(myMapper.getOutputSchema(dp.Schema));
|
|
myBaseRows ??= new BlueTable(dp.Schema);
|
|
BlueDataUtility.absorbChanges(myBaseRows, dp.deletedRows, dp.addedRows);
|
|
var dels = new BlueTable(myRows.schema);
|
|
var adds = new BlueTable(myRows.schema);
|
|
|
|
foreach (IBlueRow r in dp.deletedRows) {
|
|
var mappedRows = myMap[r.Id()];
|
|
foreach (IBlueRow mr in mappedRows) {
|
|
dels.addRow(mr);
|
|
myRows.deleteRow(mr);
|
|
}
|
|
myMap.Remove(r.Id());
|
|
}
|
|
foreach (IBlueRow r in dp.addedRows) {
|
|
IBlueRow[] mappedRows = myMapper.map(r);
|
|
foreach (IBlueRow mr in mappedRows) {
|
|
IBlueRow idChangedMapRow = generate_ids ? BlueDataUtility.getNewReplacedRow(mr, myID++, "id") : mr;
|
|
myMap.GetOrAdd(r.Id(), _ => new()).Add(idChangedMapRow);
|
|
myRows.addRow(idChangedMapRow);
|
|
adds.addRow(idChangedMapRow);
|
|
}
|
|
}
|
|
myFireEvent(new DataPacket(myRows, adds, dels));
|
|
}
|
|
|
|
private void myHandleMapChange() {
|
|
if (myBaseRows is not null) {
|
|
myHandleData(new DataPacket(myBaseRows, new BlueTable(myBaseRows), new BlueTable(myBaseRows)));
|
|
}
|
|
}
|
|
}
|
|
}
|