This commit also renames BlueTableSchemaAdaptor to BlueTableSchemaAdapter, and makes it implement IBlueTableSchema.
342 lines
12 KiB
C#
342 lines
12 KiB
C#
#nullable enable
|
|
namespace Bts.DataStreamCore;
|
|
|
|
using BCG;
|
|
|
|
using bts.api.core;
|
|
|
|
using Bts.DataStream;
|
|
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 {
|
|
readonly ImmutableDictionary<int, IBlueRow> rows;
|
|
|
|
public BlueTableImmutable(IBlueTableSchema schema)
|
|
: this(schema, ImmutableDictionary<int, IBlueRow>.Empty) { }
|
|
|
|
public BlueTableImmutable(IBlueTableSchema schema, ImmutableDictionary<int, IBlueRow> rows) {
|
|
this.schema = schema;
|
|
this.rows = rows;
|
|
}
|
|
|
|
public int Count => rows.Count;
|
|
public bool IsSynchronized => false;
|
|
public IBlueTableSchema schema { get; }
|
|
public object SyncRoot => this;
|
|
|
|
public IBlueRow this[int id] => 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(r, index++);
|
|
}
|
|
}
|
|
|
|
public IEnumerator GetEnumerator() =>
|
|
rows.Values.GetEnumerator();
|
|
|
|
public DataPacketBuilder ToDataPacketBuilder() =>
|
|
new(this);
|
|
|
|
public class DataPacketBuilder {
|
|
readonly IBlueTableSchema schema;
|
|
readonly ImmutableDictionary<int, IBlueRow>.Builder table;
|
|
readonly ImmutableDictionary<int, IBlueRow>.Builder added =
|
|
ImmutableDictionary<int, IBlueRow>.Empty.ToBuilder();
|
|
readonly ImmutableDictionary<int, IBlueRow>.Builder removed =
|
|
ImmutableDictionary<int, IBlueRow>.Empty.ToBuilder();
|
|
|
|
public DataPacketBuilder(BlueTableImmutable table) {
|
|
schema = table.schema;
|
|
this.table = table.rows.ToBuilder();
|
|
}
|
|
|
|
public void AddOrUpdate(IBlueRow row, IEqualityComparer<IBlueRow>? comparer = null) {
|
|
var id = row.Id();
|
|
if (table.TryGetValue(id, out var orig)) {
|
|
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] = orig;
|
|
}
|
|
added[id] = row;
|
|
}
|
|
|
|
public void Remove(int id) {
|
|
added.Remove(id);
|
|
if (table.TryGetValue(id, out var orig)) {
|
|
removed[id] = orig;
|
|
}
|
|
}
|
|
|
|
public void Clear() {
|
|
added.Clear();
|
|
removed.Clear();
|
|
removed.AddRange(table);
|
|
}
|
|
|
|
public void Reset() {
|
|
added.Clear();
|
|
removed.Clear();
|
|
}
|
|
|
|
public DataPacket ToDataPacket(bool reset = true) =>
|
|
ToDataPacket(out _, reset);
|
|
|
|
public DataPacket ToDataPacket(out BlueTableImmutable newRows, bool reset = true) {
|
|
table.RemoveRange(removed.Keys);
|
|
table.AddRange(added);
|
|
newRows = new BlueTableImmutable(schema, table.ToImmutable());
|
|
var addedRows = new BlueTableImmutable(schema, added.ToImmutable());
|
|
var removedRows = new BlueTableImmutable(schema, removed.ToImmutable());
|
|
if (reset) {
|
|
added.Clear();
|
|
removed.Clear();
|
|
}
|
|
return new(newRows, addedRows, removedRows);
|
|
}
|
|
|
|
public BlueTableImmutable ToImmutable() =>
|
|
new(schema, table.ToImmutable());
|
|
}
|
|
}
|
|
|
|
// public class BlueTableImmutable<T> : IBlueRowCollection<T> where T : IBlueRow {
|
|
// readonly ImmutableDictionary<int, T> rows;
|
|
|
|
// public BlueTableImmutable(IBlueTableSchema schema)
|
|
// : this(schema, ImmutableDictionary<int, T>.Empty) { }
|
|
|
|
// public BlueTableImmutable(IBlueTableSchema schema, ImmutableDictionary<int, T> rows) {
|
|
// this.schema = schema;
|
|
// this.rows = rows;
|
|
// }
|
|
|
|
// public int Count => rows.Count;
|
|
// public bool IsSynchronized => false;
|
|
// public IBlueTableSchema schema { get; }
|
|
// public object SyncRoot => this;
|
|
|
|
// public T this[int id] => 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(r, index++);
|
|
// }
|
|
// }
|
|
|
|
// public IEnumerator<T> GetEnumerator() =>
|
|
// rows.Values.GetEnumerator();
|
|
|
|
// IEnumerator IEnumerable.GetEnumerator() =>
|
|
// GetEnumerator();
|
|
|
|
// public DataPacketBuilder ToDataPacketBuilder() =>
|
|
// new(this);
|
|
|
|
// public class DataPacketBuilder {
|
|
// readonly IBlueTableSchema schema;
|
|
// 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(BlueTableImmutable<T> table) {
|
|
// schema = table.schema;
|
|
// this.table = table.rows.ToBuilder();
|
|
// }
|
|
|
|
// public void AddOrUpdate(T row, IEqualityComparer<IBlueRow>? comparer = null) {
|
|
// var id = row.Id();
|
|
// if (table.TryGetValue(id, out var orig)) {
|
|
// 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] = orig;
|
|
// }
|
|
// added[id] = row;
|
|
// }
|
|
|
|
// public void Remove(int id) {
|
|
// added.Remove(id);
|
|
// if (table.TryGetValue(id, out var orig)) {
|
|
// removed[id] = orig;
|
|
// }
|
|
// }
|
|
|
|
// public DataPacket ToDataPacket(bool reset = true) {
|
|
// table.RemoveRange(removed.Keys);
|
|
// table.AddRange(added);
|
|
// var newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
|
|
// var addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
|
|
// var removedRows = new BlueTableImmutable<T>(schema, removed.ToImmutable());
|
|
// if (reset) {
|
|
// added.Clear();
|
|
// removed.Clear();
|
|
// }
|
|
// return new(newRows, addedRows, removedRows);
|
|
// }
|
|
|
|
// public IDataPacket<T> ToDataPacketT(bool reset = true) {
|
|
// table.RemoveRange(removed.Keys);
|
|
// table.AddRange(added);
|
|
// var newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
|
|
// var addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
|
|
// var removedRows = new BlueTableImmutable<T>(schema, removed.ToImmutable());
|
|
// if (reset) {
|
|
// added.Clear();
|
|
// removed.Clear();
|
|
// }
|
|
// return new DataPacket<T>(newRows, addedRows, removedRows);
|
|
// }
|
|
|
|
// public IDataPacket<T> ToDataPacketT(
|
|
// out BlueTableImmutable<T> newRows,
|
|
// out BlueTableImmutable<T> addedRows,
|
|
// out BlueTableImmutable<T> removedRows,
|
|
// bool reset = true
|
|
// ) {
|
|
// table.RemoveRange(removed.Keys);
|
|
// table.AddRange(added);
|
|
// newRows = new BlueTableImmutable<T>(schema, table.ToImmutable());
|
|
// addedRows = new BlueTableImmutable<T>(schema, added.ToImmutable());
|
|
// removedRows = new BlueTableImmutable<T>(schema, removed.ToImmutable());
|
|
// if (reset) {
|
|
// added.Clear();
|
|
// removed.Clear();
|
|
// }
|
|
// return new DataPacket<T>(newRows, addedRows, removedRows);
|
|
// }
|
|
|
|
// public BlueTableImmutable<T> ToImmutable() =>
|
|
// 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 BlueTableImmutableAdapter<T> ToImmutable(out IDataPacket dp, 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();
|
|
}
|
|
dp = new DataPacket(newRows, addedRows, removedRows);
|
|
return newRows;
|
|
}
|
|
}
|
|
}
|