215 lines
9.3 KiB
C#
215 lines
9.3 KiB
C#
using bts;
|
|
using bts.api;
|
|
using bts.api.core;
|
|
using bts.api.orderman;
|
|
using bts.collections;
|
|
using bts.utils;
|
|
using mbts.utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
|
|
namespace mbts.spark {
|
|
class OrderBookRowCollection : ObservableCollection<OrderBookRowVM> {
|
|
static readonly IEqualityComparer<INode<OrderBookRowVM>> _node_equality_comparer = EqualityComparer.Create<INode<OrderBookRowVM>, uint>(n => n.value.order_key);
|
|
readonly Func<OrderSecurityAlgoMeta, OrderBookRowVM> _row_factory;
|
|
/// Critical: Any and all row mutations <see cref="OrderBookRowVM"/> must be executed through <see cref="SortedTree{T, TKey}.TryReplace(TKey, Func{T, T})"/>.
|
|
/// Mutations can change the sort-position of a row, and <see cref="_tree"/> will be broken if its elements aren't properly sorted (it relies heavily on binary-search logic).
|
|
SortedTree<OrderBookRowVM, uint> _tree;
|
|
IComparer<OrderBookRowVM> _comparer;
|
|
Func<OrderSecurityAlgoMeta, bool> _filter;
|
|
bool _is_refresh_pending;
|
|
int _deferment_count = 0;
|
|
/// When processing changed-items, the <see cref="SortedTree{T, TKey}.TryReplace(TKey, Func{T, T})"/> method is used to update the changed row. It is not expected to fail (return false),
|
|
/// but if it does, we don't want to overwhelm the logs with a bunch of useless noise in case it starts happening repeatedly, hence the int fields below.
|
|
/// <see cref="_max_replace_misses_logged"/>, <see cref="_n_replace_misses_logged"/>, <see cref="update(IIncrementalTableUpdateTransform{OrderSecurity, uint})"/>.
|
|
const int _max_replace_misses_logged = 10;
|
|
int _n_replace_misses_logged;
|
|
|
|
public OrderBookRowCollection(Func<OrderSecurityAlgoMeta, OrderBookRowVM> row_factory, IComparer<OrderBookRowVM> comparer) {
|
|
_row_factory = row_factory;
|
|
_tree = _mk_tree(_comparer = comparer);
|
|
}
|
|
|
|
public Func<OrderSecurityAlgoMeta, bool> filter {
|
|
get => _filter;
|
|
set {
|
|
_filter = value;
|
|
_refresh_branches(_tree.RootNodes);
|
|
reset();
|
|
}
|
|
}
|
|
public IComparer<OrderBookRowVM> comparer {
|
|
get => _comparer;
|
|
set {
|
|
_tree = _tree.Resort(_comparer = value);
|
|
reset();
|
|
}
|
|
}
|
|
public int total_row_count { get; private set; }
|
|
IEnumerable<OrderBookRowVM> _visible_rows => _tree.Where(r => r.is_visible);
|
|
|
|
bool _is_refresh_deferred => _deferment_count > 0;
|
|
|
|
public IEnumerable<INode<OrderBookRowVM>> get_inner_nodes(OrderBookRowVM row) =>
|
|
_tree.EnumerateInnerNodes(row);
|
|
|
|
public IEnumerable<OrderBookRowVM> get_descendent_rows(OrderBookRowVM row) =>
|
|
_tree.EnumerateInnerValues(row);
|
|
|
|
public void collapse_all() {
|
|
foreach (var n in _tree.RootNodes) {
|
|
collapse_node(n);
|
|
}
|
|
if (_is_refresh_deferred) {
|
|
_is_refresh_pending = true;
|
|
} else {
|
|
Clear();
|
|
foreach (var n in _tree.RootNodes) {
|
|
if (n.value.is_visible)
|
|
Add(n.value);
|
|
}
|
|
}
|
|
// local methods
|
|
void collapse_node(INode<OrderBookRowVM> node) {
|
|
node.value.collapse_internal();
|
|
foreach (var cn in node.inner) {
|
|
cn.value.is_visible = false;
|
|
collapse_node(cn);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void expand_all() {
|
|
foreach (var n in _tree.RootNodes) {
|
|
expand_node(n);
|
|
}
|
|
if (_is_refresh_deferred) {
|
|
_is_refresh_pending = true;
|
|
} else {
|
|
Clear();
|
|
foreach (var n in _tree.RootNodes) {
|
|
if (n.value.is_visible) {
|
|
Add(n.value);
|
|
foreach (var r in n.inner_values())
|
|
Add(r);
|
|
}
|
|
}
|
|
}
|
|
// local methods
|
|
void expand_node(INode<OrderBookRowVM> node) {
|
|
node.value.expand_internal();
|
|
foreach (var cn in node.inner) {
|
|
cn.value.is_visible = node.value.is_visible;
|
|
expand_node(cn);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Refreshes the collection by calling <see cref="GlobalExtensions.sync_with{T}(IList{T}, IEnumerable{T})"/> with all visible rows.
|
|
/// </summary>
|
|
public void refresh() {
|
|
if (_is_refresh_deferred) {
|
|
_is_refresh_pending = true;
|
|
} else {
|
|
_is_refresh_pending = false;
|
|
this.sync_with(_visible_rows);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resets the collection by calling <see cref="Collection{T}.Clear"/>, then adding all visible rows.
|
|
/// </summary>
|
|
public void reset(bool force = false) {
|
|
if (_is_refresh_deferred && !force) {
|
|
_is_refresh_pending = true;
|
|
} else {
|
|
Clear();
|
|
this.AddRange(_visible_rows);
|
|
_is_refresh_pending = false;
|
|
}
|
|
}
|
|
|
|
public IDisposable defer_refresh(bool force_refresh_after = false) {
|
|
// use a counter instead of a simple bool flag to gracefully handle recursive calls to defer_refresh(),
|
|
// by only calling refresh() after all IDisposables returned by defer_refresh() have been disposed.
|
|
_deferment_count++;
|
|
_is_refresh_pending |= force_refresh_after;
|
|
return Disposable.Create(() => {
|
|
_deferment_count--;
|
|
if (_is_refresh_pending)
|
|
refresh();
|
|
});
|
|
}
|
|
|
|
public void update(TableUpdate<OrderSecurityAlgoMeta, uint> table) {
|
|
IEnumerable<INode<OrderBookRowVM>> impacted_branch_roots;
|
|
int n_removed = table.removed.Count();
|
|
if (n_removed == total_row_count) {
|
|
// full refresh (most likely this is the first update or a server restart)
|
|
_tree = _mk_tree(_comparer, table.added.Select(x => _row_factory(x)));
|
|
impacted_branch_roots = _tree.RootNodes;
|
|
} else {
|
|
foreach (OrderSecurityAlgoMeta ort in table.removed)
|
|
_tree.Remove(ort.order.Key);
|
|
foreach (var change in table.changes) {
|
|
if (!_tree.TryReplace(change.New.order.Key, r => { r.update(change.New); return r; })) {
|
|
// uh oh, replace failed! Let's log this, but not a million times (this seems like the kind of error that, if it happens once, could happen a lot).
|
|
if (_n_replace_misses_logged++ < _max_replace_misses_logged)
|
|
Log.Ui.error($"{nameof(OrderBookRowCollection)} failed to process Changed {nameof(OrderSecurity)} in update (old:{change.Old.order.Key}, new:{change.New.order.Key}). This is the '{_n_replace_misses_logged}' time this has happened in this OrderBook.");
|
|
}
|
|
}
|
|
foreach (OrderSecurityAlgoMeta ort in table.added)
|
|
_tree.Insert(_row_factory(ort));
|
|
impacted_branch_roots =
|
|
table.added.Concat(table.changed).Select(ort => ort.order.Key).Select(_tree.GetRootNode)
|
|
.Concat(
|
|
table.removed
|
|
.Where(ort => ort.order.has_parent())
|
|
.Select(ort => ort.order.ParentKey)
|
|
.Distinct() // since these are ParentKeys, there could be duplicates.
|
|
.Select(_tree.GetRootNode)
|
|
.Where(n => n != null)
|
|
).Distinct(_node_equality_comparer);
|
|
}
|
|
_refresh_branches(impacted_branch_roots);
|
|
refresh();
|
|
int old_row_count = total_row_count;
|
|
total_row_count += table.added.Count() - n_removed;
|
|
if (old_row_count != total_row_count)
|
|
OnPropertyChanged(new PropertyChangedEventArgs(nameof(total_row_count)));
|
|
}
|
|
|
|
static SortedTree<OrderBookRowVM, uint> _mk_tree(IComparer<OrderBookRowVM> comp, IEnumerable<OrderBookRowVM> init = null) =>
|
|
new SortedTree<OrderBookRowVM, uint>(comp, r => r.order_key, r => r.parent_order_key, init);
|
|
|
|
void _refresh_branches(IEnumerable<INode<OrderBookRowVM>> roots) {
|
|
if (filter == null) {
|
|
foreach (var n in roots)
|
|
refresh_tree_properties(0, true, n);
|
|
} else {
|
|
foreach (var n in roots)
|
|
refresh_tree_properties(0, get_branch_passes_filter(n), n);
|
|
}
|
|
// local methods
|
|
void refresh_tree_properties(int depth_level, bool is_visible, INode<OrderBookRowVM> node) {
|
|
node.value.depth_level = depth_level;
|
|
node.value.is_visible = is_visible;
|
|
using (var en = node.inner.GetEnumerator()) {
|
|
if (en.MoveNext()) {
|
|
node.value.is_parent = true;
|
|
depth_level++;
|
|
do refresh_tree_properties(depth_level, node.value.is_visible && node.value.is_expanded, en.Current);
|
|
while (en.MoveNext());
|
|
}
|
|
}
|
|
}
|
|
bool get_branch_passes_filter(INode<OrderBookRowVM> n) =>
|
|
filter(n.value.order_tuple) || n.inner.Any(get_branch_passes_filter);
|
|
}
|
|
}
|
|
} |