refactor: Replace DataPacket w/ IDataPacket in stream handlers

This commit also renames BlueTableSchemaAdaptor to BlueTableSchemaAdapter,
and makes it implement IBlueTableSchema.
This commit is contained in:
John Colvin committed 2025-11-03 16:21:32 -05:00
1 parent 1155fd6997
commit f65b6b82f2
11 files changed
+278 -27

No files matched your search

@@ -3,10 +3,15 @@ namespace Bts.DataStreamCore;
using BCG;
using bts.api.core;
using Bts.DataStreamCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
// I would have named this ImmutableBlueTable, but that was already taken.
public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
@@ -218,3 +223,106 @@ public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
new(schema, table.ToImmutable());
}
}
public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>> {
readonly BlueTableSchemaAdapter<T> schemaAdapter;
readonly ImmutableDictionary<int, T> rows;
public BlueTableImmutableAdapter(BlueTableSchemaAdapter<T> schemaAdapter)
: this(schemaAdapter, ImmutableDictionary<int, T>.Empty) { }
public BlueTableImmutableAdapter(BlueTableSchemaAdapter<T> schemaAdapter, ImmutableDictionary<int, T> rows) {
this.schemaAdapter = schemaAdapter;
this.rows = rows;
}
public int Count => rows.Count;
public bool IsSynchronized => false;
public IBlueTableSchema schema => schemaAdapter.Schema;
public object SyncRoot => this;
public BlueRowAdapter<T> this[int id] => new(schemaAdapter, rows[id]);
public bool containsID(int id) =>
rows.ContainsKey(id);
public void CopyTo(Array array, int index) {
foreach (var r in rows.Values) {
array.SetValue(new BlueRowAdapter<T>(schemaAdapter, r), index++);
}
}
public IEnumerator<BlueRowAdapter<T>> GetEnumerator() {
foreach (var row in rows.Values) {
yield return new BlueRowAdapter<T>(schemaAdapter, row);
}
}
public DataPacketBuilder ToDataPacketBuilder() =>
new(this);
public class DataPacketBuilder {
readonly BlueTableSchemaAdapter<T> schemaAdapter;
readonly ImmutableDictionary<int, T>.Builder table;
readonly ImmutableDictionary<int, T>.Builder added =
ImmutableDictionary<int, T>.Empty.ToBuilder();
readonly ImmutableDictionary<int, T>.Builder removed =
ImmutableDictionary<int, T>.Empty.ToBuilder();
public DataPacketBuilder(BlueTableImmutableAdapter<T> table) {
schemaAdapter = table.schemaAdapter;
this.table = table.rows.ToBuilder();
}
public void AddOrUpdate(T rowData, IEqualityComparer<IBlueRow>? comparer = null) {
var row = new BlueRowAdapter<T>(schemaAdapter, rowData);
var id = row.Id();
if (table.TryGetValue(id, out var origData)) {
var orig = new BlueRowAdapter<T>(schemaAdapter, origData);
if (comparer?.Equals(row, orig) ?? false) {
// No op: This row was already in the table (according to 'comparer').
// Remove it from 'added' and 'removed'.
added.Remove(id);
removed.Remove(id);
return;
}
// Change op: A row with this id was already in the table,
// but its values have changed (according to 'comparer').
removed[id] = origData;
}
added[id] = rowData;
}
public void Remove(int id) {
added.Remove(id);
if (table.TryGetValue(id, out var orig)) {
removed[id] = orig;
}
}
public void Update<TKey>(TableUpdate<T, TKey> tableUpdate) where TKey : IEquatable<TKey> {
foreach (var r in tableUpdate.removed.Concat(tableUpdate.changes.Old())) {
Remove(new BlueRowAdapter<T>(schemaAdapter, r).Id());
}
foreach (var r in tableUpdate.changed.Concat(tableUpdate.added)) {
AddOrUpdate(r);
}
}
public IDataPacket<BlueRowAdapter<T>> ToDataPacket(bool reset = true) {
table.RemoveRange(removed.Keys);
table.AddRange(added);
var newRows = new BlueTableImmutableAdapter<T>(schemaAdapter, table.ToImmutable());
var addedRows = new BlueTableImmutableAdapter<T>(schemaAdapter, added.ToImmutable());
var removedRows = new BlueTableImmutableAdapter<T>(schemaAdapter, removed.ToImmutable());
if (reset) {
added.Clear();
removed.Clear();
}
return new DataPacket<BlueRowAdapter<T>>(newRows, addedRows, removedRows);
}
public BlueTableImmutableAdapter<T> ToImmutable() =>
new(schemaAdapter, table.ToImmutable());
}
}