refactor: Move a few low-level DataStream types to DataStreamCore
So the lex/yacc generators can reference them
This commit is contained in:
1 parent
d1f5ae86fd
commit
ac1ede5986
10 files changed
+6
-30
No files matched your search
@@ -0,0 +1,314 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using BCG.Utilities;
|
||||
using BCG.Utilities.General;
|
||||
//using BCG.Utilities.BlueConfiguration;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using mbts;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
using HighPerformanceTimer = mbts.spark.HighPerformanceTimer;
|
||||
|
||||
public class ViewStreamDatabase : IWorker, IDisposable
|
||||
{
|
||||
internal delegate void TagFireDelegate(string tag);
|
||||
public delegate void DoWorkDelegate ();
|
||||
public event DoWorkDelegate onDoWorkStart,
|
||||
onDoWorkEnd;
|
||||
internal event TagFireDelegate onTagFireStart,
|
||||
onTagFireEnd;
|
||||
//currently onTagUpdate is usd by the DataStreamPerfRecorder to keep track of how often the updates for various streams come in
|
||||
//and how big they are.
|
||||
//public delegate void OnTagUpdateDelegate(PubMetaData metaData);
|
||||
//public event OnTagUpdateDelegate onTagUpdate;
|
||||
private IDictionary myITableAccumulatorMap = new Hashtable ();
|
||||
private static ViewStreamDatabase mySelf;
|
||||
private ISubscriberCore mySubscriberCore;
|
||||
private ArrayList myWorkers = new ArrayList ();
|
||||
private bool myIsDisposed = false;
|
||||
private bool myIsInDoWork = false;
|
||||
private static object myTimeInterval;
|
||||
private HighPerformanceTimer myTimer = new HighPerformanceTimer();
|
||||
private double myLogTimeThreshold = 10000;
|
||||
private readonly double _dowork_threshold = 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();
|
||||
private readonly object myStateLock = new object();
|
||||
|
||||
public ViewStreamDatabase ()
|
||||
: this (new SubscriberCore ())
|
||||
{
|
||||
}
|
||||
|
||||
public ViewStreamDatabase (ISubscriberCore subscriberCore)
|
||||
{
|
||||
mySubscriberCore = subscriberCore;
|
||||
// if (DataStreamLoadFileConfig.instance.profileStreams)
|
||||
// new DataStreamPerfRecorder(this);
|
||||
}
|
||||
|
||||
private static int myGetTimeInterval()
|
||||
{
|
||||
if (myTimeInterval == null)
|
||||
{
|
||||
// TODO
|
||||
string timeInterval = String.Empty; //ConfigurationRequester.request("viewStreamDatabaseTimeOut");
|
||||
if (timeInterval == null || timeInterval == "")
|
||||
myTimeInterval = 0;
|
||||
else
|
||||
myTimeInterval = Int32.Parse(timeInterval);
|
||||
}
|
||||
return (int) myTimeInterval;
|
||||
}
|
||||
|
||||
public static int instance_hashcode = -1;
|
||||
public static ViewStreamDatabase instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if(mySelf == null || mySelf.myIsDisposed)
|
||||
lock(myInstanceLockObject)
|
||||
{
|
||||
if (mySelf == null || mySelf.myIsDisposed)
|
||||
{
|
||||
mySelf = new ViewStreamDatabase();
|
||||
instance_hashcode = mySelf.GetHashCode();
|
||||
}
|
||||
}
|
||||
return mySelf;
|
||||
}
|
||||
set
|
||||
{
|
||||
lock(myInstanceLockObject)
|
||||
{
|
||||
if (mySelf != null)
|
||||
mySelf.mySubscriberCore.Dispose ();
|
||||
mySelf = value;
|
||||
instance_hashcode = mySelf.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ICollection<string> getCachedTables()
|
||||
{
|
||||
List<string> result = new List<string>();
|
||||
|
||||
|
||||
foreach (string s in myITableAccumulatorMap.Keys)
|
||||
if (((ITableAccumulator)myITableAccumulatorMap[s]).isActive())
|
||||
result.Add(s);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public ISubscriberCore subscriber{get{return mySubscriberCore;}}
|
||||
|
||||
|
||||
public ITableAccumulator getITableAccumulator (string tableName)
|
||||
{
|
||||
// If theres a ITableAccumulator for the tableName its returned (to share subscriptions), otherwise
|
||||
// a new one is obtained from the Subscriber factory
|
||||
|
||||
myITableAccumulatorMap[tableName] = myITableAccumulatorMap[tableName] == null ?
|
||||
AccumulatorFactory.getAccumulator(tableName, mySubscriberCore) : myITableAccumulatorMap[tableName];
|
||||
|
||||
return myITableAccumulatorMap [tableName] as ITableAccumulator;
|
||||
}
|
||||
|
||||
public void doWork ()
|
||||
{
|
||||
verifyAccess();
|
||||
|
||||
if (myIsSuspended)
|
||||
return;
|
||||
if (myIsInDoWork)
|
||||
return;
|
||||
else
|
||||
{
|
||||
myIsInDoWork = true; // to allow reentrancy
|
||||
try
|
||||
{
|
||||
if (onDoWorkStart != null)
|
||||
onDoWorkStart();
|
||||
|
||||
ArrayList updateList = mySubscriberCore.getUpdates(myGetTimeInterval());
|
||||
IDictionary updateMap = myGetUpdateMap(updateList);
|
||||
|
||||
foreach (DictionaryEntry de in updateMap)
|
||||
{
|
||||
Stream[] allUpdates = (de.Value as ArrayList).ToArray(typeof(Stream)) as Stream[];
|
||||
string tag = de.Key.ToString();
|
||||
if (myITableAccumulatorMap[tag] == null){}
|
||||
// BlueEventLog.logEvent("Received a SubscriberCore's callback for a tag that was never subscribed", BlueEventLevel.Warning);
|
||||
else
|
||||
{
|
||||
myTimer.start();
|
||||
|
||||
|
||||
if (onTagFireStart != null)
|
||||
onTagFireStart(tag);
|
||||
|
||||
(myITableAccumulatorMap[tag] as ITableAccumulator).update(allUpdates);
|
||||
if (onTagFireEnd != null)
|
||||
onTagFireEnd(tag);
|
||||
myTimer.stop();
|
||||
if (myTimer.lastIntervalTime > myLogTimeThreshold)
|
||||
{
|
||||
// BlueEventLog.logEvent("callback on tag " + tag.ToUpper() + " took " +
|
||||
// myTimer.lastIntervalTime + " msecs", BlueEventLevel.Warning);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// to allow for registering of workers on the worker callback
|
||||
myIsCallingWorkers = true;
|
||||
myPendingOps = new ArrayList();
|
||||
foreach (IWorker w in myWorkers)
|
||||
{
|
||||
myTimer.start();
|
||||
w.doWork();
|
||||
myTimer.stop();
|
||||
if (myTimer.lastIntervalTime > _dowork_threshold){}
|
||||
// mbts.Logger.log (bts.log.Level.Warn, bts.log.Subsystem.gen, String.Format ("VSD.dowork on worker {0} took {1}", w.GetType().ToString (), myTimer.lastIntervalTime));
|
||||
}
|
||||
myIsCallingWorkers = false;
|
||||
|
||||
foreach (PendingAddRemoveWorker op in myPendingOps)
|
||||
if (op.isAdd)
|
||||
addWorker(op.w);
|
||||
else
|
||||
removeWorker(op.w);
|
||||
}
|
||||
finally
|
||||
{
|
||||
myPendingOps = null;
|
||||
myIsInDoWork = false;
|
||||
myIsCallingWorkers = false;
|
||||
if (onDoWorkEnd != null)
|
||||
onDoWorkEnd ();
|
||||
}
|
||||
}//end if we're not in do work
|
||||
|
||||
}
|
||||
|
||||
private bool myIsSuspended = false;
|
||||
|
||||
public void suspend() { myIsSuspended = true; }
|
||||
|
||||
public void resume() { myIsSuspended = false; }
|
||||
|
||||
public bool checkAccess()
|
||||
{
|
||||
if (this.myThreadOwner == null) this.myThreadOwner = Thread.CurrentThread;
|
||||
return this.myThreadOwner == Thread.CurrentThread;
|
||||
}
|
||||
|
||||
public void verifyAccess()
|
||||
{
|
||||
if (!checkAccess())
|
||||
{
|
||||
string errorMessage = string.Format("Invalid cross thread access on ViewStreamDatabase. Owning thread: {0}. Accessing thread: {1}", this.myThreadOwner.ManagedThreadId, Thread.CurrentThread.ManagedThreadId);
|
||||
|
||||
//#if DEBUG
|
||||
// BlueEventLog2.instance().assert(false, errorMessage);
|
||||
//#else
|
||||
// BlueEventLog2.instance().logEvent(errorMessage, BlueEventLog2Level.Fatal);
|
||||
//#endif
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private IDictionary myGetUpdateMap (ArrayList updates)
|
||||
{
|
||||
IDictionary retVal = new Hashtable ();
|
||||
foreach (PubSubUpdate update in updates)
|
||||
{
|
||||
if (retVal[update.tag] == null)
|
||||
retVal[update.tag] = new ArrayList ();
|
||||
(retVal[update.tag] as ArrayList).Add (update.body);
|
||||
}
|
||||
if (retVal.Count > 0)
|
||||
{
|
||||
string tagString = "";
|
||||
foreach (string tag in retVal.Keys)
|
||||
tagString += tag + " , ";
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
public bool isConsistent{get {return true;}}
|
||||
|
||||
public void addWorker(IWorker w)
|
||||
{
|
||||
verifyAccess();
|
||||
if (myIsCallingWorkers)
|
||||
myPendingOps.Add(new PendingAddRemoveWorker(true,w));
|
||||
else
|
||||
myWorkers.Add(w);
|
||||
}
|
||||
|
||||
|
||||
public void removeWorker(IWorker w)
|
||||
{
|
||||
verifyAccess();
|
||||
if (myIsCallingWorkers)
|
||||
myPendingOps.Add(new PendingAddRemoveWorker(false,w));
|
||||
else
|
||||
myWorkers.Remove(w);
|
||||
}
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!myIsDisposed)
|
||||
{
|
||||
myIsDisposed = true;
|
||||
mySubscriberCore.Dispose ();
|
||||
}
|
||||
}
|
||||
|
||||
private ArrayList myPendingOps;
|
||||
private bool myIsCallingWorkers = false;
|
||||
class PendingAddRemoveWorker
|
||||
{
|
||||
public PendingAddRemoveWorker(bool isAdd, IWorker w)
|
||||
{
|
||||
this.isAdd = isAdd;
|
||||
this.w = w;
|
||||
}
|
||||
|
||||
public bool isAdd;
|
||||
public IWorker w;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/*
|
||||
/// <summary>
|
||||
/// Returns a subscriber based on the name of the tag. Hardcoded right now, will change to config
|
||||
/// later
|
||||
/// </summary>
|
||||
public class SubscriberFactory
|
||||
{
|
||||
public static ITableAccumulator getITableAccumulator (string tag, SubscriberCore subCore)
|
||||
{
|
||||
string tableName = tag.Substring (3);
|
||||
if (!DataStreamGatewaySettings.instance.isSerializableUnderNewScheme (tableName))
|
||||
return new TableSubscriber (tag, subCore);
|
||||
else
|
||||
{
|
||||
string subName = DataStreamGatewaySettings.instance.getSubscriber (tableName);
|
||||
if (subName.Equals ("BCG.NonIncrementalTableSubscriber"))
|
||||
return new NonIncrementalTableSubscriber (tag, subCore);
|
||||
else
|
||||
return new TableSubscriber (tag, subCore);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
Reference in new issue
Block a user