chore: Add SelectorStream/Table

This commit is contained in:
John Colvin committed 2025-11-15 12:46:24 -05:00
1 parent d0c316b3fc
commit 342ecd9a52
2 files changed
+100

No files matched your search

@@ -0,0 +1,42 @@
namespace bts.BlueOrb;
using BCG;
using Bts.DataStream;
using Spark.Core.Disposables;
using System;
public class SelectorStream : ViewStream, IDisposable {
readonly ViewStream source;
readonly IBlueTableSchema outputSchema;
readonly Func<IBlueRow, IBlueRow> selector;
IDisposable? sub;
public SelectorStream(ViewStream source, IBlueTableSchema outputSchema, Func<IBlueRow, IBlueRow> selector) {
this.source = source;
this.outputSchema = outputSchema;
this.selector = selector;
}
public void Dispose() {
GC.SuppressFinalize(this);
Disposable.dispose(ref sub);
}
protected override void myOnFirstSubscriber() {
Disposable.dispose(ref sub);
sub = source.Subscribe(OnInputChanged);
void OnInputChanged(IDataPacket dp) =>
myFireEvent(new DataPacket(
new SelectorTable(dp.newRows, outputSchema, selector),
new SelectorTable(dp.addedRows, outputSchema, selector),
new SelectorTable(dp.deletedRows, outputSchema, selector)));
}
protected override void myOnLastSubscriber() =>
Disposable.dispose(ref sub);
}
@@ -0,0 +1,58 @@
namespace bts.BlueOrb;
using BCG;
using Bts.DataStreamCore;
using System;
using System.Collections.Generic;
/// <summary>
/// Represents a row transformation table (all columns except 'id' can be changed).
/// </summary>
public class SelectorTable : IBlueRowCollection {
readonly IBlueRowCollection source;
readonly IBlueTableSchema outputSchema;
readonly Func<IBlueRow, IBlueRow> selector;
public SelectorTable(IBlueRowCollection source, IBlueTableSchema outputSchema, Func<IBlueRow, IBlueRow> selector) {
this.source = source;
this.outputSchema = outputSchema;
this.selector = selector;
}
public IBlueRow this[int id] =>
selector(source[id]);
IBlueRow IList<IBlueRow>.this[int index] {
get => GetRow(index);
set => throw new NotSupportedException();
}
public IBlueTableSchema schema => outputSchema;
public int Count => source.Count;
public bool containsID(int id) =>
source.containsID(id);
public IEnumerator<IBlueRow> GetEnumerator() {
foreach (var r in source) {
yield return selector(r);
}
}
public IBlueRow GetRow(int index) =>
selector(GetRow(index));
public int IndexOf(IBlueRow item) {
int id = item.Id();
if (!source.containsID(id)) {
return -1;
}
var inputRow = source[id];
return BlueRowEqualityComparer.Equals(selector(inputRow), item)
? source.IndexOf(inputRow)
: -1;
}
}