From ca6c75be2354c9bc78f20637f21e0a1674d9af38 Mon Sep 17 00:00:00 2001 From: Simon G Date: Tue, 10 Nov 2020 15:25:57 +0100 Subject: [PATCH] #25: file handler now only handles files --- .../Exceptions/FileNotExistingException.cs | 17 +++ GBase/FileHandling/FileHandler.cs | 108 ++++++------------ GBase/FileHandling/GBaseFile.cs | 32 ++++++ GBase/Interfaces/FileHandling/IFileHandler.cs | 48 +------- GBase/Interfaces/FileHandling/IGBaseFile.cs | 19 +++ 5 files changed, 104 insertions(+), 120 deletions(-) create mode 100644 GBase/FileHandling/Exceptions/FileNotExistingException.cs create mode 100644 GBase/FileHandling/GBaseFile.cs create mode 100644 GBase/Interfaces/FileHandling/IGBaseFile.cs diff --git a/GBase/FileHandling/Exceptions/FileNotExistingException.cs b/GBase/FileHandling/Exceptions/FileNotExistingException.cs new file mode 100644 index 0000000..2981ec1 --- /dev/null +++ b/GBase/FileHandling/Exceptions/FileNotExistingException.cs @@ -0,0 +1,17 @@ +// Author: Gockner, Simon +// Created: 2020-11-10 +// Copyright(c) 2020 SimonG. All Rights Reserved. + +using System; + +namespace GBase.FileHandling.Exceptions +{ + public class FileNotExistingException : Exception + { + public FileNotExistingException() + : base($"File for the given type {typeof(T)} is not existing.") + { + + } + } +} \ No newline at end of file diff --git a/GBase/FileHandling/FileHandler.cs b/GBase/FileHandling/FileHandler.cs index e4a708b..0e330ec 100644 --- a/GBase/FileHandling/FileHandler.cs +++ b/GBase/FileHandling/FileHandler.cs @@ -2,15 +2,13 @@ // Created: 2020-02-12 // Copyright(c) 2020 SimonG. All Rights Reserved. -using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; +using GBase.FileHandling.Exceptions; using GBase.Interfaces; -using GBase.Interfaces.DataHandling; -using GBase.Interfaces.DataHandling.Pool; using GBase.Interfaces.FileHandling; namespace GBase.FileHandling @@ -18,26 +16,22 @@ namespace GBase.FileHandling /// /// Internal file handler /// - public class FileHandler : IFileHandler //TODO: Don't let file handler call data handler, have them parallel and have file handler only handle the files. this also makes more sense with the dataHandlerPool + public class FileHandler : IFileHandler { /// /// The file extension for all GBase tables /// public const string GBASE_TABLE_FILE_EXTENSION = "gb"; - private readonly IDataHandlerPool _dataHandlerPool; + private readonly List _files; private string _path; - - private readonly List<(object entry, FileStream file, string filePath, bool inUse)> _files; /// /// Internal file handler /// - /// The - public FileHandler(IDataHandlerPool dataHandlerPool) + public FileHandler() { - _dataHandlerPool = dataHandlerPool; - _files = new List<(object entry, FileStream file, string filePath, bool inUse)>(); + _files = new List(); } /// @@ -52,7 +46,7 @@ namespace GBase.FileHandling return true; //TODO: is there anything that needs to be initialized here? } - public async Task AddEntry(T entry, IGBaseTable table, CancellationToken cancellationToken) + public IGBaseFile CreateEntryFile(T entry, IGBaseTable table) { string directoryPath = Path.Combine(_path, table.FolderName); @@ -63,86 +57,50 @@ namespace GBase.FileHandling string filePath = $"{Path.Combine(directoryPath, entry.ToString())}.{GBASE_TABLE_FILE_EXTENSION}"; FileStream entryFile = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); //TODO: Stream has to be disposed - IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); - await dataHandler.AddEntry(entry, table, entryFile, cancellationToken); - - _files.Add((entry, entryFile, filePath, false)); + IGBaseFile file = new GBaseFile(entry, entryFile, filePath); + _files.Add(file); + + return file.UseFile(); } - public async Task RemoveEntry(T entry) + public async Task DeleteEntryFile(T entry) { - var file = _files.FirstOrDefault(f => f.entry.Equals(entry)); + IGBaseFile file = _files.FirstOrDefault(f => f.Entry.Equals(entry)); if (file == default) return false; - await file.file.DisposeAsync(); - File.Delete(file.filePath); + await file.File.DisposeAsync(); + File.Delete(file.FilePath); return true; } - /// - /// Set the value for the given property - /// - /// The of the property - /// The of the property - /// - /// The name of the property - /// The value to set - /// - /// A to await - public async Task SetValue(T entry, string propertyName, TProperty value, CancellationToken cancellationToken) + public async Task RequestEntryFile(T entry) { - IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); - await dataHandler.SetValue(TODO, propertyName, value, cancellationToken); - } - - /// - /// Remove the value for the given property - /// - /// The of the property - /// The of the property - /// The name of the property - /// The value to set - /// - /// A to await - public async Task RemoveValue(string propertyName, TProperty value, CancellationToken cancellationToken) - { - IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); - await dataHandler.RemoveValue(TODO, propertyName, value, cancellationToken); - } + IGBaseFile file = _files.FirstOrDefault(f => f.Entry.Equals(entry)); + if (file == default) + throw new FileNotExistingException(); - /// - /// Get the value for the given property, if multiple values are set the first is returned - /// - /// The of the property - /// The of the property - /// The name of the property - /// - /// The value for the given property - public async Task GetValue(string propertyName, CancellationToken cancellationToken) - { - IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); - return await dataHandler.GetValue(propertyName); - } + if (file.InUse) + await Task.Delay(1000); - /// - /// Get all the values that are set for the given property - /// - /// The of the property - /// The of the property - /// The name of the property - /// - /// An with all the values for the property - public async Task> GetValues(string propertyName, CancellationToken cancellationToken) - { - IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); - return await dataHandler.GetValues(propertyName); + return file.UseFile(); } /// /// Dispose used resources asynchronously /// /// A to await - public async ValueTask DisposeAsync() => await _dataHandlerPool.DisposeAsync(); + public async ValueTask DisposeAsync() + { + while (_files.Any(f => f.InUse)) + { + await Task.Delay(1000); + } + + foreach (var file in _files) + { + await file.File.DisposeAsync(); + } + } } } \ No newline at end of file diff --git a/GBase/FileHandling/GBaseFile.cs b/GBase/FileHandling/GBaseFile.cs new file mode 100644 index 0000000..aaf0f56 --- /dev/null +++ b/GBase/FileHandling/GBaseFile.cs @@ -0,0 +1,32 @@ +// Author: Gockner, Simon +// Created: 2020-11-10 +// Copyright(c) 2020 SimonG. All Rights Reserved. + +using System.IO; +using GBase.Interfaces.FileHandling; + +namespace GBase.FileHandling +{ + public class GBaseFile : IGBaseFile + { + public GBaseFile(object entry, FileStream file, string filePath) + { + Entry = entry; + File = file; + FilePath = filePath; + } + + public object Entry { get; } + public FileStream File { get; } + public string FilePath { get; } + public bool InUse { get; private set; } + + public IGBaseFile UseFile() + { + InUse = true; + return this; + } + + public void Dispose() => InUse = false; + } +} \ No newline at end of file diff --git a/GBase/Interfaces/FileHandling/IFileHandler.cs b/GBase/Interfaces/FileHandling/IFileHandler.cs index b617c6d..0e9ca47 100644 --- a/GBase/Interfaces/FileHandling/IFileHandler.cs +++ b/GBase/Interfaces/FileHandling/IFileHandler.cs @@ -3,7 +3,6 @@ // Copyright(c) 2020 SimonG. All Rights Reserved. using System; -using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -22,51 +21,10 @@ namespace GBase.Interfaces.FileHandling /// True if successful, false if not Task Init(string path, CancellationToken cancellationToken); - Task AddEntry(T entry, IGBaseTable table, CancellationToken cancellationToken); + IGBaseFile CreateEntryFile(T entry, IGBaseTable table); - Task RemoveEntry(T entry); + Task DeleteEntryFile(T entry); - /// - /// Set the value for the given property - /// - /// The of the property - /// The of the property - /// - /// The name of the property - /// The value to set - /// - /// A to await - Task SetValue(T entry, string propertyName, TProperty value, CancellationToken cancellationToken); - - /// - /// Remove the value for the given property - /// - /// The of the property - /// The of the property - /// The name of the property - /// The value to set - /// - /// A to await - Task RemoveValue(string propertyName, TProperty value, CancellationToken cancellationToken); - - /// - /// Get the value for the given property, if multiple values are set the first is returned - /// - /// The of the property - /// The of the property - /// The name of the property - /// - /// The value for the given property - Task GetValue(string propertyName, CancellationToken cancellationToken); - - /// - /// Get all the values that are set for the given property - /// - /// The of the property - /// The of the property - /// The name of the property - /// - /// An with all the values for the property - Task> GetValues(string propertyName, CancellationToken cancellationToken); + Task RequestEntryFile(T entry); } } \ No newline at end of file diff --git a/GBase/Interfaces/FileHandling/IGBaseFile.cs b/GBase/Interfaces/FileHandling/IGBaseFile.cs new file mode 100644 index 0000000..9904130 --- /dev/null +++ b/GBase/Interfaces/FileHandling/IGBaseFile.cs @@ -0,0 +1,19 @@ +// Author: Gockner, Simon +// Created: 2020-11-10 +// Copyright(c) 2020 SimonG. All Rights Reserved. + +using System; +using System.IO; + +namespace GBase.Interfaces.FileHandling +{ + public interface IGBaseFile : IDisposable + { + object Entry { get; } + FileStream File { get; } + string FilePath { get; } + bool InUse { get; } + + IGBaseFile UseFile(); + } +} \ No newline at end of file