diff --git a/src/client/spark/MarketDataAuxiliary/MarketDataAuxiliaryModel.cs b/src/client/spark/MarketDataAuxiliary/MarketDataAuxiliaryModel.cs index 8ded19c1e..d52a4886d 100644 --- a/src/client/spark/MarketDataAuxiliary/MarketDataAuxiliaryModel.cs +++ b/src/client/spark/MarketDataAuxiliary/MarketDataAuxiliaryModel.cs @@ -11,7 +11,7 @@ namespace mbts.spark { [Export] class MarketDataAuxiliaryModel : IDisposable { readonly MarketDataAuxiliaryService _service; - readonly LegacyTableUpdateDemuxer _security_orders_demuxer; + readonly TableUpdateDemuxer _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(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> volume_at_price { get; } - public IObservable> security_volume_at_price(uint security) => + public IObservable> security_volume_at_price(uint security) => _security_orders_demuxer.subscribe(security); public void Dispose() => diff --git a/src/client/spark/core/Utility/ObservableDemuxer.cs b/src/client/spark/core/Utility/ObservableDemuxer.cs index 463a199ca..2f91cb0f5 100644 --- a/src/client/spark/core/Utility/ObservableDemuxer.cs +++ b/src/client/spark/core/Utility/ObservableDemuxer.cs @@ -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 : IObserver { - IObservable subscribe(TKey key); - } - - abstract class ObservableDemuxerBase : IObservableDemuxer, IDisposable { - readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); - readonly ThreadSafeDisposable _dispose; - - protected ObservableDemuxerBase() => _dispose = new ThreadSafeDisposable(() => { }); - protected ObservableDemuxerBase(IObservable source) { - var sub = source.Subscribe(this); - _dispose = new ThreadSafeDisposable(dispose_core); - // - void dispose_core() { - sub.Dispose(); - Dispose(true); - } - } - - public IObservable 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 demux(TInput input); - protected virtual void Dispose(bool disposing) { } - - class Data : IObservable { - readonly ObservableDemuxerBase _parent; - readonly Subject _subject = new Subject(); - bool _value_set; - TOutput _last_value; - - public Data(ObservableDemuxerBase parent) => - _parent = parent; - - public void push(TOutput val) { - _value_set = true; - _subject.OnNext(_last_value = val); - } - - public IDisposable Subscribe(IObserver observer) => ( - _value_set ? _subject.StartWith(_parent.get_initial_value(_last_value)) : _subject.AsObservable() - ).Subscribe(observer); - } - } - - class ObservableDemuxer : ObservableDemuxerBase, TValue> { - protected override IReadOnlyDictionary demux(IReadOnlyDictionary input) => input; - } - - class LegacyTableUpdateDemuxer : IObservableDemuxer, ITableUpdateTransform> - where TKey : IEquatable - where TGroupKey : IEquatable { - readonly ConcurrentDictionary _map = new ConcurrentDictionary(); - readonly Func _group_key; - readonly Func _key; - - internal LegacyTableUpdateDemuxer(Func key, Func group_key) { - _key = key; - _group_key = group_key; - } - - public IObservable> subscribe(TGroupKey group_key) => - Observable.Create>(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 table) { - var changes = new Dictionary(); - 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> subject = new Subject>(); - public TableUpdateTransform current = new TableUpdateTransform(); - } - - // [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 added { get; } = new List(); - public List removed { get; } = new List(); - } - } - - class TableUpdateDemuxer : IObservableDemuxer, TableUpdate> - where TKey : IEquatable - where TGroupKey : IEquatable { - readonly Func _key; - readonly Func _group_key; - readonly ConcurrentDictionary _group_data = new ConcurrentDictionary(); - - public TableUpdateDemuxer(Func key, Func group_key) { - _key = key; - _group_key = group_key; - } - - public bool try_get_values(TGroupKey group, out IReadOnlyDictionary values) { - if (_group_data.TryGetValue(group, out Data d)) { - values = d.current.state; - return true; - } - values = DictionaryHelper.empty(); - return false; - } - - public void OnCompleted() { } - public void OnError(Exception error) { } - public void OnNext(TableUpdate update) { - var group_updates = new Dictionary(); - 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.update(data.current, kv.Value.added, kv.Value.changes, kv.Value.removed, _key)); - } - } - public IObservable> subscribe(TGroupKey group_key) => - _group_data.GetOrAdd(group_key, gk => new Data(_key)); - - class Data : IObservable> { - readonly Subject> _subject = new Subject>(); - - public Data(Func key) => - current = new TableUpdate(System.Collections.Immutable.ImmutableDictionary.Empty, Array.Empty(), Array.Empty>(), Array.Empty(), key); - - public TableUpdate current { get; private set; } - - public void update(TableUpdate next) { - lock (this) - _subject.OnNext(current = next); - } - public IDisposable Subscribe(IObserver> observer) { - lock (this) { - observer.OnNext(current.as_snapshot()); - return _subject.Subscribe(observer); - } - } - } - - class UpdateTuple { - public readonly List added = new List(), removed = new List(); - public readonly List> changes = new List>(); - } - } -} +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 : IObserver { + IObservable subscribe(TKey key); + } + + abstract class ObservableDemuxerBase : IObservableDemuxer, IDisposable { + readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); + readonly ThreadSafeDisposable _dispose; + + protected ObservableDemuxerBase() => _dispose = new ThreadSafeDisposable(() => { }); + protected ObservableDemuxerBase(IObservable source) { + var sub = source.Subscribe(this); + _dispose = new ThreadSafeDisposable(dispose_core); + // + void dispose_core() { + sub.Dispose(); + Dispose(true); + } + } + + public IObservable 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 demux(TInput input); + protected virtual void Dispose(bool disposing) { } + + class Data : IObservable { + readonly ObservableDemuxerBase _parent; + readonly Subject _subject = new Subject(); + bool _value_set; + TOutput _last_value; + + public Data(ObservableDemuxerBase parent) => + _parent = parent; + + public void push(TOutput val) { + _value_set = true; + _subject.OnNext(_last_value = val); + } + + public IDisposable Subscribe(IObserver observer) => ( + _value_set ? _subject.StartWith(_parent.get_initial_value(_last_value)) : _subject.AsObservable() + ).Subscribe(observer); + } + } + + class ObservableDemuxer : ObservableDemuxerBase, TValue> { + protected override IReadOnlyDictionary demux(IReadOnlyDictionary input) => input; + } + + class TableUpdateDemuxer : IObservableDemuxer, TableUpdate> + where TKey : IEquatable + where TGroupKey : IEquatable { + readonly Func _key; + readonly Func _group_key; + readonly ConcurrentDictionary _group_data = new ConcurrentDictionary(); + + public TableUpdateDemuxer(Func key, Func group_key) { + _key = key; + _group_key = group_key; + } + + public bool try_get_values(TGroupKey group, out IReadOnlyDictionary values) { + if (_group_data.TryGetValue(group, out Data d)) { + values = d.current.state; + return true; + } + values = DictionaryHelper.empty(); + return false; + } + + public void OnCompleted() { } + public void OnError(Exception error) { } + public void OnNext(TableUpdate update) { + var group_updates = new Dictionary(); + 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.update(data.current, kv.Value.added, kv.Value.changes, kv.Value.removed, _key)); + } + } + public IObservable> subscribe(TGroupKey group_key) => + _group_data.GetOrAdd(group_key, gk => new Data(_key)); + + class Data : IObservable> { + readonly Subject> _subject = new Subject>(); + + public Data(Func key) => + current = new TableUpdate(System.Collections.Immutable.ImmutableDictionary.Empty, Array.Empty(), Array.Empty>(), Array.Empty(), key); + + public TableUpdate current { get; private set; } + + public void update(TableUpdate next) { + lock (this) + _subject.OnNext(current = next); + } + public IDisposable Subscribe(IObserver> observer) { + lock (this) { + observer.OnNext(current.as_snapshot()); + return _subject.Subscribe(observer); + } + } + } + + class UpdateTuple { + public readonly List added = new List(), removed = new List(); + public readonly List> changes = new List>(); + } + } +}