@ -8,8 +8,8 @@ using System.Linq;
using System.Reflection ;
using System.Reflection ;
using System.Threading ;
using System.Threading ;
using System.Threading.Tasks ;
using System.Threading.Tasks ;
using GBase.Api ;
using GBase.Attributes ;
using GBase.Attributes ;
using GBase.Exceptions ;
using GBase.Factories ;
using GBase.Factories ;
using GBase.FileHandling.Factories ;
using GBase.FileHandling.Factories ;
using GBase.Helpers ;
using GBase.Helpers ;
@ -66,18 +66,6 @@ namespace GBase
/// </summary>
/// </summary>
public List < T > Entries { get ; }
public List < T > Entries { get ; }
private int CurrentLastKey
{
get
{
T lastEntry = Entries . LastOrDefault ( ) ;
if ( lastEntry = = null )
return - 1 ;
return lastEntry . Key ;
}
}
/// <summary>
/// <summary>
/// Initialize this <see cref="IGBase"/>
/// Initialize this <see cref="IGBase"/>
@ -87,7 +75,7 @@ namespace GBase
/// <param name="folderName"></param>
/// <param name="folderName"></param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the asynchronous operation</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the asynchronous operation</param>
/// <returns>True if successful, false if not</returns>
/// <returns>True if successful, false if not</returns>
public async Task Init ( Type type , string databasePath , string folderName , CancellationToken cancellationToken )
public async Task < bool > Init ( Type type , string databasePath , string folderName , CancellationToken cancellationToken ) //TODO: Remove bool return value?
{
{
Type = type ;
Type = type ;
FolderName = folderName ;
FolderName = folderName ;
@ -95,19 +83,17 @@ namespace GBase
//TODO: Init columns list depending on GBaseColumnAttributes set for this GBaseTable
//TODO: Init columns list depending on GBaseColumnAttributes set for this GBaseTable
foreach ( var property in type . GetProperties ( ) )
foreach ( var property in type . GetProperties ( ) )
{
{
GBaseColumnAttribute gBaseColumnAttribute = property . GetCustomAttribute < GBaseColumnAttribute > ( ) ? ?
GBaseColumnAttribute gBaseColumnAttribute = property . GetCustomAttribute < GBaseColumnAttribute > ( ) ;
property . GetAttributeFromInheritedInterfaces < GBaseColumnAttribute > ( ) ;
if ( gBaseColumnAttribute = = null )
if ( gBaseColumnAttribute = = null )
continue ;
continue ;
IGBaseColumn gBaseColumn = _ gBaseColumnFactory . Create ( property . Name , property . PropertyType , gBaseColumnAttribute . IsKey ) ;
IGBaseColumn gBaseColumn = _ gBaseColumnFactory . Create ( property . Name , property . PropertyType ) ;
AddColumn ( gBaseColumn ) ;
AddColumn ( gBaseColumn ) ;
}
}
List < IGBaseFile > files = _f ileHandler . Init ( databasePath , FolderName , cancellationToken ) ;
List < IGBaseFile > files = _f ileHandler . Init ( databasePath , FolderName , cancellationToken ) ;
if ( files = = null )
if ( files = = null )
return ;
return true ;
foreach ( var file in files ) //create entries for existing files
foreach ( var file in files ) //create entries for existing files
{
{
@ -116,33 +102,25 @@ namespace GBase
T entry = new T ( ) ;
T entry = new T ( ) ;
List < object > parameters = new List < object > ( ) ;
List < object > parameters = new List < object > ( ) ;
foreach ( var column in Columns . Where ( c = > ! c . IsKey ) )
foreach ( var column in Columns )
{
{
using IPoolItem < IDataHandler > dataHandlerItem = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
IDataHandler dataHandler = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
parameters . Add ( await GenericMethodCaller . CallAsync ( dataHandlerItem . Use ( ) ,
parameters . Add ( await GenericMethodCaller . CallAsync ( dataHandler ,
nameof ( IDataHandler . GetValue ) ,
nameof ( IDataHandler . GetValue ) ,
BindingFlags . Public | BindingFlags . Instance ,
BindingFlags . Public | BindingFlags . Instance ,
typeof ( T ) , column . Type ,
typeof ( T ) , column . Type ,
file . File , column . Name , cancellationToken ) ) ;
file . File , column . Name , cancellationToken ) ) ;
}
}
IGBaseColumn keyColumn = Columns . FirstOrDefault ( c = > c . IsKey ) ;
entry . Initialize ( parameters ) ;
if ( keyColumn = = null )
throw new MissingKeyColumnException < T > ( ) ;
GBaseKey key = new GBaseKey ( ) ;
using ( IPoolItem < IDataHandler > dataHandlerItem = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) )
{
key . Key = await dataHandlerItem . Use ( ) . GetValue < T , int > ( file . File , keyColumn . Name , cancellationToken ) ;
}
entry . Initialize ( key , parameters ) ;
file . Entry = entry ;
file . Entry = entry ;
Entries . Add ( entry ) ;
Entries . Add ( entry ) ;
}
}
}
}
return true ;
}
}
/// <summary>
/// <summary>
@ -173,35 +151,26 @@ namespace GBase
}
}
/// <summary>
/// <summary>
/// Add an entry that implements <see cref="IGBaseObject "/> to this <see cref="IGBaseTable"/>
/// Add an entry that implements <see cref="INotifyGBaseEntryChanged "/> to this <see cref="IGBaseTable"/>
/// </summary>
/// </summary>
/// <param name="entry">The entry implementing <see cref="IGBaseObject "/></param>
/// <param name="entry">The entry implementing <see cref="INotifyGBaseEntryChanged "/></param>
/// <param name="cancellationToken"></param>
/// <param name="cancellationToken"></param>
/// <returns>True if successful, false if not</returns>
/// <returns>True if successful, false if not</returns>
public async Task < bool > AddEntry ( T entry , CancellationToken cancellationToken )
public async Task < bool > AddEntry ( T entry , CancellationToken cancellationToken )
{
{
entry . Key ? ? = new GBaseKey ( ) ;
if ( entry . Key ! = - 1 )
throw new InvalidKeyException ( "Key of entry is already set. Make sure it is -1 when initializing." ) ;
entry . Key . Key = CurrentLastKey + 1 ; //set next possible key
Entries . Add ( entry ) ;
Entries . Add ( entry ) ;
using IGBaseFile file = _f ileHandler . CreateEntryFile ( entry , this ) ;
using IGBaseFile file = _f ileHandler . CreateEntryFile ( entry , this ) ;
using IPoolItem < IDataHandler > dataHandlerItem = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
IDataHandler dataHandler = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
await dataHandlerItem . Use ( ) . AddEntry ( entry , this , file . File , cancellationToken ) ;
await dataHandler . AddEntry ( entry , this , file . File , cancellationToken ) ;
return true ;
return true ;
}
}
public T GetEntryForKey ( int key ) = > Entries . FirstOrDefault ( e = > e . Key = = key ) ;
/// <summary>
/// <summary>
/// Remove an entry that implements <see cref="IGBaseObject "/> from this <see cref="IGBaseTable"/>
/// Remove an entry that implements <see cref="INotifyGBaseEntryChanged"/> from this <see cref="IGBaseTable"/>
/// </summary>
/// </summary>
/// <param name="entry">The entry implementing <see cref="IGBaseObject "/></param>
/// <param name="entry">The entry implementing <see cref="INotifyGBaseEntryChanged "/></param>
/// <returns>True if successful, false if not</returns>
/// <returns>True if successful, false if not</returns>
public async Task < bool > RemoveEntry ( T entry )
public async Task < bool > RemoveEntry ( T entry )
{
{
@ -216,22 +185,22 @@ namespace GBase
public async Task SetValue < TProperty > ( T entry , string propertyName , TProperty value , CancellationToken cancellationToken )
public async Task SetValue < TProperty > ( T entry , string propertyName , TProperty value , CancellationToken cancellationToken )
{
{
using IGBaseFile file = await _f ileHandler . RequestEntryFile ( entry ) ;
using IGBaseFile file = await _f ileHandler . RequestEntryFile ( entry ) ;
using IPoolItem < IDataHandler > dataHandlerItem = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
IDataHandler dataHandler = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
await dataHandlerItem . Use ( ) . SetValue < T , TProperty > ( file . File , propertyName , value , cancellationToken ) ;
await dataHandler . SetValue < T , TProperty > ( file . File , propertyName , value , cancellationToken ) ;
}
}
public async Task < TProperty > GetValue < TProperty > ( T entry , string propertyName , CancellationToken cancellationToken )
public async Task < TProperty > GetValue < TProperty > ( T entry , string propertyName , CancellationToken cancellationToken )
{
{
using IGBaseFile file = await _f ileHandler . RequestEntryFile ( entry ) ;
using IGBaseFile file = await _f ileHandler . RequestEntryFile ( entry ) ;
using IPoolItem < IDataHandler > dataHandlerItem = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
IDataHandler dataHandler = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
return await dataHandlerItem . Use ( ) . GetValue < T , TProperty > ( file . File , propertyName , cancellationToken ) ;
return await dataHandler . GetValue < T , TProperty > ( file . File , propertyName , cancellationToken ) ;
}
}
public async Task < IEnumerable < TProperty > > GetValues < TProperty > ( T entry , string propertyName , CancellationToken cancellationToken )
public async Task < IEnumerable < TProperty > > GetValues < TProperty > ( T entry , string propertyName , CancellationToken cancellationToken )
{
{
using IGBaseFile file = await _f ileHandler . RequestEntryFile ( entry ) ;
using IGBaseFile file = await _f ileHandler . RequestEntryFile ( entry ) ;
using IPoolItem < IDataHandler > dataHandlerItem = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
IDataHandler dataHandler = await _d ataHandlerPool . RequestDataHandler ( this , false , cancellationToken ) ;
return await dataHandlerItem . Use ( ) . GetValues < T , TProperty > ( file . File , propertyName , cancellationToken ) ;
return await dataHandler . GetValues < T , TProperty > ( file . File , propertyName , cancellationToken ) ;
}
}
/// <summary>
/// <summary>