Skip to content

Commit

Permalink
Implemented the DatFileDeletor as a post processing step
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidNeuroth committed Oct 14, 2021
1 parent 087a014 commit 3c98e1c
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 164 deletions.
2 changes: 1 addition & 1 deletion Automation/ResultFiles/LoadTypeInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public LoadTypeInformation([JetBrains.Annotations.NotNull] string name, [JetBrai
public string? FileName { get; set; }

[JetBrains.Annotations.NotNull]
public StrGuid? Guid { get; }
public StrGuid? Guid { get; set; }

// needed for xml deserialize
[UsedImplicitly]
Expand Down
45 changes: 38 additions & 7 deletions CalcPostProcessor/GeneralSteps/DatFileDeletor.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,64 @@
using System.Collections.Generic;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Automation;
using Automation.ResultFiles;
using CalcPostProcessor.Steps;
using Common;
using Common.SQLResultLogging;
using Common.SQLResultLogging.InputLoggers;

namespace CalcPostProcessor.GeneralSteps
{
/// <summary>
/// A postprocessing step that deletes all .dat files created during the calculation
/// </summary>
[SuppressMessage("ReSharper", "RedundantNameQualifier")]
public class DatFileDeletor: GeneralStepBase
public class DatFileDeletor : GeneralStepBase
{
[JetBrains.Annotations.NotNull]
private readonly IFileFactoryAndTracker _fft;
private readonly SqlResultLoggingService _srls;

public DatFileDeletor([JetBrains.Annotations.NotNull] CalcDataRepository repository, [JetBrains.Annotations.NotNull] ICalculationProfiler calculationProfiler,
[JetBrains.Annotations.NotNull] IFileFactoryAndTracker fft)
[JetBrains.Annotations.NotNull] SqlResultLoggingService srls)
: base(repository, AutomationUtili.GetOptionList(CalcOption.HouseholdContents), calculationProfiler, "Delete .dat files", 0)
{
_fft = fft;
_srls = srls;
}

/// <summary>
/// Performs the postprocessing step by calling the method to delete .dat files
/// </summary>
/// <param name="parameters"></param>
protected override void PerformActualStep(IStepParameters parameters)
{
// insert deleting function here
DeleteDatFiles();
}

/// <summary>
/// Deletes all .dat files if the CalcOption DeleteDatFiles is set
/// </summary>
private void DeleteDatFiles()
{
if (Repository.CalcParameters.IsSet(CalcOption.DeleteDatFiles))
{
// load a list of all created files from the database
ResultFileEntryLogger rfel = new ResultFileEntryLogger(_srls);
var resultFileEntries = rfel.Load();
// filter all result files ending with .dat
var datFileEntries = resultFileEntries.Where(f => f.FileName.ToUpperInvariant().EndsWith(".DAT", StringComparison.Ordinal));
foreach (var datFile in datFileEntries)
{
File.Delete(datFile.FullFileName);
// remove file entry from the result file list in the database
rfel.DeleteEntry(datFile);
}
}
}

[JetBrains.Annotations.NotNull]
public override List<CalcOption> NeededOptions => new List<CalcOption>() {CalcOption.DeleteDatFiles};
public override List<CalcOption> NeededOptions => new List<CalcOption>() { CalcOption.DeleteDatFiles };
}
}
156 changes: 0 additions & 156 deletions CalculationController/Queue/DatFileDeletor.cs

This file was deleted.

16 changes: 16 additions & 0 deletions Common/SQLResultLogging/InputLoggers/ResultFileEntryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ public override void Run(HouseholdKey key, object o)
}
}

