diff --git a/Source/Libraries/GSF.TimeSeries/Transport/DataSubscriber.cs b/Source/Libraries/GSF.TimeSeries/Transport/DataSubscriber.cs index ec4771f461b..9862223c348 100755 --- a/Source/Libraries/GSF.TimeSeries/Transport/DataSubscriber.cs +++ b/Source/Libraries/GSF.TimeSeries/Transport/DataSubscriber.cs @@ -3795,7 +3795,6 @@ protected virtual void SynchronizeMetadata() if (metadata.Tables.Contains("DeviceDetail")) { DataTable deviceDetail = metadata.Tables["DeviceDetail"]; - List uniqueIDs = new List(); DataRow[] deviceRows; // Define SQL statement to query if this device is already defined (this should always be based on the unique guid-based device ID) @@ -3849,14 +3848,30 @@ protected virtual void SynchronizeMetadata() // Older versions of GEP did not include the AccessID field, so this is treated as optional int accessID = 0; + List uniqueIDs = deviceRows + .Select(deviceRow => database.Guid(deviceRow, "UniqueID")) + .ToList(); + + // Remove any device records associated with this subscriber that no longer exist in the meta-data + if (uniqueIDs.Count > 0) + { + IEnumerable retiredUniqueIDs = command + .RetrieveData(database.AdapterType, queryUniqueDeviceIDsSql, m_metadataSynchronizationTimeout, parentID) + .Select() + .Select(deviceRow => database.Guid(deviceRow, "UniqueID")) + .Except(uniqueIDs); + + foreach (Guid retiredUniqueID in retiredUniqueIDs) + command.ExecuteNonQuery(deleteDeviceSql, m_metadataSynchronizationTimeout, database.Guid(retiredUniqueID)); + + UpdateSyncProgress(); + } + foreach (DataRow row in deviceRows) { Guid uniqueID = Guid.Parse(row.Field("UniqueID").ToString()); bool recordNeedsUpdating; - // Track unique device Guids in this meta-data session, we'll need to remove any old associated devices that no longer exist - uniqueIDs.Add(uniqueID); - // Determine if record has changed since last synchronization if (updatedOnFieldExists) { @@ -3956,26 +3971,6 @@ protected virtual void SynchronizeMetadata() // Periodically notify user about synchronization progress UpdateSyncProgress(); } - - // Remove any device records associated with this subscriber that no longer exist in the meta-data - if (uniqueIDs.Count > 0) - { - // Sort unique ID list so that binary search can be used for quick lookups - uniqueIDs.Sort(); - - DataTable deviceUniqueIDs = command.RetrieveData(database.AdapterType, queryUniqueDeviceIDsSql, m_metadataSynchronizationTimeout, parentID); - Guid uniqueID; - - foreach (DataRow deviceRow in deviceUniqueIDs.Rows) - { - uniqueID = database.Guid(deviceRow, "UniqueID"); - - // Remove any devices in the database that are associated with the parent device and do not exist in the meta-data - if (uniqueIDs.BinarySearch(uniqueID) < 0) - command.ExecuteNonQuery(deleteDeviceSql, m_metadataSynchronizationTimeout, database.Guid(uniqueID)); - } - UpdateSyncProgress(); - } } // Check to see if data for the "MeasurementDetail" table was included in the meta-data