//****************************************************************************************************** // DataSourceLookups.cs - Gbtc // // Copyright © 2016, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 10/31/2016 - Steven E. Chisholm // Generated original version of source code. // //****************************************************************************************************** using System; using System.Collections.Generic; using System.Data; using GSF.Diagnostics; using GSF.Units.EE; namespace GSF.TimeSeries.Data { /// /// Creates a cached lookup so certain metadata so lookups can occur with quickly. /// public static class DataSourceLookups { private static readonly LogPublisher s_log = Logger.CreatePublisher(typeof(DataSourceLookups), MessageClass.Framework); private static readonly List> s_dataSetLookups = new List>(); /// /// Gets/Creates the lookup cache for the provided dataset. /// /// The non-null dataset provided by the time-series framework /// Lookup cache for the provided dataset. public static DataSourceLookupCache GetLookupCache(DataSet dataSet) { if (dataSet == null) throw new ArgumentNullException(nameof(dataSet)); // Since adding datasets will be rare, the penalty associated with a lock on the entire set will be minor. lock (s_dataSetLookups) { DataSourceLookupCache target; for (int index = 0; index < s_dataSetLookups.Count; index++) { WeakReference item = s_dataSetLookups[index]; if (item.TryGetTarget(out target) && target.DataSet != null) { if (ReferenceEquals(target.DataSet, dataSet)) { return target; } } else { s_log.Publish(MessageLevel.Info, "A DataSet object has been disposed or garbage collected. It will be removed from the list."); s_dataSetLookups.RemoveAt(index); index--; } } s_log.Publish(MessageLevel.Info, "Creating a lookup cache for a dataset"); target = new DataSourceLookupCache(dataSet); s_dataSetLookups.Add(new WeakReference(target)); return target; } } /// /// Gets/Creates the for the provided dataset. /// /// /// for the provided dataset. public static ActiveMeasurementsTableLookup ActiveMeasurements(DataSet dataSet) => GetLookupCache(dataSet).ActiveMeasurements; /// /// Lookups up metadata record from provided . /// /// Target . /// signal ID to lookup. /// Measurement table name used for meta-data lookup. /// Metadata data row, if found; otherwise, null. public static DataRow LookupMetadata(this DataSet dataSource, Guid signalID, string measurementTable = "ActiveMeasurements") { if (dataSource == null) throw new ArgumentNullException(nameof(dataSource)); DataRow[] records = dataSource.Tables[measurementTable].Select($"SignalID = '{signalID}'"); return records.Length > 0 ? records[0] : null; } /// /// Gets signal type for given measurement key /// /// Target . /// Source . /// Measurement table name used for meta-data lookup. /// as defined for measurement key in data source. public static SignalType GetSignalType(this DataSet dataSource, MeasurementKey key, string measurementTable = "ActiveMeasurements") { if (dataSource == null) throw new ArgumentNullException(nameof(dataSource)); DataRow record = dataSource.LookupMetadata(key.SignalID, measurementTable); if (record != null && Enum.TryParse(record["SignalType"].ToString(), out SignalType signalType)) return signalType; return SignalType.NONE; } /// /// Gets signal types for given measurement keys. /// /// Target . /// Source set of values. /// Measurement table name used for meta-data lookup. /// values for each defined measurement key as configured in data source. public static SignalType[] GetSignalTypes(this DataSet dataSource, MeasurementKey[] keys, string measurementTable = "ActiveMeasurements") { if (dataSource == null) throw new ArgumentNullException(nameof(dataSource)); if (keys == null || keys.Length == 0) return Array.Empty(); SignalType[] signalTypes = new SignalType[keys.Length]; for (int i = 0; i < signalTypes.Length; i++) signalTypes[i] = dataSource.GetSignalType(keys[i], measurementTable); return signalTypes; } /// /// Gets signal types for given measurement keys. /// /// Target . /// Source set of values. /// Measurement table name used for meta-data lookup. /// values for each defined measurement key as configured in data source. public static SignalType[] GetSignalTypes(this DataSet dataSource, IMeasurement[] measurements, string measurementTable = "ActiveMeasurements") { if (dataSource == null) throw new ArgumentNullException(nameof(dataSource)); if (measurements == null || measurements.Length == 0) return Array.Empty(); SignalType[] signalTypes = new SignalType[measurements.Length]; for (int i = 0; i < signalTypes.Length; i++) signalTypes[i] = dataSource.GetSignalType(measurements[i].Key, measurementTable); return signalTypes; } } }