Example #1
0
        private DiscrepancyType InitialMatch(TableData[] datas, DiscrepancyOutputter outputter)
        {
            string query = BuildOnlyMatchableRowsQuiery();
            MatchedRecordComparer comparer = new MatchedRecordComparer(data, settings, outputter);
            Recordset             matches  = db.QueryResult(query);
            DiscrepancyType       result   = DiscrepancyType.NONE;

            while (matches.Read())
            {
                string[] matchedRecord    = new string[matches.GetFieldCount() - HASH_FIELDS_COUNT];
                int      nextIndexToWrite = 0;
                for (int i = 0; i < matches.GetFieldCount(); i++)
                {
                    // Skip hash fields.
                    if (i == HASH_FIELD_INDEX || i == datas[0].columnCount + 2)
                    {
                        continue;
                    }

                    matchedRecord[nextIndexToWrite++] = matches.GetString(i);
                }

                string[] first, second;
                ExtractRecords(matchedRecord, datas[0].columnCount + 1, datas[1].columnCount + 1, out first, out second);
                DiscrepancyType type = comparer.Compare(first, second);

                if (type > result)
                {
                    result = type;
                }
            }
            matches.Close();

            return(result);
        }
        public string[][] Match(TableData[] datas, MatchedRecordComparer comparer, DiscrepancyOutputter outputter)
        {
            List <string[]> result = new List <string[]>();

            // Extract nonmatching records.
            Dictionary <string, List <string[]> > firstRecords;
            Dictionary <string, List <string[]> > secondRecords;

            ExtractRecords(out firstRecords, out secondRecords);

            if (firstRecords.Count != 0 || secondRecords.Count != 0)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Warning: Not enough key columns. Experimental algorithm is turned on.");

                Console.WriteLine("First records: ");
                foreach (KeyValuePair <string, List <string[]> > pair in firstRecords)
                {
                    Console.WriteLine("Hash:");
                    foreach (string[] record in pair.Value)
                    {
                        for (int i = 0; i < record.Length; i++)
                        {
                            Console.Write(record[i] + ", ");
                        }
                        Console.WriteLine();
                    }
                }

                Console.WriteLine("Second records: ");
                foreach (KeyValuePair <string, List <string[]> > pair in secondRecords)
                {
                    Console.WriteLine("Hash:");
                    foreach (string[] record in pair.Value)
                    {
                        for (int i = 0; i < record.Length; i++)
                        {
                            Console.Write(record[i] + ", ");
                        }
                        Console.WriteLine();
                    }
                }

                Console.ResetColor();
            }

            foreach (KeyValuePair <string, List <string[]> > pairOne in firstRecords)
            {
                foreach (KeyValuePair <string, List <string[]> > pairTwo in secondRecords)
                {
                    // Comparing records with same hash at the moment.
                    if (pairOne.Key.Equals(pairTwo.Key))
                    {
                        MatchRecords(pairOne.Value.ToArray(), pairTwo.Value.ToArray(), comparer, outputter);
                    }
                }
            }

            return(result.ToArray());
        }
        private void MatchRecords(string[][] recordSetOne, string[][] recordSetTwo, MatchedRecordComparer comparer, DiscrepancyOutputter outputter)
        {
            HashSet <string> checkedIndices = new HashSet <string>();

            for (int i = 0; i < recordSetOne.Length; i++)
            {
                string[] recordOne = recordSetOne[i];

                string[] bestMatch     = null;
                double   smallestDelta = double.MaxValue;
                for (int j = 0; j < recordSetTwo.Length; j++)
                {
                    string[] recordTwo = recordSetTwo[j];
                    if (checkedIndices.Contains(recordTwo[1]))
                    {
                        continue;
                    }

                    double delta = CalculateGreatestDelta(recordOne, recordTwo);
                    if (delta < smallestDelta)
                    {
                        bestMatch     = recordTwo;
                        smallestDelta = delta;
                    }
                }


                if (bestMatch != null)
                {
                    checkedIndices.Add(bestMatch[1]);

                    string[] recordOneNoHash = RemoveHashFromRecord(recordOne);
                    string[] recordTwoNoHash = RemoveHashFromRecord(bestMatch);

                    int recordColumnCount = Math.Min(recordOneNoHash.Length, recordTwoNoHash.Length);

                    string[] newOne = new string[recordColumnCount];
                    string[] newTwo = new string[recordColumnCount];

                    Array.Copy(recordOneNoHash, newOne, recordColumnCount);
                    Array.Copy(recordTwoNoHash, newTwo, recordColumnCount);

                    comparer.Compare(newOne, newTwo);
                }
            }

            // Print missing.
            for (int i = 0; i < recordSetTwo.Length; i++)
            {
                if (!checkedIndices.Contains(recordSetTwo[i][1]))
                {
                    outputter.MissingRecord(RemoveHashFromRecord(recordSetTwo[i]), 0);
                }
            }

            checkedIndices.Clear();
        }
Example #4
0
        private DiscrepancyType AdditionalMatch(TableData[] datas, DiscrepancyOutputter outputter)
        {
            DiscrepancyType result = DiscrepancyType.NONE;

            MatchedRecordComparer          comparer = new MatchedRecordComparer(data, settings, outputter);
            AdditionalSQLHashRecordMatcher matcher  = new AdditionalSQLHashRecordMatcher(srcTables, data, db);

            matcher.Match(datas, comparer, outputter);

            return(result);
        }