private static async Task DiffAndStore()

in code/Diff/Components/DiffRoutes.cs [72:126]


        private static async Task DiffAndStore(IEnumerable<RouteEntity> downloadedRoutes, IEnumerable<RouteEntity> publishedRoutes, StorageManager diffStorage, StorageManager diffMetadataStorage, string regionId, string agencyId, string runId)
        {
            // create empty lists of the four different types of changes to be published
            List<RouteEntity> newRoutes = new List<RouteEntity>();
            List<RouteEntity> updatedRoutes = new List<RouteEntity>();
            List<RouteEntity> deletedRoutes = new List<RouteEntity>();
            List<RouteEntity> resurrectedRoutes = new List<RouteEntity>();

            // in the common case, most route information will not change.
            // so, start by removing those items that are common to both considering their relevant content
            IEnumerable<RouteEntity> unchangedRoutes = downloadedRoutes.Intersect(publishedRoutes.Where(r => r.RowState != DataRowState.Delete.ToString()), new RouteEqualityComparerByChecksum());
            downloadedRoutes = downloadedRoutes.Except(unchangedRoutes, new RouteEqualityComparerById());
            publishedRoutes = publishedRoutes.Except(unchangedRoutes, new RouteEqualityComparerById());

            // new routes are those in downloadedRoutes but not in publishedRoutes when solely considering route Ids
            newRoutes = downloadedRoutes.Except(publishedRoutes, new RouteEqualityComparerById()).ToList();

            // deleted routes are those in publishedRoutes but not in downloadedRoutes when solely considering routeIds,
            // and are not already marked as deleted in publishedRoutes
            deletedRoutes = publishedRoutes.Except(downloadedRoutes, new RouteEqualityComparerById()).Where(r => r.RowState != DataRowState.Delete.ToString()).ToList();

            // resurrected routes are those in publishedRoutes that are marked as deleted but are in downloadedRoutes when solely considering routeIds
            resurrectedRoutes = downloadedRoutes.Intersect(publishedRoutes.Where(r => r.RowState == DataRowState.Delete.ToString()), new RouteEqualityComparerById()).ToList();

            // updated routes : first get the routes that are in common by Id that have not been resurrected,
            // then pick those who's contents are different
            updatedRoutes = downloadedRoutes.Intersect(publishedRoutes.Where(r => r.RowState != DataRowState.Delete.ToString()), new RouteEqualityComparerById()).ToList();
            updatedRoutes = updatedRoutes.Except(publishedRoutes, new RouteEqualityComparerByChecksum()).ToList();

            // set the appropriate datarowstate values
            newRoutes.ForEach(r => r.RowState = DataRowState.Create.ToString());
            updatedRoutes.ForEach(r => r.RowState = DataRowState.Update.ToString());
            deletedRoutes.ForEach(r => r.RowState = DataRowState.Delete.ToString());
            resurrectedRoutes.ForEach(r => r.RowState = DataRowState.Resurrect.ToString());

            // write to diff table
            await diffStorage.RouteStore.Insert(newRoutes);
            await diffStorage.RouteStore.Insert(updatedRoutes);
            await diffStorage.RouteStore.Insert(deletedRoutes);
            await diffStorage.RouteStore.Insert(resurrectedRoutes);

            // write to metadata table
            DiffMetadataEntity metadata = new DiffMetadataEntity
            {
                RunId = runId,
                RegionId = regionId,
                AgencyId = agencyId,
                RecordType = RecordType.Route.ToString(),
                AddedCount = newRoutes.Count,
                UpdatedCount = updatedRoutes.Count,
                DeletedCount = deletedRoutes.Count,
                ResurrectedCount = resurrectedRoutes.Count
            };
            await diffMetadataStorage.DiffMetadataStore.Insert(new List<DiffMetadataEntity> { metadata });
        }