using System; using System.Collections.Generic; using System.Text; using BCG.Utilities; using BCG.Utilities.General; using mbts; namespace BCG { /// /// 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 /// 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 _get_schema; private Func> _map_rows; public MyFuncWrapper(Func get_schema, Func> 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 rows = _map_rows(r); return rows == null ? new IBlueRow[] { } : new List(rows).ToArray(); } public event MapperChangeDelegate onMapperChange; private void _fake() { onMapperChange(); } } //maps the id of the underlying stream to the //rows of the resulting stream private Dictionary> myMap = new Dictionary>(); private int myID = 0; private BlueTable myRows, myBaseRows; private ViewStream myStream; private ISuperStreamMapper myMapper; public bool generate_ids = true; public SuperMapStream(ViewStream vs, Func get_schema, Func> map_rows) : this(vs, new MyFuncWrapper(get_schema, map_rows)) { } public SuperMapStream(ViewStream vs, IBlueTableSchema out_schema, Func> 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(); //myMapper.onMapperChange -= new MapperChangeDelegate(myHandleMapChange); } private void myHandleData(object newValue) { DataPacket idp = newValue as DataPacket; //HighPerformanceTimer timer = new HighPerformanceTimer(); //timer.start(); DataPacket dp = newValue as DataPacket; myRows = myRows ?? new BlueTable(myMapper.getOutputSchema(dp.schema)); myBaseRows = myBaseRows ?? new BlueTable(dp.schema); BlueDataUtility.absorbChanges(myBaseRows, dp.deletedRows, dp.addedRows); BlueTable dels = new BlueTable(myRows.schema), adds = new BlueTable(myRows.schema); foreach (IBlueRow r in dp.deletedRows) { List mappedRows = myMap[(int)r["id"]]; foreach (IBlueRow mr in mappedRows) { dels.addRow(mr); myRows.deleteRow(mr); } myMap.Remove((int)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; if (!myMap.ContainsKey((int)r["id"])) { myMap.Add((int)r["id"], new List()); } myMap[(int)r["id"]].Add(idChangedMapRow); myRows.addRow(idChangedMapRow); adds.addRow(idChangedMapRow); } } //timer.stop(); // if (timer.lastIntervalTime > 20) // {}//mbts.Logger.log(bts.log.Level.Info, bts.log.Subsystem.gen, String.Format("SuperMapper:myHandlerData for mapper {0} took {1} msecs", myMapper.GetType().ToString(), timer.lastIntervalTime)); myFireEvent(new DataPacket(myRows, adds, dels)); } private void myHandleMapChange() { if (myBaseRows != null) myHandleData(new DataPacket(myBaseRows, new BlueTable(myBaseRows), new BlueTable(myBaseRows))); } } }