using System; using UAF; using UAF.Interfaces.Logging; namespace DataFileAnalyzer { /// /// Base class for the update operations. An update operation consists in updating /// a DataFile in some way. /// public abstract class UpdateOperation { /// /// The states of an update operation /// [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; } /// /// The path of the DataFile to update /// public string FilePath { get; set; } /// /// The normalized path of the DataFile to update /// public string NormalizedFilePath { get; set; } /// /// Constructor /// 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); } /// /// Set the "progress" part of the State /// protected void SetProgress(States _progress) { State &= ~(States.Pending | States.Running | States.WaitUpdate | States.Updating | States.Finished); State |= _progress; } /// /// Analyze the file to update it. Called from any thread. /// Do not update the library inside this function ! /// public abstract void Run(); /// /// Update the FileLibrary (called from the library thread) /// public abstract void UpdateLibrary(); /// /// Returns true if the otherOperation can be skipped because this one /// will do the work. /// public bool CanSkip(UpdateOperation _otherOperation) { return State == States.Pending && _otherOperation.GetType() == this.GetType() && NormalizedFilePath.Equals(_otherOperation.NormalizedFilePath); } } }