refactor: Remove LegacyTableUpdate (repl w/ TableUpdate)

This commit is contained in:
John Colvin committed 2022-04-19 11:42:39 -04:00
1 parent 8806a62af7
commit 2d112a86af
2 files changed
+152 -216

No files matched your search

@@ -11,7 +11,7 @@ namespace mbts.spark {
[Export]
class MarketDataAuxiliaryModel : IDisposable {
readonly MarketDataAuxiliaryService _service;
readonly LegacyTableUpdateDemuxer<VolumeAtPriceLevelImmutable, int, uint> _security_orders_demuxer;
readonly TableUpdateDemuxer<VolumeAtPriceLevelImmutable, int, uint> _security_orders_demuxer;
IDisposable _dispose;
[ImportingConstructor]
@@ -20,13 +20,13 @@ namespace mbts.spark {
var scheduler = ThreadPoolScheduler.Instance;
var interval = TimeSpan.FromMilliseconds(100);
volume_at_price = service.subscribei_volume_at_price().Naggle(interval, scheduler).Table().Cache();
_security_orders_demuxer = new LegacyTableUpdateDemuxer<VolumeAtPriceLevelImmutable, int, uint>(o => o.RecordId, v => v.SecurityId);
_dispose = volume_at_price.AddRemoveOnly().Subscribe(_security_orders_demuxer);
_security_orders_demuxer = new(o => o.RecordId, v => v.SecurityId);
_dispose = volume_at_price.Subscribe(_security_orders_demuxer);
}
public IObservable<TableUpdate<VolumeAtPriceLevelImmutable, int>> volume_at_price { get; }
public IObservable<ITableUpdateTransform<VolumeAtPriceLevelImmutable, int>> security_volume_at_price(uint security) =>
public IObservable<TableUpdate<VolumeAtPriceLevelImmutable, int>> security_volume_at_price(uint security) =>
_security_orders_demuxer.subscribe(security);
public void Dispose() =>
+148 -212
View File
@@ -1,212 +1,148 @@
using bts.api.core;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading;
namespace mbts.utils {
interface IObservableDemuxer<TKey, in TInput, out TOutput> : IObserver<TInput> {
IObservable<TOutput> subscribe(TKey key);
}
abstract class ObservableDemuxerBase<TKey, TInput, TOutput> : IObservableDemuxer<TKey, TInput, TOutput>, IDisposable {
readonly ConcurrentDictionary<TKey, Data> _cache = new ConcurrentDictionary<TKey, Data>();
readonly ThreadSafeDisposable _dispose;
protected ObservableDemuxerBase() => _dispose = new ThreadSafeDisposable(() => { });
protected ObservableDemuxerBase(IObservable<TInput> source) {
var sub = source.Subscribe(this);
_dispose = new ThreadSafeDisposable(dispose_core);
//
void dispose_core() {
sub.Dispose();
Dispose(true);
}
}
public IObservable<TOutput> subscribe(TKey key) =>
_cache.GetOrAdd(key, k => new Data(this));
public void OnNext(TInput input) {
if (_dispose.has_dispose_started) {
return;
}
var output = demux(input);
foreach (var kv in output) {
_cache.AddOrUpdate(kv.Key,
k => {
var d = new Data(this);
d.push(kv.Value);
return d;
}, (_, d) => {
d.push(kv.Value);
return d;
});
}
}
public virtual void OnError(Exception error) { }
public virtual void OnCompleted() { }
public void Dispose() => _dispose.Dispose();
protected virtual TOutput get_initial_value(TOutput last_value) => last_value;
protected abstract IReadOnlyDictionary<TKey, TOutput> demux(TInput input);
protected virtual void Dispose(bool disposing) { }
class Data : IObservable<TOutput> {
readonly ObservableDemuxerBase<TKey, TInput, TOutput> _parent;
readonly Subject<TOutput> _subject = new Subject<TOutput>();
bool _value_set;
TOutput _last_value;
public Data(ObservableDemuxerBase<TKey, TInput, TOutput> parent) =>
_parent = parent;
public void push(TOutput val) {
_value_set = true;
_subject.OnNext(_last_value = val);
}
public IDisposable Subscribe(IObserver<TOutput> observer) => (
_value_set ? _subject.StartWith(_parent.get_initial_value(_last_value)) : _subject.AsObservable()
).Subscribe(observer);
}
}
class ObservableDemuxer<TKey, TValue> : ObservableDemuxerBase<TKey, IReadOnlyDictionary<TKey, TValue>, TValue> {
protected override IReadOnlyDictionary<TKey, TValue> demux(IReadOnlyDictionary<TKey, TValue> input) => input;
}
class LegacyTableUpdateDemuxer<T, TKey, TGroupKey> : IObservableDemuxer<TGroupKey, LegacyTableUpdate<T, TKey>, ITableUpdateTransform<T, TKey>>
where TKey : IEquatable<TKey>
where TGroupKey : IEquatable<TGroupKey> {
readonly ConcurrentDictionary<TGroupKey, Data> _map = new ConcurrentDictionary<TGroupKey, Data>();
readonly Func<T, TGroupKey> _group_key;
readonly Func<T, TKey> _key;
internal LegacyTableUpdateDemuxer(Func<T, TKey> key, Func<T, TGroupKey> group_key) {
_key = key;
_group_key = group_key;
}
public IObservable<ITableUpdateTransform<T, TKey>> subscribe(TGroupKey group_key) =>
Observable.Create<ITableUpdateTransform<T, TKey>>(o => {
var data = _map.GetOrAdd(group_key, gk => new Data());
lock (data) {
o.OnNext(data.current.as_snapshot());
return data.subject.Subscribe(o);
}
});
public void OnCompleted() { }
public void OnError(Exception error) { }
public void OnNext(LegacyTableUpdate<T, TKey> table) {
var changes = new Dictionary<TGroupKey, ChangeSet>();
foreach (T value in table.added)
changes.AddOrUpdate(_group_key(value), gk => ChangeSet.from_added(value), (gk, cs) => cs.added.Add(value));
foreach (T value in table.removed)
changes.AddOrUpdate(_group_key(value), gk => ChangeSet.from_removed(value), (gk, cs) => cs.removed.Add(value));
//
foreach (var kv in changes)
_map.AddOrUpdate(kv.Key, gk => _update(new Data(), kv.Value), (gk, s) => _update(s, kv.Value));
}
Data _update(Data data, ChangeSet changes) {
lock (data) {
data.subject.OnNext(data.current = data.current.update(changes.removed, changes.added, _key));
return data;
}
}
class Data {
public readonly Subject<TableUpdateTransform<T, TKey>> subject = new Subject<TableUpdateTransform<T, TKey>>();
public TableUpdateTransform<T, TKey> current = new TableUpdateTransform<T, TKey>();
}
// [John]I prefer this class over anonymous tuples bc it is easy to mix up Item1/Item2
// and implicit func-types lose the aliases assigned to anonymous types
class ChangeSet {
public static ChangeSet from_added(T value) {
var cs = new ChangeSet();
cs.added.Add(value);
return cs;
}
public static ChangeSet from_removed(T value) {
var cs = new ChangeSet();
cs.removed.Add(value);
return cs;
}
public List<T> added { get; } = new List<T>();
public List<T> removed { get; } = new List<T>();
}
}
class TableUpdateDemuxer<T, TKey, TGroupKey> : IObservableDemuxer<TGroupKey, TableUpdate<T, TKey>, TableUpdate<T, TKey>>
where TKey : IEquatable<TKey>
where TGroupKey : IEquatable<TGroupKey> {
readonly Func<T, TKey> _key;
readonly Func<T, TGroupKey> _group_key;
readonly ConcurrentDictionary<TGroupKey, Data> _group_data = new ConcurrentDictionary<TGroupKey, Data>();
public TableUpdateDemuxer(Func<T, TKey> key, Func<T, TGroupKey> group_key) {
_key = key;
_group_key = group_key;
}
public bool try_get_values(TGroupKey group, out IReadOnlyDictionary<TKey, T> values) {
if (_group_data.TryGetValue(group, out Data d)) {
values = d.current.state;
return true;
}
values = DictionaryHelper.empty<TKey, T>();
return false;
}
public void OnCompleted() { }
public void OnError(Exception error) { }
public void OnNext(TableUpdate<T, TKey> update) {
var group_updates = new Dictionary<TGroupKey, UpdateTuple>();
foreach (var v in update.added)
group_updates.GetOrAdd(_group_key(v), gk => new UpdateTuple()).added.Add(v);
foreach (var v in update.removed)
group_updates.GetOrAdd(_group_key(v), gk => new UpdateTuple()).removed.Add(v);
foreach (var c in update.changes)
group_updates.GetOrAdd(_group_key(c.New), gk => new UpdateTuple()).changes.Add(c);
foreach (var kv in group_updates) {
var data = _group_data.GetOrAdd(kv.Key, gk => new Data(_key));
data.update(TableUpdate<T, TKey>.update(data.current, kv.Value.added, kv.Value.changes, kv.Value.removed, _key));
}
}
public IObservable<TableUpdate<T, TKey>> subscribe(TGroupKey group_key) =>
_group_data.GetOrAdd(group_key, gk => new Data(_key));
class Data : IObservable<TableUpdate<T, TKey>> {
readonly Subject<TableUpdate<T, TKey>> _subject = new Subject<TableUpdate<T, TKey>>();
public Data(Func<T, TKey> key) =>
current = new TableUpdate<T, TKey>(System.Collections.Immutable.ImmutableDictionary<TKey, T>.Empty, Array.Empty<T>(), Array.Empty<Change<T>>(), Array.Empty<T>(), key);
public TableUpdate<T, TKey> current { get; private set; }
public void update(TableUpdate<T, TKey> next) {
lock (this)
_subject.OnNext(current = next);
}
public IDisposable Subscribe(IObserver<TableUpdate<T, TKey>> observer) {
lock (this) {
observer.OnNext(current.as_snapshot());
return _subject.Subscribe(observer);
}
}
}
class UpdateTuple {
public readonly List<T> added = new List<T>(), removed = new List<T>();
public readonly List<Change<T>> changes = new List<Change<T>>();
}
}
}
using bts.api.core;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading;
namespace mbts.utils {
interface IObservableDemuxer<TKey, in TInput, out TOutput> : IObserver<TInput> {
IObservable<TOutput> subscribe(TKey key);
}
abstract class ObservableDemuxerBase<TKey, TInput, TOutput> : IObservableDemuxer<TKey, TInput, TOutput>, IDisposable {
readonly ConcurrentDictionary<TKey, Data> _cache = new ConcurrentDictionary<TKey, Data>();
readonly ThreadSafeDisposable _dispose;
protected ObservableDemuxerBase() => _dispose = new ThreadSafeDisposable(() => { });
protected ObservableDemuxerBase(IObservable<TInput> source) {
var sub = source.Subscribe(this);
_dispose = new ThreadSafeDisposable(dispose_core);
//
void dispose_core() {
sub.Dispose();
Dispose(true);
}
}
public IObservable<TOutput> subscribe(TKey key) =>
_cache.GetOrAdd(key, k => new Data(this));
public void OnNext(TInput input) {
if (_dispose.has_dispose_started) {
return;
}
var output = demux(input);
foreach (var kv in output) {
_cache.AddOrUpdate(kv.Key,
k => {
var d = new Data(this);
d.push(kv.Value);
return d;
}, (_, d) => {
d.push(kv.Value);
return d;
});
}
}
public virtual void OnError(Exception error) { }
public virtual void OnCompleted() { }
public void Dispose() => _dispose.Dispose();
protected virtual TOutput get_initial_value(TOutput last_value) => last_value;
protected abstract IReadOnlyDictionary<TKey, TOutput> demux(TInput input);
protected virtual void Dispose(bool disposing) { }
class Data : IObservable<TOutput> {
readonly ObservableDemuxerBase<TKey, TInput, TOutput> _parent;
readonly Subject<TOutput> _subject = new Subject<TOutput>();
bool _value_set;
TOutput _last_value;
public Data(ObservableDemuxerBase<TKey, TInput, TOutput> parent) =>
_parent = parent;
public void push(TOutput val) {
_value_set = true;
_subject.OnNext(_last_value = val);
}
public IDisposable Subscribe(IObserver<TOutput> observer) => (
_value_set ? _subject.StartWith(_parent.get_initial_value(_last_value)) : _subject.AsObservable()
).Subscribe(observer);
}
}
class ObservableDemuxer<TKey, TValue> : ObservableDemuxerBase<TKey, IReadOnlyDictionary<TKey, TValue>, TValue> {
protected override IReadOnlyDictionary<TKey, TValue> demux(IReadOnlyDictionary<TKey, TValue> input) => input;
}
class TableUpdateDemuxer<T, TKey, TGroupKey> : IObservableDemuxer<TGroupKey, TableUpdate<T, TKey>, TableUpdate<T, TKey>>
where TKey : IEquatable<TKey>
where TGroupKey : IEquatable<TGroupKey> {
readonly Func<T, TKey> _key;
readonly Func<T, TGroupKey> _group_key;
readonly ConcurrentDictionary<TGroupKey, Data> _group_data = new ConcurrentDictionary<TGroupKey, Data>();
public TableUpdateDemuxer(Func<T, TKey> key, Func<T, TGroupKey> group_key) {
_key = key;
_group_key = group_key;
}
public bool try_get_values(TGroupKey group, out IReadOnlyDictionary<TKey, T> values) {
if (_group_data.TryGetValue(group, out Data d)) {
values = d.current.state;
return true;
}
values = DictionaryHelper.empty<TKey, T>();
return false;
}
public void OnCompleted() { }
public void OnError(Exception error) { }
public void OnNext(TableUpdate<T, TKey> update) {
var group_updates = new Dictionary<TGroupKey, UpdateTuple>();
foreach (var v in update.added)
group_updates.GetOrAdd(_group_key(v), gk => new UpdateTuple()).added.Add(v);
foreach (var v in update.removed)
group_updates.GetOrAdd(_group_key(v), gk => new UpdateTuple()).removed.Add(v);
foreach (var c in update.changes)
group_updates.GetOrAdd(_group_key(c.New), gk => new UpdateTuple()).changes.Add(c);
foreach (var kv in group_updates) {
var data = _group_data.GetOrAdd(kv.Key, gk => new Data(_key));
data.update(TableUpdate<T, TKey>.update(data.current, kv.Value.added, kv.Value.changes, kv.Value.removed, _key));
}
}
public IObservable<TableUpdate<T, TKey>> subscribe(TGroupKey group_key) =>
_group_data.GetOrAdd(group_key, gk => new Data(_key));
class Data : IObservable<TableUpdate<T, TKey>> {
readonly Subject<TableUpdate<T, TKey>> _subject = new Subject<TableUpdate<T, TKey>>();
public Data(Func<T, TKey> key) =>
current = new TableUpdate<T, TKey>(System.Collections.Immutable.ImmutableDictionary<TKey, T>.Empty, Array.Empty<T>(), Array.Empty<Change<T>>(), Array.Empty<T>(), key);
public TableUpdate<T, TKey> current { get; private set; }
public void update(TableUpdate<T, TKey> next) {
lock (this)
_subject.OnNext(current = next);
}
public IDisposable Subscribe(IObserver<TableUpdate<T, TKey>> observer) {
lock (this) {
observer.OnNext(current.as_snapshot());
return _subject.Subscribe(observer);
}
}
}
class UpdateTuple {
public readonly List<T> added = new List<T>(), removed = new List<T>();
public readonly List<Change<T>> changes = new List<Change<T>>();
}
}
}