示例#1
0
        private static SnapshotTableDifferences GetTableDifferences(TableDefinition tableDefinition, Snapshot before, Snapshot after)
        {
            var beforeKeys = SnapshotKeyExtractor.GetKeys(before, tableDefinition)?.Keys ?? new Dictionary <SnapshotRowKey, SnapshotRow>().Keys;
            var afterKeys  = SnapshotKeyExtractor.GetKeys(after, tableDefinition)?.Keys ?? new Dictionary <SnapshotRowKey, SnapshotRow>().Keys;

            var deletedKeys  = beforeKeys.Except(afterKeys).ToList();
            var insertedKeys = afterKeys.Except(beforeKeys).ToList();

            var differences = beforeKeys.Intersect(afterKeys)
                              .Select(k => new { Key = k, Before = before.GetRow(k, tableDefinition.TableName), After = after.GetRow(k, tableDefinition.TableName) })
                              .Select(rr => new { rr.Key, Differences = RowDataComparer.Compare(tableDefinition, rr.Key, rr.Before, rr.After) })
                              .Where(m => !m.Differences.Matched)
                              .ToList();

            if (!deletedKeys.Any() && !insertedKeys.Any() && !differences.Any())
            {
                return(null);
            }

            var rowDiffs = deletedKeys.Select(k => new RowDifference(k, DifferenceType.Deleted, before.GetRow(k, tableDefinition.TableName), null))
                           .Concat(insertedKeys.Select(k => new RowDifference(k, DifferenceType.Inserted, null, after.GetRow(k, tableDefinition.TableName))))
                           .Concat(differences.Select(d => new RowDifference(d.Key, d.Differences, DifferenceType.Updated)))
                           .OrderBy(d => d.Key)
                           .ToList();

            return(new SnapshotTableDifferences(rowDiffs, tableDefinition));
        }
示例#2
0
        public static bool Match(SnapshotCollection collection, Snapshot before, Snapshot after)
        {
            foreach (var tableDefinition in collection.TablesInDefinitionOrder.Where(t => !t.ExcludeFromComparison))
            {
                //find all keys
                var beforeRows = SnapshotKeyExtractor.GetKeys(before, tableDefinition) ?? new Dictionary <SnapshotRowKey, SnapshotRow>();
                var afterRows  = SnapshotKeyExtractor.GetKeys(after, tableDefinition) ?? new Dictionary <SnapshotRowKey, SnapshotRow>();
                if (!beforeRows.Keys.SequenceEqual(afterRows.Keys))
                {
                    return(false); //snapshots have row difference
                }
                foreach (var snapshotRowKey in beforeRows.Keys)
                {
                    var beforeRow = beforeRows[snapshotRowKey];
                    var afterRow  = afterRows[snapshotRowKey];
                    var match     = RowDataComparer.Compare(tableDefinition, snapshotRowKey, beforeRow, afterRow);
                    if (!match.Matched)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }