JD2022-TU1/main/tools/apps/DataFileAnalyzer/FileLibrary/UpdateOperation.cs

108 lines
3.1 KiB
C#

using System;
using UAF;
using UAF.Interfaces.Logging;
namespace DataFileAnalyzer
{
/// <summary>
/// Base class for the update operations. An update operation consists in updating
/// a DataFile in some way.
/// </summary>
public abstract class UpdateOperation
{
/// <summary>
/// The states of an update operation
/// </summary>
[Flags]
public enum States
{
None = 0,
// Waiting to be launched
Pending = 1 << 0,
// Running
Running = 1 << 1,
// Waiting for update
WaitUpdate = 1 << 2,
// Updating the FileLibrary
Updating = 1 << 3,
// Finished
Finished = 1 << 4,
// Was skipped because the file has already been updated
Skipped = 1 << 5,
// Finished with errors
Error = 1 << 6,
}
protected FileUpdater m_updater;
protected FileLibrary m_library;
protected ILog m_logger;
public static uint InvalidID = uint.MaxValue;
private static uint m_nextId = 0;
public uint ID { get; protected set; }
public States State { get; protected set; }
/// <summary>
/// The path of the DataFile to update
/// </summary>
public string FilePath { get; set; }
/// <summary>
/// The normalized path of the DataFile to update
/// </summary>
public string NormalizedFilePath { get; set; }
/// <summary>
/// Constructor
/// </summary>
public UpdateOperation(FileUpdater _updater, string _filePath)
{
m_updater = _updater;
m_library = _updater.Library;
m_logger = _updater.Logger;
ID = m_nextId;
++m_nextId;
if (m_nextId == InvalidID)
m_nextId = 0;
State = States.Pending;
FilePath = _filePath;
NormalizedFilePath = PathUtils.Normalize(_filePath);
}
/// <summary>
/// Set the "progress" part of the State
/// </summary>
protected void SetProgress(States _progress)
{
State &= ~(States.Pending | States.Running | States.WaitUpdate | States.Updating | States.Finished);
State |= _progress;
}
/// <summary>
/// Analyze the file to update it. Called from any thread.
/// Do not update the library inside this function !
/// </summary>
public abstract void Run();
/// <summary>
/// Update the FileLibrary (called from the library thread)
/// </summary>
public abstract void UpdateLibrary();
/// <summary>
/// Returns true if the otherOperation can be skipped because this one
/// will do the work.
/// </summary>
public bool CanSkip(UpdateOperation _otherOperation)
{
return
State == States.Pending &&
_otherOperation.GetType() == this.GetType() &&
NormalizedFilePath.Equals(_otherOperation.NormalizedFilePath);
}
}
}