refactor: Make IBlueRowCollection inherit IList<IBlueRow>

This commit is contained in:
John Colvin committed 2025-11-15 12:38:51 -05:00
1 parent 144b29c728
commit d2a1b67717
39 files changed
+1035 -439

No files matched your search

@@ -43,7 +43,7 @@ namespace bts.BlueOrb {
_last_fire_time = DateTime.Now;
IBlueRow nr = new BlueRow(_schema, _id++, _last_fire_time,
_last_fire_time.ToString("yyyy-MM-dd HH:mm:ss.fff"));
_table.add_row(nr);
_table = _table.Add(nr);
var dp = BlueDataUtility.build_dp_inc(_table, myLastValue as DataPacket);
myFireEvent(dp);
}
@@ -1,9 +1,15 @@
using System;
using BCG;
using Bts.DataStreamCore;
using mbts.utils;
using Spark.Core.Collections;
using Spark.Core.Threading;
using System;
using System.Collections.Immutable;
namespace bts.BlueOrb {
// Keeps adding changing rows to the stream, hence preserving the history
// and creating a time series, for example this can be used to make bbo updates
@@ -11,6 +17,8 @@ namespace bts.BlueOrb {
[DataStreamUserRespresentation("TimeSeriesStream")]
public class TimeSeriesStream : ViewStream {
readonly ViewStream _stream;
readonly string timeColumn;
readonly AtomicGate gate = new();
CircularBufferTable? _table;
private int _id = 0;
private int _size = 0;
@@ -34,6 +42,9 @@ namespace bts.BlueOrb {
private void _onstream_change(object o) {
var dp = ExceptionAssert.Argument.Is<IDataPacket>(o);
DateTime now = DateTime.Now;
var addedRowsBd = ImmutableSortedSet.CreateBuilder(KeyValuePairComparer.KeyComparer<int, IBlueRow>());
var deletedRowsBd = ImmutableSortedSet.CreateBuilder(KeyValuePairComparer.KeyComparer<int, IBlueRow>());
using var _ = gate.BeginTransaction();
if (_table == null) {
IBlueTableSchema schema = new BlueTableSchema(dp.newRows.schema.columnInfo, dp.newRows.schema.tableName);
schema = schema.add_columns(Tuple.Create(typeof(DateTime), "timestamp"));
@@ -41,11 +52,18 @@ namespace bts.BlueOrb {
}
foreach (IBlueRow r in dp.addedRows) {
MutableBlueRow ar = new MutableBlueRow(_table.schema, r);
ar.setValue("timestamp", now);
ar.setValue("id", _id++);
_table.add_row(ar);
ar.setValue(timeColumn, now);
int id = _id++;
ar.setValue("id", id);
_table = _table.Add(ar, out var oldRow);
addedRowsBd.Add(new(id, ar));
if (oldRow is not null) {
deletedRowsBd.Add(new(oldRow.Id(), oldRow));
}
}
var dp_out = BlueDataUtility.build_dp(_table, myLastValue as DataPacket);
var addedRows = new BlueTableImmutable(_table.schema, addedRowsBd.ToImmutable());
var deletedRows = new BlueTableImmutable(_table.schema, deletedRowsBd.ToImmutable());
var dp_out = new DataPacket(_table, addedRows, deletedRows);
myFireEvent(dp_out);
}
}
@@ -19,13 +19,13 @@ public static class DataStreamExtensions {
public static ViewStream AsViewStream(this IObservable<IDataPacket> 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 ViewStream AsViewStream<T>(this IObservable<IDataPacket<T>> source, string? name = null, ILogger? logger = null) where T : IBlueRow =>
// source as ViewStream ?? new ObservableViewStream(source, name, logger);
public static IObservable<DataPacket> AsObservable(this ViewStream source) {
return source as IObservable<DataPacket> ?? Observable.Create<DataPacket>(Subscribe);
public static IObservable<IDataPacket> AsObservable(this ViewStream source) {
return source as IObservable<IDataPacket> ?? Observable.Create<IDataPacket>(Subscribe);
IDisposable Subscribe(IObserver<DataPacket> observer) {
IDisposable Subscribe(IObserver<IDataPacket> observer) {
source.onChange += OnChange;
return Disposable.Create(() => source.onChange -= OnChange);
@@ -35,15 +35,15 @@ public static class DataStreamExtensions {
}
}
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));
// 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);
});
// 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());
@@ -71,9 +71,33 @@ public static class DataStreamExtensions {
});
}
public static IObservable<IDataPacket<T>> AsDataPackets<T>(this IObservable<TableUpdate<T, int>> source, IBlueTableSchema schema) where T : IBlueRow =>
public static IObservable<IDataPacket> AsDataPackets(this IObservable<TableUpdate<IBlueRow, int>> source) =>
source.Scan(
(Table: new BlueTableImmutable<T>(schema), Dp: (IDataPacket<T>)DataPacket<T>.Empty(schema)),
(Table: (BlueTableImmutable?)null, Dp: (IDataPacket?)null),
(res, table) => {
var resTable = res.Table;
if (resTable is null) {
if (table.state.Count == 0) {
return (null, null);
}
resTable = new(table.state.Values.First().schema);
}
var bd = resTable.ToDataPacketBuilder();
foreach (var r in table.removed) {
bd.Remove(r.Id());
}
foreach (var r in table.added) {
bd.AddOrUpdate(r);
}
var resDp = bd.ToDataPacket(out var newRows);
return (newRows, resDp);
})
.Where(t => t.Dp is not null && (t.Dp.addedRows.Any() || t.Dp.deletedRows.Any()))
.Select(t => t.Dp!);
public static IObservable<IDataPacket> AsDataPackets<T>(this IObservable<TableUpdate<T, int>> source, IBlueTableSchema schema) where T : IBlueRow =>
source.Scan(
(Table: new BlueTableImmutable(schema), Dp: (IDataPacket)new DataPacket(schema)),
(res, table) => {
var bd = res.Table.ToDataPacketBuilder();
foreach (var r in table.removed) {
@@ -82,22 +106,22 @@ public static class DataStreamExtensions {
foreach (var r in table.added) {
bd.AddOrUpdate(r);
}
var resDp = bd.ToDataPacketT(out var newRows, out var addedRows, out var deletedRows);
var resDp = bd.ToDataPacket(out var newRows);
return (newRows, resDp);
})
.Where(t => t.Dp.addedRows.Any() || t.Dp.deletedRows.Any())
.Select(t => t.Dp);
public static IObservable<IDataPacket<BlueRowAdapter<T>>> AsDataPackets<T, TKey>(this IObservable<TableUpdate<T, TKey>> source, BlueTableSchemaAdapter<T> schema) where TKey : IEquatable<TKey> {
return Observable.Create<IDataPacket<BlueRowAdapter<T>>>(Subscribe);
public static IObservable<IDataPacket> AsDataPackets<T, TKey>(this IObservable<TableUpdate<T, TKey>> source, BlueTableSchemaAdapter<T> schema) where TKey : IEquatable<TKey> {
return Observable.Create<IDataPacket>(Subscribe);
IDisposable Subscribe(IObserver<IDataPacket<BlueRowAdapter<T>>> o) {
IDisposable Subscribe(IObserver<IDataPacket> o) {
var adapter = new BlueTableImmutableAdapter<T>(schema);
IDataPacket<BlueRowAdapter<T>> initDataPacket = DataPacket<BlueRowAdapter<T>>.Empty(schema);
IDataPacket initDataPacket = new DataPacket(schema);
return source.Scan((Adapter: adapter, DataPacket: initDataPacket), OnUpdate).Select(res => res.DataPacket).Subscribe(o);
(BlueTableImmutableAdapter<T> Adapter, IDataPacket<BlueRowAdapter<T>> DataPacket) OnUpdate(
(BlueTableImmutableAdapter<T> Adapter, IDataPacket<BlueRowAdapter<T>> DataPacket) state, TableUpdate<T, TKey> tableUpdate) {
(BlueTableImmutableAdapter<T> Adapter, IDataPacket DataPacket) OnUpdate(
(BlueTableImmutableAdapter<T> Adapter, IDataPacket DataPacket) state, TableUpdate<T, TKey> tableUpdate) {
var bd = state.Adapter.ToDataPacketBuilder();
bd.Update(tableUpdate);
var dp = bd.ToDataPacket();
@@ -114,14 +114,13 @@ import sys
executePythonCode(pythonCode.Value, pythonExe);
var rows = BlueDataUtility.createRowsFromCSVFileWithoutTypes(outputFile, out var schema);
var rowMap = rows as IDictionary<int, IBlueRow> ?? rows.Cast<IBlueRow>().ToDictionary(r => r.Id());
var comp = BlueRowEqualityComparer.Instance;
var deletedRows = new BlueTable(schema);
var addedRows = new BlueTable(schema);
if (lastRowMap is not null) {
deletedRows.SetItems(lastRowMap.Where(oldRow =>
!(rowMap.TryGetValue(oldRow.Key, out var newRow) && comp.Equals(newRow, oldRow.Value))));
!(rowMap.TryGetValue(oldRow.Key, out var newRow) && BlueRowEqualityComparer.Equals(newRow, oldRow.Value))));
addedRows.SetItems(rowMap.Where(newRow =>
!(lastRowMap.TryGetValue(newRow.Key, out var oldRow) && comp.Equals(oldRow, newRow.Value))));
!(lastRowMap.TryGetValue(newRow.Key, out var oldRow) && BlueRowEqualityComparer.Equals(oldRow, newRow.Value))));
}
lastRowMap = rowMap;
lastFireTime = DateTime.Now;
@@ -93,8 +93,7 @@ namespace BCG {
return Task.CompletedTask;
}
private void _on_str(DataPacket dp)
{
private void _on_str(IDataPacket dp) {
foreach (IBlueRow r in dp.newRows)
{
string name = r.get<string>("name");
@@ -3,7 +3,6 @@ using bts.collections;
using Bts.DataStreamCore;
using System;
using System.Collections;
using System.Collections.Generic;
namespace BCG
@@ -56,6 +55,8 @@ namespace BCG
public IBlueRow? getRow(string groupRepString) =>
myDict.GetValueOrDefault(groupRepString);
public int IndexOf(IBlueRow item) =>
myTable.IndexOf(item);
#region IBlueRowCollection members
public IBlueRow this[int id] {get{return myTable[id];}}
public IBlueTableSchema schema {get{return myTable.schema;}}
@@ -64,6 +65,8 @@ namespace BCG
index < 0 || index >= myTable.schema.columnInfo.Length
? throw new IndexOutOfRangeException()
: myDict.Values.WrapSelector(r => r[index], null);
public IBlueRow GetRow(int index) =>
myDict.Values[index];
#endregion
#region ICollection members
@@ -74,7 +77,8 @@ namespace BCG
#endregion
#region IEnumerable members
public IEnumerator GetEnumerator() {return myTable.GetEnumerator ();}
public IEnumerator<IBlueRow> GetEnumerator() =>
myTable.GetEnumerator ();
#endregion
@@ -99,6 +99,8 @@ namespace BCG
public bool containsID (int id) {return myLinearCollection.containsID (id);}
public IList<object?> GetColumn(int index) =>
myLinearCollection.GetColumn(index);
public IBlueRow GetRow(int index) =>
myLinearCollection.GetRow(index);
#endregion
#region ICollection members
@@ -106,10 +108,13 @@ namespace BCG
public bool IsSynchronized {get{return false;}}
public object SyncRoot {get{return this;}}
public void CopyTo (Array array, int index) {myLinearCollection.CopyTo (array, index);}
public int IndexOf(IBlueRow item) =>
myLinearCollection.IndexOf(item);
#endregion
#region IEnumerable members
public IEnumerator GetEnumerator() {return myLinearCollection.GetEnumerator ();}
public IEnumerator<IBlueRow> GetEnumerator() =>
myLinearCollection.GetEnumerator ();
#endregion
}
@@ -3,7 +3,6 @@ using bts.collections;
using Bts.DataStreamCore;
using System;
using System.Collections;
using System.Collections.Generic;
namespace BCG
@@ -72,10 +71,20 @@ namespace BCG
array.SetValue(myIdIndex.Values[i], index++);
}
}
public int IndexOf(IBlueRow item) {
int id = item.Id();
int index = myIdIndex.IndexOfKey(id);
if (index != -1 && BlueRowEqualityComparer.Equals(item, myIdIndex.Values[index])) {
return index;
}
return -1;
}
#endregion
#region IEnumerable members
public IEnumerator GetEnumerator () {return myIdIndex.Values.GetEnumerator ();}
public IEnumerator<IBlueRow> GetEnumerator() =>
myIdIndex.Values.GetEnumerator();
#endregion
#region IBlueRowCollection members
@@ -87,6 +96,8 @@ namespace BCG
}
return myIdIndex.Values.WrapSelector(r => r[index], null);
}
public IBlueRow GetRow(int index) =>
myIdIndex.Values[index];
#endregion
}
@@ -1,5 +1,7 @@
using bts.collections;
using Bts.DataStreamCore;
using System;
using System.Collections;
using System.Collections.Generic;
@@ -47,6 +49,12 @@ namespace BCG
? throw new IndexOutOfRangeException()
: myRows.WrapType<IBlueRow>().WrapSelector(r => r[index], null);
public IBlueRow GetRow(int index) {
var rowObj = myRows[index];
ArgumentNullException.ThrowIfNull(rowObj);
return (IBlueRow)rowObj;
}
public IBlueTableSchema schema {get {return mySchema;}}
#endregion
@@ -61,11 +69,28 @@ namespace BCG
public object SyncRoot {get{return null;}}
public int IndexOf(IBlueRow item) {
int id = item.Id();
for (int i = 0; i < myRows.Count; i++) {
if (myRows[i] is not IBlueRow r || r.Id() != id) {
continue;
}
if (BlueRowEqualityComparer.Equals(item, r)) {
return i;
}
break;
}
return -1;
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator() {return myRows.GetEnumerator ();}
public IEnumerator<IBlueRow> GetEnumerator() {
foreach (IBlueRow r in myRows) {
yield return r;
}
}
#endregion
}
@@ -66,7 +66,7 @@ namespace bts {
if (_in_rows.Count > 0) {
// fire row
IBlueRow curr_row = _in_rows.ElementAt(_in_row_ptr++);
_out_table.add_row(curr_row);
_out_table = _out_table.Add(curr_row);
DataPacket dp = BlueDataUtility.build_dp_inc(_out_table,
myLastValue == null ? null : myLastValue as DataPacket, false);
myFireEvent(dp);
@@ -74,4 +74,4 @@ namespace bts {
}
}
}
}
@@ -1,5 +1,8 @@
using bts;
using Bts.DataStreamCore;
using System;
using System.Collections;
using System.Collections.Generic;
namespace BCG
@@ -39,63 +42,36 @@ namespace BCG
var origIdx = myHelper.colMap[index];
return myRows.GetColumn(origIdx);
}
public IBlueRow GetRow(int index) =>
new ProjectedRow(myRows.GetRow(index), myHelper);
#endregion
#region ICollection Members
public bool IsSynchronized {get {return myRows.IsSynchronized;}}
public int Count {get {return myRows.Count;}}
public void CopyTo (Array array, int index)
{
foreach (IBlueRow r in myRows)
array.SetValue (this[(int)r["id"]], index++);
public int IndexOf(IBlueRow item) {
int id = item.Id();
var rowIn = myRows[id];
var projRow = new ProjectedRow(rowIn, myHelper);
if (BlueRowEqualityComparer.Equals(projRow, item)) {
var index = myRows.IndexOf(rowIn);
Log.assert(index >= 0 && index < myRows.Count);
return index;
}
return -1;
}
public object SyncRoot {get {return myRows.SyncRoot;}}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator ()
{
return new MyEnumerator (myRows, myHelper);
}
private class MyEnumerator : IEnumerator
{
#region IEnumerator Members
private IEnumerator myMaster;
private ProjectHelper myHelper;
public MyEnumerator (IBlueRowCollection master, ProjectHelper helper)
{
myMaster = master.GetEnumerator ();
myHelper = helper;
public IEnumerator<IBlueRow> GetEnumerator() {
foreach (var row in myRows) {
yield return new ProjectedRow(row, myHelper);
}
public void Reset() {myMaster.Reset();}
public object Current
{
get
{
return myMaster.Current == null ? null :
new ProjectedRow (myMaster.Current as IBlueRow, myHelper);
}
}
public bool MoveNext() {return myMaster.MoveNext ();}
#endregion
}
#endregion
}
@@ -2,12 +2,20 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using bts.collections;
using bts.utils;
using bts.collections;
using Bts.ColumnSortExpressions;
using Bts.DataStreamCore;
using mbts.utils;
using Spark.Core.Collections;
namespace BCG;
/// <summary>
@@ -93,7 +101,29 @@ public class SortedTable : IBlueRowCollection {
? throw new IndexOutOfRangeException()
: rows.WrapSelector(r => r[index], null);
public IEnumerator GetEnumerator() =>
public IBlueRow GetRow(int index) =>
rows[index];
public int IndexOf(IBlueRow item) {
if (item is not SortRow sortRow) {
return -1;
}
int id = sortRow.Id();
if (comparer is not null) {
rows.BinarySearch(sortRow, GenericComparerWrapper.Create<IBlueRow>(comparer));
}
var colComps = sortCols
.Select(s => {
IComparer<IBlueRow> colComp = new BlueRowComparer(s.Name);
if (!s.IsAscending) {
colComp = colComp.reverse();
}
return colComp;
}).ToList();
return rows.BinarySearch(sortRow, new ComparerBuilder<IBlueRow>(colComps).to_comparer());
}
public IEnumerator<IBlueRow> GetEnumerator() =>
rows.GetEnumerator();
public void CopyTo(Array array, int index) {
@@ -44,7 +44,7 @@ namespace BCG {
_last_fire_time = DateTime.Now;
IBlueRow nr = new BlueRow(_schema, _id++, _last_fire_time,
_last_fire_time.ToString("yyyy-MM-dd HH:mm:ss.fff"));
_table.add_row(nr);
_table = _table.Add(nr);
var dp = BlueDataUtility.build_dp_inc(_table, myLastValue as DataPacket);
myFireEvent(dp);
}
@@ -9,9 +9,9 @@ using mbts.utils;
using Spark.Core.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace BCG
{
@@ -89,6 +89,32 @@ namespace BCG
return allRows.as_mutable().WrapSelector(kv => kv.Value[index], null);
}
public IBlueRow GetRow(int index) {
if (index < 0 || index >= Count) {
throw new IndexOutOfRangeException();
}
if (index < myLeftIndex.Count) {
return myLeftIndex.Values[index];
}
return myRightIndex.Values[index - myLeftIndex.Count];
}
public int IndexOf(IBlueRow item) {
int id = item.Id();
int index = myLeftIndex.IndexOfKey(id);
if (index != -1) {
if (BlueRowEqualityComparer.Equals(myLeftIndex.Values[index], item)) {
return index;
}
} else {
index = myRightIndex.IndexOfKey(id);
if (index != -1 && BlueRowEqualityComparer.Equals(myRightIndex.Values[index], item)) {
return index + myLeftIndex.Count;
}
}
return -1;
}
IBlueRow leftRowFactory(KeyValuePair<int, IBlueRow> kv) =>
new AppendRow(kv.Value, leftIdMap[kv.Key], mySchema ?? kv.Value.schema);
@@ -105,40 +131,9 @@ namespace BCG
}
#region IEnumerable Members
public System.Collections.IEnumerator GetEnumerator() {
return new MyEnumerator(myLeftIndex, myRightIndex);
}
public IEnumerator<IBlueRow> GetEnumerator() =>
myLeftIndex.Values.Concat(myRightIndex.Values).GetEnumerator();
private class MyEnumerator : IEnumerator
{
private bool myIsLeft = true;
private IEnumerator myEnum;
private IDictionary myLeft, myRight;
#region IEnumerator Members
public MyEnumerator (IDictionary left, IDictionary right)
{
myLeft = left;
myRight = right;
myEnum = myLeft.Values.GetEnumerator ();
}
public void Reset() {myEnum = myLeft.GetEnumerator ();}
public object Current {get {return myEnum.Current;}}
public bool MoveNext()
{
bool retVal = myEnum.MoveNext ();
if (!retVal && myIsLeft)
{
myIsLeft = false;
retVal = (myEnum = myRight.Values.GetEnumerator()).MoveNext ();
}
return retVal;
}
#endregion
}
#endregion
}
}
@@ -152,6 +152,8 @@ namespace BCG {
public IList<object?> GetColumn(int index) =>
myRows.GetColumn(index);
public IBlueRow GetRow(int index) =>
myRows.GetRow(index);
#endregion
#region ICollection Members
@@ -164,11 +166,14 @@ namespace BCG {
public object SyncRoot { get { return myRows.SyncRoot; } }
public int IndexOf(IBlueRow item) =>
myRows.IndexOf(item);
#endregion
#region IEnumerable Members
public System.Collections.IEnumerator GetEnumerator() { return myRows.GetEnumerator(); }
public IEnumerator<IBlueRow> GetEnumerator() =>
myRows.GetEnumerator();
#endregion
}