using System; using System.Collections.Generic; using System.Linq; using System.Reactive.Disposables; using System.Reactive.Linq; using System.Reactive.Subjects; using System.Threading; namespace bts.api.core { class TableUpdateLeftJoin : IConnectableObservable> where TLeftKey : IEquatable where TRightKey : IEquatable { // left readonly Func _left_key; readonly IObservable> _left_source; // right readonly Func _right_key; readonly IObservable> _right_source; // join readonly Func _result_selector; readonly Func _absent_right_result_selector; readonly Func _join_key; readonly Dictionary> _join_index = new Dictionary>(); readonly Subject> _subject = new Subject>(); // state TableUpdate _table; Dictionary _right_state = new Dictionary(); IReadOnlyDictionary _left_state = new Dictionary(); IDisposable _subscriptions = mbts.Disposable.empty; int _connection; // unconnected = 0, connected == 1 Queue<_Op> _buffer = new Queue<_Op>(); bool _is_updating, _left_completed, _right_completed; public TableUpdateLeftJoin(IObservable> left_source, IObservable> right_source, Func left_key, Func right_key, Func join_key, Func result_selector, Func absent_right_result_selector, Func result_key) { _left_source = left_source; _left_key = left_key; _right_source = right_source; _right_key = right_key; _join_key = join_key; _result_selector = result_selector; _absent_right_result_selector = absent_right_result_selector; _table = TableUpdate.mk_empty(result_key); } public IDisposable Connect() { if (Interlocked.CompareExchange(ref _connection, 1, 0) == 0) { _subscriptions = new CompositeDisposable( _left_source.Subscribe(_on_left_update, _on_left_completed), _right_source.Subscribe(_on_right_update, _on_right_completed)); } return Disposable.Create(() => { if (Interlocked.CompareExchange(ref _connection, 0, 1) == 1) _subscriptions.Dispose(); }); } public IDisposable Subscribe(IObserver> observer) { lock (this) { if (_is_updating) { var sub = new LazySubscription>(observer); _buffer.Enqueue(new _Op(sub)); return sub; } return _subscribe_core(observer); } } IDisposable _subscribe_core(IObserver> observer) { observer.OnNext(_table.as_snapshot()); return _subject.Subscribe(observer); } #region Left void _on_left_completed() { lock (this) { _left_completed = true; if (_right_completed) _subject.OnCompleted(); } } void _on_left_update(TableUpdate left_update) { lock (this) { if (_is_updating) { _buffer.Enqueue(new _Op(left_update)); return; } _is_updating = true; } _process_left(left_update); } void _process_left(TableUpdate left_update) { var builder = _table.to_builder(); foreach (var v in left_update.removed) { var lk = _left_key(v); builder.remove(lk); var rk = _join_key(v); var joined = _join_index[rk]; if (joined.Remove(lk) && joined.Count == 0) _join_index.Remove(rk); } foreach (var v in left_update.added.Concat(left_update.changed)) { var lk = _left_key(v); var rk = _join_key(v); var result = _right_state.TryGetValue(rk, out TRight r) ? _result_selector(v, lk, r, rk) : _absent_right_result_selector(v, lk, rk); builder.add_or_update(lk, result); if (!_join_index.TryGetValue(rk, out HashSet joined)) _join_index.Add(rk, joined = new HashSet()); joined.Add(lk); } _left_state = left_update.state; _table = builder.to_table(); _subject.OnNext(_table); _on_op_completed(); } #endregion #region Right void _on_right_completed() { lock (this) { _right_completed = true; if (_left_completed) _subject.OnCompleted(); } } void _on_right_update(TableUpdate right_update) { lock (this) { if (_is_updating) { _buffer.Enqueue(new _Op(right_update)); return; } _is_updating = true; } _process_right(right_update); } void _process_right(TableUpdate right_update) { var builder = _table.to_builder(); foreach (var v in right_update.removed) { var rkey = _right_key(v); if (_join_index.TryGetValue(rkey, out HashSet lkeys)) { foreach (var k in lkeys) { var lval = _left_state[k]; var result = _absent_right_result_selector(lval, k, rkey); builder.add_or_update(k, result); } if (lkeys.Count == 0) _join_index.Remove(rkey); } _right_state.Remove(rkey); } foreach (var v in right_update.added.Concat(right_update.changed)) { var rkey = _right_key(v); if (_join_index.TryGetValue(rkey, out HashSet lkeys)) { foreach (var k in lkeys) { var lval = _left_state[k]; var result = _result_selector(lval, k, v, rkey); builder.add_or_update(k, result); } } else { _join_index.Add(rkey, new HashSet()); } _right_state[rkey] = v; } _table = builder.to_table(); if (_table.has_changes()) { _subject.OnNext(_table); } _on_op_completed(); } #endregion void _on_op_completed() { Log.assert(_is_updating); _Op buffered_op; lock (this) { if (_buffer.Count == 0) { _is_updating = false; return; } buffered_op = _buffer.Dequeue(); } switch (buffered_op.update_case) { case _Op.UpdateOneofCase.Left: _process_left(buffered_op.left); break; case _Op.UpdateOneofCase.Right: _process_right(buffered_op.right); break; case _Op.UpdateOneofCase.Subscription: buffered_op.subscription.connect(Observable.Create(new Func>, IDisposable>(_subscribe_core))); break; } _on_op_completed(); } class _Op { public readonly TableUpdate left; public readonly TableUpdate right; public readonly LazySubscription> subscription; public _Op(TableUpdate left) => this.left = left; public _Op(TableUpdate right) => this.right = right; public _Op(LazySubscription> subscription) => this.subscription = subscription; public enum UpdateOneofCase { Left, Right, Subscription } public UpdateOneofCase update_case { get { if (left != null) return UpdateOneofCase.Left; if (right != null) return UpdateOneofCase.Right; if (subscription != null) return UpdateOneofCase.Subscription; throw new InvalidOperationException("Unexpected state."); } } } } }