Add progress bar

This commit is contained in:
ProfessionalUwU 2023-07-03 23:31:33 +02:00
parent 02a0bddd7e
commit 5348a23f58
Signed by: ProfessionalUwU
GPG Key ID: 9F28CB1645C4BFB5
2 changed files with 26 additions and 4 deletions

View File

@ -26,6 +26,9 @@ public class Program {
Console.ResetColor();
try {
if (args[1] == "MD5") {
}
int bufferSize = int.Parse(args[2]);
utils.doTheThing(args[1], bufferSize);
}

View File

@ -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<string, string> CalculateChecksums(string[] filenames) {
ConcurrentDictionary<string, string> checksums = new ConcurrentDictionary<string, string>();
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<string, uint> CalculateChecksumsWithMurmur(string[] filenames, int userDefinedBufferSize) {
ConcurrentDictionary<string, uint> checksums = new ConcurrentDictionary<string, uint>();
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<string, ulong> CalculateChecksumsWithXxHash3(string[] filenames, int userDefinedBufferSize) {
ConcurrentDictionary<string, ulong> checksums = new ConcurrentDictionary<string, ulong>();
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<string, ulong>(checksums);
@ -177,7 +199,6 @@ public class ChksumUtils {
return hash;
}
public void doTheThing(string hashalgo, int bufferSize) {
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
@ -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");