diff --git a/src/DataStreamSuite/DataStream/BlueOrb/CountStream.cs b/src/DataStreamSuite/DataStream/BlueOrb/CountStream.cs index fd356447f..27f361f6e 100644 --- a/src/DataStreamSuite/DataStream/BlueOrb/CountStream.cs +++ b/src/DataStreamSuite/DataStream/BlueOrb/CountStream.cs @@ -14,9 +14,6 @@ namespace BCG { myVS = vs; } - public CountStream(string tableName) - : this(new TableStream(tableName)) { } - protected override void myOnFirstSubscriber() { myVS.onChange += myHandleChanges; } diff --git a/src/DataStreamSuite/DataStream/Offloading/RemoteQueryManager.cs b/src/DataStreamSuite/DataStream/Offloading/RemoteQueryManager.cs index d129236f0..334f23cc8 100644 --- a/src/DataStreamSuite/DataStream/Offloading/RemoteQueryManager.cs +++ b/src/DataStreamSuite/DataStream/Offloading/RemoteQueryManager.cs @@ -12,6 +12,7 @@ using Spark.Core.Disposables; using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Reactive.Linq; using System.Threading; using System.Threading.Tasks; @@ -59,10 +60,13 @@ namespace BCG { bool isDisposed; - public RemoteQueryManager(IManagedSubscriber managedSubscriber, IPubTableBroker pubTableBroker, IDataStreamFactory streamFactory) { + public RemoteQueryManager(IManagedSubscriber managedSubscriber, IPubTableBroker pubTableBroker, IEnumerable streamFactories) { _sub = managedSubscriber; _broker = pubTableBroker; - this.streamFactory = streamFactory; + var streamFactoryList = streamFactories.ToList(); + streamFactory = streamFactoryList.Count == 1 + ? streamFactoryList[0] + : new CompositeDataStreamFactory(streamFactoryList); } public Task StartAsync(CancellationToken cancellationToken) { @@ -70,7 +74,9 @@ namespace BCG { Log.assert(!isDisposed); Log.isnull(cloudStreamsSub); Log.isnull(remoteRequestsSub); - cloudStreamsSub = new TableStream("cloud_streams").AsObservable().Subscribe(_on_str); + var cloudStream = streamFactory.Create("table", new (string?, int?, object?)[] { ("name", 0, "cloud_streams") }) + ?? throw new Exception("Failed to create TableStream 'cloud_stream'"); + cloudStreamsSub = cloudStream.AsObservable().Subscribe(_on_str); remoteRequestsSub = _sub.Observe("RemoteRequest").Subscribe(data => { RemoteRequest req = RemoteRequest.construct_from_bytes(data); _spawn_remote_stream(req.published_table_name, req.stream_expression, false); diff --git a/src/DataStreamSuite/DataStream/Offloading/RemoteQueryStream.cs b/src/DataStreamSuite/DataStream/Offloading/RemoteQueryStream.cs index 9e10b7d41..1350d6126 100644 --- a/src/DataStreamSuite/DataStream/Offloading/RemoteQueryStream.cs +++ b/src/DataStreamSuite/DataStream/Offloading/RemoteQueryStream.cs @@ -2,17 +2,29 @@ using BCG; using bts; +using Bts.DataStreamCore.StreamFactories; + +using Spark.Core.Disposables; + using System; namespace BCG { - // this is a stream so we can run it from the DSQA (since dsqa can only load streams) - [DataStreamUserRespresentation("remoteQuery")] + /// + /// This is a stream so we can run it from the DSQA (since dsqa can only load streams). + /// + /// Note: It's hidden because this stream is now created by an IDataStreamFactory + /// (see RemoteQueryStreamFactory.cs for details) + /// + /// + [DataStreamUserRespresentation(false)] public class RemoteQueryStream : ViewStream { private readonly string _name, _stream_exp; + readonly TableStreamFactory tableStreamFactory; private PublisherCore _publisher_core = new PublisherCore(); - private ViewStream _source_str; + IDisposable? subscription; - public RemoteQueryStream(params string[] lines) { + public RemoteQueryStream(TableStreamFactory tableStreamFactory, params string[] lines) { + this.tableStreamFactory = tableStreamFactory; _stream_exp = get_expression(lines); _name = Guid.NewGuid().ToString(); _name = $"remotereq_{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString()}"; @@ -21,17 +33,17 @@ namespace BCG { } protected override void myOnFirstSubscriber() { + Log.isnull(subscription); RemoteRequest rem_req = new RemoteRequest(_stream_exp, _name); byte[] rem_req_bytes = rem_req.serialize(); _publisher_core.publish("System", "RemoteRequest", rem_req_bytes); - _source_str = new TableStream(_name); - _source_str.onChange += _handle_data; + var sourceStream = tableStreamFactory.Create(_name); + sourceStream.onChange += _handle_data; + subscription = Disposable.Create(() => sourceStream.onChange -= _handle_data); } - protected override void myOnLastSubscriber() { - //maybe there should be a way to turn remote requests off ? - _source_str.onChange -= _handle_data; - } + protected override void myOnLastSubscriber() => + Disposable.dispose(ref subscription); static string get_expression(params string[] lines) { for (int i = 0; i < lines.Length; i++) { diff --git a/src/DataStreamSuite/DataStream/Primitives/Count/CountStream.cs b/src/DataStreamSuite/DataStream/Primitives/Count/CountStream.cs index 2a167e1ce..d9019f152 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Count/CountStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Count/CountStream.cs @@ -15,9 +15,6 @@ namespace bts.BlueOrb { myVS = vs; } - public CountStream(string tableName) - : this(new TableStream(tableName)) { } - protected override void myOnFirstSubscriber () => myVS.onChange += myHandleChanges; diff --git a/src/DataStreamSuite/DataStream/StreamFactories/RemoteQueryStreamFactory.cs b/src/DataStreamSuite/DataStream/StreamFactories/RemoteQueryStreamFactory.cs new file mode 100644 index 000000000..32f4d16a5 --- /dev/null +++ b/src/DataStreamSuite/DataStream/StreamFactories/RemoteQueryStreamFactory.cs @@ -0,0 +1,39 @@ +#nullable enable +namespace Bts.DataStream.StreamFactories; + +using BCG; + +using Bts.DataStreamCore; +using Bts.DataStreamCore.StreamFactories; + +using System.Collections.Generic; +using System.Linq; + +public class RemoteQueryStreamFactory : IDataStreamFactory { + readonly TableStreamFactory tableStreamFactory; + + public RemoteQueryStreamFactory(TableStreamFactory tableStreamFactory) => + this.tableStreamFactory = tableStreamFactory; + + public ViewStream? Create(string streamId, IEnumerable<(string? Name, int? Index, object? Value)> args) { + if (SupportedStreams.RemoteQuery.IsMatch(streamId)) { + return createTable(args); + } + return null; + } + + public IEnumerable GetSupportedStreams() { + yield return SupportedStreams.RemoteQuery; + } + + ViewStream? createTable(IEnumerable<(string? Name, int? Index, object? Value)> args) { + var lines = args.Select(a => a.Value as string ?? a.Value?.ToString() ?? string.Empty).ToArray(); + return new RemoteQueryStream(tableStreamFactory, lines); + } + + static class SupportedStreams { + public static readonly DataStreamMetadata RemoteQuery = new() { + Name = "remoteQuery" + }; + } +} diff --git a/src/DataStreamSuite/DataStreamCore/ProxyStream.cs b/src/DataStreamSuite/DataStreamCore/ProxyStream.cs index 9ec179502..df957d594 100644 --- a/src/DataStreamSuite/DataStreamCore/ProxyStream.cs +++ b/src/DataStreamSuite/DataStreamCore/ProxyStream.cs @@ -7,6 +7,7 @@ using System.Reflection; using BCG.Utilities; using BCG.Utilities.General; +using Bts.DataStreamCore.Services; namespace BCG { @@ -126,11 +127,11 @@ namespace BCG public string getTag() { return myTag; } private string myTag; - protected ProxyStream(params object[] args) - {myInit(null, args);} + protected ProxyStream(ITableAccumulatorFactory tableAccumulatorFactory, params object[] args) => + myInit(null, tableAccumulatorFactory, args); - protected ProxyStream(ViewStreamDatabase db, params object[] args) - {myInit(db, args);} + protected ProxyStream(ViewStreamDatabase db, ITableAccumulatorFactory tableAccumulatorFactory, params object[] args) => + myInit(db, tableAccumulatorFactory, args); class MyException : System.Exception { @@ -145,7 +146,7 @@ namespace BCG throw new MyException(msg); } - private void myInit(ViewStreamDatabase db, params object[] args) + private void myInit(ViewStreamDatabase db, ITableAccumulatorFactory tableAccumulatorFactory, params object[] args) { try { @@ -155,12 +156,12 @@ namespace BCG "Null arguments are not supported."); myTag = myGetTag(ci, args, db != null); - mySetRealSource(new TableStream("prepub_" + myTag, db)); + mySetRealSource(TableStream.Create("prepub_" + myTag, tableAccumulatorFactory)); } catch (MyException e) { // BlueEventLog2.instance().logEvent(e.Message, BlueEventLog2Level.Fatal); - mySetRealSource(new TableStream("error_tag")); + mySetRealSource(TableStream.Create("error_tag", tableAccumulatorFactory)); } } diff --git a/src/DataStreamSuite/DataStreamCore/Services/DataStreamCoreServices.cs b/src/DataStreamSuite/DataStreamCore/Services/DataStreamCoreServices.cs index ead934004..27fec78f2 100644 --- a/src/DataStreamSuite/DataStreamCore/Services/DataStreamCoreServices.cs +++ b/src/DataStreamSuite/DataStreamCore/Services/DataStreamCoreServices.cs @@ -3,6 +3,8 @@ namespace Bts.DataStreamCore.Services; using BCG; using BCG.Utilities.General; +using Bts.DataStreamCore.StreamFactories; + using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -12,7 +14,7 @@ public static class DataStreamCoreServices { public static IServiceCollection AddViewStreamDatabase(this IServiceCollection services) => services.AddSingleton(sp => { - ViewStreamDatabase.SetGlobalInstance(new ViewStreamDatabase(sp.GetRequiredService())); + ViewStreamDatabase.SetGlobalInstance(new ViewStreamDatabase()); return ViewStreamDatabase.instance; }); @@ -20,6 +22,12 @@ public static class DataStreamCoreServices { services.Configure(configurationSection) .AddSingleton(); + public static IServiceCollection AddTableStreams(this IServiceCollection services) => + services.AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(sp => sp.GetRequiredService()); + public static IServiceCollection AddCompositeDataStreamFactory(this IServiceCollection services) => services.AddSingleton(); } diff --git a/src/DataStreamSuite/DataStreamCore/Services/TableAccumulatorFactory.cs b/src/DataStreamSuite/DataStreamCore/Services/TableAccumulatorFactory.cs new file mode 100644 index 000000000..98bf835d8 --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/Services/TableAccumulatorFactory.cs @@ -0,0 +1,25 @@ +namespace Bts.DataStreamCore.Services; + +using BCG; + +using System.Collections.Generic; + +public interface ITableAccumulatorFactory { + ITableAccumulator GetTableAccumulator(string tag); +} + +class TableAccumulatorFactory : ITableAccumulatorFactory { + readonly ISubscriberCore subscriberCore; + readonly Dictionary tables = new(); + readonly object syncRoot = new(); + + public TableAccumulatorFactory(ISubscriberCore subscriberCore) { + this.subscriberCore = subscriberCore; + } + + public ITableAccumulator GetTableAccumulator(string tag) { + lock (syncRoot) { + return tables.GetOrAdd(tag, _ => new TableAccumulator2(tag, subscriberCore)); + } + } +} diff --git a/src/DataStreamSuite/DataStreamCore/Services/TableAccumulatorWorker.cs b/src/DataStreamSuite/DataStreamCore/Services/TableAccumulatorWorker.cs new file mode 100644 index 000000000..2ee0048aa --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/Services/TableAccumulatorWorker.cs @@ -0,0 +1,38 @@ +namespace Bts.DataStreamCore.Services; + +using BCG; +using BCG.Utilities.General; + +using System; +using System.Diagnostics; +using System.Linq; + +class TableAccumulatorWorker : IWorker { + static readonly TimeSpan logTimeThreshold = TimeSpan.FromSeconds(10); + static readonly TimeSpan updateTimeout = TimeSpan.FromMilliseconds(10); + readonly ISubscriberCore subscriberCore; + readonly ITableAccumulatorFactory tableAccumulatorFactory; + readonly Stopwatch stopwatch = new(); + + public TableAccumulatorWorker(ISubscriberCore subscriberCore, ITableAccumulatorFactory tableAccumulatorFactory) { + this.subscriberCore = subscriberCore; + this.tableAccumulatorFactory = tableAccumulatorFactory; + } + + public void doWork() { + var updates = subscriberCore.get_updates(updateTimeout); + if (updates is null) { + return; + } + foreach (var tagUpdates in updates.Where(msg => !string.IsNullOrEmpty(msg.tag)).GroupBy(msg => msg.tag!)) { + string tag = tagUpdates.Key; + var ta = tableAccumulatorFactory.GetTableAccumulator(tag); + stopwatch.Restart(); + ta.handle_updates(tagUpdates.Select(u => u.data)); + stopwatch.Stop(); + if (stopwatch.Elapsed > logTimeThreshold) { + bts.Log.Gen.error($"ViewStreamDatabase.doWork tag: {tag} took {stopwatch.Elapsed}"); + } + } + } +} diff --git a/src/DataStreamSuite/DataStreamCore/StreamFactories/ProxyStreamFactory.cs b/src/DataStreamSuite/DataStreamCore/StreamFactories/ProxyStreamFactory.cs new file mode 100644 index 000000000..cefb67902 --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/StreamFactories/ProxyStreamFactory.cs @@ -0,0 +1,50 @@ +#nullable enable +namespace Bts.DataStreamCore.StreamFactories; + +using BCG; + +using Bts.DataStreamCore.Services; + +using Microsoft.Extensions.Logging; + +using System.Collections.Generic; + +public class ProxyStreamFactory : IDataStreamFactory { + readonly ITableAccumulatorFactory tableAccumulatorFactory; + readonly ILogger logger; + + public ProxyStreamFactory(ITableAccumulatorFactory tableAccumulatorFactory, ILogger logger) { + this.tableAccumulatorFactory = tableAccumulatorFactory; + this.logger = logger; + } + + public ViewStream? Create(string streamId, IEnumerable<(string? Name, int? Index, object? Value)> args) { + if (SupportedStreams.Table.IsMatch(streamId)) { + return createTable(args); + } + return null; + } + + public IEnumerable GetSupportedStreams() { + yield return SupportedStreams.Table; + } + + ViewStream? createTable(IEnumerable<(string? Name, int? Index, object? Value)> args) { + if (!args.TryGetFirst(out var nameArg)) { + logger.LogError("TableStream missing required 'name' param"); + return null; + } + var tableName = nameArg.Value as string ?? nameArg.Value?.ToString(); + if (string.IsNullOrWhiteSpace(tableName)) { + logger.LogError("TableStream 'name' is null, white, or empty"); + return null; + } + return new TableStream(tableName, tableAccumulatorFactory.GetTableAccumulator(tableName)); + } + + static class SupportedStreams { + public static readonly DataStreamMetadata Table = new() { + Name = "proxy" + }; + } +} diff --git a/src/DataStreamSuite/DataStreamCore/StreamFactories/TableStreamFactory.cs b/src/DataStreamSuite/DataStreamCore/StreamFactories/TableStreamFactory.cs new file mode 100644 index 000000000..c6a6b9666 --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/StreamFactories/TableStreamFactory.cs @@ -0,0 +1,53 @@ +#nullable enable +namespace Bts.DataStreamCore.StreamFactories; + +using BCG; + +using Bts.DataStreamCore.Services; + +using Microsoft.Extensions.Logging; + +using System.Collections.Generic; + +public class TableStreamFactory : IDataStreamFactory { + readonly ITableAccumulatorFactory tableAccumulatorFactory; + readonly ILogger logger; + + public TableStreamFactory(ITableAccumulatorFactory tableAccumulatorFactory, ILogger logger) { + this.tableAccumulatorFactory = tableAccumulatorFactory; + this.logger = logger; + } + + public ViewStream Create(string tableName) => + new TableStream(tableName, tableAccumulatorFactory.GetTableAccumulator(tableName)); + + ViewStream? IDataStreamFactory.Create(string streamId, IEnumerable<(string? Name, int? Index, object? Value)> args) { + if (SupportedStreams.Table.IsMatch(streamId)) { + return createTable(args); + } + return null; + } + + IEnumerable IDataStreamFactory.GetSupportedStreams() { + yield return SupportedStreams.Table; + } + + ViewStream? createTable(IEnumerable<(string? Name, int? Index, object? Value)> args) { + if (!args.TryGetFirst(out var nameArg)) { + logger.LogError("TableStream missing required 'name' param"); + return null; + } + var tableName = nameArg.Value as string ?? nameArg.Value?.ToString(); + if (string.IsNullOrWhiteSpace(tableName)) { + logger.LogError("TableStream 'name' is null, white, or empty"); + return null; + } + return Create(tableName); + } + + static class SupportedStreams { + public static readonly DataStreamMetadata Table = new() { + Name = "table" + }; + } +} diff --git a/src/DataStreamSuite/DataStreamCore/TableStream.cs b/src/DataStreamSuite/DataStreamCore/TableStream.cs index bc3977212..1c2b2284d 100644 --- a/src/DataStreamSuite/DataStreamCore/TableStream.cs +++ b/src/DataStreamSuite/DataStreamCore/TableStream.cs @@ -1,24 +1,21 @@ namespace BCG { + using Bts.DataStreamCore.Services; + // this is the stream that gets tables directly off the pubsubServer public class TableStream : ViewStream { private readonly string _table_name; private readonly ITableAccumulator _ta; - - public TableStream(string table_name) - : - this(table_name, - ViewStreamDatabase.instance) { } - - - public TableStream(string table_name, ViewStreamDatabase db) { + public TableStream(string table_name, ITableAccumulator tableAccumulator) { _table_name = table_name; - db = db == null ? ViewStreamDatabase.instance : db; - _ta = db.getITableAccumulator(_table_name); + _ta = tableAccumulator; } + public static TableStream Create(string tableName, ITableAccumulatorFactory tableAccumulatorFactory) => + new(tableName, tableAccumulatorFactory.GetTableAccumulator(tableName)); + protected override void myOnFirstSubscriber() { _ta.on_update += new TableUpdateDelegate(_handle_updates); } diff --git a/src/DataStreamSuite/DataStreamCore/ViewStreamDatabase.cs b/src/DataStreamSuite/DataStreamCore/ViewStreamDatabase.cs index 9df0e3388..71bb95fb2 100644 --- a/src/DataStreamSuite/DataStreamCore/ViewStreamDatabase.cs +++ b/src/DataStreamSuite/DataStreamCore/ViewStreamDatabase.cs @@ -7,7 +7,7 @@ using Bts.PubSub; using bts; namespace BCG { - public class ViewStreamDatabase : IWorker, IWorkScheduler, IDisposable { + public class ViewStreamDatabase : IWorker, IWorkScheduler { internal delegate void TagFireDelegate(string tag); public delegate void DoWorkDelegate(); private event DoWorkDelegate onDoWorkStart, @@ -16,45 +16,21 @@ namespace BCG { onTagFireEnd; private Dictionary _table_accumulator_map = new Dictionary(); private static ViewStreamDatabase mySelf; - private ISubscriberCore mySubscriberCore; private List _workers = new List(); - private bool myIsDisposed = false; readonly Stopwatch _timer = new(); - readonly TimeSpan myLogTimeThreshold = TimeSpan.FromSeconds(10); readonly TimeSpan _dowork_threshold = TimeSpan.FromMilliseconds(500); private Thread myThreadOwner = null; // ViewStreamDatabase takes a thread affinity when created. If called from a different thread it will assert. private static readonly object myInstanceLockObject = new object(); - public ViewStreamDatabase() - : this(new SubscriberCore()) { - } - - public ViewStreamDatabase(ISubscriberCore subscriberCore) { - mySubscriberCore = subscriberCore; - } - public static ViewStreamDatabase instance { get { var rv = mySelf; Log.notnull(rv); - Log.assert(!rv.myIsDisposed); return rv; } } - public ITableAccumulator getITableAccumulator(string table_name) { - // If theres a ITableAccumulator for the tableName its returned (to share subscriptions), otherwise - // a new one is obtained from the Subscriber factory - ITableAccumulator rv = null; - var table_tag_name = DSTagManager.get_stream_tag(table_name); - if (!_table_accumulator_map.TryGetValue(table_tag_name, out rv)) { - rv = AccumulatorFactory.getAccumulator(table_tag_name, mySubscriberCore); - _table_accumulator_map.Add(table_tag_name, rv); - } - return rv; - } - public void doWork() { // TODO verifyAccess(); @@ -66,33 +42,6 @@ namespace BCG { try { if (onDoWorkStart != null) onDoWorkStart(); - - var updates = mySubscriberCore.get_updates(TimeSpan.FromMilliseconds(10)); - if (updates != null) { - var consolidated_update_map = _consolidate_updates(updates); - - foreach (var kv in consolidated_update_map) { - string tag = kv.Key; - List tag_updates = kv.Value; - ITableAccumulator table_accumulator; - if (!_table_accumulator_map.TryGetValue(tag, out table_accumulator)) { - bts.Log.Gen.warn("Received callback for tag: {tag}"); - continue; - } - _timer.Restart(); - - if (onTagFireStart != null) - onTagFireStart(tag); - table_accumulator.handle_updates(tag_updates); - if (onTagFireEnd != null) - onTagFireEnd(tag); - _timer.Stop(); - if (_timer.Elapsed > myLogTimeThreshold) { - bts.Log.Gen.error($"ViewStreamDatabase.doWork tag: {tag} took {_timer.Elapsed}"); - } - } - } - // to allow for registering of workers on the worker callback _is_worker_call_in_progress = true; @@ -173,15 +122,6 @@ namespace BCG { void IWorkScheduler.Remove(IWorker worker) => removeWorker(worker); - #region IDisposable Members - - public void Dispose() { - if (!myIsDisposed) { - myIsDisposed = true; - mySubscriberCore.Dispose(); - } - } - internal static void SetGlobalInstance(ViewStreamDatabase vsd) { lock (myInstanceLockObject) { Log.isnull(mySelf); @@ -200,7 +140,5 @@ namespace BCG { public bool isAdd; public IWorker w; } - - #endregion } } diff --git a/src/DataStreamSuite/DataStreamExpressions/ViewNameExpression.cs b/src/DataStreamSuite/DataStreamExpressions/ViewNameExpression.cs index a1dea49d0..f0fecaff5 100644 --- a/src/DataStreamSuite/DataStreamExpressions/ViewNameExpression.cs +++ b/src/DataStreamSuite/DataStreamExpressions/ViewNameExpression.cs @@ -17,5 +17,7 @@ public class ViewNameExpression : StatementExpression { id; public override ViewStream? myGetView(IDictionary env, IDataStreamFactory streamFactory) => - env.TryGetValue(id, out var exp) ? exp.getView(env, streamFactory) : new TableStream(id); + env.TryGetValue(id, out var exp) + ? exp.getView(env, streamFactory) + : streamFactory.Create("table", new (string?, int?, object?)[] { ("name", 0, id) }); } diff --git a/src/DataStreamSuite/SharedStreams/System/PS3/TableMetaDataStream.cs b/src/DataStreamSuite/SharedStreams/System/PS3/TableMetaDataStream.cs index 49ab64a91..b4b49051b 100644 --- a/src/DataStreamSuite/SharedStreams/System/PS3/TableMetaDataStream.cs +++ b/src/DataStreamSuite/SharedStreams/System/PS3/TableMetaDataStream.cs @@ -3,6 +3,8 @@ namespace bts using BCG; using BCG.Utilities.General; + using Bts.DataStreamCore.StreamFactories; + public class TableMetaDataStream : ViewStream, IWorker { private class TableState @@ -32,14 +34,17 @@ namespace bts Tuple.Create("col_type_code", TypeCode.Int32), Tuple.Create("col_name", TypeCode.String)); + readonly TableStreamFactory tableStreamFactory; private ViewStreamDatabase _vsd; private ViewStream _base_stream; private IDictionary _table_map = new Dictionary(); private BlueTable _state; - public TableMetaDataStream() - : this(ViewStreamDatabase.instance) { } - public TableMetaDataStream(ViewStreamDatabase vsd) - { + + public TableMetaDataStream(TableStreamFactory tableStreamFactory) + : this(tableStreamFactory, ViewStreamDatabase.instance) { } + + public TableMetaDataStream(TableStreamFactory tableStreamFactory, ViewStreamDatabase vsd) { + this.tableStreamFactory = tableStreamFactory; _vsd = vsd; _base_stream = new PubSubServerStateStream(_vsd); _state = new BlueTable(_schema); @@ -106,9 +111,9 @@ namespace bts TableState ts = null; if (!_table_map.TryGetValue(table_name, out ts)) { - ts = new TableState(new TableStream(table_name, _vsd)); + ts = new TableState(tableStreamFactory.Create(table_name)); _table_map.Add(table_name, ts); } } } -} \ No newline at end of file +} diff --git a/src/client/spark/Services/SparkServices.cs b/src/client/spark/Services/SparkServices.cs index b9cdddaa0..0e29760c3 100644 --- a/src/client/spark/Services/SparkServices.cs +++ b/src/client/spark/Services/SparkServices.cs @@ -21,18 +21,23 @@ public static class SparkServices { .AddSingleton(sp => sp.GetRequiredService()) .AddHostedService(sp => sp.GetRequiredService()); - public static IServiceCollection AddSparkApp(this IServiceCollection services, IConfiguration configurationSection) => - services - .AddSpark(configurationSection.GetSection("Spark")) + public static IServiceCollection AddSparkApp(this IServiceCollection services, IConfiguration configurationSection) { + services.AddSpark(configurationSection.GetSection("Spark")) .AddSparkApi(configurationSection.GetSection("SparkApi")) .AddSparkLoginGui() .AddMasterWorker(configurationSection.GetSection("MasterWorker")) .AddViewStreamDatabase() - .AddPubSubClient(configurationSection.GetSection("PubSub")) - .AddPubTableBroker() - .AddRemoteQueryManager() .AddPrimitiveStreams(configurationSection.GetSection("PrimitiveStream")) - .AddMarketDataStreams() - .AddGarbageCollector(configurationSection.GetSection("GarbageCollector")) + .AddMarketDataStreams(); + var pubSubCfg = configurationSection.GetSection("PubSub"); + if (pubSubCfg is not null && pubSubCfg.GetValue("Enabled", true)) { + services.AddPubSubClient(configurationSection.GetSection("PubSub")) + .AddPubTableBroker() + .AddRemoteQueryManager() + .AddTableStreams(); + } + services.AddGarbageCollector(configurationSection.GetSection("GarbageCollector")) .AddHostedService(); + return services; + } }