Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Message" company="Simon Walker">
// Simon Walker
// </copyright>
// <summary>
// The message.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace SiteFrameworkMessageComparer
{
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
/// <summary>
/// The message.
/// </summary>
public class Message
{
/// <summary>
/// The Language.
/// </summary>
public readonly string Language;
/// <summary>
/// The MessageCode.
/// </summary>
public readonly string MessageCode;
/// <summary>
/// The MessageContent.
/// </summary>
public readonly string MessageContent;
/// <summary>
/// Initializes a new instance of the <see cref="Message"/> class.
/// </summary>
/// <param name="messageCode">
/// The MessageCode.
/// </param>
/// <param name="language">
/// The Language.
/// </param>
/// <param name="messageContent">
/// The MessageContent.
/// </param>
public Message(string messageCode, string language, string messageContent)
{
this.MessageCode = messageCode;
this.Language = language;
this.MessageContent = messageContent;
}
/// <summary>
/// The get hash MessageCode.
/// </summary>
/// <returns>
/// The <see cref="int"/>.
/// </returns>
public override int GetHashCode()
{
unchecked
{
int hashCode = this.Language != null ? this.Language.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ (this.MessageCode != null ? this.MessageCode.GetHashCode() : 0);
return hashCode;
}
}
/// <summary>
/// The equals.
/// </summary>
/// <param name="otherObject">
/// The otherObject.
/// </param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public override bool Equals(object otherObject)
{
if (ReferenceEquals(null, otherObject))
{
return false;
}
if (ReferenceEquals(this, otherObject))
{
return true;
}
if (otherObject.GetType() != this.GetType())
{
return false;
}
return this.Equals((Message)otherObject);
}
/// <summary>
/// The to string.
/// </summary>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
public override string ToString()
{
return "<" + this.Language + ":" + this.MessageCode + "> = " + this.MessageContent;
}
/// <summary>
/// The equals.
/// </summary>
/// <param name="other">
/// The other.
/// </param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
protected bool Equals(Message other)
{
return string.Equals(this.Language, other.Language) && string.Equals(this.MessageCode, other.MessageCode);
}
}
/// <summary>
/// The program.
/// </summary>
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass", Justification = "Reviewed. Suppression is OK here.")]
public class Program
{
/// <summary>
/// The main.
/// </summary>
private static void Main()
{
var cluster =
new MySqlConnection(
"server=dbmaster;User Id=root;password=;database=hwumc_new_devel");
var localhost = new MySqlConnection("server=localhost;User Id=root;password=;database=hwumc_devel");
cluster.Open();
localhost.Open();
const string SelectFromMessage = "SELECT * FROM message;";
var cc = new MySqlCommand(SelectFromMessage, cluster);
var cl = new MySqlCommand(SelectFromMessage, localhost);
var cctr = GetData(cc);
var cltr = GetData(cl);
Task.WaitAll(cctr, cltr);
IEnumerable<Message> clusterResults = cctr.Result.ToList();
IEnumerable<Message> localResults = cltr.Result.ToList();
var output =
clusterResults.Intersect(localResults)
.Where(x => localResults.First(x.Equals).MessageContent != x.MessageContent)
.Aggregate(
localResults.Except(clusterResults)
.Select(s => s.ToString())
.Aggregate(
clusterResults.Except(localResults)
.Select(s => s.ToString())
.Aggregate(
"Cluster only:" + Environment.NewLine,
(current, s) => current + Environment.NewLine + s)
+ Environment.NewLine + Environment.NewLine + "Local only:"
+ Environment.NewLine,
(current, s) => current + Environment.NewLine + s)
+ Environment.NewLine + Environment.NewLine + "Different:" + Environment.NewLine,
(current, message) =>
current
+ (Environment.NewLine + message + Environment.NewLine
+ localResults.First(message.Equals) + Environment.NewLine));
var sw = new StreamWriter("hwumc.resolve.txt");
sw.Write(output);
sw.Flush();
sw.Close();
}
/// <summary>
/// The get data.
/// </summary>
/// <param name="command">
/// The command.
/// </param>
/// <returns>
/// The <see cref="Task"/>.
/// </returns>
private static async Task<IEnumerable<Message>> GetData(DbCommand command)
{
var reader = await command.ExecuteReaderAsync();
return ReallyGetData(reader);
}
/// <summary>
/// The really get data.
/// </summary>
/// <param name="reader">
/// The reader.
/// </param>
/// <returns>
/// The messages.
/// </returns>
private static IEnumerable<Message> ReallyGetData(IDataReader reader)
{
while (reader.Read())
{
var vals = new object[4];
reader.GetValues(vals);
yield return
new Message(vals[1].ToString(), vals[2].ToString(), Encoding.UTF8.GetString((byte[])vals[3]));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment