feat: Add [incremental] MongoCollectionStream

This commit is contained in:
John Colvin committed 2024-03-18 15:09:27 -04:00
1 parent eea4af1f9b
commit b7c8e72a07
7 files changed
+504 -1

No files matched your search

@@ -0,0 +1,80 @@
namespace Bts.DataStreamCore;
using BCG;
using System;
using System.Collections;
using System.Collections.Immutable;
// 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) {
var id = row.Id();
added[id] = row;
if (table.TryGetValue(id, out var orig)) {
removed.Remove(id);
}
}
public void Remove(int id) {
added.Remove(id);
if (table.TryGetValue(id, out var orig)) {
removed[id] = orig;
}
}
public DataPacket ToDataPacket() {
table.RemoveRange(removed.Keys);
table.AddRange(added);
var newRows = new BlueTableImmutable(schema, table.ToImmutable());
var addedRows = new BlueTableImmutable(schema, added.ToImmutable());
var removedRows = new BlueTableImmutable(schema, removed.ToImmutable());
return new(newRows, addedRows, removedRows);
}
}
}
@@ -0,0 +1,84 @@
#nullable enable
namespace Bts.DataStream;
using BCG;
using bts.collections;
using System;
using System.Collections.Generic;
using System.Linq;
public class BlueTableSchemaAdaptor<T> {
readonly string name;
readonly List<ColumnAdaptor> columns = [];
BlueTableSchema? schema;
public BlueTableSchemaAdaptor(string name) =>
this.name = name;
public IBlueTableSchema Schema =>
schema ??= new BlueTableSchema([.. columns.WrapSelectorRO(c => c.Info)], name);
public bool Contains(string name) =>
columns.Any(c => c.Info.colName == name);
public void AddColumn(string name, Func<T, int> getValue) =>
AddColumn(name, r => getValue(r), typeof(int));
public void AddColumn(string name, Func<T, int?> getValue) =>
AddColumn(name, r => getValue(r) ?? (object)DBNull.Value, typeof(int));
public void AddColumn(string name, Func<T, long> getValue) =>
AddColumn(name, r => getValue(r), typeof(long));
public void AddColumn(string name, Func<T, long?> getValue) =>
AddColumn(name, r => getValue(r) ?? (object)DBNull.Value, typeof(long));
public void AddColumn(string name, Func<T, double> getValue) =>
AddColumn(name, r => getValue(r), typeof(double));
public void AddColumn(string name, Func<T, double?> getValue) =>
AddColumn(name, r => getValue(r) ?? (object)DBNull.Value, typeof(double));
public void AddColumn(string name, Func<T, DateTime> getValue) =>
AddColumn(name, r => getValue(r), typeof(DateTime));
public void AddColumn(string name, Func<T, DateTime?> getValue) =>
AddColumn(name, r => getValue(r) ?? (object)DBNull.Value, typeof(DateTime));
public void AddColumn(string name, Func<T, string?> getValue) =>
AddColumn(name, r => getValue(r) ?? (object)DBNull.Value, typeof(string));
public void AddColumn(string name, Func<T, object?> getValue, Type dataType) {
if (!ColumnInfo.isAllowedType(dataType)) {
throw new ArgumentException(
$"'{dataType}' is not a supported column type", nameof(dataType));
}
// Invalidate cached schema (set it to null)
schema = null;
columns.Add(new ColumnAdaptor(new(name, columns.Count, dataType), r => getValue(r)));
}
public IBlueRow GetBlueRow(T item) {
var mbr = new MutableBlueRow(Schema);
foreach (var col in columns) {
mbr.setValue(col.Info.colNo, col.GetValue(item));
}
return mbr;
}
class ColumnAdaptor {
readonly Func<T, object?> getValue;
public ColumnAdaptor(ColumnInfo info, Func<T, object?> getValue) {
Info = info;
this.getValue = getValue;
}
public ColumnInfo Info { get; }
public object? GetValue(T row) =>
getValue(row);
}
}
@@ -0,0 +1,11 @@
namespace Bts.DataStreamCore;
using BCG;
using System;
public static class BlueRowExtensions {
public static int Id(this IBlueRow row) =>
(int)(row["id"] ?? throw new ArgumentException(
"Row doesn't have an 'id' column", nameof(row)));
}