135 lines
5.8 KiB
C#
135 lines
5.8 KiB
C#
namespace Bts.DataStream;
|
|
|
|
using BCG;
|
|
|
|
using bts.api.core;
|
|
|
|
using Bts.DataStreamCore;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Spark.Core.Disposables;
|
|
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
|
|
public static class DataStreamExtensions {
|
|
public static ViewStream AsViewStream(this IObservable<DataPacket> source) =>
|
|
source as ViewStream ?? new ObservableViewStream(source);
|
|
|
|
public static ViewStream AsViewStream<T>(this IObservable<IDataPacket<T>> source, string? name = null, ILogger? logger = null) where T : IBlueRow =>
|
|
source as ViewStream ?? new ObservableViewStream(source.Select(dp => new DataPacket(dp.newRows, dp.addedRows, dp.deletedRows)), name, logger);
|
|
|
|
public static IObservable<DataPacket> AsObservable(this ViewStream source) {
|
|
return source as IObservable<DataPacket> ?? Observable.Create<DataPacket>(Subscribe);
|
|
|
|
IDisposable Subscribe(IObserver<DataPacket> observer) {
|
|
source.onChange += OnChange;
|
|
return Disposable.Create(() => source.onChange -= OnChange);
|
|
|
|
void OnChange(object? newValue) =>
|
|
observer.OnNext(newValue as DataPacket ?? throw new ArgumentException(
|
|
$"Expected DataPacket, got {newValue?.GetType()}", nameof(newValue)));
|
|
}
|
|
}
|
|
|
|
public static IObservable<IDataPacket<T>> SelectDataPacket<T>(this IObservable<IDataPacket> source, Func<IBlueRow, T> selector) where T : IBlueRow =>
|
|
source.Select(dp => {
|
|
dp.get_new_added_deleted(out var newRows, out var addedRows, out var deletedRows);
|
|
return new DataPacket<T>(CastRows(newRows), CastRows(addedRows), CastRows(deletedRows));
|
|
|
|
IBlueRowCollection<T> CastRows(ICollection rows) =>
|
|
new BlueTableSelectorWrapper<T>(
|
|
rows as IBlueRowCollection ?? new BlueRowCollection(rows, dp.Schema), selector);
|
|
});
|
|
|
|
public static IObservable<TableUpdate<IBlueRow, int>> AsTables(this IObservable<IDataPacket> source) =>
|
|
source.AsTables<IBlueRow, int>(r => r, r => r.Id());
|
|
|
|
public static IObservable<TableUpdate<T, int>> AsTables<T>(this IObservable<IDataPacket> source, Func<IBlueRow, T> valueSelector) where T : IBlueRow =>
|
|
source.AsTables<T, int>(valueSelector, r => r.Id());
|
|
|
|
public static IObservable<TableUpdate<T, int>> AsTables<T>(
|
|
this IObservable<IDataPacket> source, Func<IBlueRow, T> valueSelector, Func<T, int> keySelector) =>
|
|
source.AsTables<T, int>(valueSelector, keySelector);
|
|
|
|
public static IObservable<TableUpdate<T, TKey>> AsTables<T, TKey>(
|
|
this IObservable<IDataPacket> source, Func<IBlueRow, T> valueSelector, Func<T, TKey> keySelector, Func<IBlueRow, TKey>? rowKeySelector = null) where TKey : IEquatable<TKey> {
|
|
rowKeySelector ??= r => keySelector(valueSelector(r));
|
|
return source.Scan(TableUpdate<T, TKey>.mk_empty(keySelector), (res, dp) => {
|
|
var bd = res.to_builder();
|
|
dp.get_new_added_deleted(out var newRows, out var addedRows, out var deletedRows);
|
|
foreach (IBlueRow r in addedRows) {
|
|
bd.add_or_update(valueSelector(r));
|
|
}
|
|
foreach (IBlueRow r in deletedRows) {
|
|
bd.remove(rowKeySelector(r));
|
|
}
|
|
return bd.to_table();
|
|
});
|
|
}
|
|
|
|
public static IObservable<IDataPacket<T>> AsDataPackets<T>(this IObservable<TableUpdate<T, int>> source, IBlueTableSchema schema) where T : IBlueRow =>
|
|
source.Scan(
|
|
(Table: new BlueTableImmutable<T>(schema), Dp: (IDataPacket<T>)DataPacket<T>.Empty(schema)),
|
|
(res, table) => {
|
|
var bd = res.Table.ToDataPacketBuilder();
|
|
foreach (var r in table.removed) {
|
|
bd.Remove(r.Id());
|
|
}
|
|
foreach (var r in table.added) {
|
|
bd.AddOrUpdate(r);
|
|
}
|
|
var resDp = bd.ToDataPacketT(out var newRows, out var addedRows, out var deletedRows);
|
|
return (newRows, resDp);
|
|
})
|
|
.Where(t => t.Dp.addedRows.Any() || t.Dp.deletedRows.Any())
|
|
.Select(t => t.Dp);
|
|
|
|
public static ViewStream AsViewStream<T>(this IObservable<TableUpdate<T, int>> source, IBlueTableSchema schema) where T : IBlueRow =>
|
|
source.AsDataPackets(schema).AsViewStream();
|
|
|
|
public static int ResolveIndices(this IBlueTableSchema schema, string name) {
|
|
var indices = ResolveIndices(schema, new[] { name });
|
|
return indices[0];
|
|
}
|
|
|
|
public static (int, int) ResolveIndices(this IBlueTableSchema schema, string name1, string name2) {
|
|
var indices = ResolveIndices(schema, new[] { name1, name2 });
|
|
return (indices[0], indices[1]);
|
|
}
|
|
|
|
public static (int, int, int) ResolveIndices(this IBlueTableSchema schema, string name1, string name2, string name3) {
|
|
var indices = ResolveIndices(schema, new[] { name1, name2, name3 });
|
|
return (indices[0], indices[1], indices[2]);
|
|
}
|
|
|
|
public static (int, int, int, int) ResolveIndices(this IBlueTableSchema schema, string name1, string name2, string name3, string name4) {
|
|
var indices = ResolveIndices(schema, new[] { name1, name2, name3, name4 });
|
|
return (indices[0], indices[1], indices[2], indices[3]);
|
|
}
|
|
|
|
public static int[] ResolveIndices(this IBlueTableSchema schema, string[] columnNames) {
|
|
var columns = schema.columnInfo;
|
|
var indices = new int[columnNames.Length];
|
|
int nResolved = 0;
|
|
for (int i = 0; i < columnNames.Length; i++) {
|
|
int origNResolved = nResolved;
|
|
for (int j = 0; j < columns.Length; j++) {
|
|
if (columns[j].colName == columnNames[i]) {
|
|
indices[i] = j;
|
|
nResolved++;
|
|
break;
|
|
}
|
|
}
|
|
if (nResolved == origNResolved) {
|
|
throw new ArgumentException($"No column named '{columnNames[i]}'", nameof(schema));
|
|
}
|
|
}
|
|
return indices;
|
|
}
|
|
}
|