Files
spark/src/DataStreamSuite/DataStream/BlueOrb/IndexStream.cs
T

54 lines
1.7 KiB
C#

namespace bts.BlueOrb;
using BCG;
using Bts.DataStream;
using Bts.DataStreamCore;
using Spark.Core.Disposables;
using System;
using System.Collections.Immutable;
public class IndexStream : ViewStream {
readonly ViewStream inputStream;
ImmutableDictionary<int, int> inputToOutputIds = ImmutableDictionary<int, int>.Empty;
ImmutableDictionary<int, int> outputToInputIds = ImmutableDictionary<int, int>.Empty;
int idCounter = 0;
IDisposable? sub;
public IndexStream(ViewStream inputStream) =>
this.inputStream = inputStream;
~IndexStream() =>
Disposable.dispose(ref sub);
protected override void myOnFirstSubscriber() {
base.myOnFirstSubscriber();
sub = inputStream.Subscribe(OnInputChanged);
void OnInputChanged(IDataPacket dp) {
var inputToOutputIdsBd = inputToOutputIds.ToBuilder();
var outputToInputIdsBd = outputToInputIds.ToBuilder();
foreach (var r in dp.addedRows) {
int inputId = r.Id();
if (!inputToOutputIds.TryGetValue(inputId, out int outputId)) {
outputId = ++idCounter;
inputToOutputIdsBd[inputId] = outputId;
outputToInputIdsBd[outputId] = inputId;
}
}
inputToOutputIds = inputToOutputIdsBd.ToImmutable();
outputToInputIds = outputToInputIdsBd.ToImmutable();
myFireEvent(new DataPacket(
new IndexTable(dp.newRows, outputToInputIds, inputToOutputIds),
new IndexTable(dp.addedRows, outputToInputIds, inputToOutputIds),
new IndexTable(dp.deletedRows, outputToInputIds, inputToOutputIds)));
}
}
protected override void myOnLastSubscriber() =>
Disposable.dispose(ref sub);
}