private static async Task DiffAndStore()

in code/Diff/Components/DiffStops.cs [67:121]


        private static async Task DiffAndStore(IEnumerable<StopEntity> downloadedStops, IEnumerable<StopEntity> publishedStops, StorageManager diffStorage, StorageManager diffMetadataStorage, string regionId, string runId)
        {
            // create empty lists of the four different types of changes to be published
            List<StopEntity> newStops = new List<StopEntity>();
            List<StopEntity> updatedStops = new List<StopEntity>();
            List<StopEntity> deletedStops = new List<StopEntity>();
            List<StopEntity> resurrectedStops = new List<StopEntity>();

            // in the common case, most stop information will not change.
            // so, start by removing those items that are common to both considering their relevant content
            IEnumerable<StopEntity> unchangedStops = downloadedStops.Intersect(publishedStops.Where(r => r.RowState != DataRowState.Delete.ToString()), new StopEqualityComparerByChecksum());
            downloadedStops = downloadedStops.Except(unchangedStops, new StopEqualityComparerById());
            publishedStops = publishedStops.Except(unchangedStops, new StopEqualityComparerById());

            // new stops are those in downloadedStops but not in publishedStops when solely considering stop Ids
            newStops = downloadedStops.Except(publishedStops, new StopEqualityComparerById()).ToList();

            // deleted stops are those in publishedStops but not in downloadedStops when solely considering stopIds,
            // and are not already marked as deleted in publishedStops
            deletedStops = publishedStops.Except(downloadedStops, new StopEqualityComparerById()).Where(r => r.RowState != DataRowState.Delete.ToString()).ToList();

            // resurrected stops are those in publishedStops that are marked as deleted but are in downloadedStops when solely considering stopIds
            resurrectedStops = downloadedStops.Intersect(publishedStops.Where(r => r.RowState == DataRowState.Delete.ToString()), new StopEqualityComparerById()).ToList();

            // updated stops : first get the stops that are in common by Id that have not been resurrected,
            // then pick those who's contents are different
            updatedStops = downloadedStops.Intersect(publishedStops.Where(r => r.RowState != DataRowState.Delete.ToString()), new StopEqualityComparerById()).ToList();
            updatedStops = updatedStops.Except(publishedStops, new StopEqualityComparerByChecksum()).ToList();

            // set the appropriate datarowstate values
            newStops.ForEach(r => r.RowState = DataRowState.Create.ToString());
            updatedStops.ForEach(r => r.RowState = DataRowState.Update.ToString());
            deletedStops.ForEach(r => r.RowState = DataRowState.Delete.ToString());
            resurrectedStops.ForEach(r => r.RowState = DataRowState.Resurrect.ToString());

            // write to diff table
            await diffStorage.StopStore.Insert(newStops);
            await diffStorage.StopStore.Insert(updatedStops);
            await diffStorage.StopStore.Insert(deletedStops);
            await diffStorage.StopStore.Insert(resurrectedStops);

            // write to metadata table
            DiffMetadataEntity metadata = new DiffMetadataEntity
            {
                RunId = runId,
                RegionId = regionId,
                AgencyId = string.Empty,
                RecordType = RecordType.Stop.ToString(),
                AddedCount = newStops.Count,
                UpdatedCount = updatedStops.Count,
                DeletedCount = deletedStops.Count,
                ResurrectedCount = resurrectedStops.Count
            };
            await diffMetadataStorage.DiffMetadataStore.Insert(new List<DiffMetadataEntity> { metadata });
        }