/// <summary>
/// Deletes an entry from the result file list in the database
/// </summary>
/// <param name="rfe">The entry to delete</param>
public void DeleteEntry(ResultFileEntry rfe)
{
if (!_isTableCreated)
{
throw new LPGException("Tried to delete entries from a table that did not exist: " + TableName);
}
// create a row as the one already in the database that should be deleted
var row = RowBuilder.Start("Name", Constants.GeneralHouseholdKey)
.Add("Json", JsonConvert.SerializeObject(rfe, Formatting.Indented)).ToDictionary();
Srls.DeleteEntry(row, TableName, Constants.GeneralHouseholdKey);
}

[ItemNotNull]
[NotNull]
public List<ResultFileEntry> Load()
Expand Down
65 changes: 65 additions & 0 deletions Common/SQLResultLogging/SQLResultLoggingService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SQLite;
using System.Diagnostics.CodeAnalysis;
using System.IO;
Expand Down Expand Up @@ -491,6 +492,11 @@ private bool IgnoreThisField([JetBrains.Annotations.NotNull] string fieldname)
return false;
}*/

/// <summary>
/// Returns whether a matching table for the entry exists
/// </summary>
/// <param name="entry">The entry for which the table is intended</param>
/// <returns>True if a matching table exists, else false</returns>
private bool IsTableCreated([JetBrains.Annotations.NotNull] SaveableEntry entry)
{
if (!_createdTablesPerHousehold.ContainsKey(entry.HouseholdKey)) {
Expand Down Expand Up @@ -539,6 +545,65 @@ private void LoadFileNameDict()
_isFileNameDictLoaded = true;
}

/// <summary>
/// Deletes an entry from a database table
/// </summary>
/// <param name="entry">A dictionary containing field values of the entry to delete.</param>
/// <param name="tableName">The name of the table to delete from</param>
/// <param name="householdKey">The HouseholdKey matching the entry</param>
public void DeleteEntry(Dictionary<string, object> entry, [JetBrains.Annotations.NotNull] string tableName, HouseholdKey householdKey)
{
DeleteEntries(new List<Dictionary<string, object>> { entry }, tableName, householdKey);
}

/// <summary>
/// Deletes a list of entries from a database table
/// </summary>
/// <param name="entries">A list of dictionaries, one for each entry to delete. Each dictionary contains field values of the entry to delete.</param>
/// <param name="tableName">The name of the table to delete entries from</param>
/// <param name="householdKey">The HouseholdKey matching the entries</param>
public void DeleteEntries([JetBrains.Annotations.NotNull][ItemNotNull] List<Dictionary<string, object>> entries,
[JetBrains.Annotations.NotNull] string tableName, HouseholdKey householdKey)
{
if (entries.Count == 0)
{
// nothing to do
return;
}

// open the SQLite database connection
string dstFileName = GetFilenameForHouseholdKey(householdKey);
using SQLiteConnection conn = new SQLiteConnection("Data Source=" + dstFileName + ";Version=3");
conn.Open();

// prepare the sql command without the specific conditions
string sqlBase = "DELETE FROM " + tableName + " WHERE ";
using (var transaction = conn.BeginTransaction())
{
using (var command = conn.CreateCommand())
{
foreach (Dictionary<string, object> row in entries)
{
// get an enumerable of "field=@field" strings and concatenate them with AND in between
var conditions = row.Select(pair => pair.Key + " = @" + pair.Key);
string conditionString = string.Join(" AND ", conditions);
// combine base and conditions to full command
command.CommandText = sqlBase + conditionString;
// add all parameter values
command.Parameters.Clear();
foreach (KeyValuePair<string, object> pair in row)
{
string parameter = "@" + pair.Key;
command.Parameters.AddWithValue(parameter, pair.Value);
}
command.ExecuteNonQuery();
}
}
transaction.Commit();
}
conn.Close();
}

[JetBrains.Annotations.NotNull]
private static string MakeconnectionString([JetBrains.Annotations.NotNull] string filename) =>
"Data Source=" + filename + ";Version=3;Synchronous=OFF;Journal Mode=WAL;";
Expand Down

0 comments on commit 3c98e1c

Please sign in to comment.