SPARK-1636 We can do better

This commit is contained in:
Eric Harding committed 2018-09-05 15:41:25 -04:00
1 parent a1a591e162
commit eaacafca86
1 file changed
+29 -12
+29 -12
View File
@@ -11,6 +11,8 @@ using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Linq;
using SecurityKey = System.UInt32;
using System.Collections.Concurrent;
using System.Threading;
namespace bts {
[Export(typeof(IInitializable))]
@@ -19,9 +21,10 @@ namespace bts {
readonly Lazy<SyntheticSecurityService> _ss;
readonly MDConsumer _consumer = new MDConsumer();
readonly Co2RelayControl _relay;
HashSet<SecurityKey> _deleted_synthetics = new HashSet<SecurityKey>();
readonly Dictionary<SecurityKey, SecurityMD> _deleted_synthetics = new Dictionary<SecurityKey, SecurityMD>();
long _version = 0;
readonly ConcurrentDictionary<SecurityKey, SecurityMD> _exchange_sec_map = new ConcurrentDictionary<SecurityKey, SecurityMD>();
List<IDisposable> _dispose = new List<IDisposable>();
IReadOnlyDictionary<SecurityKey, SecurityMD> _exchange_sec_map = new Dictionary<SecurityKey, SecurityMD>();
[ImportingConstructor]
public MDSource(Lazy<IBus> bus, Lazy<SyntheticSecurityService> ss) {
@@ -36,7 +39,23 @@ namespace bts {
_ss.Value.subscribei_metadata()
.Select(upd => upd.all.ToDictionary(i => i.SecurityKey, i => i.Strategy))
.Subscribe(t => {
_deleted_synthetics = new HashSet<uint>(t.Where(kv => kv.Value.Deleted).Select(kv => kv.Key));
var deleted_synthetic_keys = new HashSet<uint>(t.Where(kv => kv.Value.Deleted).Select(kv => kv.Key));
// add back 'undeleted' synthetics
foreach (var kvp in _deleted_synthetics) {
if (!deleted_synthetic_keys.Contains(kvp.Key)) {
_exchange_sec_map.TryAdd(kvp.Key, kvp.Value);
_deleted_synthetics.Remove(kvp.Key);
_version += 1;
}
}
// Remove any newly deleted synthetics and store them for potential undelete
foreach (var key in deleted_synthetic_keys) {
if (_exchange_sec_map.TryRemove(key, out SecurityMD deleted)) {
_deleted_synthetics[key] = deleted;
_version += 1;
}
}
// Post full dictionary of syntheics here
bus.post(SparkTags.SyntheticstrategiesTag, t);
}));
Task.Run(run);
@@ -52,24 +71,22 @@ namespace bts {
public void OnError(Exception error) => Log.Md.error(error.ToString());
public void OnNext(List<SecurityAndHandle> values) {
if (values.Count > 0) {
var temp = _exchange_sec_map.ToDictionary(k => k.Key, k => k.Value);
foreach (var v in values) {
temp.Add(v.security.Id, new SecurityMD(v.security, v.handle));
var smd = new SecurityMD(v.security, v.handle);
_exchange_sec_map.AddOrUpdate(smd.security.Id, smd, (_, __) => smd);
}
_exchange_sec_map = temp;
_version += 1;
}
}
public void Dispose() => Disposable.dispose(_dispose);
async Task run() {
int exch_sec_count = 0, deleted_synth_count = 0;
long run_version = 0;
while (true) {
var exch = _exchange_sec_map;
var deleted_synths = _deleted_synthetics;
if (exch.Count > exch_sec_count || deleted_synths.Count != deleted_synth_count) {
exch_sec_count = exch.Count;
deleted_synth_count = deleted_synths.Count;
exch = exch.Where(kv => !deleted_synths.Contains(kv.Key)).ToDictionary();
long current_version = _version;
if (current_version > run_version) {
run_version = current_version;
try {
Log.Md.info($"MDSource publishing {exch.Count} securities");
bus.post(SparkTags.SecuritiesTag, exch);