225 lines
8.9 KiB
C#
225 lines
8.9 KiB
C#
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<TLeft, TLeftKey, TRight, TRightKey, TResult> : IConnectableObservable<TableUpdate<TResult, TLeftKey>>
|
|
where TLeftKey : IEquatable<TLeftKey>
|
|
where TRightKey : IEquatable<TRightKey> {
|
|
// left
|
|
readonly Func<TLeft, TLeftKey> _left_key;
|
|
readonly IObservable<TableUpdate<TLeft, TLeftKey>> _left_source;
|
|
// right
|
|
readonly Func<TRight, TRightKey> _right_key;
|
|
readonly IObservable<TableUpdate<TRight, TRightKey>> _right_source;
|
|
// join
|
|
readonly Func<TLeft, TLeftKey, TRight, TRightKey, TResult> _result_selector;
|
|
readonly Func<TLeft, TLeftKey, TRightKey, TResult> _absent_right_result_selector;
|
|
readonly Func<TLeft, TRightKey> _join_key;
|
|
readonly Dictionary<TRightKey, HashSet<TLeftKey>> _join_index = new Dictionary<TRightKey, HashSet<TLeftKey>>();
|
|
readonly Subject<TableUpdate<TResult, TLeftKey>> _subject = new Subject<TableUpdate<TResult, TLeftKey>>();
|
|
// state
|
|
TableUpdate<TResult, TLeftKey> _table;
|
|
Dictionary<TRightKey, TRight> _right_state = new Dictionary<TRightKey, TRight>();
|
|
IReadOnlyDictionary<TLeftKey, TLeft> _left_state = new Dictionary<TLeftKey, TLeft>();
|
|
|
|
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<TableUpdate<TLeft, TLeftKey>> left_source, IObservable<TableUpdate<TRight, TRightKey>> right_source, Func<TLeft, TLeftKey> left_key, Func<TRight, TRightKey> right_key,
|
|
Func<TLeft, TRightKey> join_key, Func<TLeft, TLeftKey, TRight, TRightKey, TResult> result_selector, Func<TLeft, TLeftKey, TRightKey, TResult> absent_right_result_selector, Func<TResult, TLeftKey> 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<TResult, TLeftKey>.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<TableUpdate<TResult, TLeftKey>> observer) {
|
|
lock (this) {
|
|
if (_is_updating) {
|
|
var sub = new LazySubscription<TableUpdate<TResult, TLeftKey>>(observer);
|
|
_buffer.Enqueue(new _Op(sub));
|
|
return sub;
|
|
}
|
|
return _subscribe_core(observer);
|
|
}
|
|
}
|
|
|
|
IDisposable _subscribe_core(IObserver<TableUpdate<TResult, TLeftKey>> 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<TLeft, TLeftKey> 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<TLeft, TLeftKey> 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<TLeftKey> joined))
|
|
_join_index.Add(rk, joined = new HashSet<TLeftKey>());
|
|
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<TRight, TRightKey> 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<TRight, TRightKey> 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<TLeftKey> 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<TLeftKey> 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<TLeftKey>());
|
|
}
|
|
_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<IObserver<TableUpdate<TResult, TLeftKey>>, IDisposable>(_subscribe_core)));
|
|
break;
|
|
}
|
|
_on_op_completed();
|
|
}
|
|
|
|
class _Op {
|
|
public readonly TableUpdate<TLeft, TLeftKey> left;
|
|
public readonly TableUpdate<TRight, TRightKey> right;
|
|
public readonly LazySubscription<TableUpdate<TResult, TLeftKey>> subscription;
|
|
|
|
public _Op(TableUpdate<TLeft, TLeftKey> left) => this.left = left;
|
|
public _Op(TableUpdate<TRight, TRightKey> right) => this.right = right;
|
|
public _Op(LazySubscription<TableUpdate<TResult, TLeftKey>> 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.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |