62 lines
2.5 KiB
C#
62 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
|
|
using bts;
|
|
|
|
namespace BCG
|
|
{
|
|
public class NonIncGroupByStream : ViewStream
|
|
{
|
|
private ViewStream _main_stream;
|
|
private INonIncGroupByEvaluator _groupby_func;
|
|
private BlueRowHasher _row_hasher;
|
|
private string[] _groupby_cols;
|
|
private Stopwatch _timer = new();
|
|
private int _n_total_fires = 0, _n_expensive_fires = 0;
|
|
|
|
public NonIncGroupByStream(ViewStream main_stream, INonIncGroupByEvaluator groupby_func)
|
|
{
|
|
_main_stream = main_stream;
|
|
_groupby_func = groupby_func;
|
|
_groupby_cols = _groupby_func.get_group_by_cols();
|
|
_row_hasher = new BlueRowHasher(_groupby_cols.Length);
|
|
}
|
|
|
|
protected override void myOnFirstSubscriber() { _main_stream.onChange += _main_stream_onchange; }
|
|
|
|
protected override void myOnLastSubscriber() { _main_stream.onChange -= _main_stream_onchange; }
|
|
|
|
private void _main_stream_onchange(object nv)
|
|
{
|
|
DataPacket dp = nv as DataPacket;
|
|
///grouping together
|
|
_timer.Restart();
|
|
IDictionary<int, List<IBlueRow>> grouped_rows = BlueDataUtility.group_together (dp.newRows, _groupby_cols, ref _row_hasher);
|
|
_timer.Stop();
|
|
var groupElapsedMs = _timer.ElapsedMilliseconds;
|
|
if (groupElapsedMs >= 50) {
|
|
Log.DataFlow.info($"Time to group {dp.newRows.Count} rows into {grouped_rows.Count} groups: {_timer.ElapsedMilliseconds}ms");
|
|
}
|
|
// Log.log(bts.Log.Level.Info, bts.log.Subsystem.gen, String.Format("Time to group {0} rows into {1} groups = {2}", dp.newRows.Count, grouped_rows.Count, _timer.lastIntervalTime));
|
|
/// reducing
|
|
_timer.Restart();
|
|
IBlueRowCollection total_state = _groupby_func.evaluate(grouped_rows);
|
|
_timer.Stop();
|
|
if (total_state is null) {
|
|
Log.Gen.warn("NonIncGroupByStream _groupby_func.evaluate returned null (ignoring last update).");
|
|
return;
|
|
}
|
|
var applyElapsedMs = _timer.ElapsedMilliseconds;
|
|
_n_total_fires++;
|
|
if (applyElapsedMs >= 50)
|
|
{
|
|
_n_expensive_fires++;
|
|
Log.DataFlow.info(
|
|
$"Time to evaluate {grouped_rows.Count} groups {_timer.ElapsedMilliseconds}ms, ratio {_n_expensive_fires}/{_n_total_fires}");
|
|
}
|
|
myFireEvent(new DataPacket(total_state, total_state, myLastValue == null ? new BlueTable(total_state.schema) : (myLastValue as DataPacket).newRows));
|
|
}
|
|
}
|
|
}
|