84 lines
2.7 KiB
C#
84 lines
2.7 KiB
C#
namespace bts {
|
|
using BCG;
|
|
using BCG.Utilities.General;
|
|
|
|
using Spark.Core.Diagnostics;
|
|
using Spark.Core.Disposables;
|
|
|
|
using System;
|
|
using System.Diagnostics;
|
|
|
|
// functon that joins the 2 rows and returns the outputput row. The function
|
|
// can return a null which means the 2 rows dont need to be joined
|
|
using JoinFunc = System.Func<BCG.IBlueRow, BCG.IBlueRow, BCG.IBlueRow>;
|
|
public class ProximityJoinStream : ViewStream, IWorker {
|
|
private ViewStream _lvs, _rvs;
|
|
private JoinFunc _join_func;
|
|
private bool _dirty = false;
|
|
private Stopwatch _stopwatch = new Stopwatch();
|
|
private ViewStreamDatabase _vsd = ViewStreamDatabase.instance;
|
|
IDisposable? subscription;
|
|
IBlueRowCollection? leftRows;
|
|
IBlueRowCollection? rightRows;
|
|
|
|
public ProximityJoinStream(ViewStream lvs, ViewStream rvs, JoinFunc join_func) {
|
|
_lvs = lvs;
|
|
_rvs = rvs;
|
|
_join_func = join_func;
|
|
}
|
|
|
|
protected override void myOnFirstSubscriber() {
|
|
_lvs.onChange += OnLeftChange;
|
|
_rvs.onChange += OnRightChange;
|
|
subscription = Disposable.Create(Unsubscribe);
|
|
_vsd.addWorker(this);
|
|
|
|
void OnLeftChange(object dp_obj) {
|
|
leftRows = ExceptionAssert.Argument.Is<IDataPacket>(dp_obj).newRows;
|
|
_dirty = true;
|
|
doWork();
|
|
}
|
|
|
|
void OnRightChange(object dp_obj) {
|
|
rightRows = ExceptionAssert.Argument.Is<IDataPacket>(dp_obj).newRows;
|
|
_dirty = true;
|
|
doWork();
|
|
}
|
|
|
|
void Unsubscribe() {
|
|
_lvs.onChange -= OnLeftChange;
|
|
_rvs.onChange -= OnRightChange;
|
|
}
|
|
}
|
|
|
|
protected override void myOnLastSubscriber() {
|
|
subscription?.Dispose();
|
|
subscription = null;
|
|
_vsd.removeWorker(this);
|
|
}
|
|
|
|
public void doWork() {
|
|
if (!_dirty || leftRows is null || rightRows is null) {
|
|
return;
|
|
}
|
|
_stopwatch.Start();
|
|
_dirty = false;
|
|
BlueTable? rv = null;
|
|
foreach (IBlueRow lr in leftRows) {
|
|
foreach (IBlueRow rr in rightRows) {
|
|
IBlueRow resr = _join_func(lr, rr);
|
|
if (resr == null)
|
|
continue;
|
|
rv = rv ?? new BlueTable(resr.schema);
|
|
rv.addRow(resr);
|
|
}
|
|
}
|
|
_stopwatch.Stop();
|
|
Log.Gen.info($"ProximityJoinStream left_count: {leftRows.Count} right_count: {rightRows.Count} result_count: {rv?.Count ?? 0} time: {_stopwatch.ElapsedMilliseconds} msecs");
|
|
var dp = BlueDataUtility.build_dp_inc(rv, myLastValue as DataPacket);
|
|
myFireEvent(dp);
|
|
}
|
|
|
|
}
|
|
}
|