From cd77fccfcb7a8e08d2e528464070d39eaf3bc7a8 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 2 Sep 2016 11:47:07 -0700 Subject: [PATCH] [RombaSharp] Rewrite some RefreshDatabase code --- RombaSharp/RombaSharp.cs | 69 ++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/RombaSharp/RombaSharp.cs b/RombaSharp/RombaSharp.cs index 1546ac30..19f1baec 100644 --- a/RombaSharp/RombaSharp.cs +++ b/RombaSharp/RombaSharp.cs @@ -284,15 +284,6 @@ namespace SabreTools /// /// Populate or refresh the database information /// - /// - /// Even though I already wrote a bunch of code here, I think this needs improvement - /// The process for figuring out what needs updating should be easy: - /// - Get a list of DAT hashes from the database - /// - Get a list of DAT hashes from the folder - /// - Figure out what DATs are new and which are no longer around - /// - Remove references in the keyvault to the no-longer-arounds - /// - Add only new DATs - /// private void RefreshDatabase() { // Make sure the db is set @@ -320,41 +311,51 @@ namespace SabreTools Directory.CreateDirectory(_dats); } - // Now create a new structure to replace the database - Dictionary> keyvault = new Dictionary>(); + // Create a List of dat hashes in the database + List databaseDats = new List(); - // Now parse the directory into an internal structure - int i = 0; // Dat number - foreach (string file in Directory.EnumerateFiles(_dats, "*", SearchOption.AllDirectories)) + // Populate the List from the database + string query = "SELECT UNIQUE value FROM data WHERE key=\"dat\""; + using (SqliteConnection dbc = new SqliteConnection(_connectionString)) { - Dat datdata = new Dat(); - datdata = DatTools.Parse(file, i, i, datdata, _logger); - Rom romdata = FileTools.GetSingleFileInfo(file); - - // Loop through the entire DAT and add to the structure - foreach (List roms in datdata.Files.Values) + using (SqliteCommand slc = new SqliteCommand(query, dbc)) { - List newroms = RomTools.Merge(roms, _logger); - foreach (Rom rom in roms) + using (SqliteDataReader sldr = slc.ExecuteReader()) { - if (keyvault.ContainsKey(rom.HashData)) + string hash = sldr.GetString(0); + if (!databaseDats.Contains(hash)) { - keyvault[rom.HashData].Add(romdata.HashData.SHA1); - } - else - { - List temp = new List(); - temp.Add(romdata.HashData.SHA1); - keyvault.Add(rom.HashData, temp); + databaseDats.Add(hash); } } } - - // Increment the DAT number - i++; } - // Now that we have the structure, we can create a new database + // Now create a Dictionary of dats to parse from what's not in the database + Dictionary toscan = new Dictionary(); + + // Loop through the datroot and add only needed files + foreach (string file in Directory.EnumerateFiles(_dats, "*", SearchOption.AllDirectories)) + { + Rom dat = FileTools.GetSingleFileInfo(file); + + // If the Dat isn't in the database and isn't already accounted for in the DatRoot, add it + if (!databaseDats.Contains(dat.HashData.SHA1) && !toscan.ContainsKey(dat.HashData.SHA1)) + { + toscan.Add(dat.HashData.SHA1, Path.GetFullPath(file)); + } + + // If the Dat is in the database already, remove it to find stragglers + else if (databaseDats.Contains(dat.HashData.SHA1)) + { + databaseDats.Remove(dat.HashData.SHA1); + } + } + + // At this point, we have a Dictionary of Dats that need to be added and a list of Dats that need to be removed + // I think for the sake of ease, adding the new Dats and then removing the old ones makes the most sense + // That way if any hashes are duplicates, they don't get readded. Removing is going to be a bit difficult because + // It means that "if there's no other dat associated with this hash" it's removed. That's hard } } }