@ -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
/// <summary>
/// Internal file handler
/// </summary>
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
{
/// <summary>
/// The file extension for all GBase tables
/// </summary>
public const string GBASE_TABLE_FILE_EXTENSION = "gb" ;
private readonly IDataHandlerPool _d ataHandlerPool ;
private readonly List < IGBaseFile > _f iles ;
private string _ path ;
private readonly List < ( object entry , FileStream file , string filePath , bool inUse ) > _f iles ;
/// <summary>
/// Internal file handler
/// </summary>
/// <param name="dataHandlerPool">The <see cref="IDataHandlerPool"/></param>
public FileHandler ( IDataHandlerPool dataHandlerPool )
public FileHandler ( )
{
_d ataHandlerPool = dataHandlerPool ;
_f iles = new List < ( object entry , FileStream file , string filePath , bool inUse ) > ( ) ;
_f iles = new List < IGBaseFile > ( ) ;
}
/// <summary>
@ -52,7 +46,7 @@ namespace GBase.FileHandling
return true ; //TODO: is there anything that needs to be initialized here?
}
public async Task AddEntry < T > ( T entry , IGBaseTable table , CancellationToken cancellationToken )
public IGBaseFile CreateEntryFile < T > ( 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 _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
await dataHandler . AddEntry ( entry , table , entryFile , cancellationToken ) ;
IGBaseFile file = new GBaseFile ( entry , entryFile , filePath ) ;
_f iles . Add ( file ) ;
_f iles . Add ( ( entry , entryFile , filePath , false ) ) ;
return file . UseFile ( ) ;
}
public async Task < bool > RemoveEntry < T > ( T entry )
public async Task < bool > DeleteEntryFile < T > ( T entry )
{
var file = _f iles . FirstOrDefault ( f = > f . e ntry. Equals ( entry ) ) ;
IGBaseFile file = _f iles . FirstOrDefault ( f = > f . E ntry. Equals ( entry ) ) ;
if ( file = = default )
return false ;
await file . f ile. DisposeAsync ( ) ;
File . Delete ( file . f ilePath) ;
await file . F ile. DisposeAsync ( ) ;
File . Delete ( file . F ilePath) ;
return true ;
}
/// <summary>
/// Set the value for the given property
/// </summary>
/// <typeparam name="T">The <see cref="Type"/> of the property</typeparam>
/// <typeparam name="TProperty">The <see cref="Type"/> of the property</typeparam>
/// <param name="entry"></param>
/// <param name="propertyName">The name of the property</param>
/// <param name="value">The value to set</param>
/// <param name="cancellationToken"></param>
/// <returns>A <see cref="Task"/> to await</returns>
public async Task SetValue < T , TProperty > ( T entry , string propertyName , TProperty value , CancellationToken cancellationToken )
public async Task < IGBaseFile > RequestEntryFile < T > ( T entry )
{
IDataHandler dataHandler = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
await dataHandler . SetValue < T , TProperty > ( TODO , propertyName , value , cancellationToken ) ;
}
IGBaseFile file = _f iles . FirstOrDefault ( f = > f . Entry . Equals ( entry ) ) ;
if ( file = = default )
throw new FileNotExistingException < T > ( ) ;
/// <summary>
/// Remove the value for the given property
/// </summary>
/// <typeparam name="T">The <see cref="Type"/> of the property</typeparam>
/// <typeparam name="TProperty">The <see cref="Type"/> of the property</typeparam>
/// <param name="propertyName">The name of the property</param>
/// <param name="value">The value to set</param>
/// <param name="cancellationToken"></param>
/// <returns>A <see cref="Task"/> to await</returns>
public async Task RemoveValue < T , TProperty > ( string propertyName , TProperty value , CancellationToken cancellationToken )
{
IDataHandler dataHandler = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
await dataHandler . RemoveValue < T , TProperty > ( TODO , propertyName , value , cancellationToken ) ;
}
if ( file . InUse )
await Task . Delay ( 1 0 0 0 ) ;
/// <summary>
/// Get the value for the given property, if multiple values are set the first is returned
/// </summary>
/// <typeparam name="T">The <see cref="Type"/> of the property</typeparam>
/// <typeparam name="TProperty">The <see cref="Type"/> of the property</typeparam>
/// <param name="propertyName">The name of the property</param>
/// <param name="cancellationToken"></param>
/// <returns>The value for the given property</returns>
public async Task < TProperty > GetValue < T , TProperty > ( string propertyName , CancellationToken cancellationToken )
{
IDataHandler dataHandler = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
return await dataHandler . GetValue < T , TProperty > ( propertyName ) ;
}
/// <summary>
/// Get all the values that are set for the given property
/// </summary>
/// <typeparam name="T">The <see cref="Type"/> of the property</typeparam>
/// <typeparam name="TProperty">The <see cref="Type"/> of the property</typeparam>
/// <param name="propertyName">The name of the property</param>
/// <param name="cancellationToken"></param>
/// <returns>An <see cref="IEnumerable{T}"/> with all the values for the property</returns>
public async Task < IEnumerable < TProperty > > GetValues < T , TProperty > ( string propertyName , CancellationToken cancellationToken )
{
IDataHandler dataHandler = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
return await dataHandler . GetValues < T , TProperty > ( propertyName ) ;
return file . UseFile ( ) ;
}
/// <summary>
/// Dispose used resources asynchronously
/// </summary>
/// <returns>A <see cref="ValueTask"/> to await</returns>
public async ValueTask DisposeAsync ( ) = > await _d ataHandlerPool . DisposeAsync ( ) ;
public async ValueTask DisposeAsync ( )
{
while ( _f iles . Any ( f = > f . InUse ) )
{
await Task . Delay ( 1 0 0 0 ) ;
}
foreach ( var file in _f iles )
{
await file . File . DisposeAsync ( ) ;
}
}
}
}