Skip to content

Commit

Permalink
GetInstalledArchiveNames to ContainssArchive
Browse files Browse the repository at this point in the history
  • Loading branch information
siblount committed Feb 11, 2024
1 parent cf7e997 commit 708b00f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 43 deletions.
20 changes: 6 additions & 14 deletions src/DAZ_Installer.Database/DPDatabase.Abstraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,30 +136,22 @@ private string[] GetTables(SqliteConnectionOpts opts)
/// <returns>A hashset containing successfully extracted archive file names.</returns>
// TODO: Deprecate this. As more products are installed, the hashset will grow and grow. This is not good.
// Just query the database to check if the archive exists.
private HashSet<string>? GetArchiveFileNameList(SqliteConnectionOpts opts)
private bool ArchiveNameExists(string arcName, SqliteConnectionOpts opts)
{
HashSet<string>? names = null!;
var getCmd = $@"SELECT ""ArcName"" FROM {ProductTable}";
var getCmd = $@"SELECT EXISTS(SELECT 1 FROM {ProductTable} WHERE ""ArcName"" = @A LIMIT 1);";
try
{
using var _connection = CreateAndOpenConnection(ref opts, true);
using var cmd = opts.CreateCommand(getCmd);
if (_connection == null) return names;

using var reader = cmd.ExecuteReader();
names = new HashSet<string>(reader.FieldCount);
while (reader.Read())
{
names.Add(reader.GetString(0));
}
// MainQueryCompleted?.Invoke();
ArchiveFileNames = names;
if (_connection == null) return false;
cmd.Parameters.Add(new SqliteParameter("@A", arcName));
return Convert.ToByte(cmd.ExecuteScalar()) == 1;
}
catch (Exception ex)
{
Logger.Error(ex, "Failed to get archive file name list");
}
return names;
return false;
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/DAZ_Installer.Database/DPDatabase.Public.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ public Task RemoveAllRecordsQ() => _mainTaskManager.AddToQueue((ct) =>
RemoveAllRecords(opts);
});

public async Task<HashSet<string>?> GetInstalledArchiveNamesQ(Action<HashSet<string>>? callback = null)
public async Task<bool?> ContainsArchive(string arcName, Action<bool?>? callback = null)
{
HashSet<string>? result = null;
bool? result = null;
await _priorityTaskManager.AddToQueue((t) =>
{
var opts = new SqliteConnectionOpts() { CancellationToken = t };
result = GetArchiveFileNameList(opts);
result = ArchiveNameExists(arcName, opts);
callback?.Invoke(result);
});
return result;
Expand Down
8 changes: 4 additions & 4 deletions src/DAZ_Installer.Database/IDPDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ public interface IDPDatabase
/// </summary>
Task RemoveAllRecordsQ();
/// <summary>
/// Updates the <c>ArchiveFileNames</c> variable and returns it via callback function.
/// It returns a unique set of installed archive names.
/// Queries the database for whether the archive name exists in the database.
/// </summary>
/// <param name="arcName">The archive file name to search for.</param>
/// <param name="callback">The function to return values to.</param>
/// <returns> The archive file names. </returns>
Task<HashSet<string>> GetInstalledArchiveNamesQ(Action<HashSet<string>>? callback = null);
/// <returns>Whether the database contains the archive name. Can be null if query failed.</returns>
Task<bool?> ContainsArchive(string arcName, Action<bool?>? callback = null);
}
}
8 changes: 3 additions & 5 deletions src/DAZ_Installer.DatabaseTests/DPDatabaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ record = record with { Authors = Array.Empty<string>() };
}

[TestMethod]
public void GetInstalledArchiveNamesQTest()
public void ContainsArchiveTest()
{
var record = new DPProductRecord("Test Product", new[] { "TheRealSolly" }, DateTime.UtcNow, "a.png", "arc.zip", "J:/",
new[] { "tag1", "tag2" }, new[] { "file1", "file2" }, 1);
Expand All @@ -372,10 +372,8 @@ public void GetInstalledArchiveNamesQTest()
record = record with { Authors = Array.Empty<string>() };
Database.AddNewRecordEntry(record with { ArcName = "arc2.zip" }).Wait();

var a = Database.GetInstalledArchiveNamesQ();
var result = a.Result.ToArray();
CollectionAssert.Contains(result, "arc.zip");
CollectionAssert.Contains(result, "arc2.zip");
Assert.IsTrue(Database.ContainsArchive("arc.zip").Result);
Assert.IsTrue(Database.ContainsArchive("arc2.zip").Result);
}

[TestMethod]
Expand Down
33 changes: 16 additions & 17 deletions src/DAZ_Installer.Windows/DP/DPExtractJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,22 @@ private void Processor_ArchiveExit(object sender, DPArchiveExitArgs e)

private void Processor_ArchiveEnter(object sender, DPArchiveEnterArgs e)
{
if (Program.Database.ArchiveFileNames.Contains(e.Archive.FileName))
switch (UserSettings.InstallPrevProducts)
{
switch (UserSettings.InstallPrevProducts)
{
case SettingOptions.No:
Processor.CancelCurrentArchive();
break;
case SettingOptions.Prompt:
DialogResult result = MessageBox.Show($"It seems that \"{e.Archive.FileName}\" was already processed. " +
$"Do you wish to continue processing this file?", "Archive already processed",
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.No) Processor.CancelCurrentArchive();
break;
}
case SettingOptions.No:
Processor.CancelCurrentArchive();
break;
case SettingOptions.Prompt:
CancellationTokenSource cts = new();
cts.CancelAfter(TimeSpan.FromSeconds(15));
var t = Task.Run(() => Program.Database.ContainsArchive(e.Archive.FileName), cts.Token);
progressCombo.SetText($"Checking if {e.Archive.FileName} was previously processed...");
if (t.Result != true) return;
DialogResult result = MessageBox.Show($"It seems that \"{e.Archive.FileName}\" was already processed. " +
$"Do you wish to continue processing this file?", "Archive already processed",
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.No) Processor.CancelCurrentArchive();
break;
}
}

Expand Down Expand Up @@ -198,9 +200,6 @@ private void ProcessListAsync(CancellationToken t)
$"{Path.GetFileName(x)}...({percentage}%)");
Processor.ProcessArchive(x, processSettings);
}

// Update the database after this run.
Program.Database.GetInstalledArchiveNamesQ();
} catch (Exception ex)
{
Logger.Error(ex, "An error occurred while attempting to process archive list");
Expand All @@ -227,7 +226,7 @@ public void RemoveSourceFile(string file)
var scopeSettings = new DPFileScopeSettings(new[] { file }, Array.Empty<string>(), false, true);
var fs = new DPFileSystem(scopeSettings);
var fi = fs.CreateFileInfo(file);
Exception? ex = null;
Exception? ex;
if (UserSettings.DeleteAction == RecycleOption.DeletePermanently)
fi.TryAndFixDelete(out ex);
else
Expand Down

0 comments on commit 708b00f

Please sign in to comment.