diff --git a/src/Chksum/Program.cs b/src/Chksum/Program.cs index adcaf54..8ce3406 100644 --- a/src/Chksum/Program.cs +++ b/src/Chksum/Program.cs @@ -26,6 +26,9 @@ public class Program { Console.ResetColor(); try { + if (args[1] == "MD5") { + utils.doTheThing(args[1]); + } int bufferSize = int.Parse(args[2]); utils.doTheThing(args[1], bufferSize); } @@ -66,7 +69,7 @@ public class Program { static void PrintAvailableOptions() { String[] options = { - "checksum - MD5, Murmur and XxHash", + "checksum - MD5, Murmur and XxHash - Default buffer size is 4096", "compareDatabases", "compareChecksums", "saveToSqlite", diff --git a/src/Chksum/chksum.cs b/src/Chksum/chksum.cs index 7212865..d024ec1 100644 --- a/src/Chksum/chksum.cs +++ b/src/Chksum/chksum.cs @@ -98,9 +98,19 @@ public class ChksumUtils { } } + private void UpdateProgressBar(int current, int total) { + int progress = (int)((double)current / total * 100); + string progressText = $"Progress: {progress}% [{current}/{total}]"; + + Console.Write("\r" + progressText.PadRight(Console.WindowWidth)); + } + private Dictionary CalculateChecksums(string[] filenames) { ConcurrentDictionary checksums = new ConcurrentDictionary(); + int totalFiles = filenames.Length; + int processedFiles = 0; + Parallel.ForEach(filenames, (filename, state) => { using (var md5 = MD5.Create()) { using (var stream = File.OpenRead(filename)) { @@ -111,6 +121,8 @@ public class ChksumUtils { checksums.TryAdd(filename, checksum); } } + Interlocked.Increment(ref processedFiles); + UpdateProgressBar(processedFiles, totalFiles); } }); @@ -120,12 +132,17 @@ public class ChksumUtils { private Dictionary CalculateChecksumsWithMurmur(string[] filenames, int userDefinedBufferSize) { ConcurrentDictionary checksums = new ConcurrentDictionary(); + int totalFiles = filenames.Length; + int processedFiles = 0; + Parallel.ForEach(filenames, (filename, state) => { using (var stream = File.OpenRead(filename)) { var hash = CalculateMurmurHash32(stream, userDefinedBufferSize); lock (checksums) { checksums.TryAdd(filename, hash); } + Interlocked.Increment(ref processedFiles); + UpdateProgressBar(processedFiles, totalFiles); } }); @@ -151,11 +168,16 @@ public class ChksumUtils { private Dictionary CalculateChecksumsWithXxHash3(string[] filenames, int userDefinedBufferSize) { ConcurrentDictionary checksums = new ConcurrentDictionary(); + int totalFiles = filenames.Length; + int processedFiles = 0; + Parallel.ForEach(filenames, (filename, state) => { using (var stream = File.OpenRead(filename)) { var hash = CalculateXxHash3(stream, userDefinedBufferSize); checksums.TryAdd(filename, hash); } + Interlocked.Increment(ref processedFiles); + UpdateProgressBar(processedFiles, totalFiles); }); return new Dictionary(checksums); @@ -177,8 +199,7 @@ public class ChksumUtils { return hash; } - - public void doTheThing(string hashalgo, int bufferSize) { + public void doTheThing(string hashalgo, int bufferSize = 4096) { ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost"); IDatabase db = redis.GetDatabase(); @@ -372,9 +393,7 @@ public class ChksumUtils { deleteCommand.Parameters.AddWithValue("$pathtofile", pathToFile); deleteCommand.ExecuteNonQuery(); - //Console.WriteLine("File deleted:"); - //Console.WriteLine($"\t{pathToFile}\n"); - logger.Information("File deleted: {pathToFile}", pathToFile); + logger.Information("File deleted:\n\t{pathToFile}", pathToFile); } } logger.Information("All deleted files were successfully removed from the database